Help with C++ Programming : Objective: The main goal is to implement a simple in
ID: 3839417 • Letter: H
Question
Help with C++ Programming :
Objective: The main goal is to implement a simple inheritance hierarchy between the Person and Employee class.
Description. Declare and implement the Employee class as a derived class of the Person class, which is already implemented in the demo code posted under this week (week #15). Note that your project doesn't need to include the Student class. Please also feel free to modify the Person class. For instance, you can replace the data type for birthday to string.
Specifically, your project will include Person as the base class, and Employee as the derived class of Person. Your Employee class will include the following data members:
year of employment (e.g., 2012)
base salary
a dynamic array to keep track of the annual salary increase rate since the year of employment. For instance, an employee joined the company in year 2013, his salary increase rates in 2014, 2015, and 2016 were 10%, 7%, and 11%, respectively. This dynamic array will then hold the following three numbers: 0.1, 0.07, and 0.11. Furthermore, let bSal be the base salary, the current salary of the above employee should be equal to: bSal*1.10*1.07*1.11. Note that although the size of this array is derivable from the year of employment, you are recommended to include two additional data members to manage the capacity and size of this array. (This is similar to what you did in the previous homework.)
At the minimum, your Employee class needs to include the following function members:
a default constructor
the big-3: destructor, copy constructor and overloaded assignment operator
an accessor to return the current salary of an employee
an accessor and mutator for each of the above data members. It's sufficient to return the salary increase rate in a specific year.
an overloaded put operator (<<) to print out all the information in an Employee object
override the output() function in the base class, as this function is virtual in Person.
Please provide a simple character-based interface to allow the graders to test each of the above member functions. Make sure you test the output() function using Person pointers and observe polymorphism in action.
Submission requirements: Please use separate compilation and submit your source code in one zip files. Your project will include two header files (one for the Person class, the other for the Employee class) and three .cpp files.
This is just Demo Code:
person.h
#ifndef _PERSON_H
#define _PERSON_H
#include <iostream>
#include <string>
#include "dayOfYear.h"
using namespace std;
class Person{
public:
//equalID() returns true:have the same ID; false: otherwise
friend bool equalID(const Person & person1, const Person &Person2);//
~Person(); //destructor: to release any memory that has been allocated to the object
Person(); //default constructor: id=-1, name="NA", birthday="1/1/2000"
Person( int new_id, string new_name, DayOfYear date); //constructor that initializes
//id to new_id, name to new_name, and birthday to date
Person(const Person & someone); //copy constructor: construct a new object as a copy of "someone"
void operator= (const Person &rhs); //assign the object on the right-hand-side to the left-hand-side
virtual void output() const; //print out a person's info.
int get_id() const; //return the id of a Person object
string get_name() const; //return the name of a Person object
const DayOfYear get_birthday() const; //return the birthday of a Person object
string getEmail( int i) const; //return the i-th email if exists; o.w: return "NA"
void set_id(int new_id); //change a person's id to new_id
void set_name(string new_name); // change a person's name to new_name
void set_birthday(DayOfYear new_Date); //change a person's birthday to new_date
void add_email( string the_email); //add the_email to the list
int get_num_emails() const;
private:
int id;
string name;
DayOfYear birthday;
string *emails; //list of email addresses
int numEmails;
};
void print( const Person& someone); //print out a person's id and name
bool sameID(const Person& person1, const Person& person2);//true:have the same ID; false: otherwise
//search whether there exists a student on the studentList with id=key
bool searchStudent( Person *studentList, int num_students, int key);
#endif
dayOfYear.h
#ifndef _DAYOFYEAR_H
#define _DAYOFYEAR_H
#include <iostream>
using namespace std;
class DayOfYear{
public:
friend bool equal(DayOfYear date1, DayOfYear date2);//a friend function that compares two objects
//yes: if date1 is the same as date2; no: otherwise
DayOfYear();//default constructor: 1/1/2009
DayOfYear(int new_day, int new_month, int new_year);//
void output() const; //
int get_day(); // return the value of "day"
int get_month();
int get_year();
void set_DayOfYear(int new_day, int new_month, int new_year); //new_day-->day, new_month->month, new_year-->year
void set_day(int new_day); // day-->new_day
void set_month(int new_month);
void set_year(int new_year);
private:
int day;
int month; //int --> char *
int year;
};
person.cpp
#include "person.h"
Person::~Person()
{
if ( emails != NULL ){
delete [] emails;
emails = NULL;
}
}
Person::Person()
{
id = -1;
name ="NA";
//birthday = DayOfYear( 1,1, 2000);
emails = NULL;
numEmails = 0;
}
Person::Person(int new_id, string new_name, DayOfYear date)
{
id = new_id;
name = new_name;
//birthday = date;
emails=NULL;
numEmails=0;
}
/**/
Person::Person(const Person & someone)
{
id = someone.id;
name = someone.name;
birthday = someone.birthday;
numEmails = someone.numEmails;
//allocate space to *emails if numEmails>0
if (numEmails == 0)
emails = NULL;
else{
emails = new string [numEmails];
for (int i=0;i<numEmails; i++)
emails[ i ] = someone.emails[ i ];
}
}
/**/
/**/
void Person::operator =(const Person & rhs)
{
id = rhs.id;
name = rhs.name;
birthday = rhs.birthday;
//allocate space to *emails if needed
if (numEmails > 0)
delete [] emails; //release the old memory
numEmails = rhs.numEmails;
emails = new string [numEmails];
for (int i=0;i<numEmails; i++)
emails[ i ] = rhs.emails[ i ];
}
/**/
void Person::output() const
{
// id = 200;
cout << "-------------------------- ";
cout << " id=" << (*this).id << " name=" << this->name << " birthday=";
birthday.output();
for (int i=0; i<numEmails; i++)
cout << "email-#" << i << ": " << emails[i] ;
cout << " -------------------------- ";
}
int Person::get_id() const
{
return id;
}
string Person::get_name() const
{
return name;
}
const DayOfYear Person::get_birthday() const
{
return birthday;
}
void Person::set_id(int new_id)
{
id = new_id;
}
void Person::set_name(string new_name)
{
name = new_name;
}
void Person::set_birthday(DayOfYear date)
{
birthday = date;
}
string Person::getEmail( int i) const
{
if (i>=0 && i<numEmails)
return emails[i];
else
return "NA";
}
void Person::add_email( string the_email)
{
//case 1: emails list is empty
if ( numEmails == 0){
emails = new string[1];
//verification
emails[0] = the_email;
}
else { //case 2: expand the list
string *tmp_emails=new string [numEmails];
for (int i=0; i<numEmails; i++) //save the emails
tmp_emails[i] = emails[ i ];
delete [] emails;
emails = new string [numEmails+1]; //expand the list by one
//verify the above allocation
for ( int i=0; i<numEmails; i++)
emails[ i ] = tmp_emails[ i ]; //copy the existing emails
emails[ numEmails ] = the_email; // add the new email
delete [] tmp_emails;
}
numEmails += 1;
}
int Person::get_num_emails() const
{
return numEmails;
}
void print( const Person& someone)
{
cout << "****"<<someone.get_id() << " " << someone.get_name() <<endl;
}
//perform a sequential search on the studentList
bool searchStudent( Person *studentList, int num_students, int key)
{
for ( int i=0; i<num_students; i++){
if ( studentList[ i ].get_id() == key )//found
return true;
}//for (i)
return false; //none of the students has ID=key
}
bool sameID(const Person& person1, const Person& person2)
{
return (person1.get_id() == person2.get_id());
}
bool equalID(const Person& person1, const Person& person2)
{
return (person1.id == person2.id);
}
personnel.cpp
#include "dayOfYear.h"
#include "person.h"
int main()
{
DayOfYear date(20, 7, 1985);
Person mary_p( 101, "Mary", date);
Student mary_s( 101, "Mary", date, 1), mary_c;
// Person & mary_p = mary_s;
Person & person1 = mary_s;
mary_s.output(); //student verion
person1.output();
/*
mary_p.output();
mary_s.Person::output();//person version
print( mary_s );
mary_s.add_email("@....");
mary_s.set_num_grades(2);
mary_s.set_grade(1, 98.00);
mary_s.set_grade(2, 100.00);
mary_c = mary_s;
cout << mary_s;
cout << mary_c;
*/
//mary_clone.set_id(202);
// print(mary_clone);
//mary_clone.output();
// mary = mary_clone;
// mary.output();
//sameID(mary, mary_clone);
// equalID(mary, mary_clone);
/**the code below create a dynamic array of Person
***
//1 & 2:
Person *students213 = NULL; //
int num = 200; //size of the dynamic array *students213
//3. allocate memory to students213
students213 = new Person [ num ];
//4. verify
if (students213 == NULL ){
cerr << "Memory allocation failure. ";
exit( -1 );
}
//5: use
for (int i=0; i<num; i++){
students213[ i ].set_id( i + 100);
students213[ i ].output();
}
//search based on id
cout << "Is there a student with ID=207? ";
if (searchStudent( students213, num, 207) )
cout << " Yes."<<endl;
else
cout << " No. "<<endl;
cout << "Is there a student with ID=507? ";
if (searchStudent( students213, num, 507) )
cout << " Yes."<<endl;
else
cout << " No. "<<endl;
//6&7: release the memory
delete [] students213;
students213 = NULL;
*****
**end of the dynamic array students213
**************************************/
return 0;
}
Explanation / Answer
Note: User given code is modified.
Answer:
File Name: Person.h
#ifndef _PERSON_H
#define _PERSON_H
#include <iostream>
#include <string>
using namespace std;
class Person{
public:
//equalID() returns true:have the same ID; false: otherwise
friend bool equalID(const Person & person1, const Person &Person2);//
~Person(); //destructor: to release any memory that has been allocated to the object
Person(); //default constructor: id=-1, name="NA", birthday="1/1/2000"
Person( int new_id, string new_name, string date); //constructor that initializes
//id to new_id, name to new_name, and birthday to date
Person(const Person & someone); //copy constructor: construct a new object as a copy of "someone"
void operator= (const Person &rhs); //assign the object on the right-hand-side to the left-hand-side
int get_id() const; //return the id of a Person object
string get_name() const; //return the name of a Person object
const string get_birthday() const; //return the birthday of a Person object
void set_id(int new_id); //change a person's id to new_id
void set_name(string new_name); // change a person's name to new_name
void set_birthday(string new_Date); //change a person's birthday to new_date
protected:
int id;
string name;
string birthday;
};
void print( const Person& someone); //print out a person's id and name
#endif
File Name: Person.cpp
#include "Person.h"
Person::~Person()
{
}
Person::Person()
{
id = -1;
name ="NA";
birthday = "1/1/2017";
}
Person::Person(int new_id, string new_name, string date)
{
id = new_id;
name = new_name;
birthday = date;
}
/**/
Person::Person(const Person & someone)
{
id = someone.id;
name = someone.name;
birthday = someone.birthday;
}
/**/
/**/
void Person::operator =(const Person & rhs)
{
id = rhs.id;
name = rhs.name;
birthday = rhs.birthday;
}
int Person::get_id() const
{
return id;
}
string Person::get_name() const
{
return name;
}
const string Person::get_birthday() const
{
return birthday;
}
void Person::set_id(int new_id)
{
id = new_id;
}
void Person::set_name(string new_name)
{
name = new_name;
}
void Person::set_birthday(string date)
{
birthday = date;
}
void print(const Person& someone)
{
cout << "****"<<someone.get_id() << " " << someone.get_name() <<endl;
cout<<"someone.get_birthday()<<endl;
}
bool equalID(const Person& person1, const Person& person2)
{
return (person1.id == person2.id);
}
File Name: Employee.h
#ifndef _EMPLOYEE_H
#define _EMPLOYEE_H
#include "Person.h"
#include <iostream>
#include <string>
class Employee:public Person
{
private:
int joinedYear;
double bSal;
double *increaseRate;
int capacity;
int size;
public:
Employee();
Employee(int new_id, string new_name, string date,int jyear,double bsal);
Employee(const Employee & someone);
void operator =(const Employee & rhs);
double getCurrentSalary();
void setJoinedYear(int year);
void addIncreaseRate(double newRate);
int getJoinedYear();
friend ostream &operator <<(ostream &cout, const Employee & rhs);
}
#endif
File Name: Employee.cpp
#include <iostream>
#include "Employee.h"
Employee::Employee():Person()
{
joinedYear=0;
bSal=0;
capacity=100;
increaseRate=new double[capacity];
size=0;
}
Employee::Employee(int new_id, string new_name, string date,int jyear,double bsal):Person(new_id, new_name, date)
{
joinedYear=jyear;
bSal=bsal;
capacity=100;
increaseRate=new double[capacity];
size=0;
}
Employee::Employee(const Employee & someone):Person(someone.get_id(), someone.get_name(), someone.get_birthday())
{
joinedYear=someone.joinedYear;
bSal=someone.bSal;
capacity=someone.capacity;
increaseRate=new double[capacity];
for(int kk=0;kk<someone.size;kk++)
increaseRate[kk]=someone.increaseRate[kk];
size=someone.size;
}
void Employee::operator =(const Employee & someone):Person(someone.get_id(), someone.get_name(), someone.get_birthday())
{
joinedYear=someone.joinedYear;
bSal=someone.bSal;
capacity=someone.capacity;
increaseRate=new double[capacity];
for(int kk=0;kk<someone.size;kk++)
increaseRate[kk]=someone.increaseRate[kk];
size=someone.size;
}
double Employee::getCurrentSalary()
{
double tmp=bSal;
double rate=1;
if(size>0)
{
for(int kk=0;kk<size;kk++)
rate=rate *(1+increaseRate[kk]);
tmp=bSal*rate;
}
return tmp;
}
void Employee::setJoinedYear(int year)
{
joinedYear=year;
}
void Employee::addIncreaseRate(double newRate)
{
if(size<capacity)
{
increaseRate[size]=newRate;
size++;
}
}
int Employee::getJoinedYear()
{
return joinedYear;
}
ostream& operator <<(ostream &cout, const Employee & rhs)
{
cout<<"Id:"<<rhs.get_id()<<endl;
cout<<"Name:"<<rhs.get_name()<<endl;
cout<<"Birthday:"<<rhs.get_birthday()<<endl;
cout<<"Joined Year:"<<rhs.getJoinedYear()<<endl;
cout<<"Base Salary:"<<rhs.bSal()<<endl;'
cout<<"Current Salary:"<<rhs.getCurrentSalary()<<endl;
return cout;
}
File Name: main.cpp
#include <iostream>
#include "Person.h"
#include "Employee.h"
#include <string>
using namespace std;
int main()
{
Employee myEmployee(123, "Tom","1/12/1987",2013,1000);
myEmployee.addIncreaseRate(.10);
myEmployee.addIncreaseRate(.07);
myEmployee.addIncreaseRate(.11);
cout<<myEmployee<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.