To Be Or Not To Be Virtual

Program Output, Version 1

What will be the output of the following program? (Write an X in the “True” or “False” columns)

#include <iostream>

class Base
{
public:
    void method() const
    {
        std::cout << "Base::method()\n";
    }
};

class Derived : public Base
{
public:
    void method() const
    {
        std::cout << "Derived::method()\n";
    }
};

int main()
{
    Derived d;
    d.method();
    Base* b = &d;
    b->method();

    return 0;
}

True

False

Possible output

Derived::method()
Derived::method()
Derived::method()
Base::method()

Program Output, Version 2

What will be the output of the following program? (Write an X in the “True” or “False” columns)

#include <iostream>

class Base
{
public:
    virtual void method() const
    {
        std::cout << "Base::method()\n";
    }
};

class Derived : public Base
{
public:
    void method() const override
    {
        std::cout << "Derived::method()\n";
    }
};

int main()
{
    Derived d;
    d.method();
    Base* b = &d;
    b->method();

    return 0;
}

True

False

Possible output

Derived::method()
Derived::method()
Derived::method()
Base::method()