Skip to main content

C++ Polymorphism: Overloading, Function Overloading

C++ Polymorphism

The term "Polymorphism" is the combination of "poly" + "morphs" which means many forms. It is a greek word. In object-oriented programming, we use 3 main concepts: inheritance, encapsulation, and polymorphism.

Real Life Example Of Polymorphism

Let's consider a real-life example of polymorphism. A lady behaves like a teacher in a classroom, mother or daughter in a home and customer in a market. Here, a single person is behaving differently according to the situations.

There are two types of polymorphism in C++:

C++
  • Compile time polymorphism: The overloaded functions are invoked by matching the type and number of arguments. This information is available at the compile time and, therefore, compiler selects the appropriate function at the compile time. It is achieved by function overloading and operator overloading which is also known as static binding or early binding. Now, let's consider the case where function name and prototype is same.
  1.    class A                                  //  base class declaration.  
  2.   {  
  3.        int a;  
  4.        public:  
  5.        void display()  
  6.        {   
  7.              cout<< "Class A ";  
  8.         }  
  9.   };  
  10. class B : public A                       //  derived class declaration.  
  11. {  
  12.     int b;  
  13.     public:  
  14.    void display()  
  15.   {  
  16.         cout<<"Class B";  
  17.   }  
  18. };  

In the above case, the prototype of display() function is the same in both the base and derived class. Therefore, the static binding cannot be applied. It would be great if the appropriate function is selected at the run time. This is known as run time polymorphism.

  • Run time polymorphism: Run time polymorphism is achieved when the object's method is invoked at the run time instead of compile time. It is achieved by method overriding which is also known as dynamic binding or late binding.

Differences b/w compile time and run time polymorphism.

Compile time polymorphismRun time polymorphism
The function to be invoked is known at the compile time.The function to be invoked is known at the run time.
It is also known as overloading, early binding and static binding.It is also known as overriding, Dynamic binding and late binding.
Overloading is a compile time polymorphism where more than one method is having the same name but with the different number of parameters or the type of the parameters.Overriding is a run time polymorphism where more than one method is having the same name, number of parameters and the type of the parameters.
It is achieved by function overloading and operator overloading.It is achieved by virtual functions and pointers.
It provides fast execution as it is known at the compile time.It provides slow execution as it is known at the run time.
It is less flexible as mainly all the things execute at the compile time.It is more flexible as all the things execute at the run time.

C++ Runtime Polymorphism Example

Let's see a simple example of run time polymorphism in C++.

// an example without the virtual keyword.

  1. #include <iostream>    
  2. using namespace std;    
  3. class Animal {    
  4.     public:    
  5. void eat(){      
  6. cout<<"Eating...";      
  7.     }        
  8. };     
  9. class Dog: public Animal      
  10. {      
  11.  public:    
  12.  void eat()      
  13.     {           cout<<"Eating bread...";      
  14.     }      
  15. };    
  16. int main(void) {    
  17.    Dog d = Dog();      
  18.    d.eat();    
  19.    return 0;    
  20. }    

Output:

Eating bread...

C++ Run time Polymorphism Example: By using two derived class

Let's see another example of run time polymorphism in C++ where we are having two derived classes.

// an example with virtual keyword.

  1. #include <iostream>    
  2. using namespace std;    
  3. class Shape {                                        //  base class  
  4.     public:    
  5. virtual void draw(){                             // virtual function  
  6. cout<<"drawing..."<<endl;      
  7.     }        
  8. };     
  9. class Rectangle: public Shape                  //  inheriting Shape class.  
  10. {      
  11.  public:    
  12.  void draw()      
  13.    {      
  14.        cout<<"drawing rectangle..."<<endl;      
  15.     }      
  16. };    
  17. class Circle: public Shape                        //  inheriting Shape class.  
  18.   
  19. {      
  20.  public:    
  21.  void draw()      
  22.    {      
  23.       cout<<"drawing circle..."<<endl;      
  24.    }      
  25. };    
  26. int main(void) {    
  27.     Shape *s;                               //  base class pointer.  
  28.     Shape sh;                               // base class object.  
  29.        Rectangle rec;    
  30.         Circle cir;    
  31.       s=&sh;    
  32.      s->draw();     
  33.         s=&rec;    
  34.      s->draw();      
  35.     s=?    
  36.     s->draw();     
  37. }    

Output:

drawing...
drawing rectangle...
drawing circle...

Runtime Polymorphism with Data Members

Runtime Polymorphism can be achieved by data members in C++. Let's see an example where we are accessing the field by reference variable which refers to the instance of derived class.

  1. #include <iostream>    
  2. using namespace std;    
  3. class Animal {                                          //  base class declaration.  
  4.     public:    
  5.     string color = "Black";      
  6. };     
  7. class Dog: public Animal                       // inheriting Animal class.  
  8. {      
  9.  public:    
  10.     string color = "Grey";      
  11. };    
  12. int main(void) {    
  13.      Animal d= Dog();      
  14.     cout<<d.color;     
  15. }    

Output:

Black

C++ Overloading (Function and Operator)

If we create two or more members having the same name but different in number or type of parameter, it is known as C++ overloading. In C++, we can overload:

  • methods,
  • constructors, and
  • indexed properties

It is because these members have parameters only.

Types of overloading in C++ are:

  • Function overloading
  • Operator overloading
C++ Overloading

C++ Function Overloading

Function Overloading is defined as the process of having two or more function with the same name, but different in parameters is known as function overloading in C++. In function overloading, the function is redefined by using either different types of arguments or a different number of arguments. It is only through these differences compiler can differentiate between the functions.

The advantage of Function overloading is that it increases the readability of the program because you don't need to use different names for the same action.

C++ Function Overloading Example

Let's see the simple example of function overloading where we are changing number of arguments of add() method.

// program of function overloading when number of arguments vary.

  1. #include <iostream>    
  2. using namespace std;    
  3. class Cal {    
  4.     public:    
  5. static int add(int a,int b){      
  6.         return a + b;      
  7.     }      
  8. static int add(int a, int b, int c)      
  9.     {      
  10.         return a + b + c;      
  11.     }      
  12. };     
  13. int main(void) {    
  14.     Cal C;                                                    //     class object declaration.   
  15.     cout<<C.add(10, 20)<<endl;      
  16.     cout<<C.add(12, 20, 23);     
  17.    return 0;    
  18. }    

Output:

30
55

Let's see the simple example when the type of the arguments vary.

// Program of function overloading with different types of arguments.

  1. #include<iostream>  
  2. using namespace std;  
  3. int mul(int,int);  
  4. float mul(float,int);  
  5.   
  6.   
  7. int mul(int a,int b)  
  8. {  
  9.     return a*b;  
  10. }  
  11. float mul(double x, int y)  
  12. {  
  13.     return x*y;  
  14. }  
  15. int main()  
  16. {  
  17.     int r1 = mul(6,7);  
  18.     float r2 = mul(0.2,3);   
  19.     std::cout << "r1 is : " <<r1<< std::endl;  
  20.     std::cout <<"r2 is : "  <<r2<< std::endl;  
  21.     return 0;  
  22. }  

Output:

r1 is : 42
r2 is : 0.6  

Function Overloading and Ambiguity

When the compiler is unable to decide which function is to be invoked among the overloaded function, this situation is known as function overloading.

When the compiler shows the ambiguity error, the compiler does not run the program.

Causes of Function Overloading:

  • Type Conversion.
  • Function with default arguments.
  • Function with pass by reference.
C++ Overloading
  • Type Conversion:

Let's see a simple example.

  1. #include<iostream>  
  2. using namespace std;  
  3. void fun(int);  
  4. void fun(float);  
  5. void fun(int i)  
  6. {  
  7.     std::cout << "Value of i is : " <<i<< std::endl;  
  8. }  
  9. void fun(float j)  
  10. {  
  11.     std::cout << "Value of j is : " <<j<< std::endl;  
  12. }  
  13. int main()  
  14. {  
  15.     fun(12);  
  16.     fun(1.2);  
  17.     return 0;  
  18. }  

The above example shows an error "call of overloaded 'fun(double)' is ambiguous". The fun(10) will call the first function. The fun(1.2) calls the second function according to our prediction. But, this does not refer to any function as in C++, all the floating point constants are treated as double not as a float. If we replace float to double, the program works. Therefore, this is a type conversion from float to double.

  • Function with Default Arguments

Let's see a simple example.

  1. #include<iostream>  
  2. using namespace std;  
  3. void fun(int);  
  4. void fun(int,int);  
  5. void fun(int i)  
  6. {  
  7.     std::cout << "Value of i is : " <<i<< std::endl;  
  8. }  
  9. void fun(int a,int b=9)  
  10. {  
  11.     std::cout << "Value of a is : " <<a<< std::endl;  
  12.     std::cout << "Value of b is : " <<b<< std::endl;  
  13. }  
  14. int main()  
  15. {  
  16.     fun(12);  
  17.    
  18.     return 0;  
  19. }  

The above example shows an error "call of overloaded 'fun(int)' is ambiguous". The fun(int a, int b=9) can be called in two ways: first is by calling the function with one argument, i.e., fun(12) and another way is calling the function with two arguments, i.e., fun(4,5). The fun(int i) function is invoked with one argument. Therefore, the compiler could not be able to select among fun(int i) and fun(int a,int b=9).

  • Function with pass by reference

Let's see a simple example.

  1. #include <iostream>  
  2. using namespace std;  
  3. void fun(int);  
  4. void fun(int &);   
  5. int main()  
  6. {  
  7. int a=10;  
  8. fun(a); // error, which f()?  
  9. return 0;  
  10. }  
  11. void fun(int x)  
  12. {  
  13. std::cout << "Value of x is : " <<x<< std::endl;  
  14. }  
  15. void fun(int &b)  
  16. {  
  17. std::cout << "Value of b is : " <<b<< std::endl;  
  18. }  

The above example shows an error "call of overloaded 'fun(int&)' is ambiguous". The first function takes one integer argument and the second function takes a reference parameter as an argument. In this case, the compiler does not know which function is needed by the user as there is no syntactical difference between the fun(int) and fun(int &).

C++ Operators Overloading

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types.

The advantage of Operators overloading is to perform different operations on the same operand.

Operator that cannot be overloaded are as follows:

  • Scope operator (::)
  • Sizeof
  • member selector(.)
  • member pointer selector(*)
  • ternary operator(?:)

Syntax of Operator Overloading

  1. return_type class_name  : : operator op(argument_list)  
  2. {  
  3.      // body of the function.  
  4. }  

Where the return type is the type of value returned by the function.

class_name is the name of the class.

operator op is an operator function where op is the operator being overloaded, and the operator is the keyword.

Rules for Operator Overloading

  • Existing operators can only be overloaded, but the new operators cannot be overloaded.
  • The overloaded operator contains atleast one operand of the user-defined data type.
  • We cannot use friend function to overload certain operators. However, the member function can be used to overload those operators.
  • When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument.
  • When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments.

C++ Operators Overloading Example

Let's see the simple example of operator overloading in C++. In this example, void operator ++ () operator function is defined (inside Test class).

// program to overload the unary operator ++.

  1. #include <iostream>    
  2. using namespace std;    
  3. class Test    
  4. {    
  5.    private:    
  6.       int num;    
  7.    public:    
  8.        Test(): num(8){}    
  9.        void operator ++()         {     
  10.           num = num+2;     
  11.        }    
  12.        void Print() {     
  13.            cout<<"The Count is: "<<num;     
  14.        }    
  15. };    
  16. int main()    
  17. {    
  18.     Test tt;    
  19.     ++tt;  // calling of a function "void operator ++()"    
  20.     tt.Print();    
  21.     return 0;    
  22. }    

Output:

The Count is: 10

Let's see a simple example of overloading the binary operators.

// program to overload the binary operators.

  1. #include <iostream>  
  2. using namespace std;  
  3. class A  
  4. {  
  5.     
  6.     int x;  
  7.       public:  
  8.       A(){}  
  9.     A(int i)  
  10.     {  
  11.        x=i;  
  12.     }  
  13.     void operator+(A);  
  14.     void display();  
  15. };  
  16.   
  17. void A :: operator+(A a)  
  18. {  
  19.      
  20.     int m = x+a.x;  
  21.     cout<<"The result of the addition of two objects is : "<<m;  
  22.   
  23. }  
  24. int main()  
  25. {  
  26.     A a1(5);  
  27.     A a2(4);  
  28.     a1+a2;  
  29.     return 0;  
  30. }  

Output:

The result of the addition of two objects is : 9 

C++ Function Overriding

If derived class defines same function as defined in its base class, it is known as function overriding in C++. It is used to achieve runtime polymorphism. It enables you to provide specific implementation of the function which is already provided by its base class.

C++ Function Overriding Example

Let's see a simple example of Function overriding in C++. In this example, we are overriding the eat() function.

  1. #include <iostream>  
  2. using namespace std;  
  3. class Animal {  
  4.     public:  
  5. void eat(){    
  6. cout<<"Eating...";    
  7.     }      
  8. };   
  9. class Dog: public Animal    
  10. {    
  11.  public:  
  12.  void eat()    
  13.     {    
  14.        cout<<"Eating bread...";    
  15.     }    
  16. };  
  17. int main(void) {  
  18.    Dog d = Dog();    
  19.    d.eat();  
  20.    return 0;  
  21. }  

Output:

Eating bread...

C++ virtual function

  • A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword.
  • It is used to tell the compiler to perform dynamic linkage or late binding on the function.
  • There is a necessity to use the single pointer to refer to all the objects of the different classes. So, we create the pointer to the base class that refers to all the derived objects. But, when base class pointer contains the address of the derived class object, always executes the base class function. This issue can only be resolved by using the 'virtual' function.
  • A 'virtual' is a keyword preceding the normal declaration of a function.
  • When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer.

Late binding or Dynamic linkage

In late binding function call is resolved during runtime. Therefore compiler determines the type of object at runtime, and then binds the function call.


Rules of Virtual Function

  • Virtual functions must be members of some class.
  • Virtual functions cannot be static members.
  • They are accessed through object pointers.
  • They can be a friend of another class.
  • A virtual function must be defined in the base class, even though it is not used.
  • The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions.
  • We cannot have a virtual constructor, but we can have a virtual destructor
  • Consider the situation when we don't use the virtual keyword.
  1. #include <iostream>  
  2. using namespace std;  
  3. class A  
  4. {  
  5.    int x=5;  
  6.     public:  
  7.     void display()  
  8.     {  
  9.         std::cout << "Value of x is : " << x<<std::endl;  
  10.     }  
  11. };  
  12. class B: public A  
  13. {  
  14.     int y = 10;  
  15.     public:  
  16.     void display()  
  17.     {  
  18.         std::cout << "Value of y is : " <<y<< std::endl;  
  19.     }  
  20. };  
  21. int main()  
  22. {  
  23.     A *a;  
  24.     B b;  
  25.     a = &b;  
  26.    a->display();  
  27.     return 0;  
  28. }  

Output:

Value of x is : 5

In the above example, * a is the base class pointer. The pointer can only access the base class members but not the members of the derived class. Although C++ permits the base pointer to point to any object derived from the base class, it cannot directly access the members of the derived class. Therefore, there is a need for virtual function which allows the base pointer to access the members of the derived class.

C++ virtual function Example

Let's see the simple example of C++ virtual function used to invoked the derived class in a program.

  1. #include <iostream>    
  2. {    
  3.  public:    
  4.  virtual void display()    
  5.  {    
  6.   cout << "Base class is invoked"<<endl;    
  7.  }    
  8. };    
  9. class B:public A    
  10. {    
  11.  public:    
  12.  void display()    
  13.  {    
  14.   cout << "Derived Class is invoked"<<endl;    
  15.  }    
  16. };    
  17. int main()    
  18. {    
  19.  A* a;    //pointer of base class    
  20.  B b;     //object of derived class    
  21.  a = &b;    
  22.  a->display();   //Late Binding occurs    
  23. }    

Output:

Derived Class is invoked  

Pure Virtual Function

  • A virtual function is not used for performing any task. It only serves as a placeholder.
  • When the function has no definition, such function is known as "do-nothing" function.
  • The "do-nothing" function is known as a pure virtual function. A pure virtual function is a function declared in the base class that has no definition relative to the base class.
  • A class containing the pure virtual function cannot be used to declare the objects of its own, such classes are known as abstract base classes.
  • The main objective of the base class is to provide the traits to the derived classes and to create the base pointer used for achieving the runtime polymorphism.

Pure virtual function can be defined as:

  1. virtual void display() = 0;   

Let's see a simple example:

  1. #include <iostream>  
  2. using namespace std;  
  3. class Base  
  4. {  
  5.     public:  
  6.     virtual void show() = 0;  
  7. };  
  8. class Derived : public Base  
  9. {  
  10.     public:  
  11.     void show()  
  12.     {  
  13.         std::cout << "Derived class is derived from the base class." << std::endl;  
  14.     }  
  15. };  
  16. int main()  
  17. {  
  18.     Base *bptr;  
  19.     //Base b;  
  20.     Derived d;  
  21.     bptr = &d;  
  22.     bptr->show();  
  23.     return 0;  
  24. }  

Output:

Derived class is derived from the base class.

In the above example, the base class contains the pure virtual function. Therefore, the base class is an abstract base class. We cannot create the object of the base class.



Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);   ...

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari...

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...