C++ program to access private data members of one class by another class using FRIEND CLASS

//C++ program to have two classes as your choice
//such that function members of a class use private members of another class

#include <iostream>
using namespace std;
class ABC
{
    int p,q;
    public:
        void read()
        {
            cout<<"enter p and q: ";
            cin>>p>>q;
        }
    friend class XYZ;
};
class XYZ
{
    int s;
    public:
        void add(ABC a)
        {
            s=a.p+a.q;
        }
        void display()
        {
            cout<<"the sum is "<<s;
        }
};
int main()
{
    ABC a;
    a.read();
    XYZ x;
    x.add(a);
    x.display();
    return 0;
}

No comments:

Post a Comment