Abstract classes let you create a base blueprint that other classes must follow. They help organize code by forcing certain functions to be made in child classes.
Abstract classes in C++
class AbstractClassName { public: virtual void pureVirtualFunction() = 0; // pure virtual function void normalFunction() { // some code } virtual ~AbstractClassName() {} };
A pure virtual function is declared by adding = 0 at the end.
Abstract classes cannot be instantiated directly.
Shape forces all shapes to have a draw function.class Shape { public: virtual void draw() = 0; // must be implemented by child classes virtual ~Shape() {} };
Circle implements the required draw function.class Circle : public Shape { public: void draw() override { // code to draw a circle } };
draw must be defined.class EmptyShape : public Shape { public: void draw() override { // empty implementation } };
This program shows an abstract class Animal with a pure virtual function makeSound. The classes Dog and Cat implement this function. Trying to create an Animal object directly would cause an error.
#include <iostream> class Animal { public: virtual void makeSound() = 0; // pure virtual function virtual ~Animal() {} }; class Dog : public Animal { public: void makeSound() override { std::cout << "Woof!" << std::endl; } }; class Cat : public Animal { public: void makeSound() override { std::cout << "Meow!" << std::endl; } }; int main() { // Animal animal; // Error: cannot instantiate abstract class Dog dog; Cat cat; std::cout << "Dog says: "; dog.makeSound(); std::cout << "Cat says: "; cat.makeSound(); return 0; }
Time complexity depends on the implementation of the functions in child classes.
Abstract classes use a small amount of extra memory for the virtual table pointer.
Common mistake: Trying to create an object of the abstract class directly causes a compile error.
Use abstract classes when you want to enforce a contract for subclasses, instead of using regular classes or interfaces alone.
Abstract classes define functions that child classes must implement.
They cannot be instantiated directly.
They help organize code and enforce rules for subclasses.
Practice
Solution
Step 1: Understand abstract class instantiation rules
Abstract classes have at least one pure virtual function, so they cannot create objects directly.Step 2: Check other options
The other options are incorrect because abstract classes can have member functions and data members.Final Answer:
It cannot be instantiated directly. -> Option BQuick Check:
Abstract class = no direct objects [OK]
- Thinking abstract classes can create objects
- Believing abstract classes cannot have data members
- Confusing abstract classes with interfaces
Solution
Step 1: Recall pure virtual function syntax
Pure virtual functions are declared with 'virtual' keyword and '= 0' at the end.Step 2: Analyze options
virtual void func() = 0; matches the correct syntax. void func() = 0; misses 'virtual'. virtual void func(); is a normal virtual function. void func() {}; is a normal function with a body.Final Answer:
virtual void func() = 0; -> Option AQuick Check:
Pure virtual = virtual + '= 0' [OK]
- Omitting 'virtual' keyword
- Adding function body to pure virtual
- Confusing normal virtual with pure virtual
#include <iostream>
class Base {
public:
virtual void show() = 0;
};
class Derived : public Base {
public:
void show() { std::cout << "Hello"; }
};
int main() {
Derived d;
d.show();
return 0;
}Solution
Step 1: Check abstract class and derived implementation
Base has a pure virtual function 'show'. Derived overrides it with a concrete implementation.Step 2: Analyze main function output
Derived object 'd' calls 'show()', which prints "Hello".Final Answer:
Hello -> Option CQuick Check:
Derived implements pure virtual, so prints output [OK]
- Expecting compilation error without implementation
- Thinking pure virtual functions run by themselves
- Confusing output with no output
#include <iostream>
class Abstract {
public:
virtual void display() = 0;
};
class Concrete : public Abstract {
public:
void display() { std::cout << "Hi"; }
};
int main() {
Abstract a;
Concrete c;
c.display();
return 0;
}Solution
Step 1: Check instantiation of abstract class
Abstract class 'Abstract' has a pure virtual function, so cannot create object 'a'.Step 2: Verify other parts
Concrete implements display(), so no error there. Syntax is correct.Final Answer:
Cannot instantiate abstract class 'Abstract' directly. -> Option AQuick Check:
Abstract objects not allowed [OK]
- Trying to instantiate abstract class
- Ignoring pure virtual function rules
- Assuming implementation missing in derived
Solution
Step 1: Understand requirement for enforcing area() implementation
Using a pure virtual function in an abstract class forces all derived classes to implement area().Step 2: Evaluate options
Create an abstract class Shape with a pure virtual function double area() = 0; and derive specific shapes implementing area(). uses pure virtual function, which is the best way. Create a class Shape with a normal function double area() that returns 0, and override it in derived classes. returns 0 by default, which may hide missing implementations. Create a class Shape with no functions and add area() only in derived classes. does not enforce area() in base. Create a class Shape with a virtual function double area() with empty body. has virtual but not pure, so no enforcement.Final Answer:
Create an abstract class Shape with a pure virtual function double area() = 0; and derive specific shapes implementing area(). -> Option DQuick Check:
Pure virtual enforces implementation [OK]
- Using normal virtual instead of pure virtual
- Not enforcing method in base class
- Returning default values hiding missing overrides
Ready to prove it?
Pass the test and this pattern moves unknown
