SlideShare a Scribd company logo
1 of 20
1Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Haresh Jaiswal
Rising Technologies, Jalna.
2Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Introduction
 Virtual functions belongs to the branch of Runtime Polymorphism
in C++
Polymorphism
Runtime/Late BindingCompile Time/Early Binding
Function
Overloading
Operator
Overloading
Virtual Functions /
Function Overriding
3Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Introduction
 Polymorphism is classified into 2 branches
 Compile Time Polymorphism/Early Binding/Static Binding
 Runtime Polymorphism/Late Binding/Dynamic Binding
4Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
What is Binding?
 For every function call; compiler binds or links the call to one
function definition.
 This linking can happen at 2 different time
 At the time of compiling program, or
 At Runtime
5Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Compile Time Polymorphism
 Function Overloading is an example of Compile Time
Polymorphism.
 This decision of binding among several functions is taken by
considering formal arguments of the function, their data type and
their sequence.
6Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Example of compile time polymorphism
void MyFunction(int i)
{
cout << “an int is passed”;
}
void MyFunction(char c)
{
cout << “a char is passed”;
}
int main()
{
MyFunction(10);
MyFunction(‘x’);
}
an int is passed
a char is passed
Output
7Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Runtime Polymorphism
 In late binding; call to a function is resolved at Runtime, the
compiler determines the type of object at execution time and
then binds the function call to a function definition.
 Late binding is also called as Dynamic Binding or Runtime
Binding.
 Virtual Functions are example of Late Binding in C++
 Runtime polymorphism is achieved using pointers.
8Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Pointers behaviour in Polymorphism
 A base class pointer variable can hold address of derived class
object, but it can access only members of base class.
9Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Pointers behaviour in Polymorphism
class base
{
public:
void show()
{
cout << “Show from base”;
}
};
class derived : public base
{
public:
void show()
{
cout << “Show from derived”;
}
};
Show from base
Output
int main()
{
base *ptr;
derived ob;
ptr = &ob;
ptr -> show();
}
10Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Pointers behaviour in Polymorphism
 You can see that even the pointer holds address of derived class
object; it has called the base version of show() method.
 The problem is even a base class pointer holds address of derived
type of object, it can access only members of base class, because
the base pointer variable doesn’t have any idea about the
structure of derived class.
 A base class can’t have any idea about what derived class has
added to it.
11Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Using virtual Keyword
class base
{
public:
virtual void show()
{
cout << “Show from base”;
}
};
class derived : public base
{
public:
void show()
{
cout << “Show from derived”;
}
};
Show from derived
Output
int main()
{
base *ptr;
derived ob;
ptr = &ob;
ptr -> show();
}
12Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Using virtual Keyword
 Using virtual keyword with base class version of show function;
late binding takes place and derived version of the function will
be called, because base pointer points an derived type of object.
 We know that in runtime polymorphism the call to a function is
resolved at runtime depending upon the type of object.
13Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
How virtual function works?
 Base class pointer can point to derived class object.
 In this case, using base class pointer if we call some function
which is in both classes, then base class function is invoked.
 But if we want to invoke derived class function using base class
pointer, it can be achieved by defining the function as virtual in
base class, this is how virtual functions support runtime
polymorphism.
14Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Virtual functions.
 A virtual function is a member function that is declared as virtual
within a base class and redefined by a derived class.
 To create virtual function, precede the base version of function’s
declaration with the keyword virtual.
 When a class containing virtual function is inherited, the derived
class can redefine (Override) the virtual function to suit its own
unique needs.
 The method name and type signature should be same for both
base and derived version of function.
15Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Using virtual Keyword
class circle
{
protected:
float radius;
public:
circle(float r)
{
radius = r;
}
virtual float area()
{
float a;
a = 3.14 * radius * radius;
return a;
}
};
class cylinder : public circle
{
private:
float height;
public:
cylinder(float r, float h) : circle(r)
{
height = h;
}
float area() // overriding area method
{
float a;
a = (2 * 3.14 * radius * radius)
+ (2 * 3.14 * radius * height);
return a;
}
};
16Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Using virtual Keyword
Area of Circle : 200.96
Area of Cylinder : 602.88
Output
int main()
{
circle *ptr;
circle CircleOb(8);
cylinder CylOb(8, 4);
ptr = &CircleOb;
cout << endl << "Area of Circle : " << ptr -> area();
ptr = &CylOb;
cout << endl << "Area of Cylinder : " << ptr -> area();
return 0;
}
17Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Why method overriding?
 Method overriding allows a derived class to provide a specific
implementation of a method that is already provided by one of
its base class.
 The implementation in the derived class overrides (replaces) the
implementation in the base class by providing a method that has
same name, same parameters (signature), and same return type
as the method in the parent class has.
 Using a base class pointer variable, we can point to object of any
child class, depending upon the type of object at runtime a
particular method will be called if that method is made virtual in
base class & overridden in derived class.
18Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Method Overloading vs. Overriding?
 Overloading occurs when two or more methods in one class have
the same method name but different parameters (signature),
Overriding means having two methods with the same method
name and parameters (method signature), one of the method is
in the parent class and the other is in child class.
 Call to an Overloaded method is resolved at compile time, while
call to an Overridden method is resolved at runtime depending
upon the type of object.
19Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Method Overloading vs. Overriding?
Overloading Overriding
Definition Methods having same name but
each must have different number
of parameters or parameters
having different types & order.
Sub class have method with same
name and exactly the same number
and type of parameters and same
return type as super class method.
Meaning More than one method shares the
same name in the class but having
different signature.
Method of base class is re-defined
in the derived class having same
signature.
Behaviour To Add/Extend more to method’s
behaviour.
To Change existing behaviour of
method.
Polymorphism Compile Time Run Time
Inheritance Not Required Always Required
Method Signature Must have different signature Must have same signature.
20Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in
Method Overloading vs. Overriding?
Overloading Overriding
Method Relationship Relationship is between methods
of same class.
Relationship is between methods
of super class and sub class.
No of Classes Does not require more than one
class for overloading.
Requires at least 2 classes for
overriding.
Example

More Related Content

What's hot

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract classAmit Trivedi
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++Sujan Mia
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingAmit Soni (CTFL)
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAsivasundari6
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Kamlesh Makvana
 
Functions in python
Functions in pythonFunctions in python
Functions in pythoncolorsof
 

What's hot (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 

Viewers also liked

Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming languageVasavi College of Engg
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismrattaj
 
Object Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & EncapsulationObject Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & EncapsulationDudy Ali
 
Java multi threading
Java multi threadingJava multi threading
Java multi threadingRaja Sekhar
 
Computer networks unit iii
Computer networks    unit iiiComputer networks    unit iii
Computer networks unit iiiJAIGANESH SEKAR
 
Networking interview questions and answers
Networking interview questions and answersNetworking interview questions and answers
Networking interview questions and answersAmit Tiwari
 
1. Introduction to Association Rule 2. Frequent Item Set Mining 3. Market Bas...
1. Introduction to Association Rule2. Frequent Item Set Mining3. Market Bas...1. Introduction to Association Rule2. Frequent Item Set Mining3. Market Bas...
1. Introduction to Association Rule 2. Frequent Item Set Mining 3. Market Bas...Surabhi Gosavi
 
Unit 3 computer system presention
Unit 3 computer system presentionUnit 3 computer system presention
Unit 3 computer system presentionMat_J
 
Sliding window
 Sliding window Sliding window
Sliding windowradhaswam
 
Presentation on generation of languages
Presentation on generation of languagesPresentation on generation of languages
Presentation on generation of languagesRicha Pant
 
Types of Networks,Network Design Issues,Design Tools
Types of Networks,Network Design Issues,Design ToolsTypes of Networks,Network Design Issues,Design Tools
Types of Networks,Network Design Issues,Design ToolsSurabhi Gosavi
 
Unit 1 introduction to computer networks
Unit 1  introduction to computer networksUnit 1  introduction to computer networks
Unit 1 introduction to computer networkspavan kumar Thatikonda
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming languageVasavi College of Engg
 

Viewers also liked (20)

Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
Object Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & EncapsulationObject Oriented Programming - Abstraction & Encapsulation
Object Oriented Programming - Abstraction & Encapsulation
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Computer networks unit iii
Computer networks    unit iiiComputer networks    unit iii
Computer networks unit iii
 
Networking interview questions and answers
Networking interview questions and answersNetworking interview questions and answers
Networking interview questions and answers
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
1. Introduction to Association Rule 2. Frequent Item Set Mining 3. Market Bas...
1. Introduction to Association Rule2. Frequent Item Set Mining3. Market Bas...1. Introduction to Association Rule2. Frequent Item Set Mining3. Market Bas...
1. Introduction to Association Rule 2. Frequent Item Set Mining 3. Market Bas...
 
Unit 3 computer system presention
Unit 3 computer system presentionUnit 3 computer system presention
Unit 3 computer system presention
 
Sliding window
 Sliding window Sliding window
Sliding window
 
Presentation on generation of languages
Presentation on generation of languagesPresentation on generation of languages
Presentation on generation of languages
 
Types of Networks,Network Design Issues,Design Tools
Types of Networks,Network Design Issues,Design ToolsTypes of Networks,Network Design Issues,Design Tools
Types of Networks,Network Design Issues,Design Tools
 
Protocol & Type of Networks
Protocol & Type of NetworksProtocol & Type of Networks
Protocol & Type of Networks
 
Unit 1 introduction to computer networks
Unit 1  introduction to computer networksUnit 1  introduction to computer networks
Unit 1 introduction to computer networks
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming language
 
Unit 5
Unit 5Unit 5
Unit 5
 

Similar to 07. Virtual Functions

Virtual function
Virtual functionVirtual function
Virtual functionzindadili
 
polymorphism.pdf
polymorphism.pdfpolymorphism.pdf
polymorphism.pdfriyawagh2
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to classHaresh Jaiswal
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & TemplatesMeghaj Mallick
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummaryAmal Khailtash
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++Mohamed Essam
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variablesSaurav Kumar
 
What's new in c# 5.0 net ponto
What's new in c# 5.0   net pontoWhat's new in c# 5.0   net ponto
What's new in c# 5.0 net pontoPaulo Morgado
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptSeok-joon Yun
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloadingHaresh Jaiswal
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdfvenud11
 
JAVA(module1).pptx
JAVA(module1).pptxJAVA(module1).pptx
JAVA(module1).pptxSRKCREATIONS
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docxssuser1c8ca21
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphismkiran Patel
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...Guillaume Laforge
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
Learn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator OverloadingLearn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator OverloadingEng Teong Cheah
 

Similar to 07. Virtual Functions (20)

Virtual function
Virtual functionVirtual function
Virtual function
 
polymorphism.pdf
polymorphism.pdfpolymorphism.pdf
polymorphism.pdf
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to class
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & Templates
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features Summary
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 
What's new in c# 5.0 net ponto
What's new in c# 5.0   net pontoWhat's new in c# 5.0   net ponto
What's new in c# 5.0 net ponto
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
 
JAVA(module1).pptx
JAVA(module1).pptxJAVA(module1).pptx
JAVA(module1).pptx
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphism
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
 
Function oveloading
Function oveloadingFunction oveloading
Function oveloading
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Learn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator OverloadingLearn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator Overloading
 

Recently uploaded

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

07. Virtual Functions

  • 1. 1Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Haresh Jaiswal Rising Technologies, Jalna.
  • 2. 2Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Introduction  Virtual functions belongs to the branch of Runtime Polymorphism in C++ Polymorphism Runtime/Late BindingCompile Time/Early Binding Function Overloading Operator Overloading Virtual Functions / Function Overriding
  • 3. 3Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Introduction  Polymorphism is classified into 2 branches  Compile Time Polymorphism/Early Binding/Static Binding  Runtime Polymorphism/Late Binding/Dynamic Binding
  • 4. 4Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in What is Binding?  For every function call; compiler binds or links the call to one function definition.  This linking can happen at 2 different time  At the time of compiling program, or  At Runtime
  • 5. 5Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Compile Time Polymorphism  Function Overloading is an example of Compile Time Polymorphism.  This decision of binding among several functions is taken by considering formal arguments of the function, their data type and their sequence.
  • 6. 6Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Example of compile time polymorphism void MyFunction(int i) { cout << “an int is passed”; } void MyFunction(char c) { cout << “a char is passed”; } int main() { MyFunction(10); MyFunction(‘x’); } an int is passed a char is passed Output
  • 7. 7Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Runtime Polymorphism  In late binding; call to a function is resolved at Runtime, the compiler determines the type of object at execution time and then binds the function call to a function definition.  Late binding is also called as Dynamic Binding or Runtime Binding.  Virtual Functions are example of Late Binding in C++  Runtime polymorphism is achieved using pointers.
  • 8. 8Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Pointers behaviour in Polymorphism  A base class pointer variable can hold address of derived class object, but it can access only members of base class.
  • 9. 9Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Pointers behaviour in Polymorphism class base { public: void show() { cout << “Show from base”; } }; class derived : public base { public: void show() { cout << “Show from derived”; } }; Show from base Output int main() { base *ptr; derived ob; ptr = &ob; ptr -> show(); }
  • 10. 10Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Pointers behaviour in Polymorphism  You can see that even the pointer holds address of derived class object; it has called the base version of show() method.  The problem is even a base class pointer holds address of derived type of object, it can access only members of base class, because the base pointer variable doesn’t have any idea about the structure of derived class.  A base class can’t have any idea about what derived class has added to it.
  • 11. 11Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Using virtual Keyword class base { public: virtual void show() { cout << “Show from base”; } }; class derived : public base { public: void show() { cout << “Show from derived”; } }; Show from derived Output int main() { base *ptr; derived ob; ptr = &ob; ptr -> show(); }
  • 12. 12Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Using virtual Keyword  Using virtual keyword with base class version of show function; late binding takes place and derived version of the function will be called, because base pointer points an derived type of object.  We know that in runtime polymorphism the call to a function is resolved at runtime depending upon the type of object.
  • 13. 13Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in How virtual function works?  Base class pointer can point to derived class object.  In this case, using base class pointer if we call some function which is in both classes, then base class function is invoked.  But if we want to invoke derived class function using base class pointer, it can be achieved by defining the function as virtual in base class, this is how virtual functions support runtime polymorphism.
  • 14. 14Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Virtual functions.  A virtual function is a member function that is declared as virtual within a base class and redefined by a derived class.  To create virtual function, precede the base version of function’s declaration with the keyword virtual.  When a class containing virtual function is inherited, the derived class can redefine (Override) the virtual function to suit its own unique needs.  The method name and type signature should be same for both base and derived version of function.
  • 15. 15Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Using virtual Keyword class circle { protected: float radius; public: circle(float r) { radius = r; } virtual float area() { float a; a = 3.14 * radius * radius; return a; } }; class cylinder : public circle { private: float height; public: cylinder(float r, float h) : circle(r) { height = h; } float area() // overriding area method { float a; a = (2 * 3.14 * radius * radius) + (2 * 3.14 * radius * height); return a; } };
  • 16. 16Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Using virtual Keyword Area of Circle : 200.96 Area of Cylinder : 602.88 Output int main() { circle *ptr; circle CircleOb(8); cylinder CylOb(8, 4); ptr = &CircleOb; cout << endl << "Area of Circle : " << ptr -> area(); ptr = &CylOb; cout << endl << "Area of Cylinder : " << ptr -> area(); return 0; }
  • 17. 17Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Why method overriding?  Method overriding allows a derived class to provide a specific implementation of a method that is already provided by one of its base class.  The implementation in the derived class overrides (replaces) the implementation in the base class by providing a method that has same name, same parameters (signature), and same return type as the method in the parent class has.  Using a base class pointer variable, we can point to object of any child class, depending upon the type of object at runtime a particular method will be called if that method is made virtual in base class & overridden in derived class.
  • 18. 18Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Method Overloading vs. Overriding?  Overloading occurs when two or more methods in one class have the same method name but different parameters (signature), Overriding means having two methods with the same method name and parameters (method signature), one of the method is in the parent class and the other is in child class.  Call to an Overloaded method is resolved at compile time, while call to an Overridden method is resolved at runtime depending upon the type of object.
  • 19. 19Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Method Overloading vs. Overriding? Overloading Overriding Definition Methods having same name but each must have different number of parameters or parameters having different types & order. Sub class have method with same name and exactly the same number and type of parameters and same return type as super class method. Meaning More than one method shares the same name in the class but having different signature. Method of base class is re-defined in the derived class having same signature. Behaviour To Add/Extend more to method’s behaviour. To Change existing behaviour of method. Polymorphism Compile Time Run Time Inheritance Not Required Always Required Method Signature Must have different signature Must have same signature.
  • 20. 20Rising Technologies, Jalna (MH). + 91 9423156065, www.RisingTechnologies.in Method Overloading vs. Overriding? Overloading Overriding Method Relationship Relationship is between methods of same class. Relationship is between methods of super class and sub class. No of Classes Does not require more than one class for overloading. Requires at least 2 classes for overriding. Example