Final in C++
The word "final" in the C++ language denotes that a class or Function with members is not intended to be modified or expanded by derived classes. It is a component of the C++11 standard that is mostly used to indicate that a class or method is the final one in the hierarchy and ought to be kept from being further specialized or altered.
Final class:
A class marked as "final" cannot serve as the base class for any additional classes, according to the definition of the term. In other words, it stops a class from being inherited.
Detailed information:
Purpose:
Final Classes Serve as Complete, Self-Contained Units. When creating a class, it is important to remember that other classes will not be extending or changing the behavior of the final class. By designating a class as final, you guarantee that it won't undergo any more modifications through inheritance.
Syntax:
class FinalClass final {
// Class members and methods
};
Effect on Inheritance:
class FinalClass final {
// Class members and methods
};
class Derived: public FinalClass { // Error: Cannot inherit from final class FinalClass
// ...
};
Advantages and Use Cases
Final classes help guarantee that some core classes remain intact, preventing unintended changes that might result in unexpected behavior. It helps maintain the code's integrity.
Designing APIs: Final classes can provide well-defined APIs that are ensured not to be changed by derived classes.
Performance: The compiler may optimize final classes since it is certain as no derived class will take precedence over them.
Alternative Methods
A class may be inherited by default if the "final" keyword is not used. Making the class' constructor protected or private would make it more difficult to build derived classes, precluding inheritance. The "final" keyword conveys the objective more clearly than this method.
Example:
class Vehicle final {
public:
Vehicle(int wheels) : numWheels(wheels) {}
void displayWheels() {
cout << "This vehicle has " << numWheels << " wheels." << endl;
}
private:
int numWheels;
};
class Car : public Vehicle { // Error: Cannot inherit from final class Vehicle
// ...
};
Output:
![Final in C++](https://static.tutorialandexample.com/cpp/final-in-cpp1.png)
Explanation:
The Vehicle class in the submitted code is tagged as "final," which implies it cannot be descended from. The attempt to create a Car class by deriving it from the Vehicle class would fail due to a compilation problem. The code won't compile because the Car class attempts to inherit via a final class. As a result, this code snippet will not produce any output.
This error message informs the user that the Car class cannot inherit directly from the Vehicle class due to the Vehicle class being marked as "final."
Final Member Function:
Declaring a member's Function as "final" inside a class means that descendant classes cannot override that particular Function.
Detailed information:
- Purpose
Final member functions guarantee that a particular function in a base class may not be changed or expanded in any derived classes. Doing so may retain the desired behavior and guarantee that certain fundamental methods are applied consistently throughout the class hierarchy.
Syntax:
class Base {
public:
virtual void someFunction() final {
//Function implementation
}
};
- Effect of Overriding:
Any attempt to modify a member function in a class that has been derived will cause a compilation error after it has been declared as final. The compiler imposes this constraint.
class Base {
public:
virtual void someFunction() final {
//Function implementation
}
};
class Derived : public Base {
public:
// Error: Cannot override final function someFunction
/*void someFunction() override {
// ...
}*/
};
- Advantages and Use Cases
Final member functions may be employed to protect specific behaviors or essential functionality from being changed in derived classes.
Stability of the API: When creating a class library, designating essential member features as final can help guarantee that the library's users can access a stable API.
Error prevention: Final members can assist in avoiding potential faults brought on by unintentional changes in behavior by forbidding overrides.
- Alternative Methods:
Derived classes are, by default permitted to override member methods if the "final" keyword is not used. By constructing private or protected non-virtual methods that fulfill the same role instead of offering virtual functions that may be overridden, you can obtain comparable outcomes.
Example:
class Base {
Public:
virtual void displayInfo() final {
cout << "This is the Base class." << endl;
}
};
class Derived : public Base {
public:
// Error: Cannot override final function displayInfo
/*void displayInfo() override {
cout << "This is the Derived class." << endl;
}*/
};
Output
![Final in C++](https://static.tutorialandexample.com/cpp/final-in-cpp2.png)
Explanation:
A Derived class that attempts to modify the displayInfo() function can be found in the code you provided, along with a Base class with a member function displayInfo() declared final. Any attempt to override the displayInfo() Function in the Derived class will result in a compilation error since it is declared final in the Base class. This code will not produce any output as a result.
As this error notice informs you, the displayInfo() function within the Base class is tagged as final, so it can't be modified in the Derived class.