Answer to Question #239532 in C++ for nick

Question #239532

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);

}


1
Expert's answer
2021-09-22T00:05:46-0400
#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();


}

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