What if you could control many different things with just one simple pointer?
Why Base class pointers in C++? - Purpose & Use Cases
Imagine you have different types of vehicles like cars and bikes, and you want to keep track of them all in one list. Without base class pointers, you'd have to create separate lists for each type and write different code to handle each one.
This manual approach is slow and messy because you repeat similar code for each vehicle type. It's easy to make mistakes and hard to add new vehicle types later. Managing many separate lists and functions becomes confusing and error-prone.
Base class pointers let you store addresses of different derived objects in one place using a pointer to their common base class. This way, you can write one piece of code to handle all vehicle types, making your program cleaner and easier to maintain.
Car car; Bike bike; car.drive(); bike.ride();
Vehicle* v1 = &car; Vehicle* v2 = &bike; v1->move(); v2->move();
It enables writing flexible and reusable code that works with different object types through a single interface.
Think of a game where you have many characters like knights and archers. Using base class pointers, you can store all characters in one list and call their actions without worrying about their specific types.
Base class pointers allow handling different derived objects uniformly.
They reduce code duplication and simplify program design.
They make adding new types easier without changing existing code.
Practice
Solution
Step 1: Understand base and derived classes
A base class is a general class, and derived classes extend it with more features.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.Final Answer:
Point to objects of derived classes -> Option BQuick Check:
Base pointer can point to derived objects [OK]
- Thinking base pointers only point to base objects
- Confusing pointers with references
- Assuming base pointers can't access derived objects
ptr for a class Base?Solution
Step 1: Recall pointer declaration syntax
In C++, a pointer to a type is declared asType *pointerName;.Step 2: Apply to Base class
For classBase, the pointer declaration isBase *ptr;.Final Answer:
Base *ptr; -> Option AQuick Check:
Pointer syntax = Type *name [OK]
- Placing * after variable name incorrectly
- Swapping type and variable name
- Using invalid syntax like *Base ptr
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?
Solution
Step 1: Understand virtual function behavior
Theshow()function is virtual inBase, so calls use the actual object's method.Step 2: Pointer points to Derived object
The pointerptrpoints to aDerivedobject, soDerived::show()is called.Final Answer:
Derived -> Option AQuick Check:
Virtual function calls Derived method [OK]
- Ignoring virtual keyword and expecting Base output
- Confusing pointer type with object type
- Assuming compilation or runtime error
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;
}Solution
Step 1: Check function overriding and polymorphism
Withoutvirtual, base class pointer calls base version even if pointing to derived object.Step 2: Identify missing virtual keyword
To enable polymorphism,display()inBasemust be declaredvirtual.Final Answer:
display() should be declared virtual in Base for polymorphism -> Option DQuick Check:
Missing virtual causes base method call [OK]
- Assuming pointer can't point to derived
- Thinking delete causes leak here
- Believing derived can't redefine base methods
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?Solution
Step 1: Understand polymorphic storage
To store different derived objects, use an array of base class pointers (Animal*).Step 2: Store derived objects via pointers
Storenew Dog()andnew Cat()in theAnimal*array, then callsound()polymorphically.Final Answer:
Create an array of Animal*, store new Dog() and new Cat(), then loop and call sound() -> Option CQuick Check:
Base pointer array + virtual functions = polymorphism [OK]
- Using object array causes slicing
- Casting unrelated pointers unsafely
- Using void* loses type safety and polymorphism
Ready to prove it?
Pass the test and this pattern moves unknown
