SlideShare a Scribd company logo
2
Most read
3
Most read
9
Most read
CE142 Runtime Polymorphism & Virtual
Function

Virtual Function
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() { cout<<" In Base n"; }
};
class Derived: public Base
{
public:
void show() { cout<<"In Derived n"; }
};
int main(void)
{
Base *bp = new Derived;
bp->show(); // RUN-TIME POLYMORPHISM
return 0;
}
In Derived

 class A{
 void f1();
 virtual void f2();
 virtual void f3();
 virtual void f4();
 };
 class B:public A
 {
 void f2();
 void f4(int x);
 void f1();
 }
Virtual Function
&f2() &f3() &f4()
_vtable
&f2() &f3() &f4()
_vtable
 A *p;
 A x1;
 p=&x1;
 p->f1();
 p->f2();
 p->f3();
 p->f4();
 p->f4(10);
 !!Error
//EB
//EB
//LB
//LB
//LB
p
x1
_vptr

 class A{
 void f1();
 virtual void f2();
 virtual void f3();
 virtual void f4();
 };
 class B:public A
 {
 void f2();
 void f4(int x);
 void f1();
 }
Virtual Function
&f2() &f3() &f4()
_vtable
&f2() &f3() &f4()
_vtable
 A *p;
 B x1;
 p=&x1;
 p->f1();
 p->f2();
 p->f3();
 p->f4();
 p->f4(10);
//EB
//EB
//LB
//LB
//LB
p
x1
_vptr

Can static functions be virtual in C++?
#include<iostream>
using namespace std;
class Test
{
public:
// Error: Virtual member functions cannot be static
virtual static void fun() { }
};

Virtual Function & Default
Arguments
#include <iostream>
using namespace std;
class Base
{
public:
virtual void fun ( int x = 0 )
{
cout << "Base::fun(), x = " << x << endl;
}
};
class Derived : public Base
{
public:
void fun ( int x )
{
cout << "Derived::fun(), x = " << x << endl;
}
};
int main()
{
Derived d1; Base *b = &d1; b->fun();
return 0;
}
"Derived::fun(), x =0

Virtual Function & Default
Arguments
#include <iostream>
using namespace std;
class Base
{
public:
virtual void fun ( int x = 0 )
{
cout << "Base::fun(), x = " << x << endl;
}
};
class Derived : public Base
{
public:
void fun ( int x=10 ) //Note The Changes
{
cout << "Derived::fun(), x = " << x << endl;
}
};
int main()
{
Derived d1; Base *bp = &d1; bp->fun();
return 0;
}
"Derived::fun(), x =0

Can virtual functions be private in C++?
Never.
Why?

Abstract Classes in C++
 No Implementation of function in base
class is referred as Abstract class
Example: class Shape having member
function draw().
It can only be have implementation at
derived classes. i.e. draw() in rectangle,
draw() in square.
Example: class Animal having member
function move().

 A class which does not have an implementations of
the function.
 How to create abstract class in java.
 By putting abstract keyword before class definition
 How to create abstract class in C#
 By putting abstract keyword before class definition
 How to create abstract class in C++
 By putting abstract keyword before class definition
 There is no abstract keyword in C++!!!!!
Abstract class

 How to create abstract class in C++
 A class in C++ is abstract if it has at least one pure
virtual function.
 What is pure virtual function?
 It is a kind of virtual function which has no
implementations!!!
Abstract class in C++

 Error: Undefined reference to vtable for Sha
 How to make any function as pure virtual
function

Abstract class in C++
 How to create abstract class in C++
 A class in C++ is abstract if it has at least one pure
virtual function.
 What is pure virtual function?
 It is a kind of virtual function which has no
implementations!!!
How to make any virtual function as pure
virtual function?
 By putting “=0” expression at function declaration.


Characteristics of Abstract
class in C++

1. Abstract class may also contain non virtual
functions.

2. Instances of the abstract class can not be created.

3. We can have pointers and references of abstract
class type.

4. If we do not override the pure virtual function in
derived class, then derived class also becomes
abstract class.

5. An abstract class can have constructors.

Operator in C++

Example

Example

More Related Content

PPTX
Inheritance in c++
PPTX
Static Data Members and Member Functions
PPTX
PPTX
07. Virtual Functions
PPTX
classes and objects in C++
PPTX
INLINE FUNCTION IN C++
PPTX
Polymorphism In c++
PPTX
Abstract class in c++
Inheritance in c++
Static Data Members and Member Functions
07. Virtual Functions
classes and objects in C++
INLINE FUNCTION IN C++
Polymorphism In c++
Abstract class in c++

What's hot (20)

PPTX
Constructor in java
PPTX
Characteristics of OOPS
PDF
Constructor and Destructor
PPTX
Classes objects in java
PPTX
Friend function
PPT
FUNCTIONS IN c++ PPT
PPT
Function overloading(c++)
PPT
Class and object in C++
PPTX
Function overloading and overriding
PPT
Operator Overloading
PPTX
Interface in java
PPTX
Function in C program
PPTX
JAVA AWT
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
Object Oriented Programming Using C++
PPTX
Oops concept in c++ unit 3 -topic 4
PDF
C++ OOPS Concept
PPTX
Operators in java
PPTX
Inline function
PPTX
Inheritance in c++
Constructor in java
Characteristics of OOPS
Constructor and Destructor
Classes objects in java
Friend function
FUNCTIONS IN c++ PPT
Function overloading(c++)
Class and object in C++
Function overloading and overriding
Operator Overloading
Interface in java
Function in C program
JAVA AWT
Basic Concepts of OOPs (Object Oriented Programming in Java)
Object Oriented Programming Using C++
Oops concept in c++ unit 3 -topic 4
C++ OOPS Concept
Operators in java
Inline function
Inheritance in c++
Ad

Similar to Virtual function in C++ Pure Virtual Function (20)

PPTX
Virtual function and abstract class
PDF
22 scheme OOPs with C++ BCS306B_module4.pdf
PDF
Example for Virtual and Pure Virtual function.pdf
PPT
Virtual Function and Polymorphism.ppt
PPT
Virtual Function
PDF
polymorphism for b.tech iii year students
PPT
Polymorphism in C++ for beginners reference
PPTX
Virtual, Pure Virtual functions and Abstract class.pptx
PPTX
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
PDF
polymorpisum-140106223024-phpapp01.pdf
PPTX
polymorphism ppt
PPT
pure virtual function and abstract classes.ppt
PPTX
Dynamic Polymorphism in C++
PPT
VIRTUAL FUNCTIONS c++ -POLYMORPHISM.ppt
PPTX
B.sc CSIT 2nd semester C++ Unit6
PPTX
6. Virtual base class.pptx and virtual function
PDF
Oop Extract
PDF
2 BytesC++ course_2014_c12_ polymorphism
PPTX
Pure virtual function and abstract class
PPTX
Virtual function
Virtual function and abstract class
22 scheme OOPs with C++ BCS306B_module4.pdf
Example for Virtual and Pure Virtual function.pdf
Virtual Function and Polymorphism.ppt
Virtual Function
polymorphism for b.tech iii year students
Polymorphism in C++ for beginners reference
Virtual, Pure Virtual functions and Abstract class.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
polymorpisum-140106223024-phpapp01.pdf
polymorphism ppt
pure virtual function and abstract classes.ppt
Dynamic Polymorphism in C++
VIRTUAL FUNCTIONS c++ -POLYMORPHISM.ppt
B.sc CSIT 2nd semester C++ Unit6
6. Virtual base class.pptx and virtual function
Oop Extract
2 BytesC++ course_2014_c12_ polymorphism
Pure virtual function and abstract class
Virtual function
Ad

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Empathic Computing: Creating Shared Understanding
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPT
Teaching material agriculture food technology
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
A Presentation on Artificial Intelligence
PDF
cuic standard and advanced reporting.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Understanding_Digital_Forensics_Presentation.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Empathic Computing: Creating Shared Understanding
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Teaching material agriculture food technology
NewMind AI Monthly Chronicles - July 2025
Encapsulation_ Review paper, used for researhc scholars
“AI and Expert System Decision Support & Business Intelligence Systems”
A Presentation on Artificial Intelligence
cuic standard and advanced reporting.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
20250228 LYD VKU AI Blended-Learning.pptx

Virtual function in C++ Pure Virtual Function

  • 1. CE142 Runtime Polymorphism & Virtual Function
  • 2.  Virtual Function #include<iostream> using namespace std; class Base { public: virtual void show() { cout<<" In Base n"; } }; class Derived: public Base { public: void show() { cout<<"In Derived n"; } }; int main(void) { Base *bp = new Derived; bp->show(); // RUN-TIME POLYMORPHISM return 0; } In Derived
  • 3.   class A{  void f1();  virtual void f2();  virtual void f3();  virtual void f4();  };  class B:public A  {  void f2();  void f4(int x);  void f1();  } Virtual Function &f2() &f3() &f4() _vtable &f2() &f3() &f4() _vtable  A *p;  A x1;  p=&x1;  p->f1();  p->f2();  p->f3();  p->f4();  p->f4(10);  !!Error //EB //EB //LB //LB //LB p x1 _vptr
  • 4.   class A{  void f1();  virtual void f2();  virtual void f3();  virtual void f4();  };  class B:public A  {  void f2();  void f4(int x);  void f1();  } Virtual Function &f2() &f3() &f4() _vtable &f2() &f3() &f4() _vtable  A *p;  B x1;  p=&x1;  p->f1();  p->f2();  p->f3();  p->f4();  p->f4(10); //EB //EB //LB //LB //LB p x1 _vptr
  • 5.  Can static functions be virtual in C++? #include<iostream> using namespace std; class Test { public: // Error: Virtual member functions cannot be static virtual static void fun() { } };
  • 6.  Virtual Function & Default Arguments #include <iostream> using namespace std; class Base { public: virtual void fun ( int x = 0 ) { cout << "Base::fun(), x = " << x << endl; } }; class Derived : public Base { public: void fun ( int x ) { cout << "Derived::fun(), x = " << x << endl; } }; int main() { Derived d1; Base *b = &d1; b->fun(); return 0; } "Derived::fun(), x =0
  • 7.  Virtual Function & Default Arguments #include <iostream> using namespace std; class Base { public: virtual void fun ( int x = 0 ) { cout << "Base::fun(), x = " << x << endl; } }; class Derived : public Base { public: void fun ( int x=10 ) //Note The Changes { cout << "Derived::fun(), x = " << x << endl; } }; int main() { Derived d1; Base *bp = &d1; bp->fun(); return 0; } "Derived::fun(), x =0
  • 8.  Can virtual functions be private in C++? Never. Why?
  • 9.  Abstract Classes in C++  No Implementation of function in base class is referred as Abstract class Example: class Shape having member function draw(). It can only be have implementation at derived classes. i.e. draw() in rectangle, draw() in square. Example: class Animal having member function move().
  • 10.   A class which does not have an implementations of the function.  How to create abstract class in java.  By putting abstract keyword before class definition  How to create abstract class in C#  By putting abstract keyword before class definition  How to create abstract class in C++  By putting abstract keyword before class definition  There is no abstract keyword in C++!!!!! Abstract class
  • 11.   How to create abstract class in C++  A class in C++ is abstract if it has at least one pure virtual function.  What is pure virtual function?  It is a kind of virtual function which has no implementations!!! Abstract class in C++
  • 12.   Error: Undefined reference to vtable for Sha  How to make any function as pure virtual function
  • 13.  Abstract class in C++  How to create abstract class in C++  A class in C++ is abstract if it has at least one pure virtual function.  What is pure virtual function?  It is a kind of virtual function which has no implementations!!! How to make any virtual function as pure virtual function?  By putting “=0” expression at function declaration.
  • 14.
  • 16.  1. Abstract class may also contain non virtual functions.
  • 17.  2. Instances of the abstract class can not be created.
  • 18.  3. We can have pointers and references of abstract class type.
  • 19.  4. If we do not override the pure virtual function in derived class, then derived class also becomes abstract class.
  • 20.  5. An abstract class can have constructors.