Cover Image for Default arguments and virtual function in C++
163 views

Default arguments and virtual function in C++

The C++ default arguments and virtual functions are two important features that help in creating flexible and extensible classes and functions.

Default Arguments

Default arguments allow you to provide default values for function parameters. This means that you can call a function without providing values for some or all of its parameters, and the default values will be used. Default arguments are specified in the function declaration in the form of an assignment operator (=) followed by the default value.

Here’s an example of a function with default arguments:

C++
#include <iostream>

void printMessage(const std::string& message = "Hello, World!") {
    std::cout << message << std::endl;
}

int main() {
    printMessage();               // Uses the default message
    printMessage("Custom text"); // Uses the provided message

    return 0;
}

In this example, the printMessage function has a default argument of "Hello, World!". When you call the function without providing an argument, it uses the default value.

Virtual Functions

Virtual functions are a fundamental concept in C++ for achieving polymorphism, which allows derived classes to override the behavior of base class functions. When you declare a function as virtual in a base class, you enable dynamic dispatch, which means that the correct function to call is determined at runtime based on the actual type of the object.

Here’s an example of using virtual functions in C++:

C++
#include <iostream>

class Animal {
public:
    virtual void speak() {
        std::cout << "Animal speaks" << std::endl;
    }
};

class Dog : public Animal {
public:
    void speak() override {
        std::cout << "Dog barks" << std::endl;
    }
};

class Cat : public Animal {
public:
    void speak() override {
        std::cout << "Cat meows" << std::endl;
    }
};

int main() {
    Animal* animal;

    Dog dog;
    Cat cat;

    animal = &dog;
    animal->speak(); // Calls Dog's speak()

    animal = &cat;
    animal->speak(); // Calls Cat's speak()

    return 0;
}

In this example, the speak function is declared as virtual in the base class Animal. The derived classes Dog and Cat override this function with their own implementations. When you call speak on a pointer to an Animal object, the appropriate derived class’s speak function is invoked, demonstrating polymorphism.

Here are some important points about virtual functions:

  • Virtual functions are declared in the base class with the virtual keyword.
  • Derived classes provide their own implementations of virtual functions using the override keyword.
  • The correct function to call is determined at runtime based on the actual type of the object, allowing for dynamic behavior based on the object’s type.

Using default arguments and virtual functions allows you to create more flexible and extensible code in C++, making it easier to work with different input scenarios and to achieve polymorphic behavior in your classes.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS