NAME : PATOLIYA MAULIKKUMAR NARENDRABHAI
BRANCH : CE ( 24CEUOG121 )
BATCH : A2
ROLL NO, : 041
SUB : POLYMORPHISM AND VIRTUAL FUNCTIONS
Polymorphism :
Polymorphism means one function or object can behave in different ways depending on
how it is used. It simply means 'One name, multiple forms'
Polymorphism refers to the property by which objects belonging to different classes are
able to respond to the same message.
Static Type :
Method to be executed is decided at compile time.
Dynamic Type :
Type of the object that is being pointed/referred by pointer/reference. Method to be
executed is decided at run time.
Maulik N. Patoliya
Example :
class B {
};
class D: public B {
};
int main() {
B *p;
p = new D;
return 0;
}
Here, static type of p is B. But dynamic type of p is D
Maulik N. Patoliya
Binding :
Choosing which method to execute for method call.
Static Binding :
This binding is early(fast) binding and it's depends on static type.
Dynamic Type :
It is late(slow) binding and it's depend on dynamic type.
Type of the object that is being pointed/referred by pointer/reference.
Only possible when virtual functions are invoked.
No any relation between dynamic binding and dynamic memory.
Maulik N. Patoliya
Accessing Method:
Accessing Method :
Using pointer or
Using object
reference to object
Non-Virtual
Static Binding Static Binding
Method
Virtual Method Static Binding Dynamic Binding
Maulik N. Patoliya
Virtual Functions :
When we use the same function name in both the base and derived classes , the
function in base class is declared as virtual using the keyword 'Virtual' preceding
its normal declaration.
Why can't we use the object name(with dot operator)the same way as any
other member function to call the virtual function ?
Virtual functions only work dynamically (at runtime) when we use a pointer or
reference to a base class.
When we call a virtual function using object :
obj.function() ;
The compiler decides at compile time which function to call - this is static binding.
Maulik N. Patoliya
But the main feature of virtual functions is runtime polymorphism (deciding which
function to call at runtime), which only works when using a pointer or reference to
the base class like :
Base* ptr = new Derived();
ptr->function();
Because, run time polymorphism is achieved only when a virtual function is accessed
through a pointer to the base class.
Maulik N. Patoliya
Example of Static & Dynamic Binding :
class B { int main() {
public: b.g();
OUTPUT :
void f() { B b; d.g();
cout << "B::f()\n"; D d; b_p1->g();
B::f() -- static binding
} b_p2->g();
D::f() -- static binding
virtual void g() { B *b_p1 = &b; b_r1.g();
B::f() -- static binding
cout << "B::g()\n"; B *b_p2 = &d; b_r2.g();
B::f() -- static binding
}
B::f() -- static binding
}; B &b_r1 = b; return 0;
B::f() -- static binding
B &b_r2 = d; }
B::g() -- static binding
class D: public B {
D::g() -- static binding
public: b.f();
B::g() -- dynamic binding
void f() { d.f();
D::g() -- dynamic binding
cout << "D::f()\n"; b_p1->f();
} B::g() -- dynamic binding
b_p2->f();
virtual void g() { D::g() -- dynamic binding
b_r1.f();
cout << "D::g()\n"; b_r2.f();
}
};
Maulik N. Patoliya
Rule No. Rules for Virtual Functions
1. virtual functions must be members of some class
2. They cannot be static members
3. accessed by using object pointers
4. virtual function can be a friend of another class
in base class must be defined, even though
5.
it may not be used.
Maulik N. Patoliya
Rule No. Rules for Virtual Functions
6. Function prototypes must match exactly in base and derived classes.
7. Constructors cannot be virtual, but destructors can
8. Base pointer can point to derived, but derived pointer can’t point to base
Pointer arithmetic on base pointer does not adjust for derived
9. object size (should not use this method to
move the pointer to the next object)
10. If not overridden in derived, base version is called
Maulik N. Patoliya
Polymorphic Type :
A class containing virtual member functions by definition or by inheritance is called
polymorphic type.
or
A class is called a polymorphic type if it contains a virtual function, which allows it to
support runtime polymorphism.
A polymorphic type is a class that has at least one virtual function.
Polymorphic hierarchies are of great interest for software designers.
Maulik N. Patoliya
Example :
class Animal {
public:
virtual void sound()
{ // virtual function
cout << "Animal sound" << endl;
}
};
Here, Animal is a polymorphic type because it has a virtual function.
Maulik N. Patoliya
Why constructor can't be virtual ?
Work of constructor is to build objects.
We can't use Virtual during object creation time , because virtual function need
an object to decide that which (Base or Derived) function should be call
NOTE : Virtual function can only call after the object is created.
Why destructor must be virtual (in polymorphic hierarchy) ?
If the base class doesn't have virtual destructor and we will delete derived object
using a base class pointer at that time only the destructor of base class would
run (the compiler uses static binding)
Maulik N. Patoliya
Example :
class A{ int main(){ OUTPUT :
public: A a;
A(){ B b;
A called
cout << "A called" << endl;
}
A called
A *b_ptr = &b;
~A(){ B called
cout << "~A called" << endl; delete b_ptr; ~A called
} ~B called
}; return 0; ~A called
} ~A called
class B:public A{
public:
B(){
cout << "B called" << endl;
} Problem of memory leak will generate, Because here derived
~B(){ object's memory which is pointed by base class pointer was not
cout << "~B called" << endl; deleted.
}
};
Maulik N. Patoliya
Solution (Make base class destructor virtual) :
class A{ int main(){ OUTPUT :
public: A a;
A(){ B b;
A called
cout << "A called" << endl;
} A called
A *b_ptr = &b;
virtual ~A(){ B called
cout << "~A called" << endl; delete b_ptr; ~B called
} ~A called
}; return 0; ~B called
} ~A called
class B:public A{ ~A called
public:
B(){
cout << "B called" << endl;
}
~B(){
cout << "~B called" << endl;
}
};
Maulik N. Patoliya
Pure virtual functions and Abstract Base classes :
If we have not defined any object of class and therefore any function in the base class
has been defined ‘empty’. Such functions are called “do-nothing” functions.
A “do-nothing” function may be defined as follows:
virtual void print() = 0;
Pure virtual functions :
A pure virtual function is a function declared in a base class that has no definition
relative to the base class.
Maulik N. Patoliya
Virtual Function Pure Virtual Function
Definition A function declared with virtual keyword and has a body A function declared with virtual keyword and assigned = 0
Syntax virtual void show() { } virtual void show() = 0;
Function Body in Base
Has function body in base class. Has no function body in base class — only declaration
Class
Must be overridden in derived class, or derived becomes
Derived Class Override Can be overridden in derived class (optional).
abstract
Forces derived classes to provide their own
Purpose Allows runtime polymorphism with optional override.
implementation
Maulik N. Patoliya
Abstract Base classes :
A class containing pure virtual functions cannot be used to declare any objects of its own,
such classes are called abstract base classes.
Should have at least one pure virtual function by inheritance or definition.
Once function is defined in derived class it is no longer pure virtual function.
Purpose : The main objective of an abstract base class is to provide some traits to
the derived classes and to create a base pointer required for achieving run time
polymorphism.
Maulik N. Patoliya
Example : class Rectangle: public Shape {
double length, width; int main() {
#include<iostream> public: Shape *sp[3] = {
Rectangle(double length, double width) { new Square(5.5),
#define PI 3.14 this->length = length; new Rectangle(5.5, 6.6),
using namespace std; this->width = width; new Circle(5.5)
} };
class Shape { double area() { for(int i = 0; i < 3; i++) {
public: return length * width; cout << sp[i]->area() << endl;
virtual double area() = 0; } }
}; }; return 0;
class Square: public Shape { class Circle: public Shape { }
double length; double radius;
public: public:
Square(double length) { Circle(double radius) {
OUTPUT :
this->length = length; this->radius = radius;
30.25
} }
36.3
double area() { double area() {
94.985
return length * length; return PI * radius * radius;
} } Here, class shape is ABC (Abstract Base Class)
}; };
Maulik N. Patoliya
Normal Class Abstract Base Class
Definition A class that can be instantiated directly A class that cannot be instantiated directly
Pure Virtual Function Does not contain any pure virtual function Contains at least one pure virtual function
Object Creation Objects of the class can be created Objects of the class cannot be created
Inheritance Not necessary Designed for inherited by other classes
Purpose Used to define complete functionality Used as a base class to define an interface
Function Pure virtual functions must be overridden in derived
All member functions are usually implemented
Implementation class
Maulik N. Patoliya
Function Overloading Function Hiding Function Overridding
Inheritance Not-necessary Must be there Must be there
Function Name Same Same Same
Function
Different Same or Different Same
Signature(Parameters)
Method must be present in
Virtual Not needed Not-needed
Base class
Binding Static Static Dynamic
Maulik N. Patoliya