Overloading and Overriding are the concepts of Polymorphism. In ‘overloading‘ we redefine a function of a class with the same name but with, different numbers and types of parameters. In the ‘overriding‘ prototype the overridden function is the same throughout the program but, the function to be overridden is preceded by the keyword ‘virtual’ in the base class and is redefined by the derived class without any keyword.
Polymorphism is one of the crucial features of OOP. It simply means ‘using one name for multiple forms’. Polymorphism can be implemented using ‘function overloading’, ‘operator overloading’ and ‘virtual function’. Both, ‘overloading’ and ‘overriding’ implies the concept of polymorphism.
Here, ‘overloading’ is a compile time polymorphism and ‘overriding’ is run time polymorphism. Studying further, if we talk about the major difference between ‘overloading’ and ‘overriding’. Further, we study the difference between overloading and overriding with the help of a comparison chart.
Content: Overloading Vs Overriding
Comparison Chart:
Basis for Comparison | Overloading | Overriding |
---|---|---|
Prototype | Prototype differs as number or type of parameter may differ. | All aspect of prototype must be same. |
Keyword | No keyword applied during overloading. | Function which is to be overridden is preceded by keyword 'virtual', in the base class. |
Distinguishing factor | Number or type of parameter differs which determines the version of function is being called. | Which class's function is being called by the pointer, is determined by, address of which class's object is assigned to that pointer. |
Defining pattern | Function are redefined with same name, but different number and type of parameter. | Function is defined, preceded by a keyword 'virtual' in main class and redefined by derived class with out keyword. |
Time of accomplishment | Compile time. | Run time. |
Constructor/Virtual function | Constructors can be overloaded. | Virtual function can be overridden. |
Destructor | Destructor cannot be overloaded. | Destructor can be overridden. |
Binding | Overloading achieves early binding. | Overriding refers to late binding. |
Definition of Overloading
Overloading is a compile-time polymorphism. With overloading, we can redefine a member function of a class. The overloaded functions have the same name, but different numbers and types of parameters, it makes one overloaded function distinct from another.
Now if we call an overloaded function then how does the compiler recognizes exactly which overloaded function?
Well, the compiler recognizes depending on the types and numbers of parameters of the called function.
Most commonly overloaded functions are ‘constructors’. ‘Copy constructor’ is a kind of “constructor overloading”.
Implementation of overloading in C++
class overload{ int a, b; public: int load(int x){ // first load() function a=x; return a; } int load(int x, int y){ //second load() function a=x; b=y; return a*b; } }; int main(){ overload O1; O1.load(20); //first load() function call O1.load(20,40); // second load() function call }
In the code above, we have a class ‘overload’, and we have overloaded the member function load(). We can clearly distinguish between these overloaded functions in a manner that:
- The first load() function accepts only a single integer parameter.
- The second load() function accepts two integer parameters.
When the object O1 of the class ‘overload’ calls the load() function with a single parameter, the first load() function gets called. When object calls load() function passing two parameters, second load() function gets called.
In ‘C’ we don’t have this concept of overloading. So we have to create each function with a different name. This increases the complexity as we have to remember so many function names. However, overloading has reduced the complexity of remembering so many names.
Definition of Overriding
Polymorphism achieved during run-time is ‘overriding.’ We accomplish overriding by using ‘inheritance’ and ‘virtual functions.
We know that every child class inherits all the member functions of its parent class. But what if it needs to redefine some of the functions?
Here the concept of overriding comes into the picture. At first, we have to define a parent class with a member function that we have to override, we refer to this function as an overridden function.
Next, define a child class and now you can redefine the overridden function. We refer to this redefined function as an overriding function.
Remember that prototype of overridden function and overriding function must not change. When we call overridden function, C++ determines which version of the function is called based on the ‘type of the object pointed by a pointer’ that calls the function.
Implementation of Overriding in C++
class base{ public: virtual void funct(){ //virtual function of base class cout<<"This is a base class's funct()"; } }; class derived1 : public base{ public: void funct(){ //virtual function of base class redefined in derived1 class cout<<"This is a derived1 class's funct()"; } }; class derived2 : public base{ public: void funct(){ //virtual function of base class redefined in derived2 class cout<<"This is a derived2 class's funct()"; } }; int main() { base *p, b; derived1 d1; derived2 d2; *p=&b; p->funct(); //call to base class funct(). *p=&d1; p->funct(); //call to derived1 class funct(). *p=&d2; p->funct(); //call to derived2 class funct(). return 0; }
In the code above, there is a single base class which is publicly inherited by two derived classes. The base class has a virtual function ‘funct()’ with the keyword ‘virtual’. Further, both the derived classes have redefined this virtual function with no keyword.
In main(), base class creates a pointer variable ‘p’ and a object ‘b’; ‘derived1′ class creates an object d1 and derived2 class creates an object d2’.
Now, initially, we assign the address of the base class’s object ‘b’ to the pointer of the base class ‘p’. Now, ‘p’ gives a call to the function funct(), this gives a call to the function of the base class.
Then we assign the address of object ‘d1’ to pointer ‘p’, again it gives a call to funct(). This time the funct() of derived1 class gets executed.
Finally, we assign the address of object ‘d2’ to pointer ‘p”. Then ‘p’ calls funct() which executes the function funct() of derived2 class.
Key Differences Between Overloading and Overriding
- The prototype of overloaded function changes as they differ in the type and number of the parameters. On the other hand, the prototype of the overridden function does not change. Because an overridden function perform different action for different class it belongs to but with the same type and number of parameter.
- The overloaded function name does not precede any keyword. However, the name of an overridden function precedes the keyword “Virtual” in the base class only.
- Which overloaded function is invoked depends on the type or number of the parameters passed to the function. Which overridden function is invoked depends on, which class’s object reference the pointer has, that invoked the function.
- Overloading achieves early binding as which overloaded function will be invoked gets resolved during compile time. Overriding achieve late binding as the which overridden function will be invoked gets resolved during runtime.
- Which overloaded function is invoked gets resolved during compile time. Which overridden function to be invoked gets resolved during runtime.
- We can overload the constructors but can not override them.
- We can not overload destructors, but we can override them.
Similarities
- We can apply both concepts to member functions of a class.
- Polymorphism is the basic concept behind both of them.
- Function name remains the same while we apply overload and overriding to the functions.
Conclusion
Overloading and overriding appear similar, but this is not the case. Once we overload a function then, any class can not further redefine the overloaded function in future. A virtual function cannot be overloaded; it can only be overridden.
Adam Parker says
That is a very useful article.