Answer to Question #227588 in C++ for David

Question #227588

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,

  • Take the input for the data members of both the classes using a member function of Class B.
  • Display the data members of both the classes using a member function of Class B.
  • Add the data members of both the classes and display the result.
1
Expert's answer
2021-08-20T01:09:27-0400
#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();
}

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

David
21.08.21, 19:18

Thanks a lot !!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS