This website uses cookies to enhance the user experience

C++ Interview Questions

22 Questions
C++ Programming

C++ Programming

Embedded SystemsIoTOther

Question 14

What is the difference between 'override' and 'final' specifiers in C++? Provide examples to illustrate their usage.

Answer:

In C++, override and final are specifiers introduced in C++11 that enhance the language's support for object-oriented programming by providing better control over inheritance and virtual functions.

override Specifier:
The override specifier indicates that a member function is intended to override a virtual function in a base class. It helps catch errors at compile time by ensuring that the function is indeed overriding a base class function.

Usage:

  • Ensures that the function is overriding a base class function.
  • Produces a compile-time error if the function does not match any base class function.

Example:

#include <iostream>

class Base {
public:
    virtual void show() const {
        std::cout << "Base::show()\n";
    }
};

class Derived : public Base {
public:
    void show() const override { // Correctly overrides Base::show
        std::cout << "Derived::show()\n";
    }
};

int main() {
    Base* obj = new Derived();
    obj->show(); // Outputs: Derived::show()
    delete obj;
    return 0;
}

In this example, the show function in the Derived class correctly overrides the show function in the Base class, and the override specifier ensures this at compile time.

final Specifier:
The final specifier prevents a class from being derived from or a virtual function from being overridden in derived classes. It is useful for stopping further inheritance and preventing changes to critical virtual functions.

Usage:

  • Prevents a class from being inherited.
  • Prevents a virtual function from being overridden.

Example:

#include <iostream>

class Base {
public:
    virtual void show() const {
        std::cout << "Base::show()\n";
    }
};

class Derived final : public Base { // final prevents further inheritance
public:
    void show() const override {
        std::cout << "Derived::show()\n";
    }
};

// class AnotherDerived : public Derived {}; // Error: Derived is final

int main() {
    Base* obj = new Derived();
    obj->show(); // Outputs: Derived::show()
    delete obj;
    return 0;
}

In this example, the Derived class is marked as final, preventing any further inheritance from it. Any attempt to derive from Derived will result in a compile-time error.

Combining override and final:
You can use both override and final together to specify that a function overrides a base class function and should not be overridden further.

Example:

#include <iostream>

class Base {
public:
    virtual void show() const {
        std::cout << "Base::show()\n";
    }
};

class Derived : public Base {
public:
    void show() const override final { // final prevents further overriding
        std::cout << "Derived::show()\n";
    }
};

// class AnotherDerived : public Derived { // Error: show is final
// public:
//     void show() const override {}
// };

int main() {
    Base* obj = new Derived();
    obj->show(); // Outputs: Derived::show()
    delete obj;
    return 0;
}

In this example, the show function in the Derived class overrides the show function in the Base class and is marked as final, preventing any further overriding in derived classes.

Summary:
The override and final specifiers in C++ provide better control over inheritance and virtual functions. The override specifier ensures that a function is correctly overriding a base class function, while the final specifier prevents further inheritance or overriding of a class or virtual function. Using these specifiers enhances code safety, readability, and maintainability by catching errors at compile time and clearly conveying the programmer's intent.

Recent job openings

Canada, Montreal, QC

Remote

Full-time

JavaScript

JavaScript

TypeScript

TypeScript

posted 3 days ago

Poland, Wrocław, Lower Silesian Voivodeship

Remote

SQL

SQL

Angular

Angular

+5

posted 3 days ago

United Kingdom, London, Belfast, Bristol, Gateshead, England

Remote

Full-time

posted 3 days ago

Argentina

Remote

Full-time

Flutter

Flutter

posted 3 days ago

Spain, Madrid, Community of Madrid

Remote

Java

Java

TypeScript

TypeScript

+8

posted 3 days ago