Shape Calculator is a console-based C++ application that demonstrates advanced OOP concepts through a system of shapes (e.g., Circle, Rectangle, Triangle, Square, Ellipse).
Features
Our Shape Calculator provides the following features:
- Create different shapes at runtime (Circle, Rectangle, Square, Triangle, Ellipse)
- Display area and perimeter
- Compare two shapes by area
- Menu-driven dashboard
Project Requirements
The following C++ concepts are required:
- Classes, Objects, Inheritance
- Access Specifiers, Constructors, Destructors
- Virtual Functions, Method Overriding
- Operator Overloading
- Dynamic Memory Allocation
You’ll need a working C++ environment that consists of compiler like g++, clang++, or IDE like Code::Blocks, Visual Studio Code.
Implementation
Let’s break the implementation into steps:
STEP 1: Base Shape Class
We define a Shape class with virtual functions for area, perimeter, and displaying info.
C++ #include <bits/stdc++.h> using namespace std; class Shape { public: virtual double area() const = 0; virtual double perimeter() const = 0; virtual void display() const = 0; // Virtual destructor for polymorphism virtual ~Shape() {} // Overload > operator to compare areas bool operator>(const Shape& other) const { return this->area() > other.area(); } };
STEP 2: Derived Shape Classes
Creating the derived class from the above base class shape:
C++ class Circle : public Shape { double radius; public: Circle(double r) : radius(r) {} double area() const override { return M_PI * radius * radius; } double perimeter() const override { return 2 * M_PI * radius; } void display() const override { cout << "Circle: radius = " << radius << "\n"; cout << "Area = " << area() << "\n"; cout << "Perimeter = " << perimeter() << "\n"; } }; class Rectangle : public Shape { double width, height; public: Rectangle(double w, double h) : width(w), height(h) {} double area() const override { return width * height; } double perimeter() const override { return 2 * (width + height); } void display() const override { cout << "Rectangle: width = " << width << ", height = " << height << "\n"; cout << "Area = " << area() << "\n"; cout << "Perimeter = " << perimeter() << "\n"; } }; class Square : public Rectangle { public: Square(double side) : Rectangle(side, side) {} void display() const override { cout << "Square: side = " << area() << "\n"; Rectangle::display(); } }; class Triangle : public Shape { double a, b, c; public: Triangle(double x, double y, double z) : a(x), b(y), c(z) {} double area() const override { double s = perimeter() / 2; return sqrt(s * (s - a) * (s - b) * (s - c)); } double perimeter() const override { return a + b + c; } void display() const override { cout << "Triangle: sides = " << a << ", " << b << ", " << c << "\n"; cout << "Area = " << area() << "\n"; cout << "Perimeter = " << perimeter() << "\n"; } }; class Ellipse : public Shape { double a, b; public: Ellipse(double x, double y) : a(x), b(y) {} double area() const override { return M_PI * a * b; } double perimeter() const override { return M_PI * (3*(a + b) - sqrt((3*a + b)*(a + 3*b))); } void display() const override { cout << "Ellipse: axes = " << a << ", " << b << "\n"; cout << "Area = " << area() << "\n"; cout << "Perimeter = " << perimeter() << "\n"; } };
STEP 3: Input and Dashboard
This dashboard provides interactive access to the program for creating different shapes depending on the choice of the user.
C++ vector<Shape*> shapes; void createCircle() { double r; cout << "Enter radius: "; cin >> r; shapes.push_back(new Circle(r)); } void createRectangle() { double w, h; cout << "Enter width and height: "; cin >> w >> h; shapes.push_back(new Rectangle(w, h)); } void createSquare() { double s; cout << "Enter side: "; cin >> s; shapes.push_back(new Square(s)); } void createTriangle() { double a, b, c; cout << "Enter sides a, b, c: "; cin >> a >> b >> c; shapes.push_back(new Triangle(a, b, c)); } void createEllipse() { double a, b; cout << "Enter axes a and b: "; cin >> a >> b; shapes.push_back(new Ellipse(a, b)); } void displayAllShapes() { if (shapes.empty()) { cout << "No shapes created.\n"; return; } for (size_t i = 0; i < shapes.size(); i++) { cout << "\nShape #" << i+1 << ":\n"; shapes[i]->display(); } } void compareShapes() { if (shapes.size() < 2) { cout << "Need at least 2 shapes.\n"; return; } int i, j; cout << "Enter indices of shapes (1-based): "; cin >> i >> j; if (i < 1 || j < 1 || i > (int)shapes.size() || j > (int)shapes.size()) { cout << "Invalid indices.\n"; return; } if (*shapes[i-1] > *shapes[j-1]) cout << "Shape #" << i << " has larger area.\n"; else cout << "Shape #" << j << " has larger or equal area.\n"; } void shapeDashboard() { int choice; while (true) { cout << "\n--- Shape Calculator Menu ---\n"; cout << "1. Create Circle\n"; cout << "2. Create Rectangle\n"; cout << "3. Create Square\n"; cout << "4. Create Triangle\n"; cout << "5. Create Ellipse\n"; cout << "6. Display All Shapes\n"; cout << "7. Compare Two Shapes\n"; cout << "8. Exit\n"; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: createCircle(); break; case 2: createRectangle(); break; case 3: createSquare(); break; case 4: createTriangle(); break; case 5: createEllipse(); break; case 6: displayAllShapes(); break; case 7: compareShapes(); break; case 8: cout << "Exiting Shape Calculator.\n"; return; default: cout << "Invalid choice.\n"; } } }
STEP 4: Main Function
The main function from where everything starts:
C++ int main() { shapeDashboard(); // Cleanup for (Shape* s : shapes) delete s; return 0; }
Execution
Combining all the above components, we get the executable source code:
C++ // Source code #include <bits/stdc++.h> using namespace std; class Shape { public: virtual double area() const = 0; virtual double perimeter() const = 0; virtual void display() const = 0; virtual ~Shape() {} // Compare shapes by area bool operator>(const Shape& other) const { return this->area() > other.area(); } }; class Circle : public Shape { double radius; public: Circle(double r) : radius(r) {} double area() const override { return M_PI * radius * radius; } double perimeter() const override { return 2 * M_PI * radius; } void display() const override { cout << "Circle: radius = " << radius << "\n"; cout << "Area = " << area() << "\n"; cout << "Perimeter = " << perimeter() << "\n"; } }; class Rectangle : public Shape { double width, height; public: Rectangle(double w, double h) : width(w), height(h) {} double area() const override { return width * height; } double perimeter() const override { return 2 * (width + height); } void display() const override { cout << "Rectangle: width = " << width << ", height = " << height << "\n"; cout << "Area = " << area() << "\n"; cout << "Perimeter = " << perimeter() << "\n"; } }; class Square : public Rectangle { public: Square(double side) : Rectangle(side, side) {} void display() const override { cout << "Square: side = " << area() << "\n"; Rectangle::display(); } }; class Triangle : public Shape { double a, b, c; public: Triangle(double x, double y, double z) : a(x), b(y), c(z) {} double area() const override { double s = perimeter() / 2; return sqrt(s * (s - a) * (s - b) * (s - c)); } double perimeter() const override { return a + b + c; } void display() const override { cout << "Triangle: sides = " << a << ", " << b << ", " << c << "\n"; cout << "Area = " << area() << "\n"; cout << "Perimeter = " << perimeter() << "\n"; } }; class Ellipse : public Shape { double a, b; public: Ellipse(double x, double y) : a(x), b(y) {} double area() const override { return M_PI * a * b; } double perimeter() const override { return M_PI * (3*(a + b) - sqrt((3*a + b)*(a + 3*b))); } void display() const override { cout << "Ellipse: axes = " << a << ", " << b << "\n"; cout << "Area = " << area() << "\n"; cout << "Perimeter = " << perimeter() << "\n"; } }; vector<Shape*> shapes; void createCircle() { double r; cout << "Enter radius: "; cin >> r; shapes.push_back(new Circle(r)); } void createRectangle() { double w, h; cout << "Enter width and height: "; cin >> w >> h; shapes.push_back(new Rectangle(w, h)); } void createSquare() { double s; cout << "Enter side: "; cin >> s; shapes.push_back(new Square(s)); } void createTriangle() { double a, b, c; cout << "Enter sides a, b, c: "; cin >> a >> b >> c; shapes.push_back(new Triangle(a, b, c)); } void createEllipse() { double a, b; cout << "Enter axes a and b: "; cin >> a >> b; shapes.push_back(new Ellipse(a, b)); } void displayAllShapes() { if (shapes.empty()) { cout << "No shapes created.\n"; return; } for (size_t i = 0; i < shapes.size(); i++) { cout << "\nShape #" << i+1 << ":\n"; shapes[i]->display(); } } void compareShapes() { if (shapes.size() < 2) { cout << "Need at least 2 shapes.\n"; return; } int i, j; cout << "Enter indices of shapes (1-based): "; cin >> i >> j; if (i < 1 || j < 1 || i > (int)shapes.size() || j > (int)shapes.size()) { cout << "Invalid indices.\n"; return; } if (*shapes[i-1] > *shapes[j-1]) cout << "Shape #" << i << " has larger area.\n"; else cout << "Shape #" << j << " has larger or equal area.\n"; } void shapeDashboard() { int choice; while (true) { cout << "\n--- Shape Calculator Menu ---\n"; cout << "1. Create Circle\n"; cout << "2. Create Rectangle\n"; cout << "3. Create Square\n"; cout << "4. Create Triangle\n"; cout << "5. Create Ellipse\n"; cout << "6. Display All Shapes\n"; cout << "7. Compare Two Shapes\n"; cout << "8. Exit\n"; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: createCircle(); break; case 2: createRectangle(); break; case 3: createSquare(); break; case 4: createTriangle(); break; case 5: createEllipse(); break; case 6: displayAllShapes(); break; case 7: compareShapes(); break; case 8: cout << "Exiting Shape Calculator.\n"; return; default: cout << "Invalid choice.\n"; } } } int main() { shapeDashboard(); // Cleanup for (Shape* s : shapes) delete s; return 0; }
On executing this code, we get the following output:
Similar Reads
scalbn() function in C++ The scalbn() function is defined in the cmath header file. This function is used to calculate the product of given number x and FLT_RADIX raised to the power n. Syntax:- float scalbn(float x, int n); or double scalbn(double x, int n); or long double scalbn(long double x, int n); or double scalbn(int
2 min read
std::cbrt() in C++ The std::cbrt() is an inbuilt function in C++ STL which is used to calculate the cube root of number. It accepts a number as argument and returns the cube root of that number.Syntax: // Returns cube root num (num can be // of type int, double, long double or // long long type. // The return type is
2 min read
std::function in C++ The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s
5 min read
C++ STL Tutorial âRecent Articlesâ on C++ STL ! âCoding Problemsâ on C++ STL ! The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as vector, lists, stacks, etc. Standard Template Library (STL) Algorithms Introduction to STL Sorting Se
2 min read
acos() function in C++ STL acos() is an inbuilt function in C++ STL and itâs the same as the inverse of cosine in maths. The acos() function returns the values in the range of [0, Ï] which is the angle in radians. Syntaxacos(data_type x);ParametersThis function accepts one mandatory parameter x which specifies the value whose
3 min read