Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Code
  2. Classes

Virtual functions

Inherit methods and member of parent classes.

Virtual functions in C++ are a key feature of object-oriented programming that enable polymorphism. Here's a concise explanation:

  1. Definition: A virtual function is a member function declared in a base class and redefined in derived classes.

  2. Purpose: They allow a program to call methods of a derived class through a pointer or reference of the base class type.

  3. Declaration: Declared using the 'virtual' keyword in the base class.

  4. Late binding: The function call is resolved at runtime, not compile-time.

  5. Pure virtual functions: Defined with "= 0" and make the class abstract.

  6. Virtual destructors: Often used to ensure proper cleanup of derived objects.

  7. Virtual table (vtable): C++ uses this behind the scenes for implementation.

#include <iostream>

class Animal {
public:
    virtual void makeSound() {
        std::cout << "The animal makes a sound" << std::endl;
    }
    
    virtual ~Animal() {
        std::cout << "Animal destructor called" << std::endl;
    }
};

class Dog : public Animal {
public:
    void makeSound() override {
        std::cout << "The dog barks: Woof!" << std::endl;
    }
    
    ~Dog() override {
        std::cout << "Dog destructor called" << std::endl;
    }
};

class Cat : public Animal {
public:
    void makeSound() override {
        std::cout << "The cat meows: Meow!" << std::endl;
    }
    
    ~Cat() override {
        std::cout << "Cat destructor called" << std::endl;
    }
};

int main() {
    Animal* animal1 = new Dog();
    Animal* animal2 = new Cat();
    
    animal1->makeSound();  // Outputs: The dog barks: Woof!
    animal2->makeSound();  // Outputs: The cat meows: Meow!
    
    delete animal1;  // Calls Dog destructor, then Animal destructor
    delete animal2;  // Calls Cat destructor, then Animal destructor
    
    return 0;
}
PreviousOther constructorsNextPure virtual function

Last updated 9 months ago

Run it .

🏢
here