0% found this document useful (0 votes)
6 views18 pages

CH6_

Chapter 6 discusses polymorphism in programming, distinguishing between compile-time and run-time polymorphism. It explains the necessity of virtual functions for enabling dynamic binding and the use of abstract classes for achieving run-time polymorphism. Additionally, it covers concepts like virtual destructors and run-time type information (RTTI) with operators such as dynamic_cast and typeid.

Uploaded by

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

CH6_

Chapter 6 discusses polymorphism in programming, distinguishing between compile-time and run-time polymorphism. It explains the necessity of virtual functions for enabling dynamic binding and the use of abstract classes for achieving run-time polymorphism. Additionally, it covers concepts like virtual destructors and run-time type information (RTTI) with operators such as dynamic_cast and typeid.

Uploaded by

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

Chapter 6:

Polymorphism and virtual functions


BIBHA STHAPIT
ASST. PROFESSOR
IOE, PULCHOWK CAMPUS
Polymorphism
• Having same name but different functionality
• Two categories:
– 1.Compile time polymorphism
• Early or static binding
• Static polymorphism refers to the binding of functions on the basis
of their signature (number, type and sequence of parameters).
• It is also called early binding because the calls are already bound
to the proper type of functions during the compilation of the
program depending upon type and sequence of parameters.
• E.g. Function overloading, operator overloading
2
Polymorphism
– 2. Run time polymorphism
• Late or dynamic binding
• A function is said to exhibit dynamic polymorphism if it
exists in various forms, and the resolution to different
function calls are made dynamically during execution time.
• This feature makes the program more flexible as a
function can be called, depending on the context.
• E.g. Function overriding using Virtual Functions
3
Pointers to the derived class
• We can create pointer type object of both base class as well as
derived class pointer.
• The pointer object of the derived class will always be type
compatible with base class pointer.
• Base class pointer can invoke member function
base *b1;
that is defined in base class only. base b2;
derived d;
• If member functions are defined in derived class b1=&b2;
only, then these functions cannot be accessed b1->display( );
b1=&d;
from base pointer. b1->display( );

4
class B
{
public:
void show()
{ cout<<"I am in base
show"<<endl; }
// invokes overridden function of base class only
};
// bp[1] ->display(); cannot be invoked
class D:public B
{
public:
void show()
{ cout<<"I am in derived
show"<<endl; } Output:
void display() I am in base show
{ cout<<"I am in derived only"; } I am in base show
}; I am in derived only 5
Need of virtual function
• If we use base class pointer to access the derived class member,
then the function overriding cannot be done.
• That is, if the base class and derived class have same function ,
then only base class member function can be accessed through
that pointer even if we assign the address of derived class to the
base class pointer.
• Here, the function is associated only to the type of pointer but not
the content of the pointer.
• If we want to invoke the function depending on object that is being
pointed by the pointer type of object, we need to use virtual
function. 6
Virtual function
• Virtual function is a non-static member function that is declared
within a base class and redefined by a derived class
• Determines which function to execute during runtime based on
type of object pointed by base pointer rather than type of pointer.
• To create a virtual function, precede the function's declaration in
base class by keyword “virtual”
• For every base class that has one or more virtual functions, a table
of function addresses is created during run time.
• This table of function addresses is called the virtual table that
contains the address of each and every virtual function that has
been defined in the corresponding class.
7
main()
{
B b, *bp[2];
class B
D d;
{
bp[0]=&b;
public:
virtual void show() bp[1] =&d;
{ cout<<"I am in base show"<<endl; }
void display() bp[0] ->display();
{ cout<<"I am in base display"<<endl; } bp[1] ->display();
};
class D:public B bp[0] ->show();
{ bp[1] ->show();
public: }
void show()
{ cout<<"I am in derived show"<<endl; }
void display()
{ cout<<"I am in derived
display"<<endl; }
8
};
main()
{
B b, *bp[3];
“Virtual”ness is inherited D1 d;
class B D2 e;
{ public:
virtual void show() bp[0]=&b;
{ cout<<"I am in base show"<<endl; } bp[1] =&d;
}; bp[2] =&e;
class D1:public B
{ public: bp[0] ->show();
void show() bp[1] ->show();
{ cout<<"I am in derived1 show"<<endl; } bp[2] ->show();
}; }
class D2:public D1
{ public:
void show()
{ cout<<"I am in derived2 show"<<endl; } 9
};
Pure Virtual function & abstract class/abstract base class

• Virtual function without its definition in base class is


known as “Pure Virtual Function”.
• virtual void show() = 0;
• Do-nothing function
• The base class containing pure virtual function cannot be
used to create objects, hence it is called abstract class or
abstract base class.
• Abstract class is used to create base class pointers only
for achieving runtime polymorphism.
10
class dimension class rectangle:public dimension
{ protected: {
int l,b; public:
public: rectangle(int x , int y):dimension(x,y){ }
dimension(int x, int y): l(x),b(y){ } void area()
virtual void area()=0; { cout<<"Area of rectangle is "<<l*b<<endl; }
}; };

class square:public dimension main()


{ {
public: square s(5);
square(int x):dimension(x,x){ } rectangle r(10,2);
void area() dimension *bp[2]={&s, &r};
{ cout<<"Area of square is "<<l*l<<endl; } bp[0] ->area();
}; bp[1] ->area(); 11
}
Virtual Destructor
• Destructors are invoked automatically to free memory
space
• But in derived classes, it is not invoked automatically
because destructors that are non-virtual will not get
message under late binding
• So, the destructors are made virtual to free space under
late binding method.
• But the constructors cannot be virtual because virtual
table would not have been created during object creation
so that it would not have anywhere to look up to. 12
class base main()
{ public: {
virtual void show() base *bp = new base;
{ cout<<"I am in base show"<<endl; } bp ->show();
virtual ~base()
{ cout<<"I am in base destructor"<<endl; } bp=new derived;
}; bp ->show();
delete bp;
class derived : public base }
{ public:
void show() Output:
{ cout<<"I am in derived show"<<endl; } I am in base show
~derived() I am in derived show
{ cout<<"I am in derived destructor"<<endl; } I am in derived destructor
}; I am in base destructor 13
Run Time Type Information(RTTI)
• Provides information of object during runtime
• Available only for polymorphic class (class with virtual
function).
• The “dynamic_cast” and “typeid” operators are used for
these purpose
• Must include <typeinfo>header file for typeid operator

14
dynamic_cast operator
• The dynamic_cast operator is intended to be the most heavily
used RTTI component.
• It doesn't answer the question of what type of object a pointer
points to.
• Instead, it answers the question of whether you can safely assign
the address of the object to a pointer of a particular type.
– dynamic_cast<target_type> (expr)
• Two types of casting:
– Upcasting – casting from derived to base
– Downcasting – casting from base to derived (note: base pointer must hold
the address of derived for successful casting)
15
class base
{
public:
virtual void display(){ };
void show()
{ cout<<"Base Class"<<endl; }
};
class derived : public base
{
public:
void show()
{ cout<<"Derived Class"<<endl; }
};

Output :
Upcasting successful Base Class 16
Downcasting successful Derived Class
typeid operator
• typeid is an operator which allows you to access the type of an
object at runtime
• Can also be implemented for non-polymorphic class
• This is useful for pointers to derived classes

17
main()
{
class Animal Animal *ap=new Cat;
{ Cat c;
public: int roll;
virtual void show() float marks;
{ cout<<"Type of ap="<<typeid(ap).name()<<endl;
cout<<"Animal Class"<<endl; cout<<"Type of ap="<<typeid(*ap).name()<<endl;
} cout<<"Type of c="<<typeid(c).name()<<endl;
}; cout<<"Type of roll="<<typeid(roll).name()<<endl;
class Cat:public Animal cout<<"Type of marks="<<typeid(marks).name()<<endl;
{ }
public:
void show()
{
cout<<"Cat Class"<<endl;
}
18
};

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy