Answer to Question #232346 in C++ for Arup

Question #232346

Write a program by using friend function to add data objects of two different classes ABC and XYZ.


1
Expert's answer
2021-09-02T04:01:45-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS