Multiple choice

What is the output of the following program?

#include <iostream> using namespace std;

class Base { public: virtual void fun() { cout << “Base Class” << endl; } }; class Derived : public Base { public: void fun() { cout << “Derived Class” << endl; } }; int main() { Derived objD; Base *ptrB = &objD; ptrB->fun(); }

  1. Base Class

  2. Derived Class

  3. No output. A Compilation Error

  4. No output. A Runtime Error

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Runtime binding of a virtual function based on the object the pointer is pointing to. This is called Runtime Type Identification (RTTI).