Write a class called Person, which has three private data members: first_name, last_name and year_born. The class should contain the following:
• A default constructor.
• An overloaded constructor that sets all variables to the values of the arguments passed to the parameters.
• One get function for each variable that returns the current value.
• One set function for each variable that sets a new value.
• An overloaded >> operator that inputs a person’s information.
• An overloaded << operator that outputs a person’s information.
• An overloaded == operator that compares the first and last names of two persons.
• A friend function add_person that takes an array of persons, the number of persons in the array, and another person as input parameters. The function should add the other person to the array if there is nobody in the array with that first and last name, and modify the size accordingly.
Note: Embed your class in a test program.
Code:
#include <string>
using namespace std;
class Person
{
public:
Person();
Person(string fn, string ln, int y);
string get_first_name();
string get_last_name();
int get_year_born();
void set_first_name(string fn);
void set_last_name(string ln);
void set_year_born(int y);
friend ostream& operator <<(ostream& outs, const Person& P);
friend istream& operator >>(istream& ins, Person& P);
friend void operator ==(Person& P1, const Person& P2);
friend int sum_age(Person person[], int number, int current_year);
private:
string first_name, last_name;
int year_born;
};
int main()
{
Person People[4];
People[0] = Person("Salma", "Foad", 1990);
People[1] = Person("Sophie", "Smith", 1986);
People[2] = Person("Neda'a", "Hesham", 1984);
People[3] = Person("Bushra", "Sultan", 1980);
cout << "The sum of all ages is: " << sum_age(People, 4, 2006) << " years." << endl;
return 0;
}
Person::Person() : year_born(0)
{
}
Person::Person(string fn, string ln, int y) : first_name(fn), last_name(ln), year_born(y)
{
}
string Person::get_first_name()
{
return first_name;
}
string Person::get_last_name()
{
return last_name;
}
string Person::get_year_born()
{
return year_born;
}
void Person::set_first_name(string fn)
{
first_name = fn;
}
void Person::set_last_name(string ln)
{
last_name = ln;
}
void Person::set_year_born(int y)
{
year_born = y;
}
ostream& operator <<(ostream& outs, const Person& P)
{
outs << P.first_name << ":" << P.last_name<<":"<<p.year_born;
return outs;
}
istream& operator >>(istream& ins, Person& P)
{
cout << "Enter first_name: ";
ins >> P.first_name;
cout << "Enter last_name: ";
ins >> P.last_name;
cout << "Enter year_born: ";
ins >> P.year_born;
return ins;
}
void operator ==(Person& P1, const Person& P2)
{
int extra = 0;
P1.last_name == P2.last_name;
P1.first_name == p2.first_name=extra ;
}
int sum_age(Person person[], int number, int current_year)
{
if (number < 0)
return 0;
int sum=0;
for (int i=0; i<number; i++)
sum = sum + (current_year - person[i].year_born);
return sum;
}
this question and this answer please help me about correct answer