Unit 4-Polymorphism
Unit 4-Polymorphism
Concept of Polymorphism:-
The word “polymorphism” means having many forms. In simple words, we can define polymorphism
as the ability of a message to be displayed in more than one form. A real-life example of
polymorphism is a person who at the same time can have different characteristics. A man at the same
time is a father, a husband, and an employee. So the same person exhibits different behavior in
different situations. This is called polymorphism.
Types of Polymorphism
Compile-time Polymorphism
Runtime Polymorphism
Mrs. P.D.Titavekar 1
Object Oriented Programming using c++
public:
// Function with 1 int parameter
void func(int x)
{
cout << "value of x is " << x << endl;
}
// Driver code
int main()
{
Geeks obj1;
B. Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data type, this ability is
known as operator overloading. For example, we can make use of the addition operator (+) for string
class to concatenate two strings. We know that the task of this operator is to add two operands. So a
Mrs. P.D.Titavekar 2
Object Oriented Programming using c++
single operator ‘+’, when placed between integer operands, adds them and when placed between
string operands, concatenates them.
Below is the C++ program to demonstrate operator overloading:
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
// Driver code
int main()
{
Complex c1(10, 5), c2(2, 4);
2. Runtime Polymorphism
Mrs. P.D.Titavekar 3
Object Oriented Programming using c++
This type of polymorphism is achieved by Function Overriding. Late binding and dynamic
polymorphism are other names for runtime polymorphism. The function call is resolved at runtime
in runtime polymorphism. In contrast, with compile time polymorphism, the compiler determines
which function call to bind to the object after deducing it at runtime.
A. Function Overriding
Function Overriding occurs when a derived class has a definition for one of the member functions of
the base class. That base function is said to be overridden.
// Driver code
int main(void)
{
Animal d = Dog(); // accessing the field by reference
// variable which refers to derived
Mrs. P.D.Titavekar 4
Object Oriented Programming using c++
cout << d.color;
}
Output
Black
B. Virtual Function
A virtual function is a member function that is declared in the base class using the keyword virtual
and is re-defined (Overridden) in the derived class.
Some Key Points About Virtual Functions:
Virtual functions are Dynamic in nature.
They are defined by inserting the keyword “virtual” inside a base class and are always declared with a
base class and overridden in a child class
A virtual function is called during Runtime
Below is the C++ program to demonstrate virtual function:
public:
// virtual function
virtual void display()
{
cout << "Called virtual Base Class function"
<< "\n\n";
}
void print()
{
cout << "Called GFG_Base print function"
<< "\n\n";
}
};
public:
void display()
{
cout << "Called GFG_Child Display Function"
<< "\n\n";
}
void print()
{
Mrs. P.D.Titavekar 5
Object Oriented Programming using c++
cout << "Called GFG_Child print Function"
<< "\n\n";
}
};
// Driver code
int main()
{
// Create a reference of class GFG_Base
GFG_Base* base;
GFG_Child child;
base = &child;
Output
Called virtual Base Class function
class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}
// Driver code
Mrs. P.D.Titavekar 6
Object Oriented Programming using c++
int main()
{
base* bptr;
derived d;
bptr = &d;
return 0;
}
Output
print derived class
show base class
this Pointer:-
To understand ‘this’ pointer, it is important to know how objects look at functions and data members
of a class.
Each object gets its own copy of the data member.
All-access the same function definition as present in the code segment.
#include<iostream>
using namespace std;
int main()
Mrs. P.D.Titavekar 7
Object Oriented Programming using c++
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
Output:
x = 20
When a reference to a local object is returned, the returned reference can be used to chain function
calls on a single object.
#include<iostream>
using namespace std;
class Test
{
private:
int x;
int y;
public:
Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
Test &setX(int a) { x = a; return *this; }
Test &setY(int b) { y = b; return *this; }
void print() { cout << "x = " << x << " y = " << y << endl; }
};
int main()
{
Test obj1(5, 5);
obj1.print();
return 0;
}
Output:
x = 10 y = 20
Mrs. P.D.Titavekar 8
Object Oriented Programming using c++
Exercise:
Predict the output of following programs. If there are compilation errors, then fix them.
Question 1
#include<iostream>
using namespace std;
class Test
{
private:
int x;
public:
Test(int x = 0) { this->x = x; }
void change(Test *t) { this = t; }
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj(5);
Test *ptr = new Test (10);
obj.change(ptr);
obj.print();
return 0;
}
Question 2
#include<iostream>
using namespace std;
class Test
{
private:
int x;
int y;
public:
Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
static void fun1() { cout << "Inside fun1()"; }
static void fun2() { cout << "Inside fun2()"; this->fun1(); }
};
int main()
{
Test obj;
obj.fun2();
return 0;
}
Question 3
#include<iostream>
using namespace std;
Mrs. P.D.Titavekar 9
Object Oriented Programming using c++
class Test
{
private:
int x;
int y;
public:
Test (int x = 0, int y = 0) { this->x = x; this->y = y; }
Test setX(int a) { x = a; return *this; }
Test setY(int b) { y = b; return *this; }
void print() { cout << "x = " << x << " y = " << y << endl; }
};
int main()
{
Test obj1;
obj1.setX(10).setY(20);
obj1.print();
return 0;
}
Question 4
#include<iostream>
using namespace std;
class Test
{
private:
int x;
int y;
public:
Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
void setX(int a) { x = a; }
void setY(int b) { y = b; }
void destroy() { delete this; }
void print() { cout << "x = " << x << " y = " << y << endl; }
};
int main()
{
Test obj;
obj.destroy();
obj.print();
return 0;
}
Mrs. P.D.Titavekar 10
Object Oriented Programming using c++
The pointer of Base Class pointing different objects of the derived class
Approach:
A derived class is a class that takes some properties from its base class.
It is true that a pointer of one class can point to another class, but classes must be a base and
derived class, then it is possible.
To access the variable of the base class, a base class pointer will be used.
So, a pointer is a type of base class, and it can access all, public function and variables of the
base class since the pointer is of the base class, this is known as a binding pointer.
In this pointer base class is owned by the base class but points to the derived class object.
The same works with derived class pointer, values are changed.
Example:
// C++ program to Demonstrate the
// implementation of the base class
// pointer pointing to derived class
#include <iostream>
using namespace std;
// Base Class
class BaseClass {
public:
int var_base;
Mrs. P.D.Titavekar 11
Object Oriented Programming using c++
// and derived class members
void display()
{
cout << "Displaying Base class"
<< "variable var_base: " << var_base << endl;
cout << "Displaying Derived "
<< " class variable var_derived: "
<< var_derived << endl;
}
};
// Driver Code
int main()
{
// Pointer to base class
BaseClass* base_class_pointer;
BaseClass obj_base;
DerivedClass obj_derived;
base_class_pointer->var_base = 34;
base_class_pointer->var_base = 3400;
base_class_pointer->display();
DerivedClass* derived_class_pointer;
derived_class_pointer = &obj_derived;
derived_class_pointer->var_base = 9448;
derived_class_pointer->var_derived = 98;
derived_class_pointer->display();
return 0;
}
Output:-
Displaying Base class variable var_base: 34
Displaying Base class variable var_base: 3400
Displaying Base classvariable var_base: 9448
Displaying Derived class variable var_derived: 98
Mrs. P.D.Titavekar 12
Object Oriented Programming using c++
Conclusion:
A pointer to a derived class is a pointer of a base class pointing to a derived class, but it will hold its
aspect.
This pointer of the base class will be able to temper functions and variables of its own class and can
still point to the derived class object.
#include <iostream>
using namespace std;
class base {
public:
virtual void print() { cout << "print base class\n"; }
Mrs. P.D.Titavekar 13
Object Oriented Programming using c++
int main()
{
base* bptr;
derived d;
bptr = &d;
return 0;
}
Output
print derived class
show base class
Explanation: Runtime polymorphism is achieved only through a pointer (or reference) of the base
class type. Also, a base class pointer can point to the objects of the base class as well as to the objects
of the derived class. In the above code, the base class pointer ‘bptr’ contains the address of object ‘d’
of the derived class.
/* Other members */
};
Example
A pure virtual function is implemented by classes that are derived from an Abstract class.
// C++ Program to illustrate the abstract class and virtual
// functions
#include <iostream>
using namespace std;
class Base {
// private member variable
int x;
Mrs. P.D.Titavekar 14
Object Oriented Programming using c++
public:
// pure virtual function
virtual void fun() = 0;
public:
// implementation of the pure virtual function
void fun() { cout << "fun() called"; }
};
int main(void)
{
// creating an object of Derived class
Derived d;
return 0;
}
Output
fun() called
#include <iostream>
using namespace std;
class Base {
public:
// pure virtual function
virtual void show() = 0;
};
int main(void)
{
// creating a pointer of type
// Base pointing to an object
// of type Derived
Base* bp = new Derived();
return 0;
}
Output
In Derived
2. An abstract class can have constructors.
For example, the following program compiles and runs fine.
// C++ program to demonstrate that
// an abstract class can have constructors.
#include <iostream>
using namespace std;
public:
// pure virtual function
virtual void fun() = 0;
public:
// calling the constructor of Base class
Derived(int i, int j)
: Base(i)
Mrs. P.D.Titavekar 16
Object Oriented Programming using c++
{
y = j;
}
int main(void)
{
// creating an object of Derived class
Derived d(4, 5);
return 0;
}
Output
Constructor of base called
x = 4, y = 5
Constructor of base called
x = 6, y = 7
Mrs. P.D.Titavekar 17