CPP Overloading Constructors

Constructor Overloading

  • You can create several constructors in a class with the same name. We says that these constructors overload each other.
  • As with other methods C++ requires that overloaded constructors must differ in their parameter list with a different type of parameter, a different number of parameters or both.
  • In CPP a special Constructor called Copy Constructor is called each time a copy of the object is made. It has the form of:
    Person(const Person & rPerson);     // copy constructor for the Person class
    You can write you own Copy Constructor to take control over what should be copied when that comes to action.
  • Each class has implizit a constructor without parameters if none is defined. This contructor is called the default constructor and will not exist if you define a new constructor with parameters. If you need the default constructor in such cases you have to define it again.
  • The destructor method cannot be overloaded because a class cannot have more than on destructor.
    Overload Constructor example:
    #include <iostream>
    using namespace std;
    class Cylinder {
    public:
      // example of overloaded Constructors
      Cylinder(int radius,int height ) {
        this->radius = radius;
        this->height = height; }
      // Constructor with no parameter 
      // is called default Constructor 
      // Default values are set in the 
      // initialization stage here (not the body). 
      Cylinder():radius(2),height(5) { }
      // Destructor cannot be overloaded. 
      ~Cylinder() {}
    
      // Implementing the Copy Constructor
      Cylinder(const Cylinder & rCyl) {
        this->radius = rCyl.getRadius();
        this->height = rCyl.getHeight();
        cout << "The Copy Contructuctor ...\n";
      }
      int getRadius() const {return radius; }
      int getHeight() const {return height;}
    private:
      int radius;
      int height;
    };
    
    int main(){
    // Using Constructor with no parameters (default constructor)
      Cylinder * pCyl =  new Cylinder;
      cout << "pCyl radius: " << pCyl->getRadius()<< " meter"  << endl;
    // Using Constructor with two parameters 
      Cylinder * pCylinder = new Cylinder(4,20);
      cout << "pCylinder radius: " << pCylinder->getRadius()<< " meter"  << endl;
    // Using the Copy Constructor directly;
      Cylinder * pCopiCyl= new Cylinder(* pCyl);
      cout << "pCopiCyl radius: " << pCopiCyl->getRadius() << " meter"  << endl;
    // Using the Copy Constructor indirectly;
      Cylinder copyCylinder=* pCylinder;
      cout << "copyCylinder radius: " << copyCylinder.getRadius() << " meter"  << endl;
      return 0;
    }
    When we run this application, the result will be:
    pCyl radius: 2 meter
    pCylinder radius: 4 meter
    The Copy Contructuctor ...
    pCopiCyl radius: 2 meter
    The Copy Contructuctor ...
    copyCylinder radius: 4 meter

    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.