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);
}
class person
{
public:
// assign the variable aaa the access modifier public
int age;
person(int a) { age = a; }
person max_age(person P) {
if (P.age > age) {
return person(P.age);
}
return person(age);
}
};
int main()
{person P1(1.5);
person P2(2.5);
person P3 = P1.max_age(P2);
}
Comments
Leave a comment