-3

i think that i doing something wrong when i declare the constructor in the subclass "Manager". i didn't understand if it's an inheritance problem between the two classes.

employee.h

#ifndef EMP_IMP_H_INCLUDED
    #define EMP_IMP_H_INCLUDED
    #include<string>
    
    using namespace std;
    
    class Employee
    {
        private:
            string name;
            int salary;
    
        public:
            Employee(string n,int s){name=n; salary=s;};
            virtual ~Employee() {}
            //GetName();
            //GetSalary();
            //virtual PrintInfo();
    };
    
    class Manager:public Employee
    {
        private:
            int bonus;
    
        public:
            Manager(string n,int s,int b){name=n;salary=s;bonus=b;};
            virtual ~Manager() {};
            //GetBonus();
            //PrintInfo();
    };

#endif // EMP_IMP_H_INCLUDED

main.cpp

#include <iostream>
#include <string>
#include "NODO.h"
#include "EMP_IMP.h"
using namespace std;
int main(){
    Employee *emp1=new Employee (string("ciro esposito",1000));
    Employee *emp2=new Employee(string("Gennaro Espostio"),2000);
    Employee *emp3=new Manager(string("Carmine Espostio"),2000,2000);
    Employee *emp4=new Manager(string("Salvatore Espostio"),3000,3000);  
}
scohe001
  • 13,879
  • 2
  • 28
  • 47
  • Note: you don't have to `new` everything. In fact it's better if you [reserve `new` for when you're forced to use it.](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Jan 07 '21 at 23:05
  • BTW, in C++ you don't need to allocate variables on the heap (a.k.a. dynamic memory). Try it! – Thomas Matthews Jan 07 '21 at 23:12

1 Answers1

1

Constructor of class Manager must pass n and s arguments to the base class constructor instead of initializing base class data members directly. Also, while assigning bonus = b; in the constructor body is valid, it is better to put it in the initializer list, too:

Manager(string n,int s,int b) : Employee(n,s), bonus(b){}
Eugene
  • 4,579
  • 1
  • 16
  • 29