CPP Overloading Methods

What is Method Overloading

  • In C++ you can create several methods in a class with the same name. We says that these methods overload each other.
  • C++ requires that overloaded methods must differ in their parameter list with a different type of parameter, a different number of parameters or both.
  • Different return type has no effect.
    Overload Method 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() {}
      // example of overloaded methods;  setRadius
      void setRadius(Cylinder & cylinder) {
        setRadius(cylinder.getRadius()); }
      void setRadius(int radius) {this->radius = radius;}
    
      int getRadius() const {return radius; }
      void setHeight(int height) {this->height = height;}
      int getHeight() const {return height;}
      // Using default values for method arguments
      void setRadiusAndHeight(int radius,int height=40 ) {
        this->radius = radius;
        this->height = height; }
      // Implementing the Copy Constructor
      Cylinder(const Cylinder & rCyl) {
        this->radius = rCyl.getRadius();
        this->height = rCyl.getHeight();
        cout << "The Copy Contructuctor ...\n";
      }
    private:
      int radius;
      int height;
    };
    
    int main(){
      Cylinder * pCyl =  new Cylinder;
      cout << "pCyl radius: " << pCyl->getRadius()
        << " meter"  << endl;
    
      pCyl->setRadius(10);
      cout << "pCyl radius: " << pCyl->getRadius()
        << " meter"  << endl;
    
      Cylinder newCyl(4,20);
      pCyl->setRadius(newCyl);
      cout << "pCyl radius: " << pCyl->getRadius()
        << " meter"  << endl;
    // Using the Copy Constructor directly;
      Cylinder copyCyl1(* pCyl);
      cout << "copyCyl1 radius: " << copyCyl1.getRadius()
        << " meter"  << endl;
    // Using the Copy Constructor indirectly;
      Cylinder copyCyl2=* pCyl;
      cout << "copyCyl2 radius: " << copyCyl2.getRadius()
        << " meter"  << endl;
      return 0;
    }
    When we run this application, the result will be:
    pCyl radius: 2 meter
    pCyl radius: 10 meter
    pCyl radius: 4 meter
    The Copy Contructuctor ...
    copyCyl1 radius: 4 meter
    The Copy Contructuctor ...
    copyCyl1 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.