Section 9 : Polymorphism , Virtual Flashcards
(22 cards)
What does “A pointer to a derived class is type compatible with a pointer to the base class” mean in C++?
It means that a Derived* can be assigned to a Base, because the derived class inherits from the base class. This allows base class pointers (e.g., Shape) to point to derived objects (e.g., Rectangle) and is essential for polymorphism.
Why can’t we store Rectangle or Triangle objects directly in a Shape array?
Because of object slicing — only the base part (Shape) would be stored, and derived-specific data would be lost.
Why can we store different types (Rectangle, Triangle) in a Shape* array?
Because a pointer to a derived class is type-compatible with a pointer to its base class, and all pointers are uniform in size.
Why is it important that all pointers have the same size?
It allows storing them uniformly in arrays or containers, regardless of the actual object type they point to.
What does storing a Shape* actually store?
It stores the memory address of an object (e.g., Rectangle, Triangle) that lives on the heap.
What happens if you try to store a Rectangle in a Shape variable directly?
The compiler allows it, but it slices off the Rectangle-specific parts — breaking polymorphism.
Can you achieve polymorphism without using pointers or references?
❌ No. You must use pointers (Shape*) or references (Shape&) for virtual function dispatch to work.
What is a derived class?
A class that inherits from a base class using : and an access specifier, e.g. class Rectangle : public Shape.
What is uniform storage in polymorphism?
Storing different derived objects (Rectangle, Triangle, etc.) using base class pointers (Shape*) in an array or container.
Why is uniform storage necessary?
Because derived classes can be larger than the base class. Direct storage causes object slicing; storing Shape* pointers avoids this.
What is object slicing? (this was not mentionned but it is an addition)
When you assign a derived object to a base class variable, and only the base part is stored — the rest is “sliced off”.
What is uniform handling in polymorphism?
Uniform handling means you can use the same function call (e.g. printArea()) on different derived objects (Rectangle, Triangle, etc.) through a base class pointer or reference, and the correct overridden method is called automatically at runtime — thanks to virtual functions.
What is the syntax to declare a virtual function in C++?
class Shape {
public:
virtual int area();
};
Can you override a non-virtual function and expect polymorphic behavior?
❌ No. Without virtual, the base version is always called through base pointers.
What happens if a base class method is not marked virtual and you override it in a derived class?
The overridden method in the derived class is ignored if called through a base class pointer — the base method is used instead.
What does virtual do in a base class function?
It enables dynamic dispatch, meaning the version of the function from the actual object’s class (not the pointer type) is called at runtime.
What is the main purpose of polymorphism in C++?
To allow different derived types (like Rectangle, Triangle) to be treated uniformly through a common base type (e.g., Shape), enabling consistent function calls and storage.
Why do we define a base class like Shape in polymorphism?
Not to represent a real object, but to act as a shared interface or type category that groups all derived types under one name.
Why should base classes like Shape not be instantiated directly?
Because they represent a general concept, not a real, concrete object. Instantiating them breaks the abstraction.
How do we prevent base classes from being instantiated?
By declaring at least one pure virtual function (e.g., virtual int area() = 0;) — making the class abstract.
What does an abstract class represent in polymorphism?
A class that cannot be instantiated and is used to define a common interface for related derived classes.