C++ program to find the area of shapes using class

Learn, how can we find the area of shapes using the class and object approach?
Submitted by Shubh Pachori, on September 04, 2022

Example:

Enter Circle's Radius :22
Area of Circle : 1521

Enter Rectangle's Length :12
Enter Rectangle's Breadth :21
Area of Rectangle : 252

Enter Square's Side :22
Area of Square : 484

C++ code to find the area of shapes using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Shape {
  // protected data member
 protected:
  float area;
};

// class Rectangle inheriting class Shape
class Rectangle : public Shape {
  // protected data members
 protected:
  float length, breadth;

  // public member function
 public:
  // get() function to input attributes of the class
  void get() {
    cout << "\nEnter Rectangle's Length :";
    cin >> length;

    cout << "Enter Rectangle's Breadth :";
    cin >> breadth;
  }

  // Area() function to calculate the area of the class
  double Area() {
    // calculating area
    area = length * breadth;

    // returning area
    return area;
  }
};

// class Circle inheriting class Shape
class Circle : public Shape {
  // protected data member
 protected:
  float radius;

  // public member functions
 public:
  // get() function to input attributes of the class
  void get() {
    cout << "Enter Circle's Radius :";
    cin >> radius;
  }

  // Area() function to calculate area of the class
  double Area() {
    // calculating area
    area = (22 * (radius * radius)) / 7;

    // returning area
    return area;
  }
};

// class Square inheriting
class Square : public Shape {
  // protected data member
 protected:
  float side;

  // public member function
 public:
  // get() function to input 
  // attributes of class
  void get() {
    cout << "\nEnter Square's Side : ";
    cin >> side;
  }

  // Area() function to calculate 
  // area of the class
  double Area() {
    // calculating area
    area = side * side;

    // returning area
    return area;
  }
};

int main() {
  // create an object of Circle class
  Circle C;

  // float type variable to store area
  float circle;

  // calling get() function to 
  // input radius of circle
  C.get();

  // calling Area() function to 
  // calculate area of circle
  circle = C.Area();

  cout << "Area of Circle : " << circle << endl;

  // create an object of Rectangle class
  Rectangle R;

  // float type variable to store area
  float rectangle;

  // calling get() function to input length
  // and breadth of rectangle
  R.get();

  // calling Area() function to calculate area of rectangle
  rectangle = R.Area();

  cout << "Area of Rectangle : " << rectangle << endl;

  // create an object of Square class
  Square S;

  // float type variable to store area
  float square;

  // calling get() function to 
  // input side of square
  S.get();

  // calling Area() function to 
  // calculate area of square
  square = S.Area();

  cout << "Area of Square : " << square << endl;

  return 0;
}

Output:

Enter Circle's Radius :7
Area of Circle : 154

Enter Rectangle's Length :9
Enter Rectangle's Breadth :8
Area of Rectangle : 72

Enter Square's Side :6
Area of Square : 36

Explanation:

In the above code, we have created a parent class Shape, and a protected float type data member area to calculate and store area of various shapes. A base class Rectangle inherits the parent class Shape and its elements and this base class has two public member functions get() and Area(), to store attributes and to calculate the area of the shape. Same as above there are two more base classes Circle and Square with two public member functions get() and Area() to store the attributes and to calculate the area of the shape.

In the main() function, we are creating objects of these base classes C of class Circle, R of class Rectangle and S of class Square, then initialize some float variables to store area’s of particular shapes and then calling functions to perform operations like to input attributes and to calculate the area of the shapes. In the end returning results.

C++ Class and Object Programs (Set 2) »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.