🖥️

Virtual Functions, Pure Virtual Functions, and Abstract Classes in C++

Jun 22, 2024

Virtual Functions, Pure Virtual Functions, and Abstract Classes in C++

Introduction

  • Purpose: Explain the relationship between virtual functions, pure virtual functions, and abstract classes in C++
  • C++ Builder: A C++ IDE that connects to many databases and helps create apps faster
    • Frameworks: VCL for Windows apps, FireMonkey for cross-platform UIs
  • If you have specific topics or questions, comment below
  • Main Topics: Virtual functions, pure virtual functions, abstract classes

Virtual Functions

  • Definition: A function in a base class that's redefined in a derived class to achieve runtime polymorphism
  • Purpose: Allows for the execution of the most derived version of a function when using a base class pointer/reference
  • Example:
    • Base class Instrument with function makeSound
    • Derived class Accordion with its own implementation of makeSound
  • Implementation Steps:
    1. Define base class Instrument with a virtual makeSound function
    2. Create derived class Accordion and override makeSound
    3. Use a base class pointer to call makeSound, ensuring the derived class version executes
class Instrument { public: virtual void makeSound() { std::cout << "Instrument is playing" << std::endl; } }; class Accordion : public Instrument { public: void makeSound() override { std::cout << "Accordion is playing" << std::endl; } }; Instrument* instrument = new Accordion(); instrument->makeSound(); // Outputs: Accordion is playing

Pure Virtual Functions and Abstract Classes

  • Pure Virtual Functions: A virtual function with = 0 in its declaration, forcing derived classes to implement it
  • Abstract Class: A class containing at least one pure virtual function
  • Example:
    • Modify Instrument to make makeSound a pure virtual function
    • Create derived classes like Piano which must implement makeSound
    • Abstract class enforces derived class implementation
class Instrument { public: virtual void makeSound() = 0; // Pure virtual function }; class Piano : public Instrument { public: void makeSound() override { std::cout << "Piano is playing" << std::endl; } };
  • Error Without Implementation: Derived classes without makeSound will cause compile-time errors

Polymorphic Behavior

  • Example of Polymorphism:
    • Create an array of Instrument pointers
    • Iterate through the array and call makeSound for each, demonstrating polymorphism
Instrument* instruments[2]; instruments[0] = new Accordion(); instruments[1] = new Piano(); for (int i = 0; i < 2; ++i) { instruments[i]->makeSound(); } // Outputs: Accordion is playing, Piano is playing

Conclusion

  • Virtual functions allow derived class functions to be called through base class pointers
  • Pure virtual functions enforce implementation in derived classes, making the base class abstract
  • Polymorphism lets us treat different derived objects through a common interface

  • Personal Note: The instructor used accordion in the example because they used to play it, evoking nostalgia.
  • Call to Action: For more programming tutorials, like, comment, and subscribe!