Correct option is A
The final keyword in C++ is used to restrict inheritance or method overriding. When applied to a class, it prevents the class from being inherited. When applied to a method, it prevents the method from being overridden in derived classes.
Important Key Points:
1.
Class-level restriction: Declaring a class as final ensures that no other class can inherit from it. Example:
class Base final {};
2.
Method-level restriction: Declaring a method as final ensures it cannot be overridden in a derived class. Example:
3. This is primarily used to enforce stricter design constraints and avoid unintended modifications through inheritance.
Knowledge Booster:
·
To declare a variable as constant: Use the const keyword, not final. Example: const int a = 10;
·
To allocate memory dynamically: Use the new keyword, not final. Example: int* p = new int;
·
To define a destructor: Use ~ClassName(), not final. Example:
class Base { ~Base() {}; };
·
To make a method virtual: Use the virtual keyword. Example: virtual void display();
