Govt. Exams
Entrance Exams
In C++, a derived class can override a virtual function with a different access specifier than the base class. The access specifier controls who can call the function, but it doesn't affect the polymorphic behavior. However, it's a best practice to maintain the same access level.
Method overloading allows multiple functions with the same name but different parameter lists (number, type, or order). Return type alone is insufficient for overloading.
A const member function cannot modify any non-static data members of the object. It guarantees that calling the function will not change the object's state.
Constructors have no return type, a class can have multiple constructors (constructor overloading), and constructors are not automatically inherited in C++ (until C++11 explicit inheritance). They are automatically invoked when objects are created.
class A { public: virtual void func() { cout
With virtual functions, the actual object type (C) determines which function is called, not the pointer type. C::func() is invoked through multilevel inheritance.
The 'protected' access specifier allows access within the class and derived classes, but not to external code. This is ideal for members that derived classes need to use but external code should not access.
class Base { public: void show() { cout
Without virtual keyword, function overriding doesn't occur. The pointer type (Base*) determines which function is called, so Base::show() is invoked.
Abstract base classes are used to define interfaces and contracts that derived classes must follow. They cannot be instantiated but force derived classes to implement certain behaviors through pure virtual functions.
Option B shows operator overloading as a member function. The operator is defined as a member function of the class, which takes the second operand as a parameter.
Virtual functions enable runtime polymorphism, allowing the correct derived class method to be called through a base class pointer or reference, facilitating flexible and extensible code design.