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
  • Share This:  
Newer Post Older Post Home

0 comments:

Post a Comment

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

Popular Posts

  • 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 ...
  • Implement caching in a Spring Boot microservice using Redis
    In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
  • 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...
  • Spring Boot with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Getting started with Kafka in Python
    This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
  • Getting started in GraphQL with Spring Boot
    In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

Copyright © StackStalk