Inheritance/overriding - my display function doesn't seems to print out the information from the derived class!

I instantiated 5 generic Employee (base class) and 2 OrdinaryEmployee (derived class) But when the data is printed out, they only print the information specify in the base class.
Here is the data i specified.
1
2
3
4
5
6
7
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);


And when I use display out the data, they only have the first 8 fields, they omit off the 3 additional fields for my derived class despite having the 3 additional fields in it.

1
2
3
4
5
6
else if(choice == 2) {
	for(int i = 0; i < dataCounter; i++)
	{
	empArray[i].displayData();
	}
			     }


This is my base class displayData()
1
2
3
4
5
6
7
8
9
10
11
12
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";
}


This is the displayData() in my derived class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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     : $" << OrdinaryEmployee::hourlyOTRate <<endl;
   cout << "Employee's no. of OT hours : " << OrdinaryEmployee::noOfOTHours <<endl;
   cout << "Employee's yearly bonus    : $" << OrdinaryEmployee::yearlyBonus <<endl<< "\n";
}


Is there something wrong I did? Or i forget to add in certain things? When i declare my void displayData() in my base class. I write it like this
virtual void displayData()

pls shine some light on me
Was thinking is it even possible to store 2 different objects in 1 array for inheritance..
Yes, it is possible to store different objects in 1 array, assuming they have a common base class. Declaring the method as virtual is correct in this case.

I think what's happening is when you do assignment like this empArray[5] = OrdinaryEmployee(...); it converts the OrdinaryEmployee to an Employee, so you're losing the derived object's members.

You could use pointers, i.e.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Employee* empArray[7];
...
empArray[0] = new Employee(1, "John Tan", "12/09/1987", "Yishun", "98765432", "johntan@yahoo.com", 1, 2300.0);
...
empArray[6] = new OrdinaryEmployee(7, "Eric Kwek", "09/09/1983", "Orchard", "91234123", "erickwek@live.com", 7, 7000.0, 30.0, 4.0, 30000);
...
...
for(int i = 0; i < dataCounter; i++)
{
    empArray[i]->displayData();
}
...
//when finished
for(int i = 0; i < 7; i++)
{
    delete empArray[i];
}


and see if that fixes the problem.
I added the pointer, it have error..

1
2
3
4
5
6
7
8
9
main.cpp:129:105: error: cannot convert ‘Employee’ to ‘Employee*’ in assignment
main.cpp:130:108: error: cannot convert ‘Employee’ to ‘Employee*’ in assignment
main.cpp:131:106: error: cannot convert ‘Employee’ to ‘Employee*’ in assignment
main.cpp:132:111: error: cannot convert ‘Employee’ to ‘Employee*’ in assignment
main.cpp:133:110: error: cannot convert ‘Employee’ to ‘Employee*’ in assignment
main.cpp:134:135: error: cannot convert ‘OrdinaryEmployee’ to ‘Employee*’ in assignment
main.cpp:135:133: error: cannot convert ‘OrdinaryEmployee’ to ‘Employee*’ in assignment
main.cpp:163:39: error: cannot convert ‘Employee’ to ‘Employee*’ in assignment
main.cpp:170:14: error: request for member ‘displayData’ in ‘empArray[i]’, which is of non-class type ‘Employee*’
You should use the new keyword when you do the assignment.

e.g.
empArray[0] = new Employee(1, "John Tan", "12/09/1987", "Yishun", "98765432", "johntan@yahoo.com", 1, 2300.0);

And when you call displayData, you should either do

empArray[i]->displayData()

or

(*empArray[i]).displayData().

Both mean the same thing (dereference the pointer then call the method).
Last edited on
Topic archived. No new replies allowed.