Write a program by using friend function to add data objects of two different classes ABC and XYZ.
#include <iostream>
using namespace std;
class XYZ;
class ABC {
private:
int A;
friend int Add(ABC n1 , XYZ n2);
public:
ABC (int A){
this->A=A;
cout<<"Number ABC: "<<A<<"\n";
}
};
class XYZ {
private:
int X;
friend int Add(ABC n1 , XYZ n2);
public:
XYZ (int X){
this->X=X;
cout<<"Number XYZ: "<<X<<"\n";
}
};
int Add(ABC a,XYZ x) {
return (a.A + x.X);
}
int main (){
ABC a(123);
XYZ x(456);
int sum=Add(a,x);
cout<<"\nTotal= "<<sum;
return 0;
}
Comments
Leave a comment