Creates two friendly classes, the class ABC with attribute A and class XYZ with attribute age to find maximum age among two men. Assign values for the data members using parameterized constructor, then to find maximum age among two men using friend function and display the age of men. Finally free the resources of data objects using destructor member function.
#include<iostream>
using namespace std;
class XYZ;
class ABC{
private:
int A;
friend int findMaximumAge(ABC objABC,XYZ objXYZ);
public:
// constructor of the class A
ABC(){
}
// parametrized constructor
ABC(int x){
this->A=x;
cout<<"The age of ABC is:"<<A<<endl;
}
// Destructor of the class A
~ABC(){
}
};
class XYZ{
private:
int age;
friend int findMaximumAge(ABC objABC,XYZ objXYZ);
public:
//Constructor of XYZ
XYZ (){}
//parameterized constructor
XYZ (int age){
this->age=age;
cout<<"Age XYZ: "<<age<<"\n\n";
}
//destructor
~XYZ(){
}
};
//// access members of both classes
int findMaximumAge(ABC objABC,XYZ objXYZ) {
return (objABC.A - objXYZ.age);
}
int main(){
ABC _ABC1(40);
XYZ _XYZ1(40);
int age=findMaximumAge(_ABC1,_XYZ1);
if(age>0){
cout<<"The age of ABC is greater than XYZ.\n";
}else if(age<0){
cout<<"The age of XYZ is greater than ABC.\n";
}else{
cout<<"The age of XYZ and ABC is same.\n";
}
cout<<"\n";
ABC _ABC2(45);
XYZ _XYZ2(40);
age=findMaximumAge(_ABC2,_XYZ2);
if(age>0){
cout<<"The age of ABC is greater than XYZ.\n";
}else if(age<0){
cout<<"The age of XYZ is greater than ABC.\n";
}else{
cout<<"The age of XYZ and ABC is same.\n";
}
cout<<"\n";
ABC _ABC3(45);
XYZ _XYZ3(50);
age=findMaximumAge(_ABC3,_XYZ3);
if(age>0){
cout<<"The age of ABC is greater than XYZ.\n";
}else if(age<0){
cout<<"The age of XYZ is greater than ABC.\n";
}else{
cout<<"The age of XYZ and ABC is same.\n";
}
//delay
system("pause");
return 0;
}
Comments
Leave a comment