StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Monday, May 26, 2008

C++ Virtual Functions

 May 26, 2008     CPP     No comments   

What is a virtual function?

  • Virtual function is a polymorphism technique.
  • Refers to performing the same operation in a hierarchy of classes. Typically used in scenarios where the base class pointer is used to hold derived class objects and perform the same operation. Refer example below.
  • When a virtual functions is called on a base class pointer the compiler decides to defer the decision on which function to call until the program is running thereby doing late binding.
  • The actual function called at run-time depends on the contents of the pointer and not the type.
  • Internally the compiler creates a VTABLE for each which has virtual functions.
  • Addresses of virtual functions are placed in the VTABLE. If a virtual function is not redefined in the derived class, the base class function address is used in VTABLE
  • When objects are created compiler also places a VPTR pointing to starting address of VTABLE using which the correct function is invoked.

EXAMPLE: Demonstrate the late binding using virtual functions

#include <iostream>

using namespace std;

// Base class with virtual function
class Base {
    int data1;

    public:
        Base ( int d1) { data1 = d1; }
        virtual void print() { cout << "Base" << endl; }

};

// Derived class 1
class Derived1 : public Base {
    int data2;

    public:
        Derived1(int d1, int d2) : Base (d1) { data2 = d2; }
        void print() { cout << "Derived1" << endl; }

};

// Derived class 2
class Derived2 : public Base {
    public:
        Derived2(int d1) : Base (d1) { }
        void print() { cout << "Derived2" << endl; }

};

void main()
{
    Base b(40);
    Derived1 d1(10, 20);
    Derived2 d2(30);

    Base* ptr;
    ptr=&d1;
    ptr->print();

    ptr=&d2;
    ptr->print();
}
OUTPUT:
Derived1
Derived2
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • C++ TypecastingWhat are the typecasting mechanisms in C++? C++ provides a casting syntax with 4 reserved words, dynamic_cast, const_cast, static_cast and reinterp… Read More
  • C++ Virtual DestructorWhat is a virtual destructor? Virtual destructor ensures that the object destruction happens from the most derived class towards the base class. S… Read More
  • C++ Exception HandlingWhat is Exception Handling? Exceptions are run-time program anomalies. Exception handling provides a standard mechanism to handle such anomalies. … Read More
  • C++ TemplatesWhat are templates? Templates provide a mechanism to handle different data types in the same function or class. When used in functions they are re… Read More
  • C++ NamespacesWhat is a namespace? Namespaces come in handy to avoid name conflicts when multiple programmers are involved. Any name in the namespace could be a… Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Python FastAPI file upload and download
    In this article, we will look at an example of how to implement a file upload and download API in a Python FastAPI microservice. Example bel...
  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Accessing the Kubernetes API
    In this article, we will explore the steps required to access the Kubernetes API and overcome common challenges. All operations and communic...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Scheduling jobs in Python
    When developing applications and microservices we run into scenarios where there is a need to run scheduled tasks. Examples include performi...
  • Using Tekton to deploy KNative services
    Tekton is a popular open-source framework for building continuous delivery pipelines. Tekton provides a declarative way to define pipelines ...

Copyright © 2025 StackStalk