I have 3 classes: Employee
, Manager
, Clerk
.
Manager
and Clerk
are derived classes from the base class Employee
.
There is one question being asked in my school work that I can't find a way to work it out:
Write a program that creates an array of 3 Employee objects named empArray.
Create objects for each of the array elements as follows:
For empArray[0], create an Employee object.
For empArray[1], create a Manager object.
For empArray[2], create a Clerk object.
Code I've tried:
Employee empArray[3];
empArray[0] = Employee{ "Employee A", 1000.01};
empArray[1] = Manager{ "Manager A", 1200.02, 300.30 };
empArray[2] = Clerk{ "Clerk A", 1200.22, 3 };
But by doing this, I can't have access to each derived class's methods afterwards.
The sample code of the classes:
class Employee
{
private:
string name;
double basicSalary;
public:
Employee(string aName, double aSalary)
:name(aName),basicSalary(aSalary){}
};
class Manager : public Employee
{
private:
double travelClaims;
public:
Manager(string aName, double aSalary, double aClaims)
:Employee(aName, aSalary),travelClaims(aClaims){}
//+some manager methods
};
class Clerk : public Employee
{
private:
int overtimeHours;
public:
Clerk(string aName, double aSalary, int aHour)
:Employee(aName, aSalary),overtimeHours(aHour){}
//+some clerk methods
};
I'm expecting by declaring only the instances of base class, and still able to access to the methods of the derived classes in my main program.