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;
✗ 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?
ANo, never.
BOnly if the class has a constructor.
COnly if the pure virtual function has a body.
DYes, always.
✗ Incorrect
Classes with pure virtual functions are abstract and cannot be instantiated.
What keyword does C++ use to indicate a function is virtual?
Avirtual
Binterface
Cabstract
Doverride
✗ Incorrect
The 'virtual' keyword marks a function as virtual in C++.
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.
✗ Incorrect
A virtual destructor ensures proper cleanup of derived objects.
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.
✗ 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.
Practice
(1/5)
1. What is the main purpose of an interface-like class in C++?
easy
A. To store data members for derived classes
B. To provide full function implementations for reuse
C. To define functions that derived classes must implement
D. To create objects directly without inheritance
Solution
Step 1: Understand interface concept
Interfaces specify what functions a class must have but do not provide their code.
Step 2: Identify interface role in C++
In C++, interfaces are created using pure virtual functions that derived classes must implement.
Final Answer:
To define functions that derived classes must implement -> Option C
Quick Check:
Interface = function declarations only [OK]
Hint: Interfaces declare required functions without code [OK]
Common Mistakes:
Thinking interfaces provide function code
Assuming interfaces hold data members
Believing interfaces can be instantiated
2. Which of the following is the correct syntax to declare a pure virtual function in a C++ class?
easy
A. virtual void func() = 0;
B. virtual void func();
C. void func() override;
D. virtual void func() = 1;
Solution
Step 1: Recall pure virtual function syntax
Pure virtual functions are declared with '= 0' after the function signature.
Step 2: Check options for correct syntax
virtual void func() = 0; uses '= 0' correctly, others do not match pure virtual syntax.
Final Answer:
virtual void func() = 0; -> Option A
Quick Check:
Pure virtual = '= 0' [OK]
Hint: Pure virtual functions end with '= 0;' [OK]
Common Mistakes:
Using '= 1' instead of '= 0'
Omitting 'virtual' keyword
Confusing override with pure virtual
3. Consider the following code:
class IShape {
public:
virtual double area() const = 0;
};
class Circle : public IShape {
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override { return 3.14 * radius * radius; }
};
int main() {
Circle c(2);
std::cout << c.area();
return 0;
}
What is the output of this program?
medium
A. 4
B. Compilation error
C. 3.14
D. 12.56
Solution
Step 1: Calculate area of circle with radius 2
Area = π * r² = 3.14 * 2 * 2 = 12.56
Step 2: Confirm code compiles and runs correctly
Circle implements pure virtual function area(), so no error; output is area value.
Final Answer:
12.56 -> Option D
Quick Check:
Area = 3.14 * 4 = 12.56 [OK]
Hint: Calculate area formula carefully [OK]
Common Mistakes:
Forgetting to implement pure virtual function
Using wrong radius value
Expecting compilation error
4. What is wrong with this code snippet?
class IAnimal {
public:
virtual void speak() = 0;
};
class Dog : public IAnimal {
public:
void speak() { std::cout << "Woof"; }
};
int main() {
IAnimal a;
a.speak();
return 0;
}
medium
A. Missing override keyword in Dog::speak()
B. Cannot create object of abstract class IAnimal
C. speak() should return a value
D. Dog class must be abstract
Solution
Step 1: Identify abstract class usage
IAnimal has a pure virtual function, so it is abstract and cannot be instantiated.
Step 2: Check main() for object creation
Code tries to create IAnimal a; which is illegal and causes compilation error.
Final Answer:
Cannot create object of abstract class IAnimal -> Option B
Quick Check:
Abstract class objects cannot be created [OK]
Hint: Abstract classes cannot be instantiated [OK]
Common Mistakes:
Ignoring abstract class instantiation rules
Thinking override keyword is mandatory
Assuming pure virtual functions must return values
5. You want to create an interface-like class IPrinter with a pure virtual function print(). Then create two classes ConsolePrinter and FilePrinter that implement print(). Which of the following code snippets correctly demonstrates this?
hard
A. class IPrinter {
public:
virtual void print() = 0;
};
class ConsolePrinter : public IPrinter {
public:
void print() override { std::cout << "Console output"; }
};
class FilePrinter : public IPrinter {
public:
void print() override { /* write to file */ }
};
B. class IPrinter {
public:
void print() { }
};
class ConsolePrinter : public IPrinter {
public:
void print() { std::cout << "Console output"; }
};
C. class IPrinter {
public:
virtual void print();
};
class ConsolePrinter : public IPrinter {
};
D. class IPrinter {
public:
virtual void print() = 1;
};
Solution
Step 1: Define interface with pure virtual function
IPrinter declares print() as pure virtual with '= 0', making it an interface.
Step 2: Implement print() in derived classes
ConsolePrinter and FilePrinter override print() with their own code, fulfilling interface contract.
Final Answer:
Code snippet D correctly defines interface and implementations -> Option A
Quick Check:
Pure virtual = 0 + override implementations [OK]
Hint: Use '= 0' for pure virtual and override in derived [OK]
Common Mistakes:
Using '= 1' instead of '= 0' for pure virtual
Not implementing pure virtual functions in derived classes
Defining interface functions with bodies
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.