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

Virtual Functions and Polymorphism

• Advanced concepts
• Normal member functions accessed with pointers
• Virtual member functions accessed with pointers
• Virtual functions
– Define
– Why
– Polymorphism
– Conditions to be polymorphic
• Late binding
• Static binding
• Abstract classes and pure virtual functions
Virtual Functions and Polymorphism
• Example: person class hierarchy
• Virtual destructors~
• Virtual base classes
• Friend Functions
• Friend classes
• Static functions
• ‘this’ --🡪 pointer
Normal member functions access with pointers: Predict
output
Virtual Functions accessed with pointers
Virtual Functions and Polymorphism
• Virtual means existing in appearance but not in reality.
• Programmatically
– When virtual functions are used, a program that appears to
be calling a function of one class may in reality be calling a
function of a different class
• Why are virtual functions needed?
– Suppose, you have a number of objects of different classes
but you want to put them all in an array and perform
particular operation on them using the same function call
– Ex: Sahpes-> triangle, square, circle etc.,
– Each of these classes has a member function draw() that
causes the object to be drawn on the screen
Virtual Functions and Polymorphism

Array draw()

C R Cylinder Smily someSha anotherS someSha ………


pe hape pe ……..
Virtual Functions and Polymorphism
• The scenario in the previous slide is amazing capability
• Completely different functions are executed by the
same function call
• This is called as polymorphism, which means different
forms
• The functions have the same appearance, the draw()
expression, but different actual functions are called
depending on the content of array.
• Conditions for polymorphic approach
– The different classes of shapes, rectangle, smily, circle must
be descended from a single base class
– The ‘draw()’ must be declared to be virtual in the base class
Virtual Functions and Polymorphism
• Note:
• Normal member functions accessed with pointers
– The function in the base class is always executed, The
compiler ignores the contents of the pointer and chooses the
member function that matches the type of the pointer
• Virtual member functions accessed with pointers
– The compiler selects the function based on the contents of
the pointer, not the type of the pointer.
Late Binding
• Normal member functions
– ptr->show()
– It always compiles a call to show() function in the base class
• Virtual member functions
– ptr->show()
– In this case the compiler doesn’t know what class the
contents of ptr may contain:
– Address of object of Derv1 or Derv2 class
– Which version of show() to be called?
– It defers until the program is running, at run time it
resolve the version of the show or draw to be called
– This concept is called late binding or dynamic binding
Abstract classes and pure virtual Functions

Shape

Rectangle Triangle Circle someShape

The array of shapes objects consists of only sub class

objects not objects of Shape Hence we can avoid


instantiating base class -🡪 abstract class
Abstract class
• Abstract classes are created by declaring pure virtual
functions in base classes.
• General syntax
class ClassName{
Data members………..
returnType funName([param])=0;

}
Note: additional concept
abstract classes enforces polymorphism
Abstract class
Points to remember: abstract class
• =0, is syntax to tell the compiler the function is pure virtual
• If the class consists of at-least one virtual function, that
class is called abstract class
• The abstract class can’t be instantiated
• The class which inherits abstract class , it has to override
the all the virtual functions of the base class otherwise,
that class also become abstract class.
• The abstract class provides interface for the class hierarchy
• A document makes note of abstract classes in a class
hierarchy, to help some who uses your class hierarchy as a
library
Virtual Functions and Person class hierarchy
• Person:
– Data member: name
– Functions: getName(), putName(), getData(), isOutstanding()

• Student: data member-> gpa


• Professor: data member-> publications

• The student is-a person and the Professor is-a Person


Virtual Functions and Polymorphism
Virtual Functions and Polymorphism
Virtual Functions and Polymorphism
Virtual Destructors
Virtual classes
Virtual classes

Reference to baseData is ambiguous


Virtual Classes
Friend Functions
• The concept of encapsulation and data hiding dictate
that nonmember functions should not be able to
access objects private or protected data
• The policy is, if you are not a member, you can’t get it.
• Requirement:
• I want to write a function to operate on objects of two
different classes
Friend Functions
Friend Functions
Friend Class
this-> pointer
• The object of any class is referred by ------ this
• this ------ a pointer
• The member function can access its object by a pointer
called: this
Member access with ---- this
Virtual Functions and Polymorphism
Virtual Functions and Polymorphism
Virtual Functions and Polymorphism
Virtual Functions and Polymorphism: Banking App
Working of virtual functions(concept of VTABLE and VPTR)

• If a class contains a virtual function then compiler itself


does two things:
– If object of that class is created then a virtual
pointer(VPTR) is inserted as a data member of the class to
point to VTABLE of that class. For each new object created, a
new virtual pointer is inserted as a data member of that
class.
– Irrespective of object is created or not, a static array of
function pointer called VTABLE where each cell contains the
address of each virtual function contained in that class.
Reading Assignment
• UML object diagrams
• A memory efficient string class
• Dynamic type information
• Checking the type of a class with dynamic_cast

You might also like