Polymorphism in C++





  • The word polymorphism means many-form.

  • In OOPs, polymorphism deals with behavior(method or function) part of the object.

  • If an object is able to perform single functionality in multiple ways, this is called polymorphic behavior of that object.

  • C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

Types of Polymorphism :

On the basis of concept of BINDING, the polymorphism is categorized into two category:
  1. Compile Time Polymorphism
  2. Runtime Polymorphism
Here, BINDING means the association of function calling with the function definition.

1. Compile Time Polymorphism :

  • If BINDING association is known at compile time then this is known as compile time polymorphism.

  • This is also known as static binding or early binding.

  • Compile time polymorphism can be achieved in C++ programming with the implementation of the following programming concept
    Function Overloading
    Constructor Overloading
    Operator Overloading

2. Runtime Polymorphism :

  • If BINDING association is known at compile time that decides the association at runtime then this is known as runtime polymorphism.

  • This is also known as dynamic binding or late binding.

  • Compile time polymorphism can be achieved in C++ programming with the implementation of the following programming concept
    Virtual Function
    Pure Virtual Function



Pointer to Object :

  • A pointer to a C++ class is done exactly the same way as a pointer to a structure, and to access members of a pointer to a class you use the member access operator -> operator, just as you do with pointers to structure.

  • Also as with all pointers, you must initialize the pointer before using it.

  • You can assign dynamic created object to pointer using new keyword.

  • You can assign the address of existing object to pointer using & operator.

Code Example :

#include<iostream>
using namespace std;
class Test {
public:
int x;
Test() {
x = 0;
}
Test(int var) {
x = var;
}
void show(){
cout<<"VALUE OF X : "<< x;
cout<< endl;
}
};
int main (){
Test ob1;
Test ob2(50);
Test *ob3;
//Dynamic memory allocation
Test *ob4 = new Test();
//Assigning ob2’s address to ob3
ob3 = &ob2; 
ob1.show();
ob2.show();
ob3->show();
ob4->show();
return 0;
}                      
OUTPUT:
VALUE OF X : 0
VALUE OF X : 50
VALUE OF X : 50
VALUE OF X : 0

Base Pointer to Derived Class Object :

  • Inheritance form the class hierarchy.

  • Inheritance establishes base class and derived class relationship among classes.

  • That relationship provides us the type-compatibility among classes(base-derived).

  • You can assign the address of derived class object to the pointer variable of base class.

  • As we know derived class object contains the legacy properties (inherited from base class) and their own specific properties.

  • Note :But with this base class pointer you can access only that members from derived class object those are inherited from base class.

Code Example :
#include<iostream>
using namespace std;
//base class
class A {
public:
void show(){
cout<<"SHOW FROM A"<< endl;
}
};
//derived class
class B : public A{
public:
void disp( ){
cout<<"DISP FROM B"<< endl;
}
};
int main (){
A *ob1 = new A();
//base pointer to derived object
A *ob2 = new B();
B *ob3 = new B();
ob1->show();
ob2->show();
//can't access
//ob2->disp(); //ERROR
ob3->show();
ob3->disp();
return 0;
}                    
OUTPUT:
SHOW FROM A
SHOW FROM A
SHOW FROM A
DISP FROM B


Virtual Function :

  • A virtual function is a function in base class that is declared using the keyword virtual.

  • The virtual functions can be re-define in derived class, this practice is called function overriding.

  • The non-virtual functions can also be re-define in derive class, this practice is called function overhiding.

Problem without Virtual Function :

#include<iostream>
using namespace std;
//base class
class A {
public:
void show(){
cout<<"SHOW FROM A"<< endl;
}
};
//derived class
class B : public A{
public:
void show( ){
cout<<"SHOW FROM B"<< endl;
}
};
int main (){
A *ob1 = new A();
A *ob2 = new B();
B *ob3 = new B();
ob1->show();
//this will call base version
ob2->show();
ob3->show();
return 0;
}           
OUTPUT:
SHOW FROM A
SHOW FROM A
SHOW FROM B
In the above example : show() function is overhide in derived class, thats why support static binding.
Here, static binding means the association of function definition with function call will done at compile time and association will be on the basis of pointer variable.
When we use Base class's pointer to hold Derived class's object, base class pointer or reference will always call the base version of the function.

Using Virtual Function :

#include<iostream>
using namespace std
//base class
class A {
public:
virtual void show(){
cout<<"SHOW FROM A"<< endl;
}
};
//derived class
class B : public A{
public:
void show( ){
cout<<"SHOW FROM B"<< endl;
}
};
int main (){
A *ob1 = new A();
A *ob2 = new B();
B *ob3 = new B();
ob1->show();
//this will call derived version
ob2->show();
ob3->show();
return 0;
}           
OUTPUT:
SHOW FROM A
SHOW FROM B
SHOW FROM B

The above code represents dynamic binding or runtime polymorphism.

Defining in base class a virtual function, with another version in derived class, signals to the compiler that don't want static binding for this function.

Binding of such function will depend on object type not on pointer variable type.


Points to Remember :

  1. Only the Base class Method's declaration needs the Virtual Keyword, not the definition.

  2. Once a function is declared as virtual in the base class, it will be virtual in all its derived classes.

  3. Run time polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.

  4. If a virtual function is defined in the base class , it need not be necessarily redefined in the derived class. In such cases, the respective calls will invoke the base class function.

  5. They can not be static members.

  6. A virtual function can be a friend of another class.

  7. The prototype of the base class version of a virtual function and all the derived class versions must be identical. If two functions with the same name have different prototypes, C++ considers them as overloaded functions, and the virtual function mechanism is ignored.


Next topic is Virtual Base Class





 







Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved