Answer to Question #234708 in C++ for SOM

Question #234708

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-08T07:51:48-0400
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);
} 

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