Include the definition for the function ‘max_age’, in the program given below. The
function compares the data member age of the objects P1 and P2 and returns the
object having higher value for age.
class person
{ int age;
public:
person(int a){age=a;}
};
int main()
{ person P1(1.5);
person P2(2.5);
person P3=P1.max_age(P2);
}
#include<iostream>
using namespace std;
class person
{
private:
int age;
public:
person(int a){
age=a;
}
person max_age(person &p){
if(this->age>p.age){
return *this;
}
else{
return p;
}
}
void display(){
cout<<"Age of the older person: "<<age<<endl;
}
};
int main()
{ person P1(1.5);
person P2(2.5);
person P3=P1.max_age(P2);
P3.display();
}
Comments
Leave a comment