Review the provided code. The following files are provide: Driver.cpp: This is a very basic driver to be considered an example. You will write your own. Person.h: This is the basic specification for the Person class Person.cpp: This is the implementation for the provided Person.h.
The Person class represents one person. Each person has a name, age, and a phrase that they say. The constructor requires a string for name and double for age. Use this class as a starting point. Add the following features to the Person class: • Add a copy constructor to the Person class, so that Person1 = Person2 works correctly. • Overload the <> operators so two Person objects may be compared by age. • Decide if this class requires a destructor. If it does, write one. If not, do not write one.
Modify the provided driver and test until you understand how the Person class operates. Confirm that your copy constructor and <> operators work.
Next, create a new class representing a group of Person objects. Call this class, People. The People class must hold an unlimited number of Person objects. The People class must have the following features:
- Search methods that allows searching for Persons by age or name. The search must return a People object, as the search results may return more than one person. Use overloading such that passing a string to search will search by name, and passing a double to search will search by age.
-A remove method that takes a name or age argument, like search, and deletes all Person object matching the name or age from the People object.
- Add method will add a Person to the People object. This method must accept a Person object as an argument.
- Create an iterator that allows client code to iterate through the list of People. For each next Person, return a Person object and update your next Person.
- Clear all Persons from the People object.
- GetSize() that returns the number of Person objects in the People object.
- Decide if this class requires a destructor. If it does, write one. If not, do not write one.
- Decide if this class requires a destructor. If it does, write one. If not, do not write one.
Last, create a driver that creates a single People object, loop to create and insert many Person objects, then provide a menu to allow the user to review the Persons in the People object. Add sufficient features here to demonstrate all of the features of People and Persons.
Submit screenshots of your program output along with your source code for Person, People, and driver. Be sure to provide separate files for each object header and implementation.
// Person.h
#ifndef PERSON_H_ #define PERSON_H_ #include <string> class Person{ private: std::string name; double age; std::string phrase = ""; void makeOlder(); public: Person(); Person(std::string n, double a); double getAge(); std::string getName(); std::string speak(); void setPhrase(std::string p); }; #endif
// Person.cpp
#include "Person.h"
Person::Person()
{
name = " ";
age = 0.0;
phrase = "Write anything";
}
Person::Person(std::string n, double a)
{
name = n;
age = a;
phrase = "Hi!!";
}
void Person::makeOlder()
{
age = age + .1;
}
double Person::getAge()
{
makeOlder();
return age;
}
std::string Person:: getName()
{
makeOlder();
return name;
}
std::string Person:: speak()
{
makeOlder();
return phrase;
}
void Person::setPhrase(std::string p)
{
makeOlder();
phrase = p;
}
// Driver.cpp
#include <iostream> #include "Person.h" #include "People.h" using namespace std; int main() { Person *me = new Person("Chris", 21.0); Person you; cout << "Me Name: " << me->getName() << endl; cout << "Me Age: " << me->getAge() << endl; cout << "Me Speak: " << me->speak() << endl; cout << "Me Age: " << me->getAge() << endl; cout << "Me Age: " << me->getAge() << endl; cout << "You Name: " << you.getName() << endl; cout << "You Age: " << you.getAge() << endl; cout << "You Speak: " << you.speak() << endl; Person *steve = new Person("Steve",21.0); People myPeople; myPeople.add(*me); myPeople.add(you); myPeople.add(*steve); cout << myPeople.getSize() << " People"<< endl; People results = myPeople.search("Chris"); cout << "Search results size by name: " << results.getSize() << endl; results = myPeople.search(21.0); cout << "Search results size by age: " << results.getSize() << endl; myPeople.remove("Chris"); cout << "Removed by name." << endl; cout << myPeople.getSize() << " People"<< endl; return 0; }
Driver.cpp
#include <iostream>
#include "Person.h"
#include "People.h"
using namespace std;
int main()
{
Person *me = new Person("Chris", 21.0);
Person you;
cout << "Me Name: " << me->getName()
<< endl;
cout << "Me Age: " << me->getAge()
<< endl;
cout << "Me Speak: " << me->speak()
<< endl;
cout << "You Name: " << you.getName()
<< endl;
cout << "You Age: " << you.getAge()
<< endl;
cout << "You Speak: " << you.speak()
<< endl;
Person *steve = new Person("Steve", 21.0);
Person *steve_copy=new Person(*steve);
cout << "Steve copy Name: " <<
steve_copy->getName() << endl;
cout << "Steve copy Age: " <<
steve_copy->getAge() << endl;
cout << "Steve copy Speak: " <<
steve_copy->speak() << "\n" << endl;
if (*me < *steve)
cout << "I am younger"
<< endl;
if (*steve > you)
cout << "Steve is elder"
<< endl;
People myPeople;
myPeople.add(*me);
myPeople.add(you);
myPeople.add(*steve);
cout << myPeople.getSize() << "
People"<< endl;
Person *list = myPeople.getList();
cout << "MyPeople list contains\n";
for (int i = 0; i < myPeople.getSize(); i++)
{
cout << "Name: " <<
list[i].getName() << endl;
cout << "Age: " <<
list[i].getAge() << endl;
cout << "Speak: " <<
list[i].speak() << "\n" << endl;
}
People results = myPeople.search("Chris");
cout << "Search results size by name: " <<
results.getSize() << endl;
list = results.getList();
cout << "\nSearch Results list
contains\n";
for (int i = 0; i < results.getSize(); i++)
{
cout << "Name: " <<
list[i].getName() << endl;
cout << "Age: " <<
list[i].getAge() << endl;
cout << "Speak: " <<
list[i].speak() << "\n" << endl;
}
results = myPeople.search(21.0);
cout << "Search results size by age: " <<
results.getSize() << endl;
list = results.getList();
cout << "\nSearch Results list
contains\n";
for (int i = 0; i < results.getSize(); i++)
{
cout << "Name: " <<
list[i].getName() << endl;
cout << "Age: " <<
list[i].getAge() << endl;
cout << "Speak: " <<
list[i].speak() <<"\n"<< endl;
}
myPeople.remove("Chris");
cout << "Removed by name." << endl;
cout << myPeople.getSize() << "
People"<< endl;
list = myPeople.getList();
cout << "\nMyPeople list contains\n";
for (int i = 0; i < myPeople.getSize(); i++)
{
cout << "Name: " <<
list[i].getName() << endl;
cout << "Age: " <<
list[i].getAge() << endl;
cout << "Speak: " <<
list[i].speak() << "\n" << endl;
}
return 0;
}
Person.h
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
class Person {
private:
std::string name;
double age;
std::string phrase = "";
void makeOlder();
public:
Person();
Person(std::string n, double a);
Person(const Person &b);
double getAge();
std::string getName();
std::string speak();
void setPhrase(std::string p);
bool operator<(const Person & b);
bool operator>(const Person & b);
};
#endif
Person.cpp
#include "Person.h"
Person::Person()
{
name = " ";
age = 0.0;
phrase = "Write anything";
}
Person::Person(std::string n, double a)
{
name = n;
age = a;
phrase = "Hi!!";
}
Person::Person(const Person &b)
{
name = b.name;
age = b.age;
phrase = b.phrase;
}
void Person::makeOlder()
{
age = age + .1;
}
double Person::getAge()
{
//makeOlder();
return age;
}
std::string Person::getName()
{
//makeOlder();
return name;
}
std::string Person::speak()
{
//makeOlder();
return phrase;
}
void Person::setPhrase(std::string p)
{
//makeOlder();
phrase = p;
}
bool Person::operator <(const Person &b) {
if (age < b.age)
return true;
return false;
}
bool Person::operator >(const Person &b) {
if (age > b.age)
return true;
return false;
}
People.h
#ifndef PEOPLE_H_
#define PEOPLE_H_
#include "Person.h"
class People {
int size;
Person *list;
public:
People();
int getSize();
Person * getList();
void add(Person a);
void remove(std::string n);
People search(std::string n);
People search(double a);
~People();
};
#endif
#pragma once
People.cpp
#include "People.h"
#include<iostream>
People::People()
{
size = 0;
list = new Person[0];
}
int People::getSize()
{
return size;
}
Person* People::getList()
{
return list;
}
void People::add(Person a)
{
Person *newlist = new Person[size+1];
int i = 0;
for (i = 0; i < size; i++)
{
newlist[i] = list[i];
}
newlist[size] = a;
size = size + 1;
delete[] list;
list= new Person[size + 1];
list = newlist;
}
void People::remove(std::string n)
{
int i;
for (i = 0; i < size; i++)
{
if (list[i].getName() == n)
{
break;
}
}
if (i < size)
{
Person *newlist = new Person[size -
1];
for (i = 0; i < size; i++)
{
if
(list[i].getName() == n)
{
break;
}
}
for (int j = i; j < size-1;
j++)
{
newlist[j] =
list[j + 1];
}
delete[] list;
list = new Person[size - 1];
list = newlist;
size = size - 1;
}
}
People People::search(std::string n)
{
People searchlist;
for (int i = 0; i < size; i++)
{
if (list[i].getName() == n)
{
searchlist.add(list[i]);
}
}
return searchlist;
}
People People::search(double a)
{
People searchlist;
for (int i = 0; i < size; i++)
{
if (list[i].getAge() == a)
{
searchlist.add(list[i]);
}
}
return searchlist;
}
People::~People()
{
delete[] list;
}
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.