20 Apr 2017

Difference Between Function Overloading and Overriding in C++


Overloading and overriding2which classPolymorphism 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 compile time polymorphism and ‘overriding’ is run time polymorphism. Studying further, if we talk about the major difference in ‘overloading’ and ‘overriding’. In ‘overloading’ we redefine the overloaded functions with the same function name but, different number and type of parameters. In ‘overriding’  prototype of overridden function is same throughout the program but, function to be overridden is preceded by the keyword ‘virtual’ in the base class and is redefined by the derived class without any keyword.
Further, we study the difference between overloading and overriding with the help of a comparison chart.

Content: Overloading Vs Overriding

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Similarities
  5. Conclusion


Comparison Chart:

BASIS FOR COMPARISONOVERLOADINGOVERRIDING
PrototypePrototype differs as number or type of parameter may differ.All aspect of prototype must be same.
KeywordNo keyword applied during overloading.Function which is to be overridden is preceded by keyword 'virtual', in the base class.
Distinguishing factorNumber 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 patternFunction 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 accomplishmentCompile time.Run time.
Constructor/Virtual functionConstructors can be overloaded.Virtual function can be overridden.
DestructorDestructor cannot be overloaded.Destructor can be overridden.
BindingOverloading achieves early binding.Overriding refers to late binding.

Definition of Overloading

Compile-time polymorphism is called ‘overloading.’ As overloading is generated from a concept of polymorphism, it provides “a common interface for multiple methods”. That means, if a function is overloaded, it contain same function name while it is redefined.
Overloaded functions differ in regards to, different ‘number or type of parameter(s)’, it makes one overloaded function distinct from another. In this way, the compiler recognizes which overloaded function is being called. Most commonly overloaded functions are ‘constructors’. ‘Copy constructor’ is a kind of “constructor overloading”.

Implementation of overloading in C++

  1. class overload{
  2. int a, b;
  3. public:
  4. int load(int x){ // first load() function
  5. a=x;
  6. return a;
  7. }
  8. int load(int x, int y){ //second load() function
  9. a=x;
  10. b=y;
  11. return a*b;
  12. }
  13. };
  14. int main(){
  15. overload O1;
  16. O1.load(20); //first load() function call
  17. O1.load(20,40); // second load() function call
  18. }
Here function load() of class overload has been overloaded. The two overloaded functions of the class can be distinguished in a manner that first load() function accepts only single integer parameter, whereas the second load() function accepts two integer parameter. When the object of the class overload calls the load() function with a single parameter, first load() function get called. When object calls load() function passing two parameters, second load() function gets called.

Definition of Overriding

Polymorphism achieved during run-time is called ‘overriding.’  It is accomplished by using ‘inheritance’ and ‘virtual functions’. Function to be overridden is preceded by the keyword ‘virtual’ in a base class and redefined in a derived class without any keyword.
One of the most important thing to remember in case of overriding is that the prototype of the overridden function must not change while derived class redefines it. When overridden function is given a call, C++ determines which version of the function is called based on the ‘type of the object pointed by a pointer’ by which the function calling is done.

Implementation of Overriding in C++

  1. class base{
  2. public:
  3. virtual void funct(){ //virtual function of base class
  4. cout<<"This is a base class's funct()";
  5. }
  6. };
  7. class derived1 : public base{
  8. public:
  9. void funct(){ //virtual function of base class redefined in derived1 class
  10. cout<<"This is a derived1 class's funct()";
  11. }
  12. };
  13. class derived2 : public base{
  14. public:
  15. void funct(){ //virtual function of base class redefined in derived2 class
  16. cout<<"This is a derived2 class's funct()";
  17. }
  18. };
  19. int main()
  20. {
  21. base *p, b;
  22. derived1 d1;
  23. derived2 d2;
  24. *p=&b;
  25. p->funct(); //call to base class funct().
  26. *p=&d1;
  27. p->funct(); //call to derived1 class funct().
  28. *p=&d2;
  29. p->funct(); //call to derived2 class funct().
  30. return 0;
  31. }
Here, there is a single base class which is publicly inherited by two derived classes. A virtual function is defined in a base class with a keyword ‘virtual’, and it is redefined by both the derived classes 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 the address of base class’s object ‘b’ is assigned to the pointer of the base class ‘p’. ‘p’ gives a call to the function funct(), so a function of the base class is called. Then address of derived1 class object ‘d1’ is assigned to pointer ‘p’, again it gives call to funct(); here the function funct() of derived1 class is executed. Finally, pointer ‘p’ is assigned to derived2 class’s object. Then ‘p’ calls function funct() which executes the function func() of derived2 class.
If derived1/derived2 class did not redefine funct(), then the funct() of base class would have been called, as virtual function are ‘hierarchical.’

Key Differences Between Overloading and Overriding

  1. The prototype of a function which is being overloaded differs because of the type and number of parameter that are passed to the overloaded function. 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.
  2. The overloaded function name does not precede with any keyword whereas, the name of an overridden function precede with the keyord “Virtual” in base class only.
  3. Which overloaded function is invoked depends on the type or number of parameter that is passed to the function. The overridden function of which class is invoked depends on, which class’s object address is assigned to the pointer, which invoked the function.
  4. Which overloaded function is to be invoked is resolved during compile time. Which overridden function to be invoked is resolved during runtime.
  5. Constructors can be overloaded but can not be overridden.
  6. Destructors can not be overloaded, but they can be overridden.
  7. Overloading achieves early binding as which overloaded function will be invoked is resolved during compile time. Overriding achieves late binding as the which overridden function will be invoke is resolved during runtime.

Similarities

  1. Both are applied to member functions of a class.
  2. Polymorphism is the basic concept behind both of them.
  3. Function name remains same while we apply overloading and overriding to the functions.

Conclusion

Overloading and overriding appears similar, but this is not the case. Functions can be overloaded but, any class can not further redefine the overloaded function in future. A virtual function cannot be overloaded; they can only be overridden

2 comments:

  1. But can we use function overloading in Embedded C?

    ReplyDelete
  2. no because function overloading concept is based on oops concepts,overloading means function is same but parameters,number of arguments ,type of arguments ,order of arguments are different.In,embedded c this type of facility is not there,according my knowledge.

    ReplyDelete

commnet here