Recall & Review
beginner
What is an interface in C++?
An interface in C++ is a class that has only pure virtual functions and no data members. It defines a contract that other classes can implement.Click to reveal answer
beginner
How do you declare a pure virtual function in C++?
You declare a pure virtual function by assigning 0 to the function declaration, like this:
virtual void func() = 0;.Click to reveal answer
beginner
Why can't you create an object of a class with pure virtual functions?Because such a class is abstract and incomplete. It requires derived classes to provide implementations for the pure virtual functions before objects can be created.Click to reveal answer
intermediate
How does C++ achieve interface-like behavior without a dedicated interface keyword?
By using abstract classes with only pure virtual functions, C++ simulates interfaces. Classes implementing the interface inherit and override these functions.
Click to reveal answer
intermediate
What is the role of a virtual destructor in an interface-like class?
A virtual destructor ensures that when deleting an object through a pointer to the interface, the derived class's destructor is called properly, avoiding resource leaks.
Click to reveal answer
Which of the following declares a pure virtual function in C++?
✗ Incorrect
Only 'virtual void foo() = 0;' declares a pure virtual function.
Can you instantiate an object of a class that has at least one pure virtual function?
✗ Incorrect
Classes with pure virtual functions are abstract and cannot be instantiated.
What keyword does C++ use to indicate a function is virtual?
✗ Incorrect
The 'virtual' keyword marks a function as virtual in C++.
Why should an interface-like class have a virtual destructor?
✗ Incorrect
A virtual destructor ensures proper cleanup of derived objects.
Which of these best describes an interface-like class in C++?
✗ Incorrect
Interface-like classes have only pure virtual functions and no data members.
Explain how C++ uses abstract classes to simulate interfaces.
Think about classes with functions set to zero.
You got /4 concepts.
Why is it important to have a virtual destructor in an interface-like class?
Consider what happens when deleting objects via base class pointers.
You got /4 concepts.