---
title: "Polymorphic class and RTTI mechanisms"
subject: "Object Oriented Programming"
chapter: 7
key: "semester_3/object_oriented_programming:polymorphic-class-rtti-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/polymorphic-class-rtti-program
site: BCT Engineering
---

# Polymorphic class and RTTI mechanisms

Object Oriented Programming · Chapter 7: Polymorphism and Dynamic Binding

- Asked in: 2068 Baisakh, 2+2+6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/polymorphic-class-rtti-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-7

## Question

What do you mean by polymorphic class? What are different RTTI mechanisms in C++? Write a program that shows both RTTI mechanisms.

## Short answer

A polymorphic class has at least one virtual function. RTTI uses dynamic_cast for safe downcasting (returns null if wrong type) and typeid for type comparison/name. Example: Animal* pointer to Dog or Cat; dynamic_cast<Dog*> demonstates safe Dog-specific calls.

## Answer

#### Polymorphic class
A class that has at least one virtual function (declared or inherited) is a **polymorphic class**. Its objects carry a hidden vptr, so the run-time type of an object referred to by a base pointer or reference can be found. RTTI works only for polymorphic classes.

#### RTTI mechanisms
Run-Time Type Information lets a program find the real type of an object at run time. C++ gives two mechanisms:

1. **`dynamic_cast`** – safely converts a base pointer/reference to a derived pointer/reference. For pointers it returns `NULL` if the object is not of that type (for references it throws `bad_cast`).
2. **`typeid` operator** – returns a `type_info` object (header `<typeinfo>`) describing the type of an expression. Its `name()` gives the type name and `==` compares types.

#### Program showing both

```cpp
#include <iostream>
#include <typeinfo>
using namespace std;

class Animal {                       // polymorphic: has virtual function
public:
    virtual void speak() { cout << "Animal sound\n"; }
    virtual ~Animal() {}
};
class Dog : public Animal {
public:
    void speak() { cout << "Dog barks\n"; }
    void fetch() { cout << "Dog fetches the ball\n"; }
};
class Cat : public Animal {
public:
    void speak() { cout << "Cat meows\n"; }
};

int main() {
    Animal *a[2] = { new Dog, new Cat };

    for (int i = 0; i < 2; i++) {
        // mechanism 1: typeid
        cout << "Type: " << typeid(*a[i]).name() << endl;
        if (typeid(*a[i]) == typeid(Dog))
            cout << "typeid says it is a Dog\n";

        // mechanism 2: dynamic_cast
        Dog *d = dynamic_cast<Dog*>(a[i]);
        if (d != NULL)
            d->fetch();              // safe: object really is a Dog
        else
            cout << "dynamic_cast failed: not a Dog\n";
    }
    for (int i = 0; i < 2; i++) delete a[i];
    return 0;
}
```

The `name()` string is compiler dependent (e.g. `3Dog` in GCC). For the Dog object both tests succeed; for the Cat object `dynamic_cast` returns `NULL`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is a polymorphic class?
   A. A class with multiple base classes
   B. A class with only public data
   C. A class with several constructors
   D. A class with at least one virtual function
   Answer: D. A class having at least one virtual function (declared or inherited) is polymorphic and carries a vptr. (Key point: Polymorphic class)

2. Which are the two RTTI mechanisms in C++?
   A. static_cast and const_cast
   B. dynamic_cast and typeid
   C. sizeof and typeid
   D. reinterpret_cast and static_cast
   Answer: B. RTTI is provided by dynamic_cast and the typeid operator. (Key point: RTTI mechanisms)

3. For the Cat object, what does dynamic_cast<Dog*> give in the program?
   A. A valid Dog pointer
   B. NULL, so it prints that the cast failed
   C. A bad_cast exception
   D. The program crashes
   Answer: B. A pointer dynamic_cast to the wrong type returns NULL, so the else branch runs. (Key point: Program outcome)

## Related in Object Oriented Programming

- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [RTTI with dynamic_cast and typeid](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Artti-dynamic-cast-typeid) — Object Oriented Programming, chapter 7, asked 4×
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Exception with argument and specification program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aexception-program-arg-specification) — Object Oriented Programming, chapter 10
- [Time class: add two objects](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-class-add-objects) — Object Oriented Programming, chapter 1
- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1

## Related in other subjects

- [Sum of both diagonals of matrix](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-both-diagonals) — Computer Programming, chapter 7
- [Stored program concept and processing cycle](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Astored-program-processing-cycle) — Microprocessor, chapter 1, asked 6×
- [Video rental shop class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Avideo-rental-class-diagram) — Object Oriented Analysis and Design, chapter 2
- [XYZ Marina class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Amarina-class-diagram) — Object Oriented Analysis and Design, chapter 2
