0

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
helpMe
  • 9
  • You owe it to yourself to read [What is object slicing?](https://stackoverflow.com/questions/274626/what-is-object-slicing) – user4581301 Aug 15 '19 at 23:56
  • `Employee empArray[3];` -- Since this is an array of `Employee`, you will only get `Employee` objects, not `Employee` derived objects. That, in a nutshell, is what slicing is about. – PaulMcKenzie Aug 16 '19 at 00:01
  • @user4581301 Thx, I will take a read of it.. – helpMe Aug 16 '19 at 00:41
  • @Remy Lebeau Thx for helping, as the question is marked duplicated, should I do something like close it? (new to stackoverflow) – helpMe Aug 16 '19 at 00:49
  • helpMe no worries about closing it. It was closed when marked as a duplicate. There may be the option of deleting the question, and if not you can just leave it for pruning by whatever Stack Overflow uses for cleaning up duplicates when they get old and stale. – user4581301 Aug 16 '19 at 01:06

2 Answers2

0

You need to store pointers to the objects, rather than the base Employee (otherwise you slice the tops off the derived objects, and are only storing copies of their base class members)

Employee* empArray[3] = {0, 0, 0};

empArray[0] = new Employee{ "Employee A", 1000.01};
empArray[1] = new Manager{ "Manager A", 1200.02, 300.30 };
empArray[2] = new Clerk{ "Clerk A", 1200.22, 3 };
robthebloke
  • 9,331
  • 9
  • 12
0
// e.g implementation with vectors. //
class Employee{
    public:
        Employee()=default;
        virtual void typeOf(){
            cout << "Employee" <<endl;
        };
};

class Manager: public Employee{
    public:
        Manager()=default;
        virtual void typeOf() override{
            cout << "Manager" <<endl;
        };
};

class Clerk: public Employee{
    public:
        Clerk()=default;
        virtual void typeOf()override{
            cout << "Clerk" <<endl;
        };
};


int main(int argc, char**args){
    unique_ptr<vector<Employee*>>emp{new vector<Employee*>({new Clerk{}, new Employee{}, new Manager{}})}; // all you needed to do is to make a pointer from "Employee" and that will Make derived classes available to you ;) //
    for(int i{0}; i<emp->size(); ++i){
        (emp->at(i))->typeOf(); //
    };
    return(const int&&)-1;
};
  • 3
    There is rarely a need to dynamically allocate an `std::vector` directly. And even if you do need to, `new` has not been the recommended way of dynamically creating objects since C++11. See [`std::make_unique`](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique). – François Andrieux Aug 16 '19 at 00:18
  • It's not clear to me what `return(const int&&)-1;` is intended to do. – François Andrieux Aug 16 '19 at 00:19
  • It's my habit sorry, just write `return 0;` –  Aug 16 '19 at 00:22
  • @FrançoisAndrieux `std::unique_ptr` was added in C++11, but `std::make_unique()` wasn't added until C++14 – Remy Lebeau Aug 16 '19 at 00:24
  • @François Andrieux I'm being told to not use vectors in my work , but anyways thx for helping ! :D – helpMe Aug 16 '19 at 00:44