0% found this document useful (0 votes)
31 views14 pages

C++ Polymorphism for Students

Uploaded by

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

C++ Polymorphism for Students

Uploaded by

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

CO_IF_22316_UO4

Jaishree Anerao, Lecturer, VES Polytechnic

Date: 14 July 2020

MSBTE LEAD: Learning at your Doorstep


Unit 04 :
Pointers and Polymorphism
in c++

Written by

Jaishree Sudhakar Anerao


Lecturer, Department of Computer Engineering[NBA Accredited],Vivekanand Education Society’s
Polytechnic, Mumbai
Unit Outcome 2 : Implement run
time polymorphism using virtual
functions in the given C++
programs.

Written by

Jaishree Sudhakar Anerao


Lecturer, Department of Computer Engineering[NBA Accredited],Vivekanand Education Society’s
Polytechnic, Mumbai
Learning Outcome 4 : Student will be
able to implement virtual function,
pure virtual function

Written by

Jaishree Sudhakar Anerao


Lecturer, Department of Computer Engineering[NBA Accredited],Vivekanand Education Society’s
Polytechnic, Mumbai
What we will learn today

1 Runtime Polymorphism Key takeaways

2. Virtual functions Concept of Virtual function

3. Rules of virtual functions

4. Pure virtual function

Jaishree Sudhakar Anerao


Lecturer, Department of Computer Engineering[NBA Accredited],Vivekanand Education Society’s
Polytechnic, Mumbai

Page 5 Maharashtra State Board of Technical Education 4 July 2020


Concept Map

Superclass obj= new SubClass

SuperClass

Extends

Subclass

Page 6 Maharashtra State Board of Technical Education 4 July 2020


Learning Objective/ Key learning

► What is runtime polymorphism?


► To use virtual function in program.
► What are rules for virtual function.

Page 7 Maharashtra State Board of Technical Education 4 July 2020


Concept Explanation: RUNTIME POLYMORPHISM

► To achieve Run time polymorphism, C++ supports a mechanism known as virtual function.

Page 8 Maharashtra State Board of Technical Education 4 July 2020


Concept Explanation: RUNTIME POLYMORPHISM

► What is Runtime Polymorphism?


► The address of function to be called is determined at the runtime rather than compile time.
► The compiler adds code that identifies the kind of object at runtime then gives the call with the right
function definition
► It is also known as late binding or dynamic binding and overriding as well.
► Late binding can be implemented by using virtual function.
► It provides slow execution as compare to early binding.

Page 9 Maharashtra State Board of Technical Education 4 July 2020


Concept : Virtual Function

► Virtual function belongs to runtime polymorphism in C++.


► A virtual function is a member function in the base class that you expect to redefine in derived class.
► Virtual functions are declared with a keyword “virtual” in the base class.
► When a virtual function is declared compiler decides to execute a function based on the type of
object pointed by the base pointer and not on the type of pointer.
► Example :

virtual void draw();

Page 10 Maharashtra State Board of Technical Education 4 July 2020


Concept virtual function

► Rules of virtual function :


► The virtual function must be members of some class.
► They cannot be static members.
► They are accessed by using object pointers.
► A virtual function can be friend of another class.
► A virtual function in a base class must be defined, even though it may not be used.

Page 11 Maharashtra State Board of Technical Education 4 July 2020


Example of Virtual Function :

Program Output

Program for virtual function

Page 12 Maharashtra State Board of Technical Education 4 July 2020


Concept : Pure virtual function

► A pure virtual function is a function declared in a base class that has no definition.
► The compiler requires each derived class to either define the function or redeclare it as a pure virtual
function.
► Such functions are also called as ’do-nothing’ functions.
► Ex:-
class ABC
{
public:
virtual void display( )=0; 13
};

Page 13 Maharashtra State Board of Technical Education 4 July 2020


Pure virtual Function example:

Program for Pure virtual


function

Program Output

Page 14 Maharashtra State Board of Technical Education 4 July 2020

You might also like