Write a program to create a class 'A' with an integer data member. Create another class ' B' with one integer data.
Using friend class do the following,
#include<iostream>
using namespace std;
class A{
private:
int x;
public:
friend class C;
};
class B{
private:
int y;
public:
friend class C;
};
class C{
private:
int a; //Will store the value of x from class B
int b; //Will store the value of y from class C
public:
void inputAB(){
A s;
B r;
a = s.x;
b = r.y;
cout<<"Enter the value of x\n";
cin>>a;
cout<<"Enter the value of y\n";
cin>>b;
}
void displayAB(){
cout<<"The value of x is: "<<a<<"\nThe value of y is: "<<b<<endl;
}
void addAB(){
cout<<"The addition of "<<a<<" and "<<b<<" is "<<a +b<<endl;
}
};
int main(){
C c;
c.inputAB();
c.displayAB();
c.addAB();
}
Comments
Thanks a lot !!
Leave a comment