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

Polymorphism Means More Than One Function With Same Name

Polymorphism allows functions to have the same name but different implementations. There are two types: static polymorphism is resolved at compile-time using function overloading and operator overloading, while dynamic polymorphism is resolved at runtime using virtual functions. Function overloading allows multiple functions with the same name but different parameters, and operator overloading allows operators to work on user-defined types. Virtual functions allow derived classes to override base class functions and have the correct version called based on the object's actual type.

Uploaded by

Ashutosh Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Polymorphism Means More Than One Function With Same Name

Polymorphism allows functions to have the same name but different implementations. There are two types: static polymorphism is resolved at compile-time using function overloading and operator overloading, while dynamic polymorphism is resolved at runtime using virtual functions. Function overloading allows multiple functions with the same name but different parameters, and operator overloading allows operators to work on user-defined types. Virtual functions allow derived classes to override base class functions and have the correct version called based on the object's actual type.

Uploaded by

Ashutosh Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Polymorphism means more than one function with same name, with different working.

Polymorphism can be static or dynamic. In static polymorphism memory will be allocated at


compile-time. In dynamic polymorphism memory will be allocated at run-time. Both function
overloading and operator overloading are an examples of static polymorphism. Virtual function
is an example of dynamic polymorphism.

 Static polymorphism is also known as early binding and compile-time polymorphism.

 Dynamic polymorphism is also known as late binding and run-time polymorphism.

 Function Overloading : Function overloading is an example of static polymorphism.


More than one function with same name, with different signature in a class or in a same
scope is called function overloading.

More than one function with same name, with different signature in a class or in a same scope is
called function overloading. Signature of function includes :

 Number of arguments

 Type of arguments

 Sequence of arguments

When you call an overloaded function, the compiler determines the most appropriate definition
to use by comparing the signature of calling statement with the signature specified in the
definitions.

Example of function overloading

#include<iostream.h>
#include<conio.h>

class CalculateArea
{

public:
void Area(int r) //Overloaded Function 1
{
cout<<"\n\tArea of Circle is : "<<3.14*r*r;
}

void Area(int l,int b) //Overloaded Function 2


{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}

void Area(float l,int b) //Overloaded Function


3
{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}

void Area(int l,float b) //Overloaded Function


4
{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}

};

void main()
{

CalculateArea C;

C.Area(5); //Statement 1
C.Area(5,3); //Statement 2
C.Area(7,2.1f); //Statement 3
C.Area(4.7f,2); //Statement 4

Output :

Area of Circle is : 78.5


Area of Rectangle is : 15
Area of Rectangle is : 14.7
Area of Rectangle is : 29.4

In the above example, we have four member functions named Area. Statement 1 will invoke the
function 1 b'coz the signature of function 1 is similar to the statement 1. Similarly Statement 3
will invoke function 4 b'coz statement 3 is passing two arguments, 1st is of integer type and 2nd
is of float type. Function 4 is the only function who is receiving integer and float respectively.
 Operator Overloading : Another example of static polymorphism is Operator
overloading. Operator overloading is a way of providing new implementation of existing
operators to work with user-defined data types.

An operator is a symbol that tells the compiler to perform specific task. Every operator have their
own functionality to work with built-in data types. Class is user-defined data type and compiler
doesn't understand, how to use operators with user-defined data types. To use operators with
user-defined data types, they need to be overload according to a programmer's requirement.

Operator overloading is a way of providing new implementation of existing operators to work


with user-defined data types.

An operator can be overloaded by defining a function to it. The function for operator is declared
by using the operator keyword followed by the operator.

There are two types of operator overloading in C++

 Binary Operator Overloading

 Unary Operator Overloading

Overloading of Binary operator

Binary operator is an operator that takes two operand(variable). Binary operator overloading is
similar to unary operator overloading except that a binary operator overloading requires an
additional parameter.

Binary Operators

 Arithmetic operators (+, -, *, /, %)

 Arithmetic assignment operators (+=, -=, *=, /=, %=)

 Relational operators (>, <, >=, <=, !=, ==)

Example

#include<iostream.h>
#include<conio.h>

class Rectangle
{

int L,B;

public:
Rectangle() //Default Constructor
{
L = 0;
B = 0;
}

Rectangle(int x,int y) //Parameterize Constructor


{
L = x;
B = y;
}

Rectangle operator+(Rectangle Rec) //Binary operator


overloading func.
{
Rectangle R;

R.L = L + Rec.L;
R.B = B + Rec.B;

return R;
}

void Display()
{
cout<<"\n\tLength : "<<L;
cout<<"\n\tBreadth : "<<B;
}

};

void main()
{

Rectangle R1(2,5),R2(3,4),R3;
//Creating Objects

cout<<"\n\tRectangle 1 : ";
R1.Display();

cout<<"\n\n\tRectangle 2 : ";
R2.Display();

R3 = R1 + R2; Statement 1

cout<<"\n\n\tRectangle 3 : ";
R3.Display();
}

Output :

Rectangle 1 :
L : 2
B : 5
Rectangle 2 :
L : 3
B : 4

Rectangle 3 :
L : 5
B : 9

In statement 1, Left object R1 will invoke operator+() function and right object R2 is passing as
argument.

Another way of calling binary operator overloading function is to call like a normal member
function as follows

R3 = R1.operator+ ( R2 );

Overloading Unary operator

Unary operator is an operator that takes single operand(variable). Both increment(++) and
decrement(--) operators are unary operators.

Example of Unary Operator Overloading

#include<iostream.h>
#include<conio.h>

class Rectangle
{

int L,B;

public:

Rectangle() //Default Constructor


{
L = 0;
B = 0;
}

void operator++() Unary operator overloading func.


{
L+=2;
B+=2;
}

void Display()
{
cout<<"\n\tLength : "<<L;
cout<<"\n\tBreadth : "<<B;
}

};
void main()
{

Rectangle R;
//Creating Object

cout<<"\n\tLength Breadth before increment";


R.Display();

R++;

cout<<"\n\n\tLength Breadth after increment";


R.Display();
}

Output :

Length Breadth after increment


L : 0
B : 0

Length Breadth after increment


L: 2
B: 2

Virtual function : Virtual function is an example of dynamic polymorphism. Virtual function is


used in situation, when we need to invoke derived class function using base class pointer. Giving
new implementation of derived class method into base class and the calling of this new
implemented function with base class's object is done by making base class function as virtual
function. This is how we can achieve "Runtime Polymorphism".

Giving new implementation of base class method into derived class and the calling of this new
implemented function with derived class's object is called function overriding.

Giving new implementation of derived class method into base class and the calling of this new
implemented function with base class's object is done by making base class function as virtual
function.

Virtual function is used in situation, when we need to invoke derived class function using base
class pointer. We must declare base class function as virtual using virtual keyword preceding its
normal declaration. The base class object must be of pointer type so that we can dynamically
replace the address of base class function with derived class function. This is how we can
achieve "Runtime Polymorphism".
If we doesn't use virtual keyword in base class, base class pointer will always execute function
defined in base class.

Example of virtual function


#include<iostream.h>
#include<conio.h>

class BaseClass
{

public:
virtual void Display()
{
cout<<"\n\tThis is Display() method of Base Class";
}

void Show()
{
cout<<"\n\tThis is Show() method of Base Class";
}

};

class DerivedClass : public BaseClass


{

public:
void Display()
{
cout<<"\n\tThis is Display() method of Derived Class";
}

void Show()
{
cout<<"\n\tThis is Show() method of Derived Class";
}

};
void main()
{

DerivedClass D;

BaseClass *B; //Creating Base Class Pointer


B = new BaseClass;

B->Display(); //This will invoke Display() method of


Base Class
B->Show(); //This will invoke Show() method of
Base Class

B=&D;

B->Display(); //This will invoke Display() method of


Derived Class
//bcoz Display() method is
virtual in Base Class

B->Show(); //This will invoke Show() method of


Base Class
//bcoz Show() method is not
virtual in Base Class

Output :

This is Display() method of Base Class


This is Show() method of Base Class
This is Display() method of Derived Class
This is Show() method of Base Class

You might also like