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.( Initialize the class members using constructors and destroy the objects by using destructor)
#include <iostream>
using namespace std;
class XYZ;
class ABC {
private:
int A;
// friend function declaration
friend int findMaximumAge(ABC objectABC,XYZ objectXYZ);
public:
//Constructor
ABC (){}
//parameterized constructor
ABC (int A){
this->A=A;
cout<<"Age ABC: "<<A<<"\n";
}
//destructor
~ABC (){}
};
class XYZ {
private:
int age;
// find maximum age among two men using friend function and display the age of men.
// friend function declaration
friend int findMaximumAge(ABC objectABC,XYZ objectXYZ);
public:
//Constructor
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 objectABC,XYZ objectXYZ) {
return (objectABC.A - objectXYZ.age);
}
//The start point of the program
int main (){
//test data
ABC _ABC1(45);
XYZ _XYZ1(45);
int age=findMaximumAge(_ABC1,_XYZ1);
if(age>0){
cout<<"The age of man ABC is the maximum age.\n";
}else if(age<0){
cout<<"The age of man XYZ is the maximum age.\n";
}else{
cout<<"The ages of men XYZ and ABC are the same.\n";
}
cout<<"\n";
ABC _ABC2(45);
XYZ _XYZ2(40);
age=findMaximumAge(_ABC2,_XYZ2);
if(age>0){
cout<<"The age of man ABC is the maximum age.\n";
}else if(age<0){
cout<<"The age of man XYZ is the maximum age.\n";
}else{
cout<<"The ages of men XYZ and ABC are the same.\n";
}
cout<<"\n";
ABC _ABC3(45);
XYZ _XYZ3(50);
age=findMaximumAge(_ABC3,_XYZ3);
if(age>0){
cout<<"The age of man ABC is the maximum age.\n";
}else if(age<0){
cout<<"The age of man XYZ is the maximum age.\n";
}else{
cout<<"The ages of men XYZ and ABC are the same.\n";
}
//delay
system("pause");
return 0;
}
Comments
Leave a comment