Section 9 : Polymorphism , Virtual Flashcards

(22 cards)

1
Q

What does “A pointer to a derived class is type compatible with a pointer to the base class” mean in C++?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why can’t we store Rectangle or Triangle objects directly in a Shape array?

A

Because of object slicing — only the base part (Shape) would be stored, and derived-specific data would be lost.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why can we store different types (Rectangle, Triangle) in a Shape* array?

A

Because a pointer to a derived class is type-compatible with a pointer to its base class, and all pointers are uniform in size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is it important that all pointers have the same size?

A

It allows storing them uniformly in arrays or containers, regardless of the actual object type they point to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does storing a Shape* actually store?

A

It stores the memory address of an object (e.g., Rectangle, Triangle) that lives on the heap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens if you try to store a Rectangle in a Shape variable directly?

A

The compiler allows it, but it slices off the Rectangle-specific parts — breaking polymorphism.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can you achieve polymorphism without using pointers or references?

A

❌ No. You must use pointers (Shape*) or references (Shape&) for virtual function dispatch to work.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a derived class?

A

A class that inherits from a base class using : and an access specifier, e.g. class Rectangle : public Shape.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is uniform storage in polymorphism?

A

Storing different derived objects (Rectangle, Triangle, etc.) using base class pointers (Shape*) in an array or container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why is uniform storage necessary?

A

Because derived classes can be larger than the base class. Direct storage causes object slicing; storing Shape* pointers avoids this.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is object slicing? (this was not mentionned but it is an addition)

A

When you assign a derived object to a base class variable, and only the base part is stored — the rest is “sliced off”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is uniform handling in polymorphism?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the syntax to declare a virtual function in C++?

A

class Shape {
public:
virtual int area();
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can you override a non-virtual function and expect polymorphic behavior?

A

❌ No. Without virtual, the base version is always called through base pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happens if a base class method is not marked virtual and you override it in a derived class?

A

The overridden method in the derived class is ignored if called through a base class pointer — the base method is used instead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does virtual do in a base class function?

A

It enables dynamic dispatch, meaning the version of the function from the actual object’s class (not the pointer type) is called at runtime.

17
Q

What is the main purpose of polymorphism in C++?

A

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.

18
Q

Why do we define a base class like Shape in polymorphism?

A

Not to represent a real object, but to act as a shared interface or type category that groups all derived types under one name.

19
Q

Why should base classes like Shape not be instantiated directly?

A

Because they represent a general concept, not a real, concrete object. Instantiating them breaks the abstraction.

20
Q

How do we prevent base classes from being instantiated?

A

By declaring at least one pure virtual function (e.g., virtual int area() = 0;) — making the class abstract.

21
Q

What does an abstract class represent in polymorphism?

A

A class that cannot be instantiated and is used to define a common interface for related derived classes.