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
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.
Folder structure
Complete source code
Makefile
What each file does
- src/shapes.h
- The abstract
Shapeinterface, the four concrete classes and themake_shapefactory 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
Shapepointers. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
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 10is rejected by the triangle inequality;circle -1by the positive check.- 3-4-5 is a right triangle with area 6; scaling it by 2 multiplies the area by 4.
copy 3creates 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
- 1Add a
contains(x, y)virtual function for point-in-shape tests. - 2Implement equality between shapes of the same type.
- 3Group the listing by shape type with subtotals.
Built and tested it yourself? Mark it to update your dashboard.