Question

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 Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse 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];
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor

Related Homework Solutions

C Programming Problems
Homework Solution
$23.00
Computer Science
C Programming
Algorithms
Lowercase Letters
Sentences
ASCII Values
Integers
Functions
Iterations
Console Standard Output
Students
Command Line
C Programming Questions
Homework Solution
$10.00
Computer Science
C Programming
Variables
Integers
Data Types
Pages
Statements
Codes
Algorithms
Errors
C++ Programming Problems
Homework Solution
$40.00
Computer Science
C++ Programming
Algorithms
Functions
Recursion
Positive Integers
Reverse Order
Digits
Arguments
Nodes
Strings
Definitions
Grade Books Using Data Structures
Homework Solution
$60.00
Computer Science
Programming
Data Structures
Algorithms
C Languages
Grade Books
Courses
Students
Average Values
Header Files
Functions
Variables
Statements
Fun with Binary Search Trees
Homework Solution
$63.00
Computer Science
C++ Programming
Binary Search Trees
Keys
Software Engineering
Repositories
Projects
Integers
Data
Nodes
Member Functions
Pairs
Pseudocodes
C Programming Problems
Homework Solution
$20.00
Computer Science
C Programming
Algorithms
Switch Statements
Logic
Operators
Division
Numbers
Break Statements
Leap Years
Lines
Get help from a qualified tutor
Live Chats