Category: Uncategorized

  • Difference between Virtual Functions and Pure Virtual functions

    In C++, virtual functions and pure virtual functions are key components of achieving polymorphism, a core feature of object-oriented programming. Let’s break down each concept with examples to understand the differences and typical use cases.

    1. Virtual Function

    A virtual function is a member function in a base class that is declared using the virtual keyword. It is meant to be overridden in a derived class. Virtual functions enable runtime polymorphism, meaning the function that gets called depends on the actual type of the object (i.e., the type of the derived class object) rather than the type of the pointer or reference to the base class.

    • Characteristics:
    • Declared in the base class using the virtual keyword.
    • Can be overridden in the derived class.
    • If not overridden, the base class version of the function will be called.

    Example:

    #include <iostream>
    
    class Animal {
    public:
        // Virtual function
        virtual void speak() {
            std::cout << "Animal sound!" << std::endl;
        }
    };
    
    class Dog : public Animal {
    public:
        // Overriding the virtual function
        void speak() override {
            std::cout << "Woof!" << std::endl;
        }
    };
    
    int main() {
        Animal* animal = new Dog();
        animal->speak(); // Outputs: "Woof!" due to runtime polymorphism
        delete animal;
    }

    Here, speak() is a virtual function in the base class Animal. The pointer animal points to a Dog object, so the Dog class’s version of speak() is called, even though the type of the pointer is Animal*.

    2. Pure Virtual Function

    A pure virtual function (also called an abstract function) is a virtual function that must be overridden in any derived class. It has no implementation in the base class and is assigned = 0 in its declaration. A class that contains at least one pure virtual function is called an abstract class, and it cannot be instantiated.

    • Characteristics:
    • Declared in the base class with = 0, indicating it’s pure virtual.
    • Must be overridden in any non-abstract derived class.
    • Abstract classes cannot be instantiated directly; they serve as interfaces or base classes.

    Example:

    #include <iostream>
    
    class Animal {
    public:
        // Pure virtual function
        virtual void speak() = 0;  // No implementation in base class
    };
    
    class Dog : public Animal {
    public:
        // Overriding the pure virtual function
        void speak() override {
            std::cout << "Woof!" << std::endl;
        }
    };
    
    class Cat : public Animal {
    public:
        // Overriding the pure virtual function
        void speak() override {
            std::cout << "Meow!" << std::endl;
        }
    };
    
    int main() {
        // Animal* animal = new Animal(); // Error: Cannot instantiate abstract class
        Animal* dog = new Dog();
        Animal* cat = new Cat();
    
        dog->speak(); // Outputs: "Woof!"
        cat->speak(); // Outputs: "Meow!"
    
        delete dog;
        delete cat;
    }

    Here, speak() is a pure virtual function in the base class Animal. The Dog and Cat classes must provide their own implementations of the speak() function. Since Animal is an abstract class, you cannot create an instance of Animal, but you can create instances of Dog or Cat.

    Key Differences

    Virtual FunctionPure Virtual Function
    Provides a default implementation in the base class, but can be overridden in derived classes.Does not provide an implementation in the base class. Derived classes must override it.
    A class with virtual functions can be instantiated.A class with at least one pure virtual function is an abstract class and cannot be instantiated.
    Declared with the virtual keyword.Declared with the virtual keyword and assigned = 0 to indicate it’s pure virtual.
    Overriding in derived classes is optional.Overriding in derived classes is mandatory.

    When to Use

    • Virtual functions are used when you want to provide a common interface in the base class but allow derived classes to optionally override the behavior.
    • Pure virtual functions are used when you want to enforce that all derived classes provide their own specific implementation of a function, making the base class an abstract blueprint.

    Example Use Cases

    • Virtual Function: A general logging system where you want a default log() function, but derived classes (like file loggers or network loggers) can optionally override it to provide specific logging mechanisms.
    • Pure Virtual Function: An interface for shapes (Shape) that mandates all derived classes (like Circle, Square, Triangle) implement their own version of area().

    In summary, virtual functions allow derived classes to optionally override behavior, while pure virtual functions enforce that derived classes provide their own specific implementations.

  • Hello world!

    Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

  • Hello world!

    Welcome to WordPress. This is your first post. Edit or delete it, then start writing!