CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2013
    Posts
    4

    [RESOLVED] 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?

    Code:
    #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.

    Code:
    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.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Need Help For Understanding C++ Pointer Usage

    You're close. You're using pointers so that the virtual functions will be called. If you used objects directly, the method in the class the object is an instance of will be called, not both.

  3. #3
    Join Date
    Aug 2013
    Posts
    55

    Re: Need Help For Understanding C++ Pointer Usage

    Quote Originally Posted by fazee6 View Post
    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?
    That's because otherwise the objects would be copied into the vector. The Employee object would work fine but not the Manager object. It's because when it's copied into an Employee storage position of the vector it will be sliced. This is called "the slicing problem" and it happens whenever a derived object is copied to a base variable. Pointers don't have the slicing problem so that's why they're used in the vector.

    The code you have works but it's quite brittle. Much better would be to allocate the objects using new. Then the objects must be deleted when not used anymore and for that to work properly a virtual destructor should be added to the base class (Employee). Seems complicated maybe but I think you should adopt this usage right away to avoid sneaky memory problems in the future.

  4. #4
    Join Date
    Aug 2013
    Posts
    4

    Talking Re: Need Help For Understanding C++ Pointer Usage

    Quote Originally Posted by GCDEF View Post
    You're close. You're using pointers so that the virtual functions will be called. If you used objects directly, the method in the class the object is an instance of will be called, not both.
    Quote Originally Posted by zizz View Post
    That's because otherwise the objects would be copied into the vector. The Employee object would work fine but not the Manager object. It's because when it's copied into an Employee storage position of the vector it will be sliced. This is called "the slicing problem" and it happens whenever a derived object is copied to a base variable. Pointers don't have the slicing problem so that's why they're used in the vector.

    The code you have works but it's quite brittle. Much better would be to allocate the objects using new. Then the objects must be deleted when not used anymore and for that to work properly a virtual destructor should be added to the base class (Employee). Seems complicated maybe but I think you should adopt this usage right away to avoid sneaky memory problems in the future.
    Thanks GCDEF and zizz, 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured