Menu
Coddy logo textTech

Recap - Shape Calculator

Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 63 of 104.

challenge icon

Challenge

Easy

Let's build a shape calculator that brings together everything you've learned about polymorphism. You'll create an abstract Shape class and implement concrete shapes that calculate their area and perimeter, all accessible through a common interface.

You'll organize your code across four files:

  • Shape.h: Define an abstract Shape class that serves as the blueprint for all shapes. Your base class should have:
    • A protected std::string name member to identify the shape
    • A constructor that initializes the name
    • Pure virtual methods area() and perimeter() that return double
    • A getName() method that returns the shape's name
    • A virtual destructor
  • Circle.h: Implement a Circle class that inherits from Shape:
    • A private double radius member
    • A constructor taking the radius (set the name to "Circle")
    • Implement area() using the formula: 3.14159 * radius * radius
    • Implement perimeter() using: 2 * 3.14159 * radius
  • Rectangle.h: Implement a Rectangle class that inherits from Shape:
    • Private double width and double height members
    • A constructor taking width and height (set the name to "Rectangle")
    • Implement area() as width * height
    • Implement perimeter() as 2 * (width + height)
  • main.cpp: Read four inputs (each on a separate line):
    1. Circle radius (double)
    2. Rectangle width (double)
    3. Rectangle height (double)
    4. Second circle radius (double)

    Create all three shapes dynamically and store them in an array of Shape* pointers. Loop through the array and for each shape, print its information in this format:

    <name>:
      Area: <area>
      Perimeter: <perimeter>

    Print a blank line between each shape. Clean up your dynamically allocated objects when finished.

For example, with inputs 5, 4, 6, and 3:

Circle:
  Area: 78.5397
  Perimeter: 31.4159

Rectangle:
  Area: 24
  Perimeter: 20

Circle:
  Area: 28.2743
  Perimeter: 18.8495

Notice how the same printShapeInfo logic works for any shape type — this is the power of polymorphism. Your code processes circles and rectangles identically through the Shape interface, and each shape knows how to calculate its own measurements. Use the override keyword on all overridden methods to ensure correct function signatures.

Try it yourself

#include <iostream>
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"

using namespace std;

int main() {
    // Read inputs
    double circleRadius1, rectWidth, rectHeight, circleRadius2;
    cin >> circleRadius1;
    cin >> rectWidth;
    cin >> rectHeight;
    cin >> circleRadius2;

    // TODO: Create an array of Shape* pointers with 3 elements
    // Shape* shapes[3];

    // TODO: Create shapes dynamically and store in array
    // shapes[0] = new Circle(...);
    // shapes[1] = new Rectangle(...);
    // shapes[2] = new Circle(...);

    // TODO: Loop through the array and print each shape's info
    // Format:
    // <name>:
    //   Area: <area>
    //   Perimeter: <perimeter>
    // (blank line between shapes, but not after the last one)

    // TODO: Clean up dynamically allocated objects
    // delete shapes[i];

    return 0;
}

All lessons in Object Oriented Programming