POLYMORPHISM

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

POLYMORPHISM

DEFINITION

• 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
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 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.
• #include <iostream.h>
• Class ABC {
• 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;
• }
• // Function with same name and
• // 2 int parameters
• void func(int x, int y)
• {
• cout << "value of x and y is " << x << ", " << y
• << endl;
• }
• };
• // Driver code
• int main()
• {
• ABC obj1;
• // Function being called depends
• // on the parameters passed
• // func() is called with int value
• obj1.func(7);

• // func() is called with double value


• obj1.func(9.132);

• // func() is called with 2 int values
• obj1.func(85, 64);
• return 0;
• }
OUTPUT

• Output
• value of x is 7
• value of x is 9.132
• value of x and y is 85, 64
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.
• #include <iostream>
• #include<conio.h>
• class Count {
• private:
• int value;
• public:
• // Constructor to initialize count to 5
• Count()
• {
• Value=5;
• }
• // Overload ++ when used as prefix
• void operator ++ () {
• ++value;
• }
• void display() {
• cout << "Count: " << value << endl;
• }
• };
int main() {
• Count count1;
• // Call the "void operator ++ ()" function
• ++count1;
• count1.display();
• return 0;
• }
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.
EXAMPLE

• #include <iostream>
• using namespace std;

• class Base {
• public:
• void print() {
• cout << "Base Function" << endl;
• }
• };

• class Derived : public Base {


• public:
• void print() {
• cout << "Derived Function" << endl;
• }
• };

• int main() {
• Derived d1;
• d1.print();
• return 0;
• }
OUTPUT

Derived Function
B. VIRTUAL FUNCTION

• A virtual function (also known as virtual


methods) is a member function that is declared
within a base class and is re-defined
(overridden) 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 execute the
derived class’s version of the method.
• Virtual functions ensure that the correct
function is called for an object, regardless of
the type of reference (or pointer) used for the
function call.
• They are mainly used to achieve Runtime
polymorphism.
• Functions are declared with a virtual keyword
in a base class.
• The resolving of a function call is done at
runtime.
RULES FOR VIRTUAL FUNCTION

• The rules for the virtual functions in C++ are as


follows:
• Virtual functions cannot be static.
• A virtual function can be a friend function of
another class.
• Virtual functions should be accessed using a
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 the derived class.
• They are always defined in the base class and
overridden in a derived class. It is not
mandatory for the derived class to override (or
re-define the virtual function), in that case, the
base class version of the function is used.
• A class may have a virtual destructor but it
cannot have a virtual constructor.
• #include <iostream.h>
• class Base
• {
• public:
• virtual void print() {
• cout << "Base Function" << endl;
• }
• };
• class Derived : public Base
•{
• public:
• void print() {
• cout << "Derived Function" << endl;
• }
• };
• int main() {
• Derived derived1;;
• // pointer of Base type that points to derived1
• Base* base1 = &derived1;
• // calls member function of Derived class
• base1->print();
• return 0;
• }

You might also like