Question

Describe the difference between making a class a member of another class (object composition) and making...

Describe the difference between making a class a member of another class (object composition) and making a class a friend of another class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
Describe the difference between making a class a member of another class (object composition) and making...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT