Question

Review the provided code. The following files are provide: Driver.cpp: This is a very basic driver...

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


Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
Utilize the code from last week Add a default, full, and copy constructor. Also add a...
Utilize the code from last week Add a default, full, and copy constructor. Also add a constructor that allows you to specify only the name of the video game with no high score or times played specified. Adjust your code to demonstrate use of all 4 constructors and output of the resulting objects. #include <iostream> #include <string> #include <iomanip> using namespace std; class VideoGame { private:     string name;     int highScore;     int numOfPlays; public:     VideoGame() {        ...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read code and all comments carefully): Student.h StudentList.h StudentList.cpp main.cpp This program: Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name. Displays the list Read and understand this program, then do the following: finish writing Student.h and other files fix errors in StudentList.cpp Student.h: #ifndef STUDENT_H #define STUDENT_H //using namespace std; //<==== This statement //...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...
In main.cpp, write a templated function more which takes in two variables of the same type...
In main.cpp, write a templated function more which takes in two variables of the same type and returns whichever variable is greater than (>) the other. You can and may need to add something to the food class in order for more to be able to be called properly. //food.h #ifndef _FOOD_H #define _FOOD_H #include <iostream> #include <string> using namespace std; class Food { private: string name; int quantity; public: Food(); void setName(string newName); void setQuantity(int newQuantity); string getName(); int...
Circle.cpp /******************************************************* * * Assignment: Lab 2 *    * Class member functions look like other...
Circle.cpp /******************************************************* * * Assignment: Lab 2 *    * Class member functions look like other functions * with the following differences: * 1) The function name has the format Class::Method * 2) The function can access any private or public data * defined for the class, e.g. "radius" in this example * 3) The constructor function(s) have no return data type ********************************************************/ #include <iostream> #include <string> #include "circle.h" // the definion of our Circle class using namespace std; /****************************************************...
Below is my C++ program. There is a function to add data, display it and search...
Below is my C++ program. There is a function to add data, display it and search the data. Just add two more functions to this porgram. One function to update the student data and second function to delete the data. Also, change the display function little bit. Right now, it shows all data entered. Make it so you have to put the uin number and it only displays the data of that specific student. #include <iostream> #include <string> using namespace...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT