Complete the code to declare a base class pointer.
Base* [1];We declare a pointer named ptr to the base class Base.
Complete the code to assign a derived class object to a base class pointer.
Derived d;
Base* ptr = [1];We assign the address of the derived object d to the base class pointer ptr.
Fix the error in the virtual function declaration to enable polymorphism.
class Base { public: virtual void show() [1] };
The virtual function must have a function body or be declared pure virtual. Here, we provide an empty body with {}.
Fill both blanks to call the correct overridden function using polymorphism.
Base* ptr = new Derived(); ptr->[1](); delete [2];
We call the show function through the base pointer to invoke the derived version. Then we delete the pointer ptr to free memory.
Fill all three blanks to implement polymorphism with virtual functions and base pointer.
class Base { public: virtual void [1]() [2] }; class Derived : public Base { public: void [3]() override { std::cout << "Derived show" << std::endl; } };
The virtual function show is declared with an empty body {} in the base class and overridden in the derived class.