SlideShare ist ein Scribd-Unternehmen logo
1 von 10
VIRTUAL FUNCTION
PRESENTED BY
Ms. V.VENNILA., MCA.,M.Phil.,
Asst.Prof of Information Technology,
Bon Secours College for Women,
Thanjavur
C++ VIRTUAL FUNCTION
Virtual function is a function in base class which
is overrided in the derived class , and which
tells the compiler to perform late binding on this
function.
Virtual keyword is used to make a member
function of the base class virtual,
VIRTUAL FUNCTION
A virtual function is a member function that is
declared as virtual within a base class and
redefined by a deriver class.
To create virtual function, precede the base
version of function’s declare with the keyword
virtual.
The method name and type signature should
be same for both base and derived version of
function.
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 function base class pointer, it can be
achieved by defining the function as virtual in base
class, this is how virtual function supports runtime
polymorphism.
HOW VIRTUAL FUNCTION WORKS?
Using virtual keyword with base class version of
show function ; late binding takes place and
derived of the function will be called , because
base pointer pointes 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.
USING VIRTUAL KEYWORD
Virtual return _ type function _ name ( )
{
……
……
……
}
Ex: virtual void print( )
SYNTAX
Class class _ name
{
Public:
virtual return _ type function _ name (arguments)
{
….
…..
}
};
Class A
{
GENDRAL FORMAT
Virtual function belongs to the branch of
runtime polymorphism in c++
polymorphism
Compile time/early binding Run time/late binding
Function
overloading
Operator
overloading
VIRTUAL
function/function
overloading
#include<iostream.h>
Class base
{
Public:
Virtual void print ( )
{
Cout<<“print base class”;
}
Void show ( )
{
Cout<<“show base class”;
} };
Class derived : public base
{
Public:
Void print ( )
{
Cout<<“print derived class”;
}
Void show ( )
EXAMPLE
{
cout<<“show derived class”;
}};
int main ( )
{
base*bptr;
bptr =d;
derived d;
bptr print( );
bptr show( );
}
OUTPUT
Print derived class
Show base class

Weitere ähnliche Inhalte

Was ist angesagt?

class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Kumar
 
Comparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphismComparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphism
CHAITALIUKE1
 

Was ist angesagt? (20)

Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
class and objects
class and objectsclass and objects
class and objects
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Friend function
Friend functionFriend function
Friend function
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Comparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphismComparison between runtime polymorphism and compile time polymorphism
Comparison between runtime polymorphism and compile time polymorphism
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 

Ähnlich wie virtual function

Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
nirajmandaliya
 

Ähnlich wie virtual function (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
virtual
virtualvirtual
virtual
 
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfExample for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdf
 
Oop Extract
Oop ExtractOop Extract
Oop Extract
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Virtual function and abstract class
Virtual function and abstract classVirtual function and abstract class
Virtual function and abstract class
 
Virtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOPVirtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOP
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & Templates
 
Lecture6.ppt
Lecture6.pptLecture6.ppt
Lecture6.ppt
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
Dependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony ContainerDependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony Container
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
C questions
C questionsC questions
C questions
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
polymorphism.pdf
polymorphism.pdfpolymorphism.pdf
polymorphism.pdf
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Mehr von VENNILAV6

Mehr von VENNILAV6 (20)

WORKING WITH GRAPHICS
WORKING WITH GRAPHICSWORKING WITH GRAPHICS
WORKING WITH GRAPHICS
 
CREATING A MASTER PAGE
CREATING A MASTER PAGECREATING A MASTER PAGE
CREATING A MASTER PAGE
 
CREATING A NEW PUBLICATION IN PAGEMAKER
CREATING A NEW PUBLICATION IN PAGEMAKERCREATING A NEW PUBLICATION IN PAGEMAKER
CREATING A NEW PUBLICATION IN PAGEMAKER
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Joins & constraints
Joins & constraintsJoins & constraints
Joins & constraints
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
 
Database architecture
Database architectureDatabase architecture
Database architecture
 
Data definition language
Data definition languageData definition language
Data definition language
 
INTERCONNECTION STRUCTURE
INTERCONNECTION STRUCTUREINTERCONNECTION STRUCTURE
INTERCONNECTION STRUCTURE
 
CACHE MEMORY
CACHE MEMORYCACHE MEMORY
CACHE MEMORY
 
Machine control flow
Machine control flowMachine control flow
Machine control flow
 
COMPUTER FUNCTIONS
COMPUTER FUNCTIONSCOMPUTER FUNCTIONS
COMPUTER FUNCTIONS
 
EXTERNAL DEVICE
EXTERNAL DEVICEEXTERNAL DEVICE
EXTERNAL DEVICE
 
CORELDRAW EXAMPLES
CORELDRAW EXAMPLESCORELDRAW EXAMPLES
CORELDRAW EXAMPLES
 
BASICS OF DATA STRUCTURE
BASICS OF DATA STRUCTUREBASICS OF DATA STRUCTURE
BASICS OF DATA STRUCTURE
 
object oriented system development
object oriented system development object oriented system development
object oriented system development
 
constructor and destructor
constructor and destructorconstructor and destructor
constructor and destructor
 
INHERITANCE
INHERITANCEINHERITANCE
INHERITANCE
 
BASICS OF MOBILE COMPUTING
BASICS OF MOBILE COMPUTINGBASICS OF MOBILE COMPUTING
BASICS OF MOBILE COMPUTING
 
BASICS OF JAVA
BASICS OF JAVABASICS OF JAVA
BASICS OF JAVA
 

Kürzlich hochgeladen

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Kürzlich hochgeladen (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

virtual function

  • 1. VIRTUAL FUNCTION PRESENTED BY Ms. V.VENNILA., MCA.,M.Phil., Asst.Prof of Information Technology, Bon Secours College for Women, Thanjavur
  • 2. C++ VIRTUAL FUNCTION Virtual function is a function in base class which is overrided in the derived class , and which tells the compiler to perform late binding on this function. Virtual keyword is used to make a member function of the base class virtual,
  • 3. VIRTUAL FUNCTION A virtual function is a member function that is declared as virtual within a base class and redefined by a deriver class. To create virtual function, precede the base version of function’s declare with the keyword virtual. The method name and type signature should be same for both base and derived version of function.
  • 4. 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 function base class pointer, it can be achieved by defining the function as virtual in base class, this is how virtual function supports runtime polymorphism. HOW VIRTUAL FUNCTION WORKS?
  • 5. Using virtual keyword with base class version of show function ; late binding takes place and derived of the function will be called , because base pointer pointes 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. USING VIRTUAL KEYWORD
  • 6. Virtual return _ type function _ name ( ) { …… …… …… } Ex: virtual void print( ) SYNTAX
  • 7. Class class _ name { Public: virtual return _ type function _ name (arguments) { …. ….. } }; Class A { GENDRAL FORMAT
  • 8. Virtual function belongs to the branch of runtime polymorphism in c++ polymorphism Compile time/early binding Run time/late binding Function overloading Operator overloading VIRTUAL function/function overloading
  • 9. #include<iostream.h> Class base { Public: Virtual void print ( ) { Cout<<“print base class”; } Void show ( ) { Cout<<“show base class”; } }; Class derived : public base { Public: Void print ( ) { Cout<<“print derived class”; } Void show ( ) EXAMPLE
  • 10. { cout<<“show derived class”; }}; int main ( ) { base*bptr; bptr =d; derived d; bptr print( ); bptr show( ); } OUTPUT Print derived class Show base class