---
title: "Polygon abstract class with Area()"
subject: "Object Oriented Programming"
chapter: 7
key: "semester_3/object_oriented_programming:polygon-abstract-area"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/polygon-abstract-area
site: BCT Engineering
---

# Polygon abstract class with Area()

Object Oriented Programming · Chapter 7: Polymorphism and Dynamic Binding

- Asked in: 2072 Chaitra, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/polygon-abstract-area
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-7

## Question

Write a program having Polygon as an abstract class with Length and Height as its data member. Create derived class Rectangle and Triangle. Make Area () as pure virtual function and redefine it in derived class to calculate respective area.

## Short answer

Polygon is abstract with pure virtual Area(). Rectangle computes Length*Height, Triangle 0.5*Length*Height. A base pointer calls the correct overridden function at run time via the vtable, outputting 50 for rectangle and 25 for triangle.

## Answer

A class with at least one pure virtual function is an **abstract class**; objects of it cannot be created. `Polygon` declares `Area()` as pure virtual, and `Rectangle` and `Triangle` override it.

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

class Polygon {
protected:
    float Length, Height;
public:
    Polygon(float l, float h) : Length(l), Height(h) {}
    virtual float Area() = 0;          // pure virtual function
    virtual ~Polygon() {}
};

class Rectangle : public Polygon {
public:
    Rectangle(float l, float h) : Polygon(l, h) {}
    float Area() { return Length * Height; }          // l x h
};

class Triangle : public Polygon {
public:
    Triangle(float l, float h) : Polygon(l, h) {}
    float Area() { return 0.5f * Length * Height; }   // 1/2 x b x h
};

int main() {
    Polygon *p;                 // base pointer, no Polygon object
    Rectangle r(10, 5);
    Triangle t(10, 5);

    p = &r;
    cout << "Area of rectangle = " << p->Area() << endl;
    p = &t;
    cout << "Area of triangle  = " << p->Area() << endl;
    return 0;
}
```

**Output**

```text
Area of rectangle = 50
Area of triangle  = 25
```

The call `p->Area()` is resolved at run time through the virtual table, so the area function of the actual object is executed.

## Check yourself

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

1. What makes Polygon abstract?
   A. Its protected data Length and Height
   B. Area() declared as a pure virtual function
   C. Its constructor
   D. Its virtual destructor
   Answer: B. The pure virtual Area() = 0 makes the class abstract. (Key point: Abstract Polygon)

2. For length 10 and height 5, what are the rectangle and triangle areas?
   A. 50 and 50
   B. 25 and 50
   C. 50 and 25
   D. 15 and 7.5
   Answer: C. Rectangle = 10 x 5 = 50, triangle = 1/2 x 10 x 5 = 25. (Key point: Triangle area)

3. Why must Rectangle and Triangle redefine Area()?
   A. Each shape has its own formula, and otherwise the class stays abstract
   B. To hide Length and Height
   C. To make the class abstract
   D. To avoid using a constructor
   Answer: A. Each derived class supplies its own area formula; without it, it remains abstract and cannot be instantiated. (Key point: Why override)

## 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×
- [Area of triangle and rectangle program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aarea-triangle-rectangle-overload) — Object Oriented Programming, chapter 3
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [Static data member and member function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-member-function) — Object Oriented Programming, chapter 4, asked 3×
- [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
- [Access overridden members from derived class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aaccess-overridden-members) — Object Oriented Programming, chapter 6, asked 2×

## Related in other subjects

- [Create classes from design class diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclasses-from-design-class-diagram) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [Thiessen polygon method with rainfall data](https://www.bctengineer.com/question.md?key=semester_6%2Fhydro_power%3Athiessen-polygon-explain-numerical) — Hydro Power, chapter 2
