Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Design a class named ‘Person’ with required personal details and store the details of at least 5 persons. Now find out the eldest among the five persons and display the name of the same [using the overloaded operator ‘<’ or ‘>’]
Input:

Arun

12 5 2002

Banu

11 8 2001

Chitra

5 1 2002

David

14 9 2001

Elan

21 8 2002

 

Output:

Banu

What I have tried:

TRIED JUST TOOK THE INPUT AND SAVED IT.
Posted
Updated 27-May-22 7:30am
Comments
0x01AA 26-May-22 13:09pm    
Quote: "Design a class named ‘Person’ with required personal details and store the details of at least 5 persons"
Mistake: A class Person should hold the data of exactly _one_ person.
To hold several person you should invent something like a class 'Persons'
jeron1 26-May-22 13:48pm    
Do you have a question?

Here's a class definition. You'll have to implement its functions, create its instances, and find the eldest person.
C++
class Person
{
public:
   Person(const std::string& name, int year, int month, int day);
   friend bool operator<(const Person& lhs, const Person& rhs);
private:
   std::string name_;
   int year_;
   int month_;
   int day_;
};
For a discussion of comparison operators like operator<, see the discussion halfway down this page[^]. Bookmark that site, which is my go-to reference for C++.

A good argument can be made that your design should first define a Date class for the year_, month_, and day_ members, so that Person would only have name_ and date_ data members. operator< would then be a member of Date; it makes more sense to compare two dates than it does two people, and the Date class could then also be used by other classes that required a date.
 
Share this answer
 
v2
Comments
CPallini 27-May-22 10:46am    
You should also provide the appropriate access specifiers to your class members.
Greg Utas 27-May-22 11:09am    
Grazie. I usually find those details when the compiler complains. :)
I give you an example
C++
#include <iostream>
using namespace std;

class Box
{
  int m_weight;
  //...;

public:
  Box(int weight):m_weight{weight}{}

  friend bool operator < (const Box & first_box, const Box & second_box)
  {
    return (first_box.m_weight < second_box.m_weight);
  }
};



int main()
{
  Box b1(1), b2(2);

  cout << "b1 is" << (b1 < b2 ? " less" : " equal or greater" ) << " than b2\n";
}
 
Share this answer
 
When thinking about it I got the idea that you write a Date class which has this operator, so you can write code like this:

C++
if( person1.age() > person2.age() ) {
 print("person: %s is older", person1.fullName() );
}
looks really better than
C++
if( person1 < person2 ) {
 print("person: %s is older", person1.fullName() );
}
because the code is totally explaning that you are comparing their age and not the persons themselfs.

PS: think about the edge case that persons have the totally same age. Like twins ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900