Govt. Exams
Entrance Exams
class Test { int x; public: Test() { x = 0; } ~Test() { cout
Two objects t1 and t2 are created. When the program exits, both objects are destroyed in the reverse order of creation (t2 then t1), calling the destructor twice.
Virtual functions use dynamic (runtime) binding where the function to be called is determined based on the actual object type at runtime. Non-virtual functions use static (compile-time) binding.
class A { public: int x = 5; };
class B : public A { public: int x = 10; };
int main() { B obj; cout
The derived class B has its own member variable 'x' with value 10, which shadows the base class member. obj.x refers to B's x, which is 10.
The scope resolution operator (::), member access operator (.), pointer-to-member operator (.*), and ternary operator (?:) cannot be overloaded in C++.
A const member function cannot modify any member variables of the object. It guarantees that the function will not alter the state of the object.
An abstract class in C++ must have at least one pure virtual function. It cannot be instantiated directly, but it can have constructors and other member functions.
class Base { public: virtual void display() { cout
This demonstrates runtime polymorphism. The virtual function is called based on the actual object type (Derived), not the pointer type (Base), so 'Derived' is printed.
Abstract classes with pure virtual functions cannot be instantiated. Derived classes must override pure virtual functions. Option B correctly shows this pattern.
Composition represents a 'has-a' relationship where a class contains objects of other classes. Inheritance represents an 'is-a' relationship where a class extends another class.
Function overloading is resolved at compile time. Virtual functions and dynamic casting are examples of runtime polymorphism.