POLYMORPHISM
The word polymorphism means having
many forms.
TYPES OF POLYMORPHISM
Polymorphism in C++ can be classified into two
types:
• Compile-time Polymorphism
• Runtime Polymorphism
Function Overloading:
Function overloading is a feature of object-oriented
programming where two or more functions can have
the same name but behave differently for different
parameters.
#include <iostream.h>
using namespace std;
class Sample {
public:
// Function to add two integers
void add(int a, int b) {
cout << "Integer Sum = " << a + b << endl;
}
// Function to add two floating point values
void add(double a, double b) {
cout << "Float Sum = " << a + b << endl ;
}
};
int main() {
Sample s1;
// add() called with int values
s1.add(10, 2);
// add() called with double value
s1.add(5.3, 6.2);
return 0;
}
Operator Overloading:
C++ has the ability to provide the operators
with a special meaning for particular data
type.
#include <iostream>
using namespace std;
class Complex {
public:
int real, imag;
Complex(int r, int i) :
real(r), imag(i) {}
// Overloading the '+' operator
Complex operator+(const Complex& obj) {
return Complex(real + obj.real,
imag + obj.imag);
}
};
int main() {
Complex c1(10, 5), c2(2, 4);
// Adding c1 and c2 using + operator
Complex c3 = c1 + c2;
cout << c3.real << " + i" << c3.imag;
return 0;
}
Inheritance:
Inheritance, a core concept in object-oriented
programming, allows classes to inherit properties
and behaviors from other classes.
TYPES OF INHERITANCE
single
multiple
multilevel
hierarchical
hybrid
1.Single Inheritance: A class inherits from only one parent class.
2.Multiple Inheritance: A class inherits from multiple parent classe
3.Multilevel Inheritance: A class inherits from a class that is itself
derived class.
4.Hierarchical Inheritance: Multiple classes inherit from a single p
class.
5.Hybrid Inheritance: A combination of two or more inheritance
types (e.g., multiple and multilevel).
Base Class
•A base class (or parent class) is a class that
provides common attributes and methods.
•Other classes can inherit from this base class
and reuse or extend its functionality.
Derived Class
•A derived class (or child class) inherits
properties and behaviors from a base class.
•It can add new members or override existing
ones from the base class.
Syntax:
class BaseClass {
// members of base class
};
class DerivedClass : access_specifier
BaseClass {
// members of derived class
};
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void sound() {
cout << "Animal makes a sound" << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void sound() {
cout << "Dog barks" << endl;
}
};
int main() {
Animal a;
a.sound();
Dog d;
d.sound();
return 0;
}
Access specifiers
•public inheritance makes public members
of the base class public in the derived class,
and the protected members of the base
class remain protected in the derived class.
•protected inheritance makes the public
and protected members of the base class
protected in the derived class.
•private inheritance makes the public and
protected members of the base class private
in the derived class.
C++ public Inheritance
public inheritance is demonstrated. Since private and protected
members will not be directly accessed from main( ) so we have had to
create functions name getPVT( ) to access the private variable and
getProt( ) to access the protected variable from the inherited class.
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() { return pvt; }
};
class PublicDerived : public Base {
public:
// function to access protected member from Base
int getProt() { return prot; }
};
int main()
{
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}
C++ Protected Inheritance
We know that protected members can only
be accessed from the Derived class. These
members cannot be directly accessed from
outside the class.
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() { return pvt; }
};
class ProtectedDerived : protected Base {
public:
// function to access protected member from Base
int getProt() { return prot; }
// function to access public member from Base
int getPub() { return pub; }
// function to get access to private members from Base
int try_getPVT() {return Base::getPVT(); }
};
int main()
{
ProtectedDerived object1;
cout << "Private = " << object1.try_getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Constructor in Multiple Inheritance in C++
Constructor is a class member function with the same name
as the class. The main job of the constructor is to allocate memory
for class objects. Constructor is automatically called when the object
is created.
Multiple Inheritance:
Multiple Inheritance is a feature of C++ where a class can
derive from several(two or more) base classes. The constructors of
inherited classes are called in the same order in which they are
inherited.
#include<iostream>
using namespace std;
class A1
{
public:
A1()
{
cout << "Constructor of the base class A1 \n";
}
};
class A2
{
public:
A2()
{
cout << "Constructor of the base class A2 \n";
}
};
class S: public A1, virtual A2
{
public:
S(): A1(), A2()
{
cout << "Constructor of the derived class S \n";
}
};
// Driver code
int main()
{
S obj;
return 0;
}
#include<iostream>
using namespace std;
class A1
{
public:
A1()
{
int a = 20, b = 35, c;
c = a + b;
cout << "Sum is:" <<
c << endl;}};
class A2
{
public:
A2()
{
int x = 50, y = 42, z;
z = x - y;
cout << "Difference is:" <<
z << endl; }};
class S: public A1,virtual A2
{
public:
S(): A1(), A2()
{
int r = 40, s = 8, t;
t = r * s;
cout << "Product is:" <<
t << endl; }};
// Driver code
int main()
{
S obj;
return 0;
}
#include<iostream>
using namespace std; class S: public A1, virtual A2
class A1 {
{ public:
public: S(): A1(), A2()
A1() {
{ cout << "Constructor of the derived class S \
n";
cout << "Constructor of the base class A1 \n";
}
}
~S()
~A1()
{
{
cout << "Destructor of the derived class S \n";
cout << "Destructor of the base class A1 \n";
}
}};
};
class A2
{
// Driver code
public:
int main()
A2()
{
{
S obj;
cout << "Constructor of the base class A2 \n"; }
return 0;
~A2()
}
{
cout << "Destructor of the base class A2 \n";}};
Method Overriding
Method overriding means a subclass provides a
specific implementation of a method that is already
defined in its superclass. The method must have the same
name, return type, and parameters.
Virtual function
A virtual function is a function in a base class that
you expect to be overridden in derived classes. It allows
dynamic (runtime) polymorphism, so the actual
object type decides which version of the function runs —
even if you're using a pointer or reference to the base
class.
#include <iostream>
#include <cmath> void displayArea() override {
using namespace std; cout << "Circle area: " << M_PI * radius * radius << endl;
}
class Shape {
public: ~Circle() {
Shape() { cout << "Circle destructor called" << endl;
cout << "Shape constructor called" << endl; }
displayArea(); // Virtual method called in constructor };
} int main() {
virtual void displayArea() { Circle c(5.0);
cout << "Shape area: Unknown" << endl; cout << "Calling displayArea() after object is created:" <<
} endl;
virtual ~Shape() { c.displayArea();
cout << "Shape destructor called" << endl; return 0;
} }
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {
cout << "Circle constructor called" << endl;
}
Abstract base class
Abstract base classes are designed to be inherited by
derived classes. Derived classes are then responsible for
providing concrete implementations for all pure virtual
functions inherited from the abstract base class.
class AbstractBase {
public:
virtual void pureVirtualFunction() = 0; // Pure virtual
function
virtual void anotherVirtualFunction()
{ // Can have concrete virtual functions too
} };
#include <iostream>
class Shape { // Abstract Base Class
public:
virtual double calculateArea() = 0; // Pure virtual function
virtual void display() = 0; // Pure virtual function
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() override {
return 3.14159 * radius * radius;
}
void display() override {
std::cout << "Circle with radius: " << radius << std::endl;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double calculateArea() override {
return length * width;
}
void display() override {
std::cout << "Rectangle with length: " << length << ", width: " << width << std::endl;
}
};
int main() {
// Shape s; // Error: cannot instantiate abstract class
Circle c(5.0);
c.display();
std::cout << "Area: " << c.calculateArea() << std::endl;
Rectangle r(4.0, 6.0);
r.display();
std::cout << "Area: " << r.calculateArea() << std::endl;
Shape* s1 = &c; // Polymorphic use
s1->display();
return 0;
}
OUTPUT:
Circle with radius: 5
Area: 78.5398
Rectangle with length: 4, width: 6
Area: 24
Circle with radius: 5