Polymorphism
Polymorphism
to be treated as instances of their base class rather than their actual class. It provides a way to
perform a single action in different forms. There are mainly two types of polymorphism:
1. Compile-Time Polymorphism
Function Overloading
#include <iostream>
using namespace std;
class Display {
public:
// Overloaded function to display an integer
void show(int i) {
cout << "Integer: " << i << endl;
}
int main() {
Display d;
d.show(42); // Calls show(int)
d.show(3.14); // Calls show(double)
d.show("Hello!"); // Calls show(string)
return 0;
}
Operator Overloading
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
void display() {
cout << "Real: " << real << " Imaginary: " << imag << endl;
}
};
int main() {
Complex c1(3.5, 2.5), c2(1.5, 3.5);
Complex c3 = c1 + c2; // Calls operator+
c3.display();
return 0;
}
2. Run-Time Polymorphism
Virtual Functions
Run-time polymorphism is achieved using virtual functions and inheritance. The function call is
resolved at runtime based on the actual object type.
#include <iostream>
using namespace std;
// Base class
class Base {
public:
virtual void display() { // Virtual function
cout << "Display from Base" << endl;
}
// Derived class
class Derived : public Base {
public:
void display() override { // Overriding the base class function
cout << "Display from Derived" << endl;
}
};
int main() {
Base* basePtr;
Derived derivedObj;
basePtr = &derivedObj;
return 0;
}
Pure virtual functions are a special kind of virtual function that do not have an implementation
in the base class. They are used to create abstract classes that cannot be instantiated directly.
Derived classes must provide an implementation for pure virtual functions.
1. Abstract Class: A class with at least one pure virtual function is called an abstract class.
2. No Instantiation: You cannot create objects of an abstract class.
3. Derived Classes: Derived classes must override all pure virtual functions to be
instantiated.
Syntax:
Example:
#include <iostream>
using namespace std;
class AbstractBase {
public:
// Pure virtual function
virtual void display() = 0;
};
class ConcreteDerived : public AbstractBase {
public:
void display() override { // Providing implementation
cout << "ConcreteDerived display function" << endl;
}
};
int main() {
// AbstractBase ab; // Error: cannot instantiate abstract class
ConcreteDerived cd;
cd.display(); // Outputs: "ConcreteDerived display function"
return 0;
}
Abstract Classes
Characteristics
int main() {
// Cannot create an object of an abstract class
// Shape shape; // Error
// Using polymorphism
Shape* shape1 = new Circle();
Shape* shape2 = new Square();
delete shape1;
delete shape2;
return 0;
}
● Abstract Class (Shape): The Shape class has a pure virtual function draw(), making it
an abstract class. It also contains a regular member function description(), which is
available to any derived class.
● Derived Classes (Circle and Square): These classes inherit from Shape and provide
their own implementations of the draw() function. Since they implement the pure virtual
function, objects of these classes can be created.
● Polymorphism: A base class pointer (Shape*) is used to point to derived class objects.
This enables the use of the draw() function through the base class pointer, which calls
the appropriate version of the function based on the object type (Circle or Square).
Why Use Abstract Classes?
1. Interface Design: Abstract classes are used to create interfaces that other classes must
follow. It defines the required functions without specifying how they should be
implemented, allowing each derived class to provide its own behavior.
2. Polymorphism: Abstract classes allow for polymorphic behavior, where a base class
pointer or reference can be used to invoke methods from derived classes, even though
the exact type of the object is determined at runtime.
3. Code Reusability: Common code can be placed in the abstract class, and different
derived classes can share this code. Only the specific implementations of pure virtual
functions need to be provided by derived classes.
4. Enforcing a Contract: Abstract classes ensure that all derived classes implement
certain methods, thereby enforcing a contract that must be followed by all subclasses.
● A virtual function is a function that can be overridden in a derived class but still has a
default implementation in the base class.
● A pure virtual function has no implementation in the base class and must be
overridden in the derived class. It is used to make a class abstract.