Bird
Raised Fist0
C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does a base class pointer in C++ allow you to do?
easy
A. Only point to base class objects
B. Point to objects of derived classes
C. Only point to primitive data types
D. Prevent any pointer arithmetic

Solution

  1. Step 1: Understand base and derived classes

    A base class is a general class, and derived classes extend it with more features.
  2. Step 2: Role of base class pointers

    A base class pointer can point to any object of the base class or its derived classes, enabling polymorphism.
  3. Final Answer:

    Point to objects of derived classes -> Option B
  4. Quick Check:

    Base pointer can point to derived objects [OK]
Hint: Base pointers can point to derived objects [OK]
Common Mistakes:
  • Thinking base pointers only point to base objects
  • Confusing pointers with references
  • Assuming base pointers can't access derived objects
2. Which of the following is the correct syntax to declare a base class pointer named ptr for a class Base?
easy
A. Base *ptr;
B. ptr Base*;
C. Base ptr*;
D. *Base ptr;

Solution

  1. Step 1: Recall pointer declaration syntax

    In C++, a pointer to a type is declared as Type *pointerName;.
  2. Step 2: Apply to Base class

    For class Base, the pointer declaration is Base *ptr;.
  3. Final Answer:

    Base *ptr; -> Option A
  4. Quick Check:

    Pointer syntax = Type *name [OK]
Hint: Pointer syntax is Type *name; [OK]
Common Mistakes:
  • Placing * after variable name incorrectly
  • Swapping type and variable name
  • Using invalid syntax like *Base ptr
3. Consider the code:
class Base {
public:
    virtual void show() { std::cout << "Base"; }
};
class Derived : public Base {
public:
    void show() override { std::cout << "Derived"; }
};

int main() {
    Base *ptr = new Derived();
    ptr->show();
    delete ptr;
    return 0;
}

What is the output when this code runs?
medium
A. Derived
B. Base
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Understand virtual function behavior

    The show() function is virtual in Base, so calls use the actual object's method.
  2. Step 2: Pointer points to Derived object

    The pointer ptr points to a Derived object, so Derived::show() is called.
  3. Final Answer:

    Derived -> Option A
  4. Quick Check:

    Virtual function calls Derived method [OK]
Hint: Virtual functions call derived version via base pointer [OK]
Common Mistakes:
  • Ignoring virtual keyword and expecting Base output
  • Confusing pointer type with object type
  • Assuming compilation or runtime error
4. What is wrong with this code snippet?
class Base {
public:
    void display() { std::cout << "Base"; }
};
class Derived : public Base {
public:
    void display() { std::cout << "Derived"; }
};

int main() {
    Base *ptr = new Derived();
    ptr->display();
    delete ptr;
    return 0;
}
medium
A. delete ptr causes memory leak
B. Pointer ptr cannot point to Derived object
C. Derived class must not redefine display()
D. display() should be declared virtual in Base for polymorphism

Solution

  1. Step 1: Check function overriding and polymorphism

    Without virtual, base class pointer calls base version even if pointing to derived object.
  2. Step 2: Identify missing virtual keyword

    To enable polymorphism, display() in Base must be declared virtual.
  3. Final Answer:

    display() should be declared virtual in Base for polymorphism -> Option D
  4. Quick Check:

    Missing virtual causes base method call [OK]
Hint: Declare base methods virtual for polymorphism [OK]
Common Mistakes:
  • Assuming pointer can't point to derived
  • Thinking delete causes leak here
  • Believing derived can't redefine base methods
5. You have a base class Animal with a virtual function sound(). Two derived classes Dog and Cat override sound(). You want to store multiple animals in an array and call sound() on each. Which approach correctly uses base class pointers to achieve this?
hard
A. Create an array of Animal objects and assign Dog and Cat objects directly
B. Create an array of Dog* and cast Cat* to Dog*
C. Create an array of Animal*, store new Dog() and new Cat(), then loop and call sound()
D. Use an array of void* pointers and cast to Animal* before calling sound()

Solution

  1. Step 1: Understand polymorphic storage

    To store different derived objects, use an array of base class pointers (Animal*).
  2. Step 2: Store derived objects via pointers

    Store new Dog() and new Cat() in the Animal* array, then call sound() polymorphically.
  3. Final Answer:

    Create an array of Animal*, store new Dog() and new Cat(), then loop and call sound() -> Option C
  4. Quick Check:

    Base pointer array + virtual functions = polymorphism [OK]
Hint: Use base class pointer array for polymorphic objects [OK]
Common Mistakes:
  • Using object array causes slicing
  • Casting unrelated pointers unsafely
  • Using void* loses type safety and polymorphism
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard