Complete the code to declare an abstract class with a pure virtual function.
class Shape { public: virtual void draw() [1]; };
= 0 makes the function not pure virtual.{} defines a normal function, not pure virtual.In C++, a pure virtual function is declared by assigning = 0 to the function declaration inside the class.
Complete the code to prevent instantiation of the abstract class.
Shape s; // Error: cannot instantiate abstract class class Shape { public: virtual void draw() = [1]; };
= 1 or other values instead of = 0.Assigning = 0 to a virtual function makes the class abstract, so it cannot be instantiated.
Fix the error in the derived class by correctly overriding the pure virtual function.
class Circle : public Shape { public: void draw() [1] { // draw circle } };
override keyword is allowed but not recommended.The derived class must override the pure virtual function with the exact signature and use override keyword for clarity.
Fill both blanks to declare an abstract class with a pure virtual destructor and a pure virtual function.
class Interface { public: virtual ~Interface() [1]; virtual void execute() [2]; };
{} instead of = 0 does not make the function pure virtual.Both the destructor and the function are declared pure virtual by assigning = 0.
Fill all three blanks to implement a derived class that overrides all pure virtual functions and destructor.
class Derived : public Interface { public: ~Derived() [1] override {} void execute() [2] override { // implementation } void extra() [3] { // extra method } };
override keyword can cause silent errors.All overridden functions must match the base class signature exactly, which is () with no parameters.