QuestionQuestion

10.8 Coding lab: Person-Employee inheritance hierarchy and polymorphism

The description of this coding lab has been finalized.
Please don't submit your solutions yet until all the test cases are ready.
The main goals of this coding project include:
Implement a simple inheritance hierarchy between the Person and Employee class, where both classes include a pointer-based dynamic array. As a result, you are required to include the big-3 in both classes.
Implement a set of virtual functions to realize polymorphism.
Take advantage of polymorphism to manage a dynamic array that consists of both Person objects and Employee projects.
This project is going to be graded for a total of 40 points.
Description.
The two header files below describe the interface of the base class Person and its derived class Empolyee, respectively. Please implement all the functions declared in these two header files in their corresponding .cpp files.
1. Header file: Person.h
#ifndef Person_h
#define Person_h

#include <iostream>
#include <string>
#include <new>

using namespace std;

class Person{
public:
    //******************Note*********
    //This interface is by no means complete. Please feel free to
    // add more functions if needed. The test cases however will only
    // test the following functions.

    virtual ~Person();
    Person(); //0->SSN, "na"->name, "personal@"->personalEmail, 10->cntPlaces, allocate space to placesVisited and initializes each place to "unknown"
    Person& operator=(const Person& rhs );
    //copy constructor
    Person( const Person &clone );

    string getPlace(int i) const; //return the i-th place in the placesVisited array if i is valid; otherwise return "out-of-boundary".

    virtual string getEmail() const; //return personalEmail
    virtual void setEmail( string new_email); //new_email-->personalEmail
    virtual string getTypeOfObj() const; //return "Person"


private:
    int SSN;
    string name;
    string personalEmail;

    string *placesVisited;
    int cntPlaces; //companion variable of the above pointer
};


#endif /* Person_h */
2. Header file: Employee.h
#ifndef Employee_h
#define Employee_h

#include "Person.h"

class Employee:public Person
{
private:
    string work_email;
    double salary;
    double *sal_change_rates;
    int    cnt_sal_changes;
public:
    virtual ~Employee();
    Employee(); //0.0->salary, 10->cnt_sal_changes, "work@"->work_email, sal_change_rates: allocate memory to hold 10 doubles, each of which has an initial value of 0.0
    Employee( const Employee& clone);
    Employee operator=(const Employee& rhs );

    double getChangeRate( int i) const;////return the i-th rate in the sal_change_rates array if i is valid; otherwise return -1.00

    virtual string getEmail() const; //return work_email
    virtual void setEmail( string new_email); //new_email-->work_email
    virtual string getTypeOfObj() const; //return "Employee"
};

//see below for more details about this function.
void mixedArray( Person** &arrayPersonEmp, int numPersons, int numEmployees);

//see below for more details about this function.
void deleteMixedArray(Person** &arrayPersonEmp, int size );

#endif /* Employee_hpp */
Notice that Employee.h contains two stand-alone functions. The first one
void mixedArray( Person** &arrayPersonEmp, int numPersons, int numEmployees);
The first parameter of this function is an array of Person pointers (i.e., Person *).
This function is going to first allocate an array of (numPersons + numEmployees ) Person pointers.
Next, for each of the first numPersons pointers on this array, say arrayPersonEmp[ i ], this function will create a Person object using the new operator and make it the pointee of arrayPersonEmp[ i ]. Furthermore, it will call Person's setEmail() member function to set each newly created Person object's personalEmail to personal@gmail.com.
Finally, for each of the next numEmployees pointers on this array, say arrayPersonEmp[ j ], this function will create an Employee object using the new operator and make it the pointee of arrayPersonEmp[ j ]. Similarly, it will call Employee's setEmail()member function to set each newly created Employee object's work_email to work@gmail.com.
For example, if numPersons=12 and numEmployees=10, this function will result in the following:
arrayPersonEmp will be an array of 22 Person pointers
each of the first 12 Person pointers' pointees will be a Person object. Each Person object's personalEmail will bepersonal@gmail.com
each of the next 10 Person pointers' pointees will be an Employee object. Each Employee object's work_email will bework@gmail.com.
Good to know: the following statement will make p point to a single Person object:
Person *p = new Person;
The second standalone function is
void deleteMixedArray(Person** &arrayPersonEmp, int size );
This function is going to deallocate the space held by arrayPersonEmp, which is an array of size Person pointers. Each of such Personpointers has a dynamically allocated pointee, which is either a Person object or an Employee one.
Your responsibility in this coding lab is to implement the following three program files:
Person.cpp: implement all the member functions declared in Person.h
Employee.cpp: implement all the member functions and the two stand-alone functions declared in Employee.h
testPersonEmployee.cpp: test your Person and Employee ADTs and the two stand-alone functions.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/*
* File:   Person.cpp
* Author:
*
*
*/

#include "Person.h"

//0->SSN, "na"->name, "personal@"->personalEmail, 10->cntPlaces, allocate space to placesVisited and initializes each place to "unknown"

Person::Person() {
    SSN = 0;
    name = "na";
    personalEmail = "personal@";
    cntPlaces = 10;
    placesVisited = new string[cntPlaces];
    for (int i = 0; i < cntPlaces; i++) {
       placesVisited[i] = "unknown";
    }
}

Person::Person(const Person& o) {
    SSN = o.SSN;
    name = o.name;
    personalEmail = o.personalEmail;
    cntPlaces = o.cntPlaces;
    placesVisited = new string[cntPlaces];
    for (int i = 0; i < cntPlaces; i++) {
       placesVisited[i] = o.placesVisited[i];
    }
}

Person::~Person() {
    if (placesVisited != 0) {
       delete [] placesVisited;
    }
    placesVisited = 0;
}

string Person::getEmail() const {
    return personalEmail;
}

//return the i-th place in the placesVisited array if i is valid; otherwise return "out-of-boundary".

string Person::getPlace(int i) const {
    if (i < 0 || i >= cntPlaces) {
       return "out-of-boundary";
    }
    return placesVisited[i];
}...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$61.20
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats