What is the output of this C++ code?
#include <iostream> using namespace std; class Base { public: virtual void show() { cout << "Base" << endl; } }; class Derived : public Base { public: void show() override { cout << "Derived" << endl; } }; int main() { Base* b = new Derived(); b->show(); delete b; return 0; }
Think about how virtual functions work with base class pointers.
The pointer b is of type Base* but points to a Derived object. Because show() is virtual, the Derived version is called, printing "Derived".
What is the output of this C++ program?
#include <iostream> using namespace std; class Base { public: Base() { cout << "Base constructor\n"; } ~Base() { cout << "Base destructor\n"; } }; class Derived : public Base { public: Derived() { cout << "Derived constructor\n"; } ~Derived() { cout << "Derived destructor\n"; } }; int main() { Base* b = new Derived(); delete b; return 0; }
Check if destructors are virtual or not.
The Base destructor is not virtual, so deleting a Derived object through a Base* pointer calls only the Base destructor. The Derived destructor is not called, causing incomplete cleanup.
What error does this code produce?
#include <iostream> using namespace std; class Base { int x; public: Base(int val) : x(val) {} }; class Derived : public Base { int y; public: Derived(int val) { y = val; } }; int main() { Derived d(5); return 0; }
Check how base class constructors are called in derived classes.
The Base class has no default constructor. The Derived constructor must explicitly call Base(int) in its initializer list. Otherwise, compilation fails.
Given the base class below, which derived class method correctly overrides void display() const?
class Base { public: virtual void display() const; };
Check the method signature including const qualifier and override keyword.
To override a method, the signature must match exactly including const. Option B matches void display() const and uses override keyword correctly.
Consider the following code. How many times are constructors and destructors called in total?
#include <iostream> using namespace std; class Base { public: Base() { cout << "Base constructed\n"; } virtual ~Base() { cout << "Base destroyed\n"; } }; class Derived : public Base { public: Derived() { cout << "Derived constructed\n"; } ~Derived() { cout << "Derived destroyed\n"; } }; int main() { Base* arr[3]; arr[0] = new Base(); arr[1] = new Derived(); arr[2] = new Derived(); for (int i = 0; i < 3; ++i) { delete arr[i]; } return 0; }
Count each object creation and destruction carefully, including base and derived parts.
Three objects are created: one Base and two Derived. Each Derived object calls Base constructor first, so total constructors called: 1 (Base) + 2*(Base+Derived) = 1 + 4 = 5. But actually, each Derived calls Base constructor once, so total constructors are 1 (Base) + 2 (Base part of Derived) + 2 (Derived) = 5 constructors. However, the code prints one line per constructor call, so total printed lines are 5 (Base constructed printed 3 times, Derived constructed printed 2 times). Similarly for destructors, virtual destructor ensures both Derived and Base destructors are called for Derived objects, so total destructor calls are 5. So total constructor and destructor calls printed are 5 each.