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
✗ Incorrect
A pure virtual function is declared by adding = 0 after its declaration 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
✗ Incorrect
A class with at least one pure virtual function is abstract and cannot be instantiated directly.
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
✗ Incorrect
A pure virtual function can have a body, but the class remains abstract.
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
✗ Incorrect
Pure virtual functions force derived classes to provide their own implementation.
What keyword is used to declare a pure virtual function?
A= 0
Boverride
Cabstract
Dvirtual
✗ Incorrect
The = 0 syntax marks a function as pure virtual.
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.
Practice
(1/5)
1. What is the main purpose of a pure virtual function in C++?
easy
A. To force derived classes to implement the function
B. To allow creating objects of the base class
C. To provide a default implementation in the base class
D. To make the function private
Solution
Step 1: Understand pure virtual function role
A pure virtual function is declared with = 0 and has no body in the base class.
Step 2: Effect on derived classes
Derived classes must provide their own implementation of this function, or they become abstract too.
Final Answer:
To force derived classes to implement the function -> Option A
Quick Check:
Pure virtual function = forces implementation [OK]
Hint: Pure virtual means must override in child class [OK]
Common Mistakes:
Thinking pure virtual allows base class objects
Assuming pure virtual provides default code
Confusing pure virtual with private functions
2. Which of the following is the correct syntax to declare a pure virtual function named draw in a class?
easy
A. void draw() = 0;
B. virtual void draw() = 0;
C. virtual void draw() {}
D. void virtual draw() = 0;
Solution
Step 1: Recall pure virtual function syntax
Pure virtual functions use the keyword virtual and end with = 0;.
Step 2: Check each option
virtual void draw() = 0; matches the correct syntax: virtual void draw() = 0;. Others have wrong order, missing virtual, or provide a body.
Final Answer:
virtual void draw() = 0; -> Option B
Quick Check:
Syntax = virtual + = 0 [OK]
Hint: Pure virtual = virtual + = 0 at end [OK]
Common Mistakes:
Placing 'virtual' after return type incorrectly
Adding function body {} for pure virtual
Omitting 'virtual' keyword
3. What will be the output of the following code?
#include <iostream>
class Shape {
public:
virtual void draw() = 0;
};
class Circle : public Shape {
public:
void draw() { std::cout << "Circle drawn" << std::endl; }
};
int main() {
Circle c;
c.draw();
return 0;
}
medium
A. No output
B. Compilation error: cannot instantiate abstract class
C. Circle drawn
D. Runtime error
Solution
Step 1: Analyze class Shape
Shape has a pure virtual function draw(), so Shape is abstract and cannot be instantiated.
Step 2: Analyze class Circle
Circle overrides draw() with its own implementation, so Circle is concrete and can be instantiated.
Step 3: Check main function
Circle object c is created and c.draw() prints "Circle drawn".
Final Answer:
Circle drawn -> Option C
Quick Check:
Derived class implements pure virtual = prints output [OK]
Hint: Derived class must implement to create objects [OK]
Common Mistakes:
Trying to instantiate abstract base class
Forgetting to override pure virtual function
Expecting no output due to pure virtual
4. Identify the error in this code snippet:
class Animal {
public:
virtual void sound() = 0;
};
class Dog : public Animal {
};
int main() {
Dog d;
d.sound();
return 0;
}
medium
A. No error, code runs fine
B. Animal class should not have pure virtual functions
C. Dog object cannot be created because main is missing
D. Dog class does not implement pure virtual function sound()
Solution
Step 1: Check pure virtual function in Animal
Animal declares sound() as pure virtual, so Animal is abstract.
Step 2: Check Dog class implementation
Dog inherits Animal but does not implement sound(), so Dog remains abstract and cannot be instantiated.
Step 3: Analyze main function
Trying to create Dog object d causes compilation error because Dog is abstract.
Final Answer:
Dog class does not implement pure virtual function sound() -> Option D
Quick Check:
Abstract class must implement all pure virtual functions [OK]
Hint: Derived must implement all pure virtual functions [OK]
Common Mistakes:
Assuming base class cannot have pure virtual functions
Thinking missing main causes error here
Believing code runs without overriding pure virtual
5. Given the following classes, which statement is true?
#include <iostream>
class Base {
public:
virtual void f() = 0;
virtual void g() { std::cout << "Base g" << std::endl; }
};
class Derived : public Base {
public:
void f() override { std::cout << "Derived f" << std::endl; }
void g() override { std::cout << "Derived g" << std::endl; }
};
int main() {
Base* b = new Derived();
b->f();
b->g();
delete b;
return 0;
}
hard
A. Output is:
Derived f
Derived g
B. Output is:
Base f
Base g
C. Compilation error because Base has pure virtual function
D. Runtime error due to calling pure virtual function
Solution
Step 1: Understand Base class
Base has pure virtual f() and virtual g() with implementation.
Step 2: Analyze Derived class
Derived overrides both f() and g() with its own implementations.
Step 3: Check main function calls
Pointer b of type Base* points to Derived object. Virtual calls invoke Derived versions of f() and g().
Final Answer:
Output is:
Derived f
Derived g -> Option A
Quick Check:
Virtual functions call derived overrides [OK]
Hint: Virtual calls use derived class methods [OK]
Common Mistakes:
Expecting base class methods to run
Thinking pure virtual causes compile error here
Confusing pure virtual with runtime error
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.