0
0
C++programming~5 mins

Interface-like behavior in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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++?
Avoid foo() {}
Bvirtual void foo() = 0;
Cvirtual void foo();
Dvoid foo() = 0;
Can you instantiate an object of a class that has at least one pure virtual function?
ANo, never.
BOnly if the class has a constructor.
COnly if the pure virtual function has a body.
DYes, always.
What keyword does C++ use to indicate a function is virtual?
Avirtual
Binterface
Cabstract
Doverride
Why should an interface-like class have a virtual destructor?
ATo make all functions pure virtual.
BTo prevent the class from being instantiated.
CTo allow derived class destructors to run correctly when deleting via base pointer.
DTo improve performance.
Which of these best describes an interface-like class in C++?
AA class with private constructors.
BA class with no functions.
CA class with only static functions.
DA class with 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.