[go: up one dir, main page]

0% found this document useful (0 votes)
16 views24 pages

Polymorphism

Uploaded by

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

Polymorphism

Uploaded by

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

Polymorphism

Polymorphism
Polymorphism means implementing one
thing in many forms(“Poly” means many
and “morphs” means forms)
In C++ we have two types:
1)Compile time polymorphism(or early
binding or static binding), Examples:
Function overloading and Operator
overloading.
2) Run time polymorphism( or late binding
or Dynamic binding), achieved with the help
of virtual functions/pure virtual
functions/abstract classes
Compile Time Polymorphism
(or Early binding or Static
binding)
The concept of binding refers to the linking of
function call to the code of the function to be
executed in response to the function call.
•In compile time polymorphism ,static binding is
performed.
•In this compiler makes the decision regarding
selection of appropriate function to be called in
response to function call at compile time.
•During early binding, compiler considers the type of
pointer only, and a particular function call is decided
there only, no concern is there for what type of
object’s address is assigned to base pointer
•This is because all the address information requires
to call a function is known at compile time.
• It is also known as early binding as
decision of binding is made by the
compiler at the earliest possible moment.
• Since all the information needed to call a
function is available at the compile time,
so early binding results in faster
execution of a program
• The disadvantage is lack of flexibility.
• The compile time polymorphism is
implemented using function overloading
and operator overloading
Run time Polymorphism
(Late binding or Dynamic
binding)
• In this dynamic binding is performed
• In dynamic binding the decision regarding the
selection of appropriate function to be called is made
by compiler at run time.
• In this type of polymorphism, compiler determines
the type of object at runtime(or address of what type
of object is being assigned to base class pointer),
and then binds the function call.
• This is because the information pertaining to the
selection of appropriate function definition
corresponding to a function call is known only at the
run time.
• It is also called the late binding as the compiler
delays the binding decision until run time.
• Run time polymorphism offers flexibility as
compared to compile time polymorphism
as all decisions are taken at run time
• A function call is not resolved until runtime,
hence this kind of polymorphism results in
somewhat slower execution of code
• Run time polymorphism can be achieved
through virtual functions
Compile time Polymorphism(or Early
binding)Example-1
Function overloading
#include<iostream> int main()
#include<conio.h> {
using namespace std; overloading obj1;
class overloading int square,rectangle;
{ float circle;
public: square=obj1.area(5);
int area(int side) cout<<"\n Area of square
{ is:"<<square;
return (side*side); rectangle=obj1.area(3,4);
} cout<<"\n Area of rectangle
int area(int length, int breadth) is:"<<rectangle;
{ circle=obj1.area(3.4f);
return (length*breadth); cout<<"\n Area of circle
} is:"<<circle;
float area(float radius) return 0;
{ }
return (3.14*radius*radius);
}
};
Compile time Polymorphism(or Early
binding)Example-2
Operator overloading
#include<iostream> void show_data()
using namespace std; {
class complex1 cout<<x<<" +i"<<y<<"\n";
{ }
float x,y; };
public: int main()
complex1() {
{ complex1
x=0.0; o1(2.7,3.6),o2(4.1,5.7),o3;
y=0.0; o3=o1+o2;
} //o3=o1.operator+(o2);
complex1(float real,float imag) o3.show_data();
{ }
x=real;
y=imag;
}
complex1 operator+(complex1
obj1)
{
complex1 temp;
temp.x=x+obj1.x;
temp.y=y+obj1.y;
return temp;
Pointer to Base and Pointer to derived
• We can have a pointer to base(Base class pointer) and pointer to derived
(Derived class pointer)
• Base pointer can point towards base class as well as derived class(i.e. We can
assign the address of base class object / or we can assign the address of derived
class object to base class pointer)
• Although base class pointer can point towards derived class, but it can access
only those features of derived class, which are common in both classes(i.e.
Inherited features from base), hence base class pointer cannot access the
specific features of derived class directly.
• If we want to access the specific features of derived class with the help of base
class pointer, then we need to typecast it with the derived class pointer
• Derived class pointer can point towards derived class only(i.e. We can assign
address of only derived class object to derived class pointer), if we try to assign
address of base class object to derived class pointer, then error will arise(So, we
can say derived class pointer cannot point towards base class)
Program example-Pointer to Base and Pointer to derived
#include<iostream> class DC:public BC
using namespace std; {
class BC public:
{ void printDC()
public: {
void printBC() cout<<"\nPrinting
{ message in derived
cout<<"\nPrinting class"<<endl;
}
message in base
void show()
class"<<endl;
{
} cout<<"\nshow() of
void show() derived class"<<endl;
{ }
cout<<"\nshow() of };
base class"<<endl;
} //Continued to next
}; slide………….
Program example-Pointer to Base and Pointer to derived….Continued

int main() //accessing data using a pointer of type


{ derived class DC//
BC *bptr; DC *dptr; //derived type pointer
BC base; dptr=&derived;//derived pointer can
bptr=&base;//Base pointer can point towards its own object only
point towards base class cout<<"dptr is derived type pointer\
cout<<"bptr points to base n";
objects\n"; dptr->show();//derived pointer can
bptr->show(); access its own members
//derived class
dptr->printDC();//derived pointer can
DC derived;
bptr=&derived;//Base pointer can access its own members
point towards derived class cout<<"using ((DC*)bptr)\n";
cout<<"bptr now points to derived ((DC*)bptr)->show();//Base pointer
objects\n"; can access members of derived through
//bptr->printDC();//Base pointer type casting
cannot access specific members of ((DC*)bptr)->printDC();//Base pointer
derived directly (error) can access members of derived through
bptr->show(); //Base pointer can type casting
access the common members in base //dptr=&base;//Derived pointer
and derived, show() of cannot point towards base class(error)
base is called due to early binding
return 0;
}
Compile time Polymorphism(or Early
binding)Example-3
#include<iostream> int main()
using namespace std; {
class BC BC *bptr;
{ BC base;
public: bptr=&base;
void show() cout<<"\n-----Early Binding------";
{ cout<<"bptr points to base objects\
cout<<"\nshow() of base n";
class"<<endl; bptr->show();//Base class show is
} called
}; //derived class
class DC:public BC DC derived;
{ bptr=&derived;
public: cout<<"bptr now points to derived
void show() objects\n";
{ bptr->show(); //Base class show is
cout<<"\nshow() of derived class"<<endl; called
} return 0;
}; }
Explanation of Early binding program in previous slide
• In main() function first show() is called for base
class, as base pointer is pointing towards base
class(address of base class object is assigned to
it),but when base pointer is pointing towards
derived class object, even then also, show() of base
class is called.
• This is due to early binding, compiler is just looking
at the type of the pointer(i.e. base), it is not looking
at, address of what type of object is assigned to the
base pointer, hence no matter what type of object’s
address is passed, it is always calling the base
class’s show(), because it has done early binding
after looking at the type of the pointer(i.e. base)
• So, this is a sort of problem, which can be resolved
using late binding(or runtime polymorphism)[Will
Virtual function
• A virtual function is a member function which is
declared within a base class and is re-
defined(Overriden) by a derived class.
• They are mainly used to achieve Runtime
polymorphism
• Functions are declared with a virtual keyword in
base class(i.e. no need to use virtual keyword
again in the derived class, when they are redefined
or overridden)
• Virtual functions ensure that the correct function is
called for an object, regardless of the type of
reference (or pointer) used for function call.
Virtual functions continued….
• To implement run time polymorphism using virtual function
,it must be invoked through the base class pointer that can
contain the address of objects of different derived classes.
• When the virtual function is invoked through the
base class pointer the compiler chooses the
appropriate member function of the derived class at
run time depending upon the contents of base class
pointer ( or What type of object’s address is
assigned to base class pointer) and not the type of
pointer.[This is known as late binding/or run time
polymorphism)
• Thus by making the base class pointer pointing to objects
of different classes ,the different versions of virtual
functions can be called at run time
Runtime polymorphism(or Late binding)-Program Example
( or Program example of Virtual function)
#include<iostream> int main()
using namespace std; {
class BC BC *bptr;
{ BC base;
public: bptr=&base;
cout<<"\n-----Runtime
virtual void show() polymorphism-----";
{ cout<<"bptr points to base
cout<<"\nshow() of objects\n";
base class"<<endl; bptr->show();//Base class show is
} called
//derived class
}; DC derived;
class DC:public BC bptr=&derived;
{ cout<<"bptr now points to
public: derived objects\n";
void show() bptr->show(); //Derived class
show is called
{ return 0;
cout<<"\nshow() of derived }
class"<<endl;
}
Rules of Virtual function

• Virtual functions cannot be static in nature


• Virtual functions should be accessed using pointer or reference of base
class type to achieve run time polymorphism.
• The prototype of virtual functions should be same in base as well as
derived class.
• They are always defined in base class and overridden in derived class. It is
not mandatory for derived class to override (or re-define the virtual
function), in that case base class version of function is used.
• It is possible to have a virtual destructor, but virtual constructor is not
allowed.
 Differentiate Early and Late binding(Or Differentiate
Compile time polymorphism and Run time polymorphism)
or(Static binding vs Dynamic binding)
 Program example to show the difference also

ANSWER IN NEXT 2 SLIDES(SUMMARY OF ALL SLIDES COVERED SO FAR…)


Early binding vs Late binding
Compile time polymorphism(Early binding) Run time polymorphism(Late binding)
Also known as static binding Also known as dynamic binding
In this, compiler makes the decision In dynamic binding the decision regarding
regarding selection of appropriate function the selection of appropriate function to be
to be called in response to function call at called is made by compiler at run time.
compile time.

During early binding, compiler considers In this type of polymorphism, compiler


the type of pointer, and a particular determines the type of object at
function call is decided there only(at runtime(or address of what type of object
compile time), no concern is there for what is being assigned to base class pointer),
type of object’s address is assigned to and then binds the function call.
base pointer

All the address information requires to call Information pertaining to the selection of
a function is known at compile time. appropriate function definition
corresponding to a function call is known
only at the run time.

Lack of flexibility, as all decisions are It offers flexibility as compared to compile


taken at compile time time polymorphism as all decisions are
taken at run time
Early binding vs Late binding(Program example)
class derived : public base {
//Program to show Early and Latepublic:
binding void print()
{
#include <iostream> cout << "print derived class" << endl;
using namespace std; }
class base { void show()
public: {
cout << "show derived class" <<
virtual void print()
endl;
{ }
cout << "print base class"
};
<< endl; int main()
} {
base* bptr;
void show() derived d;
{ bptr = &d;
cout << "show base class" // virtual function, binded at runtime
<< endl; (Late binding)
bptr->print();
}
// Non-virtual function, binded at compile
}; time(Early binding)
bptr->show();
}
Pure virtual function
• A pure virtual function (or abstract function) in C++ is a virtual
function for which we don’t have implementation( or definition
or body) in the base class, we only declare it. A pure virtual
function is declared by assigning 0 in declaration.
• A pure virtual function is always defined in its derived class
• As, it has no definition(or body) in the base class, sometimes it
is also known as: do-nothing(or dummy) function.
• Following syntax is used:
virtual <return_type><function_name>()=0;
Specific example: virtual void show()=0; or we can also write:
void virtual show()=0
It means virtual keyword can be used before/ or after return type
• A function is declared as pure virtual function, when we don’t
know its implementation in the base class, hence it can be
defined with variety of implementations in various derived
classes(Main application of pure virtual function)
Abstract class
• A class containing at least one pure virtual function is
known as abstract class( at least one/ or more than one)
• We cannot create object of abstract class
• We can create pointer /or reference to abstract class
• Abstract class may contain normal member
functions(even constructors also) apart from pure virtual
functions.
• Abstract class is required to be inherited by some derived
class, so that pure virtual function can be defined or
implemented in that derived class
• A derived class which is inheriting abstract class, is
required to implement(or define) all pure virtual functions
of abstract class, otherwise that derived class will also
become abstract in nature.
• A derived class which is inheriting abstract class is also
Program example for Pure Virtual
function/
or Program example for abstract class
class derived1:public
#include<iostream> sample
using namespace std; {
class sample public:
{ void example()
public: {
virtual void cout<<"C++";
example()=0; }
void show() };
{ int main()
cout<<"\nThis is sample {
abstract class"; sample *ptr;
} derived1 obj1;
}; ptr=&obj1;
ptr->example();
ptr->show();
Virtual function vs Pure virtual functions
Virtual function Pure virtual function
A virtual function is a member function of base class A pure virtual function is a member function of base
which can be redefined by derived class class whose only declaration is provided in base class
and must defined in derived class.
Classes having virtual functions are not abstract. Base class containing pure virtual function becomes
abstract.
Syntax : Syntax :
virtual<func_type><func_name>() virtual<func_type><func_name>()=0;
{
// code
}

Definition is given in base class. No definition is given in base class.


Base class having virtual function can be instantiated Base class having pure virtual function becomes
i.e. its object can be made. abstract i.e. it cannot be instantiated.

If derived class do not redefine virtual function of If derived class do not redefine virtual function of
base class, then it does not affect compilation. base class, then compilation error occurs.

All derived class may or may not redefine virtual All derived class must redefine pure virtual function
function of base class. of base class[ if they are not redefining, then derived
class will also become abstract in nature]

You might also like