Answer to Question #173562 in C++ for Vijay Raj

Question #173562

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.


1
Expert's answer
2021-03-22T12:44:12-0400
#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;
}

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