web analytics

Method Overloading and Method Overriding in C++

Options

codeling 1602 - 6666
@2016-01-07 09:00:53

In C++, method overloading and method overriding are two different features to support object-oriented programming.  

  • Method overloading is the ability for functions of the same name to be defined as long as these methods have different signatures.
  • Method overriding is the ability of the inherited class rewriting the virtual method of the base class.

Overloading Method

Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters).

In the following example, AddAndDisplay function is overloaded with different types of parameters.

void AddAndDisplay(int x, int y)
{
    cout<<" C++ Tutorial - Integer result: "<<(x+y);
}

void AddAndDisplay(double x, double y)
{
    cout<< " C++ Tutorial - Double result: "<<(x+y);
}


void AddAndDisplay(float x, float y)
{
    cout<< " C++ Tutorial - float result: "<<(x+y);

}

 

@2016-01-07 10:35:41

Overriding Method

Method overriding is the ability of the inherited class rewriting the virtual method of the base class. With overriding, the method in the inherited class has the identical signature to the method in the base class. The inherited class can selectively use some base class methods as they are, and override others.

In the following example, the speak method of the Pet class will be overridden in each inherited class.

#include <iostream>
#include <string>
using namespace std;

class Pet
{
public:
    // Constructors, Destructors
    Pet () : weight(1), food("Pet Chow") {}
    Pet(int w) : weight(w), food("Pet Chow") {}
    Pet(int w, string f) : weight(w), food(f) {}
    ~Pet() {}

    //Accessors
    void setWeight(int w) {weight = w;}
    int getWeight() {return weight;}

    void setfood(string f) {food = f;}
    string getFood() {return food;}

    //General methods
    void eat();


    virtual void speak();

protected:
    int weight;
    string food;

};

void Pet::eat()
{
    cout << "Eating " << food << endl;
}

void Pet::speak()
{
    cout << "Growl" << endl;
}

 

class Rat: public Pet
{
public:
    Rat() {}
    Rat(int w) : Pet(w) {}
    Rat(int w, string f) : Pet(w,f) {}
    ~Rat() {}

    //Other methods
    void sicken() {cout << "Speading Plague" << endl;}


    void speak();

};

void Rat::speak()
{
    cout << "Rat noise" << endl;
}

 

class Cat: public Pet
{
public:
    Cat() : numberToes(5) {}
    Cat(int w) : Pet(w), numberToes(5) {}
    Cat(int w, string f) : Pet(w,f), numberToes(5) {}
    Cat(int w, string f, int toes) : Pet(w,f), numberToes(toes) {}
    ~Cat() {}

    //Other accessors
    void setNumberToes(int toes) {numberToes = toes;}
    int getNumberToes() {return numberToes;}

    //Other methods
    void speak();

private:
    int numberToes;
};

void Cat::speak()
{
    cout << "Meow" << endl;
}


int main()
{
    Pet aPet;
    Rat aRat;
    Cat aCat;

    aPet.speak();
    aRat.speak();
    aCat.speak();

    return 0;

 

Output:

Crowl

Rat noise

Meow

Notice that each inherited class implements and uses its own speak method. The base class speak method is overridden.

Also, remember that the return type and signature of the inherited class method must match the base class method exactly to override.

@2016-01-07 10:38:58

Overriding Overloaded Methods

Another important point is that if the base class had overloaded a particular method, overriding a single one of the overloads will hide the rest. For instance, suppose the Pet class had defined several speak methods.

    void speak();
    void speak(string s);
    void speak(string s, int loudness);

If the inherited class, Cat, defined only

    void speak();

Then speak() would be overridden. speak(string s) and speak(string s, int loudness) would be hidden. This means that if we had a cat object, aCat2, we could call:

    aCat2.speak();

But the following would cause compilation errors.

         //error!!

    aCat2.speak("Hello");
    aCat2.speak("Hello", 10);

Generally, if you override an overloaded base class method you should either override every one of the overloads, or carefully consider why you are not.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com