In this chapter, we will be looking into function overloading and function overriding.
So, let's first start with function overloading.
Function overloading
Same as constructors, we can also overload functions. Conditions for function overloading are:-
- Functions to be overloaded must have the same name.
- All functions must have different arguments( either a different number of parameters or different type of parameters ).
Let's see an example.
#include <iostream>
using namespace std;
class Rectangle
{
public:
void printArea(int x, int y)
{
cout << x * y << endl;
}
void printArea(int x)
{
cout << x * x << endl;
}
void printArea(int x, double y)
{
cout << x * y << endl;
}
void printArea(double x)
{
cout << x * x << endl;
}
};
int main()
{
Rectangle rt;
rt.printArea(2,4);
rt.printArea(2,5.1);
rt.printArea(10);
rt.printArea(2.3);
return 0;
}
10.2
100
5.29
Here, we defined four functions with the same name 'printArea' but different parameters.
In the main class, firstly the function printArea is called with 2 and 4 passed to it. Since both 2 and 4 are integers, so the function named printArea with both its parameters of type int (int x, int y) is called.
After that, the second function is called with 2 and 5.1 passed to it. Since 2 is of type int and 5.1 is of type double, so the function with the first parameter of type int and the second one of type double (int x,double y) is called.
Similarly, after that the function with only one integer value as its parameter is called and at last, the function with a single double value as its parameter is called.
Function Overriding
Function overriding is also known as Polymorphism. To understand method overriding, let's first look at an example.
#include <iostream>
using namespace std;
class Animals
{
public:
void sound()
{
cout << "This is parent class" << endl;
}
};
class Dogs : public Animals
{
public:
void sound()
{
cout << "Dogs bark" << endl;
}
};
int main()
{
Dogs d;
d.sound();
return 0;
}
In the above example, the class 'Dogs' and its parent class 'Animals' have the same function void sound(). When the object 'd' of class Dogs calls this function, then the function of the child class 'Dogs' is called, not that of the parent class. Thus, the function of child class overrides the function in parent class when called by an object of the child class.
This is called function overriding.
Conditions for Function Overriding
- Functions of both parent and child class must have the same name.
- Functions must have the same argument list and return type.
- A function declared static cannot be overridden.
- If a function cannot be inherited, it cannot be overridden.
Let's see an another example of function overriding.
#include <iostream>
using namespace std;
class Animals
{
public:
void sound()
{
cout << "This is parent class" << endl;
}
};
class Dogs : public Animals
{
public:
void sound()
{
cout << "Dogs bark" << endl;
}
};
class Cats : public Animals
{
public:
void sound()
{
cout << "Cats meow" << endl;
}
};
class Pigs : public Animals
{
public:
void sound()
{
cout << "Pigs snort" << endl;
}
};
int main()
{
Dogs d;
Cats c;
Pigs p;
d.sound();
c.sound();
p.sound();
return 0;
}
Cats meow
Pigs snort
Great thoughts reduced by practice become great acts.
-William Hazlitt