ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
class Base {
public:
    Base(const Base&) = delete;
    Base() = default;
    virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
    void show() override { cout << "Derived"; }
};
void print(Base& b) { b.show(); }
int main() {
    Derived d;
    print(d);
}
  1. A. Derived
  2. B. Base
  3. C. Compilation error: Base's deleted copy constructor blocks passing d by reference
  4. D. Compilation error: Derived must also delete its copy constructor

Correct answer: A. Derived

Passing by reference (Base&) never invokes any copy constructor at all — only pass-by-value would. So the deleted Base copy constructor is irrelevant here; print(d) binds the reference to d directly, and b.show() dispatches virtually to Derived::show, printing "Derived".

More OOP with C++ and Java questions

Exams that ask this topic