---
title: "Pure virtual function and abstract class"
subject: "Object Oriented Programming"
chapter: 7
key: "semester_3/object_oriented_programming:pure-virtual-abstract-class"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/pure-virtual-abstract-class
site: BCT Engineering
---

# Pure virtual function and abstract class

Object Oriented Programming · Chapter 7: Polymorphism and Dynamic Binding

- Asked in: 2074 Chaitra, 3+5 marks; 2074 Asoj, 5 marks; 2070 Asar, 2+2 marks; 2070 Chaitra, 2 marks; 2068 Chaitra, 2 marks (asked 5 times)
- Video: [Abstract Class and Pure Virtual function in  C++ -51](https://www.youtube.com/watch?v=SvesRBYu65k) — Codearchery
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/pure-virtual-abstract-class
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-7

## Question

What is pure virtual function and abstract class? With suitable example explain run time polymorphism.

## Short answer

A pure virtual function (declared with = 0) has no implementation in base, making the class abstract (non-instantiable). Overriding it in derived classes enables run-time polymorphism: a base pointer calls the derived function, determined dynamicly at program execution.

## Answer

#### Pure virtual function
A **pure virtual function** is a virtual function with no body in the base class. It is declared by writing `= 0`:

```cpp
virtual void draw() = 0;
```
It forces every non-abstract derived class to provide its own definition.

#### Abstract class
A class that has at least one pure virtual function is an **abstract class**. Objects of an abstract class cannot be created, but pointers and references to it can be. It is used as a base class that defines an interface. A derived class that does not override all pure virtual functions is also abstract.

#### Run-time polymorphism
It means the function to be called is decided at **run time** from the actual type of the object that the base pointer points to. It needs (a) inheritance, (b) a virtual function overridden in the derived class, (c) a base class pointer or reference.

```cpp
#include <iostream>
using namespace std;

class Shape {                               // abstract class
public:
    virtual void area() = 0;                // pure virtual function
};

class Circle : public Shape {
    float r;
public:
    Circle(float x) { r = x; }
    void area() { cout << "Circle area = " << 3.14159 * r * r << endl; }
};

class Rect : public Shape {
    float l, b;
public:
    Rect(float x, float y) { l = x; b = y; }
    void area() { cout << "Rectangle area = " << l * b << endl; }
};

int main() {
    // Shape s;                // error: cannot create abstract object
    Shape *p;
    Circle c(2);
    Rect r(3, 4);

    p = &c;  p->area();        // Circle::area  (decided at run time)
    p = &r;  p->area();        // Rect::area
    return 0;
}
```

**Output:**

```text
Circle area = 12.5664
Rectangle area = 12
```

The same call `p->area()` runs different code depending on the object `p` points to. This is run-time polymorphism (dynamic binding), implemented through the virtual table (vtable).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is a pure virtual function declared?
   A. pure void draw();
   B. virtual void draw() = 0;
   C. virtual void draw() {}
   D. void draw() = virtual;
   Answer: B. A pure virtual function has no body and is declared with = 0. (Key point: Pure virtual syntax)

2. What can you do with an abstract class like Shape?
   A. Create objects of it directly
   B. Create pointers and references to it, but not objects
   C. Do nothing with it, not even create pointers
   D. Create objects only inside main()
   Answer: B. Objects of an abstract class cannot be created, but pointers and references can, which enables polymorphism. (Key point: Abstract class restriction)

3. What if a derived class does not override all pure virtual functions of its base?
   A. The compiler deletes the function
   B. It becomes a concrete class with default behaviour
   C. It becomes abstract too
   D. The program compiles and uses the base body
   Answer: C. A derived class that leaves any pure virtual function undefined remains abstract. (Key point: Derived class not overriding)

4. With Shape *p pointing first to a Circle and then to a Rect, why does p->area() give different results?
   A. The pointer type changes
   B. The function is chosen at compile time
   C. The function is chosen at run time from the actual object via the vtable
   D. Both area() functions run together
   Answer: C. Because area() is virtual, the call is bound at run time to the actual object's version. (Key point: Run-time polymorphism)

## Related in Object Oriented Programming

- [Polygon abstract class with Area()](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apolygon-abstract-area) — Object Oriented Programming, chapter 7
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [Abstract class with example](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aabstract-class-example) — Object Oriented Programming, chapter 7
- [Need of virtual base class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-base-class) — Object Oriented Programming, chapter 6
- [Need of virtual function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-function-need) — Object Oriented Programming, chapter 7, asked 4×
- [Member function overriding with virtual function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-member-override-reason) — Object Oriented Programming, chapter 7

## Related in other subjects

- [Quarter-load heat run and 30% overload time](https://www.bctengineer.com/question.md?key=semester_5%2Felectric_machine_design%3Aquarter-load-overload-30-40) — Electric Machine Design, chapter 2, asked 2×
- [Quarter-load heat run and 20% overload time](https://www.bctengineer.com/question.md?key=semester_5%2Felectric_machine_design%3Aquarter-load-overload-40-50) — Electric Machine Design, chapter 2, asked 2×
- [Full-load heat run and 50% overload time](https://www.bctengineer.com/question.md?key=semester_5%2Felectric_machine_design%3Afull-load-overload-20-32) — Electric Machine Design, chapter 2
- [RL circuit current as a function of time](https://www.bctengineer.com/question.md?key=semester_1%2Fmathematics_i%3Aapp-rl-circuit-current) — Mathematics I, chapter 4, asked 4×
