Name and ID not showing up in Inheritance Assignment

So i have an assignment to make an Employee program with Inheritance but the name and the ID isn't showing up in the output. I know the issue is either with my constructors or getName, as for the ID i know its probably because of getID but i'm stumped on how to fix it any ideas here is the output the assignment is looking for:

Name: Peter
ID: XXX-L (where X is the employee number and L is the employee letter)
Hourly Pay Rate: 35.89

here are my 4 header and cpp file codes:

Employee.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "Employee.h"
#include <sstream>

Employee::Employee() {
    name = "";
    number = 0;
    letter = 0;
}

Employee::Employee(string newName, int newNumber, char newLetter) 
{
	name = newName;
	number = newNumber;
	letter = newLetter;

}

Employee::Employee(string newName) 
{
    name = newName;
}

Employee::Employee(int newNumber) 
{
    number = newNumber;
}

Employee::Employee(char newLetter) 
{
    letter = newLetter;
}

string Employee::getName() 
{
    return name;
}

string Employee::getID() 
{
    string result1;
    ostringstream convert;
    convert << number;
    result1 = convert.str();

    convert << int(letter);
    string result2 = convert.str();
    // TODO Return the ID with the matching format
	return number + "-" + letter;
}

void Employee::setName(string newName) 
{
    name = newName;
}

void Employee::setID(int newNumber, char newLetter) 
{
	number = newNumber;
	letter = newLetter;
}



Employee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#if !defined(_EMPLOYEE_H)
#define _EMPLOYEE_H
#include <string>

using namespace std;
class Employee {
public:
	Employee();
	Employee(string newName, int newNumber, char newLetter);
	Employee(string newName);
	Employee(int newNumber);
	Employee(char newLetter);
	string getName();
	string getID();
	void setName(string newName);
	void setID(int newNumber, char newLetter);
private:
	string name;
	int number;
	char letter;
};
#endif  //_EMPLOYEE_H 


ProductionWorker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "ProductionWorker.h"
#include <iostream>

using namespace std;

//TODO Write the constructor ProductionWorker(string newName, int newNumber, char newLetter, int newShift, double newHourlyPayRate)
ProductionWorker::ProductionWorker(string newName, int newNumber, char newLetter, int newShift, double newHourlyPayRate)
{
	newName = "";
	newNumber = 0;
	newLetter = 0;
	shift = newShift;
	hourlyPayRate = newHourlyPayRate;
}

int ProductionWorker::getShift() 
{
    return shift;
}

double ProductionWorker::getHourlyPayRate() 
{
	return hourlyPayRate;
}

void ProductionWorker::setShift(int newShift) {
    shift = newShift;
}

void ProductionWorker::setHourlyPayRate(double newHourlyPayRate) {
	hourlyPayRate = newHourlyPayRate;
}

int main(){
    ProductionWorker *peter = new ProductionWorker("Peter", 12345, 'P', 1, 35.89);
    cout << "Name: " << peter->getName() << endl
         << "ID: " << peter->getID() << endl
	 << "Hourly pay rate: " << peter->getHourlyPayRate() << endl;

	cin.ignore(1124, '\n');
	cin.get();

    return 0;
}

ProductionWorker.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#if !defined(_PRODUCTIONWORKER_H)
#define _PRODUCTIONWORKER_H

#include "Employee.h"

class ProductionWorker : public Employee {
public:
	ProductionWorker(string newName, int newNumber, char newLetter, int newShift, double newHourlyPayRate);
	int getShift();
	double getHourlyPayRate();
	void setShift(int newShift);
	void setHourlyPayRate(double newHourlyPayRate);
private:
	int shift;
	double hourlyPayRate;
};

#endif  //_PRODUCTIONWORKER_H 
Last edited on
We'd expect name, number and letter passed to ProductionWorker would eventually be stored in the Employee part of ProductionWorker. But it isn't.

You'd do this by using the Employer contructor within the ProductionWorker constructor. Now, C++ is a strongly typed language, so why isn't the type system stopping you from making a "type" mistake? Because Employee is a default constructor, and the compiler is using that within ProductionWorker.

So first, remove the default constructor from Employee.

Next, initialize the Employee part of ProductionWorker.
1
2
3
4
5
6
ProductionWorker::ProductionWorker(string newName, int newNumber, char newLetter, int newShift, double newHourlyPayRate) :
  Employee(newName, newNumber, newLetter),
  shift(newShift),
  hourlyPayRate(newHourlyPayRate)
{
}


Next, although you haven't hit any problems yet, object in C++ always use virtual functions. Also, the base class must have a virtual destructor. If you don't do this, the object oriented stuff doesn't work properly for a variety of reasons. So, make the methods virtual, and add a destructor to Employee.
Thank you its working now
Topic archived. No new replies allowed.