0
0
C++programming~20 mins

Base class pointers in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Base Class Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of base class pointer to derived object
What is the output of this C++ code when using a base class pointer to a derived class object?
C++
#include <iostream>
using namespace std;

class Base {
public:
    void show() { cout << "Base show" << endl; }
};

class Derived : public Base {
public:
    void show() { cout << "Derived show" << endl; }
};

int main() {
    Derived d;
    Base* ptr = &d;
    ptr->show();
    return 0;
}
ABase show
BDerived show
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Remember that without virtual functions, base class pointers call base class methods.
❓ Predict Output
intermediate
2:00remaining
Output with virtual function and base class pointer
What will this C++ program print when a base class pointer points to a derived class object and calls a virtual function?
C++
#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() { cout << "Base show" << endl; }
};

class Derived : public Base {
public:
    void show() override { cout << "Derived show" << endl; }
};

int main() {
    Derived d;
    Base* ptr = &d;
    ptr->show();
    return 0;
}
ARuntime error
BBase show
CCompilation error
DDerived show
Attempts:
2 left
πŸ’‘ Hint
Virtual functions enable runtime polymorphism.
❓ Predict Output
advanced
2:00remaining
Output of slicing with base class pointer and object copy
What is the output of this C++ code involving object slicing and base class pointers?
C++
#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() { cout << "Base show" << endl; }
};

class Derived : public Base {
public:
    void show() override { cout << "Derived show" << endl; }
};

int main() {
    Derived d;
    Base b = d;  // Object slicing
    Base* ptr = &b;
    ptr->show();
    return 0;
}
ADerived show
BBase show
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Object slicing copies only the base part of the derived object.
πŸ”§ Debug
advanced
2:00remaining
Identify the error with base class pointer and deleted derived object
What error will this C++ code produce when deleting a derived object through a base class pointer?
C++
#include <iostream>
using namespace std;

class Base {
public:
    ~Base() { cout << "Base destructor" << endl; }
};

class Derived : public Base {
public:
    ~Derived() { cout << "Derived destructor" << endl; }
};

int main() {
    Base* ptr = new Derived();
    delete ptr;
    return 0;
}
AOnly Base destructor is called, causing resource leak
BBoth Derived and Base destructors are called correctly
CCompilation error due to missing virtual destructor
DRuntime crash due to invalid delete
Attempts:
2 left
πŸ’‘ Hint
Base class destructor should be virtual to delete derived objects safely.
🧠 Conceptual
expert
2:00remaining
Why use base class pointers for polymorphism?
Which is the main reason to use base class pointers to refer to derived class objects in C++?
ATo prevent object slicing when copying objects
BTo save memory by storing only base class data
CTo enable runtime polymorphism and call derived class methods via base pointers
DTo avoid the need for virtual functions in derived classes
Attempts:
2 left
πŸ’‘ Hint
Think about how base pointers can call different versions of functions depending on the actual object type.