Describe the difference between making a class a member of another class (object composition) and making a class a friend of another class.
Making a Class a member of another class:
Properties of one class can be used in another class using inheritance or using the object of a class as a member in another class.Declaring the object as a class data member in another class is also know as delegation. When a class has an object of another class as its member,such a class is known as a container class.
Example program:
One class in Another class as a member using object:
Source code:
#include<iostream.h>
#include<conio.h>
class A
{
public:
int x;
A()
{
cout<<"\n constructor of class A";
x=20;
}
};
class B
{
public:
int k;
A y;
B()
{
k=30;
cout<<"\n Constructor of class B";
}
void show()
{
cout<<"\n x="<<y.x<<"k="<<k;
}
};
int main()
{
clrscr();
B b;
b.show();
return 0;
}
Output:
Constructor of Class A
Constructor of Class B
x=20 k=30
Explanation:
In the above program, the class B contains integer k and object y of class A. In function main(), object b is an object of Class B. The constructor of class A is executed first, because when the compiler reaches the class B, it finds an object of class A. we know that the object declaration always executes the constructor of that class. Thus, the object of class A is declared, and the constructor of Class A is executed. The constructor of class B is executed, and the variable of class B is initialized.The member function show() displays the contents of x and k. The contents of class A member is obtained from object y. The dot(.) operator is used to access the elements of class A.
Making a class a Friend of another class:
A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class. For example a LinkedList class may be allowed to access private members of Node.
Code:
class Node {
private:
int key;
Node* next;
/* Other members of Node Class */
// Now class LinkedList can
// access private members of Node
friend class LinkedList;
};
Friend Function Like friend class, a friend
function can be given special grant to access private and protected
members. A friend function can be:
a) A method of another class
b) A global function
Code:
class Node {
private:
int key;
Node* next;
/* Other members of Node Class */
friend int LinkedList::search();
// Only search() of linkedList
// can access internal members
};
Following are some important points about friend functions and
classes:
1) Friends should be used only for limited
purpose. too many functions or external classes are declared as
friends of a class with protected or private data, it lessens the
value of encapsulation of separate classes in object-oriented
programming.
2) Friendship is not mutual. If class A is a friend of B, then B doesn’t become a friend of A automatically.
3) Friendship is not inherited.
4) The concept of friends is not there in Java.
Example:
A simple and complete C++ program to demonstrate friend Class
Source code:
#include <iostream.h>
#include<conio.h>
class A
{
private:
int a;
public:
A() { a = 0; }
friend class B; // Friend Class
};
class B
{
private:
int b;
public:
void showA(A& x)
{
// Since B is friend of A, it can access
// private members of A
std::cout << "A::a=" << x.a;
}
};
int main()
{
A a;
B b;
b.showA(a);
return 0;
}
OUTPUT:
A: :a=0
This all are the difference between making a class a member of another class and making a class a friend of another class.
Describe the difference between making a class a member of another class (object composition) and making...
1. Discuss the difference between inheritance and composition. What are the advantages and disadvantages of each? Can you implement one with the other? 2.How is a class similar to a database table? How is it different? How do these similarities and differences justify the need for class models and for data models? Or do they? 3.Discuss the difference between association and composition. What are the advantages and disadvantages of each? 4.When would you apply inheritance? When would you not? Provide...
hello there, still confused. What is the difference between a c-string and a string class object? In a short paragraph describe three differences.
Please describe the difference between classes and objects. How are they related? What does it mean to instantiate an object? Also, please describe the difference between composition and aggregation and when it is better to use one over the other?
1. Discuss the difference between inheritance and composition. What are the advantages and disadvantages of each? Can you implement one with the other? 2.How is a class similar to a database table? How is it different? How do these similarities and differences justify the need for class models and for data models? Or do they? 3.Discuss the difference between association and composition. What are the advantages and disadvantages of each? 4.When would you apply inheritance? When would you not? Provide...
what is a composition of functions? what is the difference between the composition and the product of two funtions?
Describe the difference between the images seen in a spherical, concave mirror when the object is “up close” (closer to the image than the focal length) compared to “far away” (outside the focal length).
What is the difference between the String class and the StringBuilder class in the API? Group of answer choices they are in different packages a String can only contain letters while a StringBuilder can contain any character a String is immutable and a StringBuilder is mutable there is no difference A class can directly extend many classes. Group of answer choices True False With this declaration: Student s1; What possible values can s1 hold? Group of answer choices с a...
1. Describe the difference between incremental and and Carnegie models of decision-making. Please not less than 100 words or more than 200 words please make reference if answer is copied from any site or material so as to avoid PLAGIARISM. thanks
In c++ What does operator overloading allow us to do ? Why must the << and the >> operators be overloaded as friend functions instead of member functions, for a user defined class ? What is the difference between an instance member variable and a static member variable ? How are call to static member functions made ? Describe the difference between reading a file using the >> operator and with the getline function What is inheritance What is a...
solve it in c++10 Create a composition between the classes Job and Salary. Class Salary a. data member: 1. money b. constructors: 1. default constructor 2. user defined constructor with a parameter to set money c. standard accessor and mutator functions Class Job a. data members: title (name of the job) salary (object of type Salary) b. constructors: 1. default constructor 2. user defined constructor to set title and salary 3. implement constructor delegation c. standard accessor and mutator function...