Complete the code to declare a virtual function in the base class.
class Base { public: virtual void [1]() { std::cout << "Base function" << std::endl; } };
The function name display is used as the virtual function in the base class.
Complete the code to override the virtual function in the derived class.
class Derived : public Base { public: void [1]() override { std::cout << "Derived function" << std::endl; } };
override keyword.The derived class overrides the display function declared virtual in the base class.
Fix the error in the code to call the derived class function through a base class pointer.
Base* ptr = new Derived();
ptr->[1]();Calling display on the base class pointer invokes the derived class version due to virtual function.
Fill both blanks to define a base class pointer and call the virtual function.
Base* [1] = new [2](); [1]->display();
The pointer ptr of type Base* points to a new Derived object to demonstrate runtime polymorphism.
Fill all three blanks to create a vector of base class pointers and call the virtual function on each.
#include <vector> #include <iostream> std::vector<Base*> [1]; [1].push_back(new [2]()); [1].push_back(new [3]()); for (auto ptr : [1]) { ptr->display(); }
The vector named objects holds pointers to Derived objects. Calling display on each pointer demonstrates runtime polymorphism.