Polymorphism, Virtual Functions and Abstract Class
- 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.
- 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.
- 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.
- Inheritance is a mechanism in which one object naturally acquires all of its parent object's properties and behaviors.
- In C++, a pointer variable of a base class type can point to an object of its derived class.
- There are situations when this feature of C++ can be used to develop generic code for a variety of applications.
Pointer of base class
Consider the following program to understand pointer compatibility property
#include <iostream.h>
#include <conio.h>
class Shape
{
protected:
double width, height;
public:
void set_data (double a, double b)
{
width = a;
height = b;
}
};
class Rectangle: public Shape
{
public:
double area ()
{
return (width * height);
}
};
int main ()
{
Shape *sPtr; //declare pointer variables of type Shape
Rectangle Rect; //create the object rect of type Rectangle
sPtr = &Rect; //make sPtr point to the object rect.
sPtr->set_data (5,3); //set length and width of object rect
cout << sPtr -> area() << endl; //Compile Error !!
return 0;
}
- Notice that even though rectPtr is poining to rect (object of type Rectangle), when the program executes, the statement sets length and width of rectangle.
- If you tried to access area function of class Rectangle with sPtr it will give you compiler error.
sPtr -> area()
is a compiler error !
- It means base class pointer can not access the additional member function of its derived class.
- If we want to do this we need to type cast the base class pointer.
Using Type Casts with Base Class Pointers
- We can use a type cast to get the compiler to accept the statement:
static_cast <Rectangle *> (sPtr)->area()
so we should write the statement
cout << static_cast <Rectangle *> (sPtr) -> area() << endl;
- The type cast informs the compiler that sPtr is actually pointing to a Rectangle object derived from the Shape base class.
- In general, a pointer to a base class that actually points to a derived class object must first be appropriately cast before the additional features of the derived class can be used.
Virtual Function
- A virtual function is a member function of base class that you expect to redefine in derived classes.
- Virtual function is a member function in the base class that you can 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.
//Let's take an example to implement Virtual Function.
#include <iostream.h>
#include <conio.h>
class Weapon
{
public:
void loadFeatures()
{ cout << "Loading weapon features.\n"; }
};
class Bomb : public Weapon
{
public:
void loadFeatures()
{ cout << "Loading bomb features.\n"; }
};
class Gun : public Weapon
{
public:
void loadFeatures()
{ cout << "Loading gun features.\n"; }
};
int main()
{
Weapon *w = new Weapon;
Bomb *b = new Bomb;
Gun *g = new Gun;
w->loadFeatures();
b->loadFeatures();
g->loadFeatures();
return 0;
}
Expected Output:-
Loading weapon features.
Loading bomb features.
Loading gun features.
class A
{
int x=5;
public:
void display()
{
std::cout << "Value of x is : " << x<<std::endl;
}
};
class B: public A
{
int y = 10;
public:
void display()
{
std::cout << "Value of y is : " <<y<< std::endl;
}
};
int main()
{
A *a;
B b;
a = &b;
a->display();
return 0;
}
Expected Output:-
Value of x is : 5
{
public:
void my_features()
{
cout << "I am an animal.";
}
};
class Mammal : public Animal
{
public:
void my_features()
{
cout << "\nI am a mammal.";
}
};
class Reptile : public Animal
{
public:
void my_features()
{
cout << "\nI am a reptile.";
}
};
int main()
{
Animal *obj1 = new Animal;
Mammal *obj2 = new Mammal;
Reptile *obj3 = new Reptile;
obj1->my_features();
obj2->my_features();
obj3->my_features();
return 0;
}
Expected Output:-
I am an animal.
I am a mammal.
I am a reptile.
//Illustration of abstract class and pure virtual function in C++.
{
public:
virtual void display() = 0;
};
class derived_class : public base_class
{
public:
void display()
{
cout<<"This is simple illustration of abstract class and pure virtual function";
}
};
int main()
{
derived_class obj;
obj.display();
return 0;
}
Expected Output:-
This is simple illustration of abstract class and pure virtual function
//Let's take another example to implement Virtual Function.
#include <iostream.h>
#include <conio.h>
{
int x=5;
public:
void display()
{
std::cout << "Value of x is : " << x<<std::endl;
}
};
class B: public A
{
int y = 10;
public:
void display()
{
std::cout << "Value of y is : " <<y<< std::endl;
}
};
int main()
{
A *a;
B b;
a = &b;
a->display();
return 0;
}
Expected Output:-
Value of x is : 5
//Let's take another example to implement Virtual Function.
#include <iostream.h>
#include <conio.h>
class Animal{
public:
void my_features()
{
cout << "I am an animal.";
}
};
class Mammal : public Animal
{
public:
void my_features()
{
cout << "\nI am a mammal.";
}
};
class Reptile : public Animal
{
public:
void my_features()
{
cout << "\nI am a reptile.";
}
};
int main()
{
Animal *obj1 = new Animal;
Mammal *obj2 = new Mammal;
Reptile *obj3 = new Reptile;
obj1->my_features();
obj2->my_features();
obj3->my_features();
return 0;
}
Expected Output:-
I am an animal.
I am a mammal.
I am a reptile.
Abstract class and pure virtual function
- C++ abstract class is a class which is designed to perform the role of the base class having at least one pure virtual function.
- A class without a pure virtual function cannot be termed as an abstract base class in C++.
- Abstract classes are used as a framework upon which new sub-classes are derived.
Syntax:-
//declaring abstract base class
class base_class
{
virtual return_type func_name() = 0; //pure virtual function
}
//Illustration of abstract class and pure virtual function in C++.
#include <iostream.h>
#include <conio.h>
class base_class{
public:
virtual void display() = 0;
};
class derived_class : public base_class
{
public:
void display()
{
cout<<"This is simple illustration of abstract class and pure virtual function";
}
};
int main()
{
derived_class obj;
obj.display();
return 0;
}
Expected Output:-
This is simple illustration of abstract class and pure virtual function
Tags:
c++ by mskuthar
mskuthar
Polymorphism
Virtual Functions and Abstract Class
www.mskuthar.blogspot.com