0
0
C++programming~5 mins

Abstract classes in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an abstract class in C++?
An abstract class is a class that cannot be instantiated on its own and usually contains at least one pure virtual function. It serves as a blueprint for other classes.
Click to reveal answer
beginner
What is a pure virtual function?
A pure virtual function is a function declared by assigning 0 in its declaration (e.g., virtual void func() = 0;). It must be overridden by derived classes.
Click to reveal answer
beginner
Why can't you create an object of an abstract class?
Because abstract classes have pure virtual functions without implementations, the compiler prevents creating objects to ensure derived classes provide those implementations.
Click to reveal answer
beginner
How do you declare a pure virtual function in C++?
By adding = 0 at the end of the function declaration inside the class, for example: virtual void draw() = 0;.
Click to reveal answer
intermediate
What happens if a derived class does not override all pure virtual functions?
The derived class also becomes abstract and cannot be instantiated until it overrides all pure virtual functions from its base classes.
Click to reveal answer
Which keyword is used to declare a pure virtual function in C++?
A= 0
Bvirtual
Cabstract
Doverride
Can you create an object of an abstract class?
AYes, always
BNo, never
COnly if it has no pure virtual functions
DOnly if derived classes exist
What must a derived class do if its base class has a pure virtual function?
AMake the function private
BIgnore the pure virtual function
CDelete the pure virtual function
DOverride the pure virtual function
What happens if a derived class does not override a pure virtual function?
AIt causes a runtime error
BIt compiles without errors
CIt becomes an abstract class
DIt overrides automatically
Which of these is true about abstract classes?
AThey can have implemented functions
BThey cannot have any functions
CThey must be final classes
DThey are always empty
Explain what an abstract class is and why it is useful in C++.
Think about classes that define rules but don't create objects directly.
You got /4 concepts.
    Describe how pure virtual functions enforce behavior in derived classes.
    Consider how a base class can require derived classes to provide specific functions.
    You got /4 concepts.