Polymorphism (1) 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

CONTENTS

 Polymorphism
 Types Of Polymorphism
 Static Polymorphism(compile Time)
 Function Overloading
 Operator Overloading
 Dynamic Polymorphism(run Time)
 Virtual Function
 Pure Virtual Function
Types of Polymorphism
Compile Time Polymorphism Run time Polymorphism
In Compile time Polymorphism, the call is In Run time Polymorphism, the call is not
resolved by the compiler. resolved by the compiler.

It is also known as Static binding, Early It is also known as Dynamic binding, Late
binding . binding .

It is achieved by function overloading and It is achieved by virtual functions and


operator overloading. pointers.

It provides fast execution because the method It provides slow execution as compare to
that needs to be executed is known early at early binding because the method that needs
the compile time. to be executed is known at the runtime.

Compile time polymorphism is less flexible Run time polymorphism is more flexible as
as all things execute at compile time. all things execute at run time.

Inheritance is not involved. Inheritance is involved.


Function Overloading

• In C++, two functions can have the same name if the number and/or type
of arguments passed is different.

• These functions having the same name but different arguments are known
as overloaded functions.
Example 1: Overloading Using Different Types of Parameter

#include <iostream>
using namespace std;
// function with float type parameter
float absolute(float var)
{
if (var < 0.0)
var = -var;
return var;
}
// function with int type parameter
int absolute(int var)
{
if (var < 0)
var = -var;
return var;
}
continued

int main()
{
// call function with int type parameter
cout << "Absolute value of -5 = " << absolute(-5) << endl;
// call function with float type parameter
cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;
return 0;
}
Example 2: Overloading Using Different Number of Parameters

#include <iostream>
using namespace std;

// function with 2 parameters


void display(int var1, double var2)
{
cout << "Integer number: " << var1;
cout << " and double number: " << var2 << endl;
}
// function with double type single parameter
void display(double var)
{
cout << "Double number: " << var << endl;
}
// function with int type single parameter
void display(int var)
{
cout << "Integer number: " << var << endl;
}
Continued

int main()
{
int a = 5;
double b = 5.5;
// call function with int type parameter
display(a);
// call function with double type parameter
display(b);
// call function with 2 parameters
display(a, b);
return 0;
}
Operator Overloading
Operator overloading is basically function overloading, where different
operator functions have the same symbol but different operands.

Syntax:
class className
{
public
returnType operator symbol (arguments)

{
//code to be executed
}
};
• returnType - is the return type of the function.
• operator - is a keyword.
• symbol - is the operator we want to overload. Like: +, <, -, ++, etc.
• arguments - is the arguments passed to the function.
Example1: ++ Operator (Unary Operator) Overloading

#include <iostream>
using namespace std;
class Count
{
private: int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ ()
{
value = value + 1;
}
void display()
{
cout << "Count: " << value << endl;
}
};
continued

int main()
{
Count count1;
// Call the "void operator++ ()" function
++count1;
count1.display();
return 0;
}
Example 2: ++ Operator (Unary Operator) Overloading
// Overload ++ when used as prefix and postfix
#include <iostream>
using namespace std;
class Count
{
private: int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ ()
{
++value;
}
// Overload ++ when used as postfix
void operator ++ (int)
{
value++;
}
continued

void display()
{
cout << "Count: " << value << endl;
}
};
int main()
{
Count count1;
// Call the "void operator ++ (int)" function
count1++;
count1.display();
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
There are 4 operators that cannot be overloaded in C++.

They are:
:: (scope resolution)
. (member selection)
.* (member selection through pointer to function)
?: (ternary operator)
Virtual functions
Virtual functions are used to achieve Dynamic polymorphism
(run time polymorphism)

A virtual function is a member function that is declared within


a base class and redefined by a derived class.

To create a virtual function, precede the function's declaration


in the base class with the keyword virtual.

Functions are declared with a virtual keyword in base class.

16
Rules for Virtual Functions:

•Virtual functions cannot be static.


•A virtual function can be a friend function of another class.
•Virtual functions should be accessed using pointer or reference of base class
type to achieve runtime polymorphism.
•The prototype of virtual functions should be the same in the base as well as
derived class.
•They are always defined in the base class and overridden in a derived class. A
class may have virtual destructor but it cannot have a virtual constructor.
•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 automatically considered as virtual functions in the
derived class.
Virtual functions
#include <iostream>
Class A
{
public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};
class B:public A
{
public: void display()
{
cout << "Derived Class is invoked"<<endl;
}
};
int main()
{
A *a;
B b;
a = &b;
a->display(); 18
return=0;
}
i#include<iostream>
using namespace std; int main()
class Base {
{ Base *b;
public: Derived d;
virtual void show() = 0; b = &d;
}; b->show();
class Derived : public Base return 0;
}
{
public:
void show()
{
cout << "Derived class is derived from the base class.";
}
};
Pure Virtual functions

•The "do-nothing" function is known as a pure virtual


function.
• A pure virtual function is a function declared in the base class
that has no definition relative to the base class.
•The main objective of the base class is to provide the traits to the
derived classes and to create the base pointer used for achieving
the runtime polymorphism.

20
Pure Virtual functions
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
cout << "Derived class is derived from the base class.";
}
};
int main()
{
Base *b;
Derived d;
b = &d;
b->show();
return 0;
}
EXERCISE
PROGRAMS

1.Write A Program To overload sum() function.


2.Write A Program On Binary Operator(+) Overloading which adds two complex num
3.Write A Program On Unary Operator(++) Overloading.
4.Write A Program To calculate area of square and circle using Pure Virtual Function.
5.Write A Program To Implement Virtual Function Using Pointers.

You might also like