Vector of objects from diffrent class

Hello. I want to create vector of objects from diffrent classes. I am wondering if Base class should be abstract class or not

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
 #include <iostream>
#include <vector>
using namespace std;
class Employee
{
public:

    virtual void GetInfo() = 0;
     ~Employee();

};
class Engineer:public Employee
{
public:
    string name;
    string surname;
    int salary;
    
    Engineer(){};
    Engineer(string name,string surname,int salary)
    {
        this->name = name;
        this->surname = surname;
        this->salary = salary;
    }
    void GetInfo()
    {
       // Info 
    }
    ~Engineer(){}
};
class Secretary:public Employee
{
public:
 Secretary(){};
    Secreatry(string name,string surname,int salary)
    {
        this->name = name;
        this->surname = surname;
        this->salary = salary;
    }
    void GetInfo()
    {
       //Info   
    }
    ~Secretary(){}
};
class Manager:public Employee
{
public:
    Manager(){};
    Manager(string name,string surname,int salary)
    {
        this->name = name;
        this->surname = surname;
        this->salary = salary;
    }
   void GetInfo()
    {
       //Info   
    }
    ~Manager(){}

};
class SecurityGuard:public Employee
{
public:
    SecurityGuard(){};
    SecurityGuard(string name,string surname,int salary)
    {
        this->name = name;
        this->surname = surname;
        this->salary = salary;
    }
    void GetInfo()
    {
       //Info   
    }
   ~SecurityGuard(){}
};
int main()
{

  vector<Employee*>company;
   Employee *John  = new Engineer;
   Employee *Ann  = new Secretary;
   Employee *Linda  = new Engineer;
   Employee *Peter  = new SecurityGuard;
   Employee *Mark  = new Manager;
   company.push_back(John);
   company.push_back(Ann);
   company.push_back(Linda);
   company.push_back(Peter);
   company.push_back(Mark);
   
    return 0;
}
I'm not really sure about the answer to your question but based off what you have posted it looks like name, surname and salary could all be members of employee and just be inherited. seems kinda pointless to have it written in there however many times. The only real difference at this point is the job title. That being said kinda pointless to have separate classes at all. I'm sure you have your reasons. you wouldn't have to worry about making vectors storing different types or whatever either.
No need to make your base class as abstract.
If Employee is a generic class that should not be instantiated directly, and it only provides a "framework" for other specialized classes to implement directly, then make it abstract.

If Employee can be instantiated directly and an Employee object should be able to getInfo on itself, then don't make it abstract.
asxxx, is that your actual question? Because your code doesn't even compile currently. Whether the base class remains abstract is the least of your worries.

And as markyrocks mentioned, you haven't shown any reasonable differentiation between your subclasses, so I agree that it's pointless to have separate classes here; it's currently just a bunch of code duplication.
Last edited on
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
 #include <iostream>
#include <vector>
using namespace std;
class Employee
{
public:
    string name;
    string surname;
    int salary;
    virtual void GetInfo() = 0;
     ~Employee();

};
class Engineer:public Employee
{
public:
   
    
    Engineer(){};
    Engineer(string name,string surname,int salary)
    {
        this->name = name;
        this->surname = surname;
        this->salary = salary;
    }
    void GetInfo()
    {
       // Info 
    }
    ~Engineer(){}
};
class Secretary:public Employee
{
public:
 Secretary(){};
    Secretary(string name,string surname,int salary)
    {
        this->name = name;
        this->surname = surname;
        this->salary = salary;
    }
    void GetInfo()
    {
       //Info   
    }
    ~Secretary(){}
};
class Manager:public Employee
{
public:
    Manager(){};
    Manager(string name,string surname,int salary)
    {
        this->name = name;
        this->surname = surname;
        this->salary = salary;
    }
   void GetInfo()
    {
       //Info   
    }
    ~Manager(){}

};
class SecurityGuard:public Employee
{
public:
    SecurityGuard(){};
    SecurityGuard(string name,string surname,int salary)
    {
        this->name = name;
        this->surname = surname;
        this->salary = salary;
    }
    void GetInfo()
    {
       //Info   
    }
   ~SecurityGuard(){}
};
int main()
{

  vector<Employee*>company;
   Employee *John  = new Engineer;
   Employee *Ann  = new Secretary;
   Employee *Linda  = new Engineer;
   Employee *Peter  = new SecurityGuard;
   Employee *Mark  = new Manager;
   company.push_back(John);
   company.push_back(Ann);
   company.push_back(Linda);
   company.push_back(Peter);
   company.push_back(Mark);
   
    return 0;
}


Now code does compile and I am still wondering about base class(should be abstract or not). In fact I could inherit from Employee almost everything to create new worker/employee but in others cases it can impossible(for example class Animal) what would look like base class Animal. If I want to use abstract class, variables (name,salary etc) should be call virtual too ?
If there is never a need to instantiate a "Employee" object itself, then it is OK for Employee to be abstract. Otherwise, GetInfo needs an implementation for Employee. The way you have it right now is fine enough, but in my opinion the inheritance here is still unnecessary.

Variables can't be virtual, only functions can. A class is, by definition, abstract if it contains a pure virtual function, or doesn't implement an inherited pure virtual function.

Notice how you're doing basically the same thing in each of your constructors.
You should make it so your base class does the necessary work, and your subclass constructors call your base class constructor.
base class:
1
2
3
4
5
6
    Employee(string name,string surname,int salary)
    {
        this->name = name;
        this->surname = surname;
        this->salary = salary;
    }


derived class:
1
2
3
4
5
Manager(string name,string surname,int salary)
: Employee(name, surname, salary)
    {

    }
Last edited on
Thanks for help!
Topic archived. No new replies allowed.