Inheritance help and advice needed

I'm trying to inherit my displayData() for my derived OrdinryEmplyee class from my base Employee class. However I need some guidance on how can I do that..

Employee.h
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
#include <iostream>
#include <sstream>


using namespace std;

/*================================================= Employee Class ====================================================*/

class Employee
{
protected:
int id;
string name;
string dob;
string address;
string phoneNo;
string email;
int deptNo;
float basicPay;

public:
Employee();
Employee(int empId, string empName, string empDOB, string empAdd, string phone, string emailAdd, int deptNum, float bPay);
void setFields(int empId, string empName, string empDOB, string empAdd, string phone, string emailAdd, int deptNum, float bPay);
string toString();

// Get&Set Function for id
int getId();
void setId(int empId);
// Get&Set Function for name
string getName();
void setName(string empName);
// Get&Set Function for dob
string getDOB();
void setDOB(string empDOB);
// Get&Set Function for address
string getAddress();
void setAddress(string empAdd);
// Get&Set  Function for phoneNo
string getPhone();
void setPhone(string phone);
// Get&Set  Function for email
string getEmail();
void setEmail(string emailAdd);
// Get&Set  Function for deptNo
int getDeptNo();
void setDeptNo(int deptNum);
// Get$Set  Functions for basicPay
float getBasicPay();
void setBasicPay(float bPay);

//float calculatePay();
//float calculateTax
virtual void displayData();


// End of Employee class declarations
}; 


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <sstream>
#include "Employee.h"

/*================================================ Functions Definitions ==================================================*/

//Employee default constructor
Employee::Employee()
{
 id = 0;
 name = "";
 dob = "";
 address = "";
 phoneNo = "";
 email = "";
 deptNo = 0;
 basicPay = 0.0;
}

//Employee 2nd constructor
Employee::Employee(int empId, string empName, string empDOB, string empAdd, string phone, string emailAdd, int deptNum, float bPay)
{
	id = empId;
	name = empName;
	dob = empDOB;
	address = empAdd;
	phoneNo = phone;
	email = emailAdd;
	deptNo = deptNum;
	basicPay = bPay;
}

// Get&Set Function for id
int Employee::getId()
{
   return id;
}
void Employee::setId(int empId)
{
   id = empId;
}
// Get&Set Function for name
string Employee::getName()
{
   return name;
}
void Employee::setName(string empName)
{
   name = empName;
}
// Get&Set Function for dob
string Employee::getDOB()
{
   return dob; 
}
void Employee::setDOB(string empDOB)
{
   dob = empDOB;
}
// Get&Set Function for address
string Employee::getAddress()
{
   return address;
}
void Employee::setAddress(string empAdd)
{
   address = empAdd;
}
// Get&Set Function for phoneNo
string Employee::getPhone()
{
   return phoneNo;
}
void Employee::setPhone(string phone)
{
   phoneNo = phone;
}
// Get&Set Function for email
string Employee::getEmail()
{
   return email;
}
void Employee::setEmail(string emailAdd)
{
   email = emailAdd;
}
// Get&Set Function for deptNo
int Employee::getDeptNo()
{
   return deptNo;
}
void Employee::setDeptNo(int deptNum)
{
   deptNo = deptNum;
}

// Get&Set Function for basicPay
float Employee::getBasicPay()
{
   return basicPay;
}
void Employee::setBasicPay(float bPay)
{
   basicPay = bPay;
}

/*string Employee::toString()
{
stringstream data;
data.str("");

data << "Employee's id            : " << Employee::id << endl;
data << "Employee's name          : " << Employee::name << endl;
data << "Employee's DOB           : " << Employee::dob << endl;
data << "Employee's address       : " << Employee::address << endl;
data << "Employee's phone no.     : " << Employee::phoneNo << endl;
data << "Employee's email address : " << Employee::email << endl;
data << "Employee's dept. no.     : " << Employee::deptNo << endl;
data << "Employee's basic pay     : $" << Employee::basicPay <<endl <<"\n";

return data.str();
}*/


void Employee::displayData()
{
   //cout<<Employee::toString();
   cout << "Employee's id            : " << Employee::id << endl;
   cout << "Employee's name          : " << Employee::name << endl;
   cout << "Employee's DOB           : " << Employee::dob << endl;
   cout << "Employee's address       : " << Employee::address << endl;
   cout << "Employee's phone no.     : " << Employee::phoneNo << endl;
   cout << "Employee's email address : " << Employee::email << endl;
   cout << "Employee's dept. no.     : " << Employee::deptNo << endl;
   cout << "Employee's basic pay     : $" << Employee::basicPay <<endl <<"\n";
}

/*	float Employee::calculateGrossPay(float bPay)
{
   float netPay = 0.8 * basicPay;
   
}*/
   

void Employee::setFields(int empId, string empName, string empDOB, string empAdd, string phone, string emailAdd, int deptNum, float bPay)
{
	id = empId;
	name = empName;
	dob = empDOB;
	address = empAdd;
	phoneNo = phone;
	email = emailAdd;
	deptNo = deptNum;
	basicPay = bPay;
}



OrdinaryEmployee.h
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
#include <iostream>
#include <string>


using namespace std;

class OrdinaryEmployee : public Employee
{
private:
   float hourlyRate;
   float noOfOTHours;
   float yearlyBonus;
public:
   OrdinaryEmployee(int empId, string empName, string empDOB, string empAdd, string phone, string emailAdd, int deptNum, float bPay, float hrlyRate, float OTHours, float bonus);
   
// Get&Set Functions for hourlyRate
   float getHourlyRate();
   void setHourlyRate(float hrlyRate);
   
// Get&Set Functions for noOfOTHours
   float getNoOfOTHours();
   void setNoOfOTHours(float OTHours);
   
// Get&Set Functions for yearlyBonus
   float getYearlyBonus();
   void setYearlyBonus(float bonus);
   
   //calculatePay() <-- needs to override
   void displayData(); // needs to be overridden
};


OrdinaryEmployee.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
#include <iostream>
#include "OrdinaryEmployee.h"


// Functions Implementation

// Get&Set Functions for hourlyRate
float OrdinaryEmployee::getHourlyRate()
{
   return hourlyRate;
}
void OrdinaryEmployee::setHourlyRate(float hrlyRate)
{
   hourlyRate = hrlyRate;
}
// Get&Set Functions for noOfOTHours
float OrdinaryEmployee::getNoOfOTHours()
{
   return noOfOTHours;
}
void OrdinaryEmployee::setNoOfOTHours(float OTHours)
{
   noOfOTHours = OTHours;
}
// Get&Set Functions for yearlyBonus
float OrdinaryEmployee::getYearlyBonus()
{
   return yearlyBonus;
}
void OrdinaryEmployee::setYearlyBonus(float bonus)
{
   yearlyBonus = bonus;
}

//calculatePay()

void OrdinaryEmployee::displayData()
{
   cout << "Employee's id              : " << Employee::id << endl;
   cout << "Employee's name            : " << Employee::name << endl;
   cout << "Employee's DOB             : " << Employee::dob << endl;
   cout << "Employee's address         : " << Employee::address << endl;
   cout << "Employee's phone no.       : " << Employee::phoneNo << endl;
   cout << "Employee's email address   : " << Employee::email << endl;
   cout << "Employee's dept. no.       : " << Employee::deptNo << endl;
   cout << "Employee's basic pay       : $" << Employee::basicPay <<endl;
   cout << "Employee's Hourly Rate     : $" << Employee::hourlyRate <<endl;
   cout << "Employee's no. of OT hours : " << Employee::noOfOTHours <<endl;
   cout << "Employee's yearly bonus    : $" << Employee::yearlyBonus <<endl<< "\n";
}


I'm getting an error saying my 3 fields from my derived class is not member of Employee - hourlyRate, noOfOTHours, yearlyBonus.

How can I make it inherit from my base class Employee by calling out displayData()? Such that it will print any additional fields that the derived class have.
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <sstream>
#include <string>
#include "Employee.cpp"	
#include "OrdinaryEmployee.cpp"
//#include "SeniorEmployee.h"

using namespace std;


int dataCounter = 0;
const static int MAXIMUM_DATA_SIZE = 100;
Employee empArray[MAXIMUM_DATA_SIZE];

// To prevent invalid input type (int)
int getline_i()
{
	while(cin.fail()) {
		cin.clear();
	}
	string s;
	getline(cin, s);
	stringstream ss;
	ss.str(s);

	int i;
	ss >> i;
	return i;
	}
	
// To prevent invalid input type (float)
float getline_f()
{
	while(cin.fail()) {
		cin.clear();
	}
	string s;
	getline(cin, s);
	stringstream ss;
	ss.str(s);

	float f;
	ss >> f;
	return f;
}



// Add new employee data function
Employee addEmpData()
{
int id;
string name;
string dob;
string address;
string phoneNo;
string email;
int deptNo;
float basicPay;

cout<<"\n[  Add new employee data  ]\n"<<endl
<<"Enter new id : ";
id = getline_i();
cout<<"Enter new name : ";
getline(cin, name);
cout<<"Enter your date of birth : ";
getline(cin, dob);
cout<<"Enter your address : ";
getline(cin, address);
cout<<"Enter your phone number : ";
getline(cin, phoneNo);
cout<<"Enter your email address : ";
getline(cin, email);
cout<<"Enter your dept. number : ";
deptNo = getline_i();
cout<<"Enter your basic pay : ";
basicPay = getline_f();
cout<<"\n\nNew employee data have been keyed into the system...\n";

Employee emp = Employee(id, name, dob, address, phoneNo, email, deptNo, basicPay);

return emp;

}

string toUppercase(string s)
{
    string upperS = s;
    int sLen = s.length();
    for(int i = 0; i < sLen; i++)
    {
        upperS[i] = toupper(upperS[i]);
    }
    return upperS;
}

void searchAndDisplayEmployee(string searchFor)
{
	bool matchFound = false;
	
    searchFor = toUppercase(searchFor);

	for(int i = 0; i < dataCounter; i++)
	{
		string emp = toUppercase(empArray[i].getName());
		
		if(emp.find(searchFor) != -1)
		{
			empArray[i].displayData();
			cout << endl;
			
			matchFound = true;
		}
	}
	
	if(!matchFound)
	{
		cout << "No match found." << endl;
	
	}
}



int main()
{

// Instantiate few employee data
empArray[0] = Employee(1, "John Tan", "12/09/1987", "Yishun", "98765432", "johntan@yahoo.com", 1, 2300.0);
empArray[1] = Employee(2, "Pierre Png", "13/04/1984", "Bedok", "91234567", "pierrepng@yahoo.com", 2, 4000.0);
empArray[2] = Employee(3, "Rose Lau", "17/03/1999", "Pasir Ris", "98674646", "rosly@gmail.com", 3, 1700.0);
empArray[3] = Employee(4, "Vincent Tan", "28/07/1986", "Braddel", "98787878", "vincenttan@live.com", 4, 3600.0);
empArray[4] = Employee(5, "Kelvin Lau", "16/03/1976", "Punggol", "90909093", "kelvinlau@gmail.com", 5, 5000.0);
//empArray[5] = OrdinaryEmployee(6, "Ronald Toh", "10/09/1967", "Yee Tew", "90909898", "ronaldtoh@live.com", 6, 6000.0, 20.0, 3.0, 20000); 
//empArray[6] = OrdinaryEmployee(7, "Eric Kwek", "09/09/1983", "Orchard", "91234123", "erickwek@live.com", 7, 7000.0, 30.0, 4.0, 30000);

dataCounter += 5;

int choice = 0;

while(true) {

	cout<<"\n\n";
	cout<<"Welcome to the Salary System"  <<endl
	    <<"1) Add New Employee Data"           <<endl
	    <<"2) Display Employee's Profile" <<endl
	    <<"3) Search for Employee by Name" <<endl
	    <<"4) Quit"			      
	    <<endl;
	// Waiting for user to enter choice
	cout<<"Please enter your choice: ";
	choice = getline_i();
	
	if( choice < 1 || choice > 4 ) {
	cout<<"Invalid input ! Please enter your choice again...";
	choice = 0;
	}
	
	if(choice == 1) {
	if(dataCounter < MAXIMUM_DATA_SIZE)
	{
	empArray[dataCounter++] = addEmpData();
	}
	    		} // End of option 1
	    		
	else if(choice == 2) {
	for(int i = 0; i < dataCounter; i++)
	{
	empArray[i].displayData();
	}
			     } // End of Option 2
	    		
	else if(choice == 3) {
	string searchFor;
	stringstream ss;
	ss.str("");
	cout<<"\nSearch by Employee's name : ";
	getline(cin, searchFor);
	ss << searchFor;
	searchFor = ss.str();
	cout<<"\n";
	searchAndDisplayEmployee(searchFor);
		
			      } // End of option 3
			      
	else if(choice == 4) {
	cout<< "\nHave a nice day, good bye.\n\n";
	break;
			      } // End of option 3
}
}
I'm getting an error saying my 3 fields from my derived class is not member of Employee - hourlyRate, noOfOTHours, yearlyBonus.
The compiler is right they are not. Omit the 'Employee::' in front of those. You can access the variable of the base class always without 'Employee::'. (Or you can't when it's private)

You need this 'base_class::' only when the variable/function is hidden


How can I make it inherit from my base class Employee by calling out displayData()? Such that it will print any additional fields that the derived class have.
Make the function virtual (write that keyword in front of the function) in the base class and call the base function like so: Employee::displayData();

So you don't have to repeat code from the base class
You mean those OrdinaryEmployee::getHourlyRate?
I thought those are out of the class scope that's why need OrdinaryEmployee to access it?

I already declared a virtual void displayData() in my base class as shown in the codes.
So for my OrdinaryEmployee class, do I use Employee::displayData? or OrdinaryEmployee::displayData()?
Anyone can help on this?
You can always access member through ther (correct) scope operator ::. If you are within that scope then you don't need it. If you inherit from a base class you're automatically within that class scope too. So no scope operator is needed if you want to access a member (variable or function that is) of that base class.

if you have a member with the same name like in the base class than you hide that member of the base class. You must specify the sope of the base class in order to call the function of the base class.

So yes in OrdinaryEmployee::displayData() you must call Employee::displayData(). If you'd call OrdinaryEmployee::displayData() you'd hav a endless recusion.

hope the make things somewhat clear?
Following on from -- and repeating bits of -- the previous posts, you can use Employee::displayData() from Oridnary::displayData()....

1
2
3
4
5
6
7
8
9
10
11
void Employee::displayData()
{
   cout << "Employee's id            : " << id << endl;
   cout << "Employee's name          : " << name << endl;
   cout << "Employee's DOB           : " << dob << endl;
   cout << "Employee's address       : " << address << endl;
   cout << "Employee's phone no.     : " << phoneNo << endl;
   cout << "Employee's email address : " << email << endl;
   cout << "Employee's dept. no.     : " << deptNo << endl;
   cout << "Employee's basic pay     : $" << basicPay <<endl <<"\n";
}


1
2
3
4
5
6
7
8
void OrdinaryEmployee::displayData()
{
   Employee::displayData(); // get base class to display its data

   cout << "Employee's Hourly Rate     : $" << hourlyRate <<endl;
   cout << "Employee's no. of OT hours : " << noOfOTHours <<endl;
   cout << "Employee's yearly bonus    : $" << yearlyBonus <<endl<< "\n";
}


In this case, when you add a new piece of data and display code to Employee, OridinaryEmployee will follow suite without further work.

Andy

Last edited on
hey Andy I tried your method mentioned but it produced the same outcome.
It shortened my code by quite some though, thanks anyway for the tip

I think the prg didn't recognize the additional fields from the derived classes.
This is my how my derived classes look like:

base class
1
2
3
4
5
6
7
8
9
10
11
12
13
void Employee::displayData()
{
   //cout<<Employee::toString();
   cout << "Employee's id            : " << id << endl;
   cout << "Employee's name          : " << name << endl;
   cout << "Employee's DOB           : " << dob << endl;
   cout << "Employee's address       : " << address << endl;
   cout << "Employee's phone no.     : " << phoneNo << endl;
   cout << "Employee's email address : " << email << endl;
   cout << "Employee's dept. no.     : " << deptNo << endl;
   cout << "Employee's basic pay     : $" << basicPay <<endl <<"\n";
   cout << "=================================================\n";
}


derived class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void OrdinaryEmployee::displayData()
{
   Employee::displayData();
   /*cout << "Employee's id              : " << id << endl;
   cout << "Employee's name            : " << name << endl;
   cout << "Employee's DOB             : " << dob << endl;
   cout << "Employee's address         : " << address << endl;
   cout << "Employee's phone no.       : " << phoneNo << endl;
   cout << "Employee's email address   : " << email << endl;
   cout << "Employee's dept. no.       : " << deptNo << endl;
   cout << "Employee's basic pay       : $" << basicPay <<endl;*/
   cout << "Employee's Hourly Rate     : $" << hourlyOTRate <<endl;
   cout << "Employee's no. of OT hours : " << noOfOTHours <<endl;
   cout << "Employee's yearly bonus    : $" << yearlyBonus <<endl<< "\n";
   cout << "======================================================";
}


This is the static objects I coded in:
1
2
3
4
5
empArray[4] = Employee(5, "Kelvin Lau", "16/03/1976", "Punggol", "90909093", "kelvinlau@gmail.com", 5, 5000.0);
empArray[5] = OrdinaryEmployee(6, "Ronald Toh", "10/09/1967", "Yee Tew", "90909898", "ronaldtoh@live.com", 6, 6000.0, 20.0, 3.0, 20000.0); 
empArray[6] = OrdinaryEmployee(7, "Eric Kwek", "09/09/1983", "Orchard", "91234123", "erickwek@live.com", 7, 7000.0, 30.0, 4.0, 30000.0);
empArray[7] = SeniorEmployee(8, "Andy Lau", "01/01/1973", "Sentosa", "98787654", "andylau@live.com", 8, 8000.0, 50000.0);
empArray[8] = SeniorEmployee(9, "Melvin Kok", "07/07/1987", "Bedok", "99878723", "melvinkok@gmail.com",9, 7500.0, 45000.0);



And i print them out like this.
1
2
3
4
5
6
7
	// Display all employees profile
	else if(choice == 2) {
	for(int i = 0; i < dataCounter; i++)
	{
	empArray[i].displayData();
	}
			     } // End of Option 2 


All the information are able to output, but for the derived classes with additional different fields, they are skipped.

As showned: Array [7] and [8]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Employee's id            : 8
Employee's name          : Andy Lau
Employee's DOB           : 01/01/1973
Employee's address       : Sentosa
Employee's phone no.     : 98787654
Employee's email address : andylau@live.com
Employee's dept. no.     : 8
Employee's basic pay     : $8000

=================================================
Employee's id            : 9
Employee's name          : Melvin Kok
Employee's DOB           : 07/07/1987
Employee's address       : Bedok
Employee's phone no.     : 99878723
Employee's email address : melvinkok@gmail.com
Employee's dept. no.     : 9
Employee's basic pay     : $7500

=================================================

All the information are able to output, but for the derived classes with additional different fields, they are skipped.
The reason is that the type of the array is 'Employee'.

if you're doing this
empArray[5] = OrdinaryEmployee(6, "Ronald Toh", "10/09/1967", "Yee Tew", "90909898", "ronaldtoh@live.com", 6, 6000.0, 20.0, 3.0, 20000.0);
The data of 'Employee' is copied and the rest omitted.

You need a pointer to 'Employee' and not copying the content.
Thanks alot! It worked
Topic archived. No new replies allowed.