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
Member Function in C++
PPTX
Inline function
PPTX
Static Data Members and Member Functions
PPTX
virtual function
PPTX
Friend function
PPTX
Constructors in C++
PPTX
Polymorphism in c++(ppt)
PPTX
Constructor and Types of Constructors
Member Function in C++
Inline function
Static Data Members and Member Functions
virtual function
Friend function
Constructors in C++
Polymorphism in c++(ppt)
Constructor and Types of Constructors

What's hot (20)

PPT
Operator Overloading
PPTX
INLINE FUNCTION IN C++
PPTX
07. Virtual Functions
PPT
Class and object in C++
PPT
RECURSION IN C
PPTX
sSCOPE RESOLUTION OPERATOR.pptx
PDF
Function overloading ppt
PPT
Method overriding
PPT
Function overloading(c++)
PPTX
Event Handling in java
PPTX
Pure virtual function and abstract class
PPTX
classes and objects in C++
PPT
friend function(c++)
PDF
Polymorphism In Java
PPTX
Data types in c++
PPTX
This pointer
PPTX
Friend function & friend class
PPTX
Functions in c++
PPTX
Call by value
PPTX
Array of objects.pptx
Operator Overloading
INLINE FUNCTION IN C++
07. Virtual Functions
Class and object in C++
RECURSION IN C
sSCOPE RESOLUTION OPERATOR.pptx
Function overloading ppt
Method overriding
Function overloading(c++)
Event Handling in java
Pure virtual function and abstract class
classes and objects in C++
friend function(c++)
Polymorphism In Java
Data types in c++
This pointer
Friend function & friend class
Functions in c++
Call by value
Array of objects.pptx
Ad

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

PPTX
Journey of a C# developer into Javascript
PPTX
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
PDF
Oop Presentation
PPT
SystemVerilog OOP Ovm Features Summary
PDF
Java 8: the good parts!
PDF
JavaOne 2013: Java 8 - The Good Parts
PDF
E5
PDF
OO-like C Programming: Struct Inheritance and Virtual Function
PPTX
EcmaScript unchained
PPTX
Things about Functional JavaScript
PPTX
Language fundamentals ocjp
PDF
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
PDF
polymorphism for b.tech iii year students
PPTX
CAP444-Unit-3-Polymorphism.pptx
PDF
The Present and The Future of Functional Programming in C++
PPT
Paradigmas de Linguagens de Programacao - Aula #4
PDF
The present and the future of functional programming in c++
PPTX
function in c
PDF
Javascript Design Patterns
PPT
Polymorphism in C++ for beginners reference
Journey of a C# developer into Javascript
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Oop Presentation
SystemVerilog OOP Ovm Features Summary
Java 8: the good parts!
JavaOne 2013: Java 8 - The Good Parts
E5
OO-like C Programming: Struct Inheritance and Virtual Function
EcmaScript unchained
Things about Functional JavaScript
Language fundamentals ocjp
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
polymorphism for b.tech iii year students
CAP444-Unit-3-Polymorphism.pptx
The Present and The Future of Functional Programming in C++
Paradigmas de Linguagens de Programacao - Aula #4
The present and the future of functional programming in c++
function in c
Javascript Design Patterns
Polymorphism in C++ for beginners reference
Ad

Recently uploaded (20)

PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
REPORT: Heating appliances market in Poland 2024
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Dell Pro 14 Plus: Be better prepared for what’s coming
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PPTX
Belt and Road Supply Chain Finance Blockchain Solution
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
KodekX | Application Modernization Development
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
PDF
Modernizing your data center with Dell and AMD
Reimagining Insurance: Connected Data for Confident Decisions.pdf
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Automating ArcGIS Content Discovery with FME: A Real World Use Case
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Sensors and Actuators in IoT Systems using pdf
REPORT: Heating appliances market in Poland 2024
Chapter 3 Spatial Domain Image Processing.pdf
CroxyProxy Instagram Access id login.pptx
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Dell Pro 14 Plus: Be better prepared for what’s coming
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
GamePlan Trading System Review: Professional Trader's Honest Take
Belt and Road Supply Chain Finance Blockchain Solution
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
KodekX | Application Modernization Development
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
Modernizing your data center with Dell and AMD

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.