C++ · Module 20 · Lesson 11 min read

C++ Output Prediction Questions

How to approach them

  1. Track each object's construction and destruction (write them down)
  2. Note what is a copy, a reference, or a move
  3. Check whether calls are virtual and through pointer/reference or by value
  4. Watch integer division, static variables, default arguments, overload resolution
  5. If there's undefined behaviour, say so

Classic C++ traps ⭐

TrapKey fact
Virtual call in a constructorcalls the base version
Object slicingpassing Derived by value as Base loses the override
Default arguments + virtualdefaults come from the static (pointer) type, the body from the dynamic type
Destruction orderreverse of construction
static local in a functioninitialized once
Copy vs reference in range-forfor (auto x : v) x *= 2; changes nothing
sizeof an empty class1, not 0
Overload with 0/nullptr/NULLf(0) picks int, f(nullptr) picks the pointer
Member initialization orderdeclaration order, not initializer-list order
Temporary lifetimedestroyed at the end of the full expression (unless bound to const&)
Integer division / promotion5 / 2 = 2, 'a' + 1 = 98
std::vector<int> v(3) vs {3}three zeros vs one element
Short-circuitfalse && f() never calls f

Answering well in interviews

Explain why each line prints what it does. Interviewers care more about the reasoning (e.g. "the default argument is bound statically") than the final text.

Example

C++
#include <iostream>
#include <string>
#include <vector>

struct Base {
    Base() { std::cout << "Base() calls "; hello(); }
    virtual ~Base() { std::cout << "~Base "; }
    virtual void hello() const { std::cout << "Base::hello\n"; }
    virtual void greet(const std::string& who = "base default") const { std::cout << "Base::greet " << who << '\n'; }
};

struct Derived : Base {
    Derived() { std::cout << "Derived() calls "; hello(); }
    ~Derived() override { std::cout << "~Derived "; }
    void hello() const override { std::cout << "Derived::hello\n"; }
    void greet(const std::string& who = "derived default") const override { std::cout << "Derived::greet " << who << '\n'; }
};

void by_value(Base b) { b.hello(); }
void by_ref(const Base& b) { b.hello(); }

void f(int) { std::cout << "f(int)\n"; }
void f(const char*) { std::cout << "f(const char*)\n"; }

int counter() { static int n = 0; return ++n; }

struct Empty {};

int main() {
    std::cout << "1) ";
    {
        Derived d;                                    // virtual call in Base() runs Base::hello
        std::cout << "2) by_value: ";
        by_value(d);                                  // slicing (Base copy also destroyed: ~Base)
        std::cout << "\n3) by_ref: ";
        by_ref(d);
        std::cout << "4) ";
        const Base& b = d;
        b.greet();                                    // Derived body, Base default argument!
        std::cout << "5) end of scope: ";
    }
    std::cout << '\n';

    std::cout << "6) ";
    f(0);
    std::cout << "7) ";
    f(nullptr);

    std::cout << "8) " << counter() << counter() << counter() << " (unspecified order before C++17; left-to-right since C++17 for <<)\n";

    std::vector<int> v = { 1, 2, 3 };
    for (auto x : v) x *= 10;                         // copies: v unchanged
    std::cout << "9) " << v[0] << v[1] << v[2] << '\n';

    std::vector<int> a(3), b{ 3 };
    std::cout << "10) a.size()=" << a.size() << " b.size()=" << b.size() << '\n';
    std::cout << "11) sizeof(Empty)=" << sizeof(Empty) << '\n';
    std::cout << "12) " << 7 / 2 << ' ' << 7 / 2.0 << ' ' << 'a' + 1 << ' ' << static_cast<char>('a' + 1) << '\n';
    return 0;
}

/* Output:
1) Base() calls Base::hello
Derived() calls Derived::hello
2) by_value: Base::hello
~Base
3) by_ref: Derived::hello
4) Derived::greet base default
5) end of scope: ~Derived ~Base
6) f(int)
7) f(const char*)
8) 123 (unspecified order before C++17; left-to-right since C++17 for <<)
9) 123
10) a.size()=3 b.size()=1
11) sizeof(Empty)=1
12) 3 3.5 98 b
*/