---
title: "Abstract class with example"
subject: "Object Oriented Programming"
chapter: 7
key: "semester_3/object_oriented_programming:abstract-class-example"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/abstract-class-example
site: BCT Engineering
---

# Abstract class with example

Object Oriented Programming · Chapter 7: Polymorphism and Dynamic Binding

- Asked in: 2074 Asoj, 5 marks
- Video: [Abstract class in C++ | what is abstract class and pure virtual function in c++](https://www.youtube.com/watch?v=7Dy0uw0zLy0) — Learn Coding
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/abstract-class-example
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-7

## Question

Explain abstract class with example.

## Short answer

An abstract class is a base class with at least one pure virtual function (e.g., virtual float area() = 0;). It cannot be instantiated; derived classes must override all pure virtual functions. Example: Shape defines area() as pure virtual, Circle and Square override it with different formulas.

## Answer

An **abstract class** is a class that cannot be instantiated and is meant to be used only as a base class. A class becomes abstract when it has at least one **pure virtual function**, declared as `virtual return_type f() = 0;`.

#### Key points
- No object of an abstract class can be created, but pointers and references to it can.
- It defines a common interface; derived classes must override all pure virtual functions, otherwise they are abstract too.
- It can contain data members, constructors and ordinary member functions.
- It is used to achieve run-time polymorphism.

#### Example

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

class Shape {                         // abstract class
public:
    virtual float area() = 0;         // pure virtual function
    void show() { cout << "Area = " << area() << endl; }
};

class Circle : public Shape {
    float r;
public:
    Circle(float x) : r(x) {}
    float area() { return 3.14159f * r * r; }
};

class Square : public Shape {
    float s;
public:
    Square(float x) : s(x) {}
    float area() { return s * s; }
};

int main() {
    // Shape s;              // error: cannot create object of abstract class
    Shape *p;
    Circle c(2);
    Square q(3);
    p = &c; p->show();
    p = &q; p->show();
    return 0;
}
```

**Output**

```text
Area = 12.5664
Area = 9
```

`Shape` only states that every shape must have an `area()`, while each derived class supplies its own formula.

## Check yourself

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

1. When does a class become abstract?
   A. When it has a private constructor
   B. When it has at least one pure virtual function
   C. When it has no data members
   D. When it is a base class
   Answer: B. At least one pure virtual function (= 0) makes a class abstract. (Key point: Definition)

2. Which statement about abstract classes is true?
   A. Objects can be created in main()
   B. Pointers and references to them can be created
   C. They cannot have data members
   D. They cannot have ordinary functions
   Answer: B. Only objects are disallowed; pointers and references are fine, and they may contain data and normal functions. (Key point: Allowed)

3. For Circle(2) and Square(3), what areas are printed?
   A. 6.28 and 9
   B. 12.5664 and 6
   C. 12.5664 and 9
   D. 9 and 12.5664
   Answer: C. 3.14159 x 2 x 2 = 12.5664 and 3 x 3 = 9. (Key point: Output)

## Related in Object Oriented Programming

- [Pure virtual function and abstract class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apure-virtual-abstract-class) — Object Oriented Programming, chapter 7, asked 5×
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [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
