UNIT-4
Polymorphism
The word polymorphism means ‘having many forms’. Thus, we can say that polymorphism
is the ability for an entity (functions, operators, methods etc.) to exist in multiple forms.
● Polymorphism is a very important feature of C++ as it helps in implementing
object-oriented programming.
● Polymorphism helps in making object-oriented programming more realistic.
This is so because the same person or object may behave differently or attain
different characteristics in different scenarios.
● Polymorphism is generally applied on a hierarchy of classes to better illustrate
the relation between them.
● There are two types of polymorphism in C++: Compile time Polymorphism and
Runtime Polymorphism.
Compile time Polymorphism
● In compile time polymorphism, the compiler knows which form of the method to call
during compilation.
● Compile time polymorphism is implemented by functional overloading, operator
overloading and constructor overloading.
2
● In compile time polymorphism the different forms of the method are differentiated
and identified based on the number and type of input parameters of the method.
● Compile time polymorphism is also referred to as static binding or early binding as
the function call here is resolved statically, that is at compile time.
● Also known as early binding and static polymorphism.
1. Function overloading
#include <iostream>
using namespace std;
void test(int i) {
cout << " The int is " << i << endl;
void test(double f) {
cout << " The float is " << f << endl;
void test(char const *ch) {
cout << " The char* is " << ch << endl;
int main() {
test(5);
test(5.5);
test("five");
return 0;
2. Operator overloading: …..?
3
Runtime Polymorphism
● In runtime polymorphism, the compiler doesn’t know which form of the method will
be called during compilation. This is resolved at the time of execution
● Runtime polymorphism in C++ is implemented by virtual functions and functional
overriding.
● In runtime polymorphism, all the different forms of the function may or may not have
the same signature (return type and parameter list). However, the name of the
function has to be the same.
● Runtime polymorphism is also referred to as dynamic binding or late binding as the
function call here is resolved dynamically, that is at time of execution of program.
● Also known as late binding and dynamic polymorphism.
3. Function Overriding
#include <iostream>
using namespace std;
class Mammal {
public:
void eat() {
cout << "Mammals eat...";
}
};
class Cow: public Mammal {
public:
void eat() {
cout << "Cows eat grass...";
}
};
int main(void) {
Cow c = Cow();
[Link]();
return 0;
}
4
Output:
Mammals eat...
Virtual Functions
A virtual function is a form of a member function declared within a base class and redefined
by a derived class. The keyword virtual is used to create a virtual function, preceding the
function's declaration in the base class. If a class includes a virtual function and gets
inherited, the virtual class redefines a virtual function to go with its own need. In other
words, a virtual function is a function that gets overridden in the derived class and instructs
the C++ compiler to execute late binding on that function. A function call is resolved at
runtime in late binding, so the compiler determines the object type at runtime.
#include <iostream>
using namespace std;
class base
public:
virtual void show()
cout<<"\n Showing base class....";
};
class derived:public base
public:
void show()
5
cout<<"\n Showing derived class....";
};
int main()
Base * b;
derived d;
b=&d;
b->show();
return 0;
}
Output:
Showing derived class....
Pure Virtual Functions & Abstract classes
A pure virtual function is a function declared in a base class that must be overridden in
any derived class.
It is written like this:
virtual void draw() = 0; // Pure virtual function
● The = 0 means “no default implementation”.
● It forces derived classes to implement the function.
An abstract class is a class that cannot be used to create objects directly. It is meant to be
a base class that provides a common interface but doesn't define full behavior.
A class becomes abstract if it has at least one pure virtual function.
6
Example of Abstract Class and Pure Virtual Function
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing Circle\n";
};
int main() {
// Shape s; Error: Cannot create object of abstract class
Shape* s = new Circle(); // Pointer to abstract class
s->draw(); // Output: Drawing Circle
delete s;
● An abstract class defines what derived classes must do, but not how.
7
● A pure virtual function is a function with = 0 and must be overridden.
● You can't create objects of abstract classes.
● Useful when you want to define a common interface but let derived classes
define specific behavior.