which 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
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
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++
- 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
- }
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++
- 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;
- }
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
- 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.
- 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.
- 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.
- Which overloaded function is to be invoked is resolved during compile time. Which overridden function to be invoked is resolved during runtime.
- Constructors can be overloaded but can not be overridden.
- Destructors can not be overloaded, but they can be overridden.
- 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
- Both are applied to member functions of a class.
- Polymorphism is the basic concept behind both of them.
- 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
But can we use function overloading in Embedded C?
ReplyDeleteno 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