0% found this document useful (0 votes)
10 views6 pages

C++ Polymorphism

Uploaded by

gurly101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

C++ Polymorphism

Uploaded by

gurly101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C++ 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.

Polymorphism is considered one of the important features of Object-Oriented Programming.

Types of Polymorphism

 Compile-time Polymorphism
 Runtime Polymorphism

Types of Polymorphism

1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator overloading.

A. Function Overloading

When there are multiple functions with the same name but different data type or parameters, then the
functions are said to be overloaded, hence this is known as Function Overloading.

Functions can be overloaded by changing the number of arguments or/and changing the type of
arguments.

In simple terms, it is a feature of object-oriented programming providing many functions that have
the same name but distinct parameters when numerous tasks are listed under one function name.
There are certain Rules of Function Overloading that should be followed while overloading a
function.

Below is the C++ program to show function overloading or compile-time polymorphism:

// CompileTimePolymorphism.cpp
// EXAMPLE 01: C++ program to demonstrate
// function overloading or Compile-time Polymorphism
#include <bits/stdc++.h>
C++ Polymorphism

using namespace std;

class example {
public:
// Function with one int parameter
void func(int x) {
cout << "value of x is " << x << endl;
}

// Function with same name but one double parameter


void func(double x) {
cout << "value of x is " << x << endl;
}

// Function with same name and 2 int parameters


void func(int x, int y) {
cout << "value of x and y is " << x << ", " << y
<< endl;
}
}; //end of class example

// main function
int main() {
example obj1;

// Function being called depends on the parameters passed


obj1.func(7); // func() is called with int value
obj1.func(9.132); // func() is called with double value
obj1.func(85, 64); // func() is called with 2 int values
return 0;
} //end main

Output

value of x is 7
value of x is 9.132
value of x and y is 85, 64

Explanation: In the above example, a single function named function func() acts differently in three
different situations, which is a property of polymorphism.

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 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:

// OperatorOverloading.cpp
// example 02 : C++ program to demonstrate
// Operator Overloading or Compile-Time Polymorphism
#include <iostream>
using namespace std;
C++ Polymorphism

class Complex {
private:
int real, imag; //private data member

public:
Complex(int r = 0, int i = 0) { // constructor with parameter
real = r; // initialize to zero
imag = i; // initialize to zero
} // end of constructor

// This is automatically called when '+' is used with between


// two Complex objects
Complex operator+(Complex const& obj) { // constructor with
// parameter
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
} // end of functions

void print() {
cout << real << " + i" << imag << endl;
} // end of print ()
}; // end of class Complex

// main function
int main() {
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
} // end of main

Output

12 + i9

Explanation: In the above example, the operator ‘+’ is overloaded. Usually, this operator
is used to add two numbers (integers or floating point numbers), but here the operator is
made to perform the addition of two imaginary or complex numbers.

2. Runtime Polymorphism

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.
C++ Polymorphism
Class Parent {
Public :
void func() {
Statements or body of func;
}
}; //end of class Parent

Class Child : public Parent {


Public :
void func() {
Statements or body of func;
}
}; //end of class Child (Derived class of Parent)

Int main () {
Child child_obj;
Child_obj.func();
Return 0;

Function overriding Explanation

Runtime Polymorphism with Data Members

Runtime Polymorphism cannot be achieved by data members in C++. Let’s see an example
where we are accessing the field by reference variable of parent class which refers to the
instance of the derived class.

// FunctionOverriding.cpp
// example 03: C++ program for function overriding with data members
#include <bits/stdc++.h>
using namespace std;

// base class declaration.


class Animal {
public:
string color = "Black";
}; //end of Animal (base class)

// inheriting Animal class.


class Dog : public Animal {
public:
string color = "Grey";
}; // end of sub class Dog (derived class)

// main function
int main(void) {
Animal d = Dog(); // accessing the field by reference
// variable which refers to derived
cout << d.color;
} // end of class
C++ Polymorphism
Output

Black

We can see that the parent class reference will always refer to the data member of the parent class.

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:

// VirtualFunction.cpp
// example 04 : C++ Program to demonstrate the Virtual Function
#include <iostream>
using namespace std;

// Declaring a Base class


class example_Base {

public:
virtual void display() { // virtual function
cout << "Called virtual Base Class function"
<< "\n\n";
}

void print() {
cout << "Called example_Base print function"
<< "\n\n";
}
}; //end of Parent Class

// Declaring a Child Class


class example_Child : public example_Base {

public:
void display() {
cout << "Called example_Child Display Function"
<< "\n\n";
} // end of display

void print() {
cout << "Called example_Child print Function"
<< "\n\n";
} //end of print
}; // end of sub class

// main function
int main() {
example_Base *base; // Create a reference of class example_Base
C++ Polymorphism

example_Child child;
base = &child;
base -> example_Base::display();// This will call
// the virtual function
base -> print(); // this will call the non-virtual function
} // end main

Output

Called virtual Base Class function


Called example_Base print function

Example 2:

// VirtualFunctionOverriding.cpp
// example 05 : C++ program for virtual function overriding
#include <bits/stdc++.h>
using namespace std;

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, we could also declared as
// virtual void print () explicitly
void print() { cout << "print derived class" << endl; }

void show() { cout << "show derived class" << endl; }


};

// main function
int main() {
base* b_ptr;
derived d;
b_ptr = &d;

// Virtual function, bind at runtime (Runtime polymorphism)


b_ptr -> print();

// Non-virtual function, bind at compile time


b_ptr -> show();

return 0;
}

Output

print derived class


show base class

You might also like