Pure virtual functions in C++ - Time & Space Complexity
Let's explore how time complexity relates to pure virtual functions in C++.
We want to see how the program's running time changes when using pure virtual functions.
Analyze the time complexity of the following code snippet.
#include <vector>
class Shape {
public:
virtual void draw() = 0; // pure virtual function
};
class Circle : public Shape {
public:
void draw() override {
// drawing circle code
}
};
void renderShapes(std::vector<Shape*> shapes) {
for (auto shape : shapes) {
shape->draw();
}
}
This code defines a pure virtual function draw() in a base class and calls it on a list of shapes.
Look for loops or repeated calls that affect time.
- Primary operation: Calling
draw()on each shape pointer. - How many times: Once per shape in the list, so as many times as the list size.
As the number of shapes grows, the number of draw() calls grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to draw() |
| 100 | 100 calls to draw() |
| 1000 | 1000 calls to draw() |
Pattern observation: The work grows directly with the number of shapes.
Time Complexity: O(n)
This means the time to run increases linearly as you add more shapes to draw.
[X] Wrong: "Using pure virtual functions makes the program slower by a lot because of virtual calls."
[OK] Correct: While virtual calls add a small overhead, the main time cost comes from how many times you call the function, not the virtual call itself.
Understanding how virtual functions affect time helps you explain design choices clearly and shows you know how code structure impacts performance.
"What if the draw() function called another loop inside it? How would that change the time complexity?"
Practice
pure virtual function in C++?Solution
Step 1: Understand pure virtual function role
A pure virtual function is declared with= 0and 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 AQuick Check:
Pure virtual function = forces implementation [OK]
- Thinking pure virtual allows base class objects
- Assuming pure virtual provides default code
- Confusing pure virtual with private functions
draw in a class?Solution
Step 1: Recall pure virtual function syntax
Pure virtual functions use the keywordvirtualand 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 BQuick Check:
Syntax = virtual + = 0 [OK]
- Placing 'virtual' after return type incorrectly
- Adding function body {} for pure virtual
- Omitting 'virtual' keyword
#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;
}Solution
Step 1: Analyze class Shape
Shape has a pure virtual functiondraw(), so Shape is abstract and cannot be instantiated.Step 2: Analyze class Circle
Circle overridesdraw()with its own implementation, so Circle is concrete and can be instantiated.Step 3: Check main function
Circle objectcis created andc.draw()prints "Circle drawn".Final Answer:
Circle drawn -> Option CQuick Check:
Derived class implements pure virtual = prints output [OK]
- Trying to instantiate abstract base class
- Forgetting to override pure virtual function
- Expecting no output due to pure virtual
class Animal {
public:
virtual void sound() = 0;
};
class Dog : public Animal {
};
int main() {
Dog d;
d.sound();
return 0;
}Solution
Step 1: Check pure virtual function in Animal
Animal declaressound()as pure virtual, so Animal is abstract.Step 2: Check Dog class implementation
Dog inherits Animal but does not implementsound(), so Dog remains abstract and cannot be instantiated.Step 3: Analyze main function
Trying to create Dog objectdcauses compilation error because Dog is abstract.Final Answer:
Dog class does not implement pure virtual function sound() -> Option DQuick Check:
Abstract class must implement all pure virtual functions [OK]
- Assuming base class cannot have pure virtual functions
- Thinking missing main causes error here
- Believing code runs without overriding pure virtual
#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;
}Solution
Step 1: Understand Base class
Base has pure virtualf()and virtualg()with implementation.Step 2: Analyze Derived class
Derived overrides bothf()andg()with its own implementations.Step 3: Check main function calls
Pointerbof type Base* points to Derived object. Virtual calls invoke Derived versions off()andg().Final Answer:
Output is: Derived f Derived g -> Option AQuick Check:
Virtual functions call derived overrides [OK]
- Expecting base class methods to run
- Thinking pure virtual causes compile error here
- Confusing pure virtual with runtime error
Ready to prove it?
Pass the test and this pattern moves unknown
