Need Help For Understanding C++ Pointer Usage

I know what are pointer's and how to you them but there is one point i am not able to understand. Below is the example code

I understand everything in the below code except 1 thing why i am using pointer to base class object in vector int the main() Function?

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
99
100
101
102
103
104
105
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

// base class
class Employee {
    protected:
  string name;
  double pay;
    public:
  Employee() {
    name = "";
    pay = 0;
  }
  Employee(string empName, double payRate) {
    name = empName;
    pay = payRate;
  }

  string getName() const {
    return name;
  }

  void setName(string empName) {
    name = empName;
  }

  int getPay() {
    return pay;
  }

  void setPay(int payRate) {
    pay = payRate;
  }

  string toString() {
    stringstream stm;
    stm << name << ": " << pay;
    return stm.str();
  }

  virtual double grossPay(int hours) {
    return pay * hours;
  }
};

// derived class

class Manager : public Employee {
    private:
  bool salaried;

    public:

  Manager(string name, double pay, bool isSalaried)
  : Employee(name, pay)
  {
     salaried = isSalaried;
  }

  bool getSalaried() {
    return salaried;
  }

  virtual double grossPay(int hours) {
    if (salaried) {
    return pay;
    } else {
    return pay * hours;
    }
  }

  string toString() {
    stringstream stm;
    string salary;
    if (salaried) {
    salary = "Salaried";
    } else {
    salary = "Hourly";
    }
    stm << name << ": " << pay
    << ": " << salary << endl;
    return stm.str();
  }
};

int main()
{
    Employee emp1("Jones", 25.00);
    Manager mgr1("Smith", 1200, true);

    vector<Employee*> employees;
    employees.push_back(&emp1);
    employees.push_back(&mgr1);
    for (int i = 0; i < employees.size(); ++i) {
  cout << "Name: " << employees[i]->getName()
     << endl;
  cout << "Pay: " << employees[i]->grossPay(40)
     << endl;
    }
    return 0;
}


Here is the lines of code i want to understand.

1
2
3
vector<Employee*> employees;
    employees.push_back(&emp1);
    employees.push_back(&mgr1);


I know if i will not use the pointer base class function "virtual double grossPay" will be called for both base class object and derived class object and when i will use pointer with reference to the object because base class function is virtual it will look for same function in derived class and if available it will execute it.

I think my concept about pointers is not clear any help to understand this will be highly appreciated.

Sorry for my english but i think i explain my point.
check out `object slicing'
Because that's the point of subclassing. :) The whole point is that the caller doesn't need to know whether it's an employee, a manager, or whatever. It doesn't care. It can take a pointer to either. When you call a virtual function on such a pointer, it'll use the appropriate virtual function - if it's pointer to an employee, it'll call employee::grossPay, while if it's a pointer to a manager, it'll call manager::grossPay. Any function that's not overridden will use the base class's version.

If you needed to know whether you were dealing with a pointer to a manager or a regular employee, that'd kind of defeat the point, now wouldn't it? :)

Thanks ne555 and Nafnlaus, for both of you for replying to this question and now i am totally understand that what was happening and why i need pointer when dealing with virtual functions.

Object Slicing was confusing me so much but i am happy that i understand what was happening.

Thanks again both of you for helping me.
Topic archived. No new replies allowed.