Question #232864

Write a program in C++ to create two classes separately each having one integer value as its private data member. Compare both the values and display in main functions whether the values are equal or not. Use pass by address method to execute the code.

Expert's answer

#include <iostream>


using namespace std;
class B;


class A  {
    private:
    	int a;
    	friend void compare(A n1 , B n2);
    	
    public:
    	A (int a){
    		this->a=a;
    		cout<<"Number A: "<<a<<"\n";
    	}
};


class B {
    private:
    	int b;
    	friend void compare(A n1 , B n2);
    public:
    	B (int b){
    		this->b=b;
    		cout<<"Number B: "<<b<<"\n";
    	}
};


void compare(A n1,B n2) {
	if (n1.a==n2.b)
	    cout<<"\nThe two numbers are equal.";
	else
	    cout<<"\nThe two numbers are not equal.";
}
int main (){
	A a(67);
	B b(67);
	compare(a,b);
	return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS