0
0
C++programming~5 mins

Pure virtual functions in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a pure virtual function in C++?
A pure virtual function is a function declared in a base class that has no implementation and is set to 0. It forces derived classes to provide their own implementation.
Click to reveal answer
beginner
How do you declare a pure virtual function in C++?
You declare a pure virtual function by adding = 0 at the end of the function declaration inside a class, for example: virtual void func() = 0;
Click to reveal answer
beginner
What happens if a class has at least one pure virtual function?
The class becomes abstract, meaning you cannot create objects of that class directly. You must derive a subclass and implement the pure virtual functions.
Click to reveal answer
intermediate
Why use pure virtual functions in a base class?
They define an interface that derived classes must follow, ensuring certain functions are implemented in subclasses, which helps design flexible and extendable programs.
Click to reveal answer
advanced
Can a pure virtual function have a body (implementation) in C++?
Yes, a pure virtual function can have a body, but it still makes the class abstract. Derived classes can call the base class implementation if needed.
Click to reveal answer
How do you mark a function as pure virtual in C++?
ABy adding = 0 after the function declaration
BBy using the keyword pure
CBy declaring the function static
DBy defining the function inside the class
What is true about a class with at least one pure virtual function?
AIt cannot have derived classes
BIt can be instantiated directly
CIt must have no other functions
DIt is called an abstract class
Can a pure virtual function have an implementation in C++?
ANo, it cannot have any implementation
BYes, but the class remains abstract
CYes, and the class becomes concrete
DOnly if it is static
Why would you use a pure virtual function?
ATo make the function private
BTo prevent inheritance
CTo force derived classes to implement the function
DTo allow multiple definitions
What keyword is used to declare a pure virtual function?
A= 0
Boverride
Cabstract
Dvirtual
Explain what a pure virtual function is and how it affects a class in C++.
Think about how pure virtual functions create a blueprint for subclasses.
You got /4 concepts.
    Describe why and when you would use pure virtual functions in your C++ programs.
    Consider how pure virtual functions help organize code and design.
    You got /4 concepts.