Skip to content
C++ project #11Intermediate

Shape Geometry Toolkit

A polymorphic shape library — circles, rectangles, classified triangles and regular polygons — managed through std::unique_ptr with sorting, scaling and cloning.

Estimated time
~4 hours to build
Files
7 files · 467 lines
Technologies
C++17, <memory>, <cmath>, <algorithm>, Header/source split

Introduction

This is the textbook example of runtime polymorphism, done with modern C++. An abstract Shape base class defines the interface; four concrete classes implement it; the program only ever talks to Shape&. A factory turns text into objects, and the clone idiom lets you copy a shape without knowing its real type.

Problem statement

Build a toolkit that creates shapes from text commands, validates their dimensions (positive sizes, the triangle inequality, 3–100 polygon sides), lists them with name, dimensions, area and perimeter, sorts by area or perimeter, scales a shape, duplicates one, removes one and reports the largest.

Objectives

  • Design an abstract base class with pure virtual functions
  • Own polymorphic objects with std::unique_ptr in a vector
  • Implement a factory function and the clone idiom
  • Separate interface (.h) from implementation (.cpp)

Features

  • Circle, rectangle/square, triangle (classified as right, equilateral, isosceles or scalene), regular polygon
  • Validation with clear error messages via exceptions
  • Sorting with lambdas over unique_ptrs
  • Deep copy of a shape through clone()

Concepts demonstrated

  • Abstract classes
  • Virtual functions
  • std::unique_ptr
  • Factory pattern
  • Virtual clone idiom
  • Heron's formula

System architecture

shapes.h declares Shape (area, perimeter, name, dimensions, scale, clone) and the four derived classes; shapes.cpp implements them and make_shape, which reads the kind and a list of numbers and throws std::invalid_argument for anything wrong. main.cpp keeps a std::vector<std::unique_ptr<Shape>>, so destruction is automatic and copying the vector is impossible by accident. Every list, sort and comparison works through the base-class interface.

TextArchitecture diagram
Shape (abstract)
area() perimeter() name() dimensions() scale() clone()
/ | | \
Circle Rectangle Triangle RegularPolygon
make_shape("triangle 3 4 5") --> unique_ptr<Shape> --> vector

Folder structure

TextProject tree
shape-geometry-toolkit/
├── README.md
├── Makefile
├── src/main.cpp
├── src/shapes.cpp
├── src/shapes.h
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/shapes.h
The abstract Shape interface, the four concrete classes and the make_shape factory declaration.
src/shapes.cpp
Implementations: validation in constructors, Heron's formula, triangle classification, polygon area n·s²/(4·tan(π/n)) and the factory.
src/main.cpp
The command loop and table output; all work goes through Shape pointers.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Shape Geometry Toolkit ===
add <circle r | square s | rectangle w h | triangle a b c | polygon n s>
list | sort area|perimeter | scale <#> <factor> | copy <#> | remove <#> | largest | quit
> Added circle #1
> Added rectangle #2
> Added right triangle #3
> Error: sides break the triangle inequality
> Added square #4
> Added regular hexagon #5
> Error: unknown shape 'hexagon'
> Error: radius must be positive
> Added equilateral triangle #6
> # Shape Dimensions Area Perimeter
1 circle r=2 12.57 12.57
2 rectangle 4x2.5 10.00 13.00
3 right triangle 3/4/5 6.00 12.00
4 square 3x3 9.00 12.00
5 regular hexagon 6 x 2 10.39 12.00
6 equilateral triangle 5/5/5 10.83 15.00
Total area: 58.78
> Sorted by area
> # Shape Dimensions Area Perimeter
1 right triangle 3/4/5 6.00 12.00
2 square 3x3 9.00 12.00
3 rectangle 4x2.5 10.00 13.00
4 regular hexagon 6 x 2 10.39 12.00
5 equilateral triangle 5/5/5 10.83 15.00
6 circle r=2 12.57 12.57
Total area: 58.78
> Scaled #1 by 2: area x4
> Copied #3 to #7
> Error: no shape with that number
> Largest: right triangle 6/8/10
> # Shape Dimensions Area Perimeter
1 right triangle 6/8/10 24.00 24.00
2 square 3x3 9.00 12.00
3 rectangle 4x2.5 10.00 13.00
4 regular hexagon 6 x 2 10.39 12.00
5 equilateral triangle 5/5/5 10.83 15.00
6 circle r=2 12.57 12.57
7 rectangle 4x2.5 10.00 13.00
Total area: 86.78
>
Input
add circle 2
add rectangle 4 2.5
add triangle 3 4 5
add triangle 1 2 10
add square 3
add polygon 6 2
add hexagon 2
add circle -1
add triangle 5 5 5
list
sort area
list
scale 1 2
copy 3
remove 9
largest
list
quit

Testing

  • Compare the scripted session with tests/expected-output.txt.
  • triangle 1 2 10 is rejected by the triangle inequality; circle -1 by the positive check.
  • 3-4-5 is a right triangle with area 6; scaling it by 2 multiplies the area by 4.
  • copy 3 creates a real Rectangle, not a sliced Shape.

Common errors

error: cannot allocate an object of abstract type 'Shape'

You tried to create a Shape directly or forgot to override a pure virtual function in a derived class.

undefined reference to `make_shape`

Compile both .cpp files: g++ ... src/*.cpp.

Possible improvements

  • Add ellipses and trapezoids
  • Read and write shapes from a file
  • Draw shapes as ASCII art

Additional challenges

  1. 1Add a contains(x, y) virtual function for point-in-shape tests.
  2. 2Implement equality between shapes of the same type.
  3. 3Group the listing by shape type with subtotals.

Built and tested it yourself? Mark it to update your dashboard.