• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Classes » Virtual Function in C++
Next →
← Prev

Virtual Function in C++

By Dinesh Thakur

We know that runtime polymorphism is achieved when the objects of different classes in the class hierarchy respond to the same function call each in its way. To invoke same-named functions present both in the base and derived classes using a single interface. But we found that it still could not execute the derived class’s functions and executed the same-named function defined in the base class. Thus it could not achieve polymorphism. To implement polymorphism for invoking the exact version of the same-named member functions in the class hierarchy, we use virtual functions.

A virtual function is a member function declared in the base class using the keyword virtual whose functionality is redefined (same function name) by its derived classes. To declare the virtual function in the base class, precede its function declaration with a keyword virtual. Virtual functions employ late binding by allocating memory space during execution time and not during compilation time. In this case when a pointer of the base class is defined in a main() function and derived class object’s address is passed on to the base class pointer, then calling the overridden function will invoke the derived class member function and not the base class member function as mentioned earlier.

To implement run time polymorphism using the virtual function, it must be invoked through the base class pointer that can contain objects of different derived classes. When the virtual function is invoked through the base class pointer, the compiler chooses the appropriate member function of the derived class at run time depending upon the base class pointer contents and not the type of pointer. Thus by making the base class pointer pointing to different classes’ objects, the different versions of virtual functions can be called at run time. Hence, run time polymorphism is implemented.

Virtual functions are named so because when virtual functions are used in the class hierarchy, the programmer appears to call a function defined in the base class (common interface) but may, in reality, be calling a function of its derived class.

To understand how virtual function works. The complete program that implements runtime polymorphism using the virtual function is as shown.

#include<iostream.h>
#include<conio.h>
class base {
  public:
    virtual void display() //virtual function
      {cout<<"Base class display is called\n"; }
};
class derv1: public base {
  public:
    void display()
     {cout<<"Derv1' s display called\n"; }
};
class derv2: public base {
  public:
    void display ()
     { cout<<"Derv2's display called\n"; }
};
int main() {
  clrscr();
  base *ptr;
  derv1 d1;
  derv2 d2;
  ptr=&d1;
  ptr->display();
  ptr=&d2
  ptr->display() ;
  getch() ; return 0;
}
Output:
Dervl's display called
Derv2's display called

Explanation: This program’s output reveals that the member function display() of the derived classes are invoked and not that of the base class as expected. By defining the member function display () as virtual in the base class,

virtual void display(){ ... )

every call to this function through the base class pointer (ptr) will be dynamically bound (run time) and not compile. So if the base class pointer ptr contains the address of the object d1 of the derv1 class, then the statement.

ptr->display();

invokes the member function defined in the derived class derv1. The compiler chooses the appropriate member function of derived class derv1 at run time depending upon the base class pointer contents and not the type of pointer. Here the compiler does not perform static binding as addresses of the derived class objects are not known to the base class pointer at compile time which is available only at run time. Hence binding decision is postponed until run time. Similarly, if the base class pointer ptr contains the address of object d2 of derv2 class, then the statement ptr->display(); invokes the display() function defined in the derv2 class.

Thus, we see that the same function call.

prt->display();

executes a different function based on the contents of ptr and hence implements run-time polymorphism.

You’ll also like:

  1. Write a C++ Program of Virtual Function
  2. Write a C++ program illustrates Abstract class and Pure virtual function.
  3. Virtual and virtual path Circuit
  4. Difference between Virtual Circuit (VC) and Permanent Virtual Circuit (PVC)
  5. Virtual Base Class in C++
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW