460004823 Polymorphism in C
460004823 Polymorphism in C
The word polymorphism means having many forms. In simple words, we can define
Real life example of polymorphism, a person at the same time can have different
characteristics. Like a man at the same time is a father, a husband, an employee. So the same
★ Function Overloading: When there are multiple functions with same name but
different parameters then these functions are said to be overloaded. Functions
can be overloaded by change in number of arguments or/and c
hange in type of
arguments.
// C++ program for function overloading
class A
{
public:
void func(int x, int y) // function with same name and 2 int parameters
{
cout << "value of x and y is " << x << ", " << y << endl;
}
};
int main() {
A obj1;
Output:
value of x is 7
value of x is 9.132
value of x and y is 85, 64
In the above example, a single function named func acts differently in three different situations
★ Operator Overloading: C++ also provide option to overload operators. For example, we
can make the operator (‘+’) for string class to concatenate two strings. We know that this
is the addition operator whose task is to add two operands. So a single operator ‘+’ when
placed between integer operands , adds them and when placed between string operands,
concatenates them.
Example: // CPP program to illustrate Operator Overloading
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{ real = r , imag = i; }
// This is automatically called when '+' is used with between two Complex objects
class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }
void show ()
{ cout<< "show base class" <<endl; }
};
class derived:public base
{
public: //print () is already virtual function in derived class,
void print () //we could also declared as virtual void print ()explicitly
{ cout<< "print derived class" <<endl; }
void show ()
{ cout<< "show derived class" <<endl; }
};
int main()
{
base *bptr;
derived d;
bptr = &d;
re-defined(Overriden) by a derived class. When you refer to a derived class object using a
pointer or a reference to the base class, you can call a virtual function for that object and
● Virtual functions ensure that the correct function is called for an object, regardless
of the type of reference (or pointer) used for function call.
● They are mainly used to achieve Runtime polymorphism
● Functions are declared with a v
irtual keyword in base class.
● The resolving of function call is done at Run-time.
1. Virtual functions cannot be static and also cannot be a friend function of another
class.
2. Virtual functions should be accessed using pointer or reference of base class type
to achieve run time polymorphism.
3. The prototype of virtual functions should be same in base as well as derived class.
4. They are always defined in base class and overridden in derived class. It is not
mandatory for derived class to override (or re-define the virtual function), in that
case base class version of function is used.
5. A class may have virtual destructor but it cannot have a virtual constructor.
Compile-time(early binding) VS run-time(late binding) behavior of Virtual Functions
Consider the following simple program showing run-time behavior of virtual functions.
class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}
void show()
{
cout << "show base class" << endl;
}
};
class type. Also, a base class pointer can point to the objects of base class as well as to the
objects of derived class. In above code, base class pointer ‘bptr’ contains the address of object
Late binding(Runtime) is done in accordance with the content of pointer (i.e. location pointed to
by pointer) and Early binding(Compile time) is done according to the type of pointer, since
print() function is declared with virtual keyword so it will be bound at run-time (output is p
rint
derived class as pointer is pointing to object of derived class ) and show() is non-virtual so it will
be bound during compile time(output is show base class as pointer is of base type ).
NOTE: If we have created a virtual function in the base class and it is being overridden in the
derived class then we don’t need virtual keyword in the derived class, functions are
The idea is that virtual functions are called according to the type of the object instance pointed
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show()
{
cout << " In Base \n";
}
};
class Derived : public Base
{
public:
void show()
{
cout << "In Derived \n";
}
};
int main(void)
{
Base* bp = new Derived;
// RUN-TIME POLYMORPHISM
bp->show();
return 0;
}
Output:
In Derived
Virtual functions allow us to create a list of base class pointers and call methods of any of the