CPP Polymorphism and Constructors

Polymorphism, Constructors and Destructors

  • A Destructor can be declared as virtual. This causes that the right destructor for the object executes even if the pointer or reference is of a base class type.
  • On the other hand Constructors cannot be virtual and so technically, no such thing exists as a virtual copy constructor.

Polymorphism and Copy Constructor

  • At times, your program desperately needs to be able to pass in a pointer of a base object and have a copy of the correct derived object that is created.
  • Common solution for this is:
    1. Create a method, CloneObject(), in the base class and to make that be virtual.
    2. The CloneObject() method creates a new object copy of the current class it is implemented in and returns that object.
    3. Each derived class overrides the CloneObject() method with it's own implementation.

    Note: It is possible for a virtual override method to return a pointer reference of base class type.

    Example :
    #include <iostream>
    
    class Person {
    public:
      Person (int age, int weight){
        this->age = age;
        this->weight = weight;
      }
      Person (const Person & refObject) {
        std::cout << "Person Copy Constructor...\n";
        this->age = refObject.getAge();
        this->weight = refObject.getWeight();
      }
      virtual ~Person () {}
      // this method is made virtual
      virtual void showInfo() {
        std::cout << "I am " << age << " years old " ;
        std::cout << "and weighs " << weight << " kilo.\n" ;
      }
      virtual Person* CloneObject() { 
        // Using the Person Copy-Constructor to create an object
        return new Person(*this); 
      }
      int getAge() const { return age; }
      int getWeight() const { return weight; }
    protected:
      int age;
      int weight;
    };
    
    class Employee : public Person {
    public:
      Employee (int age, int weight, int salary): Person(age,weight){
        this->salary = salary;
      }
      Employee(const Employee & refObject): Person(refObject){
        this->salary=refObject.getSalary();
        std::cout << "Employee copy constructor...\n";
      }
      virtual ~Employee () {}
      // The base has made this method virtual
      void showInfo() {
        std::cout << "I am " << age << " years old " ;
        std::cout << "and weighs " << weight << " kilo " ;
        std::cout << "and earns " << salary << " dollar.\n" ;
      }
      Person * CloneObject() {
        // Using the Employee Copy-Constructor to create an object
        return new Employee(*this);
      }
      int getSalary() const { return salary; }
    private:
      int salary;
    };
    int main()
    {
      // Testing with pointers and virtual copy constructor 
      Person * pObama = new Employee(45, 65, 50000);
      pObama->showInfo();
      // This causes the CloneObject method on Employee to be executed
      Person * pPhil =pObama->CloneObject();
      pPhil->showInfo();
      std::cout << "\n" ;
      // Testing with references and virtual copy constructor 
      Employee Ricard(40, 70,80000);
      Person & rRicard=Ricard;
      rRicard.showInfo();
      // This causes the CloneObject method on Employee to be executed
      Person & rFrank =*rRicard.CloneObject();
      rFrank.showInfo();
      std::cout << "\n" ;
    
      return 0;
    }
    When we run this application the result will be:
    I am 45 years old and weighs 65 kilo and earns 50000 dollar.
    Person Copy Constructor...
    Employee copy constructor...
    I am 45 years old and weighs 65 kilo and earns 50000 dollar.
    
    I am 40 years old and weighs 70 kilo and earns 80000 dollar.
    Person Copy Constructor...
    Employee copy constructor...
    I am 40 years old and weighs 70 kilo and earns 80000 dollar.

    You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.