0% found this document useful (0 votes)
14 views

Polymorphism

The document discusses different types of polymorphism in object-oriented programming, including compile-time polymorphism through function overloading and operator overloading, and runtime polymorphism through function overriding. Examples in C++ demonstrate how each type of polymorphism works.

Uploaded by

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

Polymorphism

The document discusses different types of polymorphism in object-oriented programming, including compile-time polymorphism through function overloading and operator overloading, and runtime polymorphism through function overriding. Examples in C++ demonstrate how each type of polymorphism works.

Uploaded by

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

Polymorphism

Unit 3
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

• This type of polymorphism is achieved by function


overloading or operator overloading.
Function Overloading

• When there are multiple functions with the same name but
different 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.
C++ program to demonstrate function overloading or Compile-time Polymorphism

#include <iostream>
using namespace std;

class Geeks {
public:
void func(int x)
{
cout << "value of x is " << x << endl;
}

// Function with same name but 1 double parameter


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

int main()
{
Geeks obj1;

// Function being called depends on the parameters passed

obj1.func(7);

obj1.func(9.132);

obj1.func(85, 64);
return 0;
}
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.
C++ program to demonstrate Operator Overloading or Compile-Time Polymorphism

#include <iostream>
using namespace std;

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

Complex operator+(Complex obj)


{
Complex c;
c.real = real + obj.real;
c.imag = imag + obj.imag;
return c;
}
void print()
{
cout << real << " + i" << imag << endl; }
};

int main()
{
Complex c1(10, 5), c2(2, 4);

Complex c3 = c1 + c2;
c3.print();
}
Runtime Polymorphism

• This type of polymorphism is achieved by Function


Overriding.

• Late binding and dynamic polymorphism are other names for


runtime polymorphism.
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.

You might also like