Govt. Exams
Entrance Exams
Can class C access x?
When B inherits privately from A, the protected and public members of A become private in B. C cannot access them because they are not accessible through the inheritance chain.
Without a virtual destructor, only the base class destructor is called, leading to incomplete cleanup of derived class resources and potential memory leaks.
Interfaces (abstract classes) define a contract specifying what methods derived classes must implement, promoting loose coupling and design flexibility.
The 'mutable' keyword allows a member variable to be modified even within a const member function or when the object itself is const.
A virtual destructor ensures that when deleting a base class pointer pointing to a derived object, the derived class destructor is called first, preventing resource leaks.
Shallow copy only copies the pointer references, while deep copy allocates new memory and copies the actual data, preventing dangling pointers.
LSP states that objects of a derived class should be able to replace objects of the base class without breaking the program. This ensures predictable polymorphic behavior.
Good OOP design aims to minimize code duplication through inheritance and composition, not maximize it. High cohesion, low coupling, and proper encapsulation are fundamental principles.
Virtual inheritance ensures that when a class appears multiple times in an inheritance hierarchy, only one copy of it is included, resolving ambiguity and the Diamond Problem.
class A { public: virtual ~A() {} };
class B : public A { public: ~B() {} };
int main() { A* ptr = new B(); delete ptr; }
What is the significance of virtual destructor here?
A virtual destructor ensures that when a derived class object is deleted through a base class pointer, the derived class destructor is called first, preventing memory leaks and ensuring proper cleanup.