C++ Program to find the area and perimeter of a square using class and object.


January 30, 2023, Learn eTutorial
2147

Define the area and perimeter of a shape(square)?

The area of a shape(square) is the space occupied enclosed by its shape(square). It can be calculated as the product of the length of each side. That is,  

Area A = s x s

where

  •  s is the length of a side. 

Perimeter is the distance around the boundary lines of the shape. The perimeter of a square is defined as the length of the boundary line of the four sides. The perimeter can be calculated by finding the sum of all the sides. in the case of a square, all side's lengths are equal so we can calculate the perimeter as 

The perimeter = 4 * s,

  • where "s" is the length of a side.

How to write a C++ program for calculating the area and perimeter of a square?

We are writing this C++ program with the help of classes and objects,  First, Ask the user to enter the side length of the square. Declare two float-type variables a and p to get the area and perimeter. Create a class with a named sq. Declare a float-type variable privately to store the side length. Declare three functions for getting the input data, calculating the area, and calculating the perimeter. Call the function to get the input. Call the function area with the object s and store the value in the variable a and call the function perimeter with the object s and store the value into the variable p. Display the result.

  1. getdata() is used for getting the input from the user.
  2. area() function will calculate the area using side*side
  3. perimeter() function will calcuale the perimeter using the formula 4*side

Now create an object s for the class sq
Sq s;

Class sq
{
   Private:
      Float side;
   Public:
      Void getdata();
      Float area();
      Float perimeter();
};

 

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3:Create a class square;

Step 4: Declare a float-type variable side;

Step 5: Define the function for getting the input.
             read and store the value that the user enters for the side length

Step 6: Define the function for calculating the area of the square.
             Side * side;

Step 7: Define the function for calculating the perimeter of the square.
             4 * side;

Step 8: Open the integer type main function; int main().

Step 9: Create an object s for the class square;

Step 10: Declare two float type variables a and p;

Step 11: get the function to read the input
               s . step 5;

Step 12: get the area to the variable
                a = s.step 6;

Step 13: get the perimeter to the variable p
               P = s.step 7;

Step 14: Display the value of a and p;

Step 15: Exit

C++ Source Code

                                          #include<iostream>
using namespace std;
class square
{
    private:
        float side;
    public:
        void getdata();
        float Area();
        float Perimeter();
};
void square::getdata()
{
    cout<<"Enter the Length of the side of a Square: ";
    cin>>side;
}
float square::Area()
{
    return (side*side);
}
float square::Perimeter()
{
    return (4*side);
}
int main()
{
   square s;
    float a, p;
    s.getdata();
    a = s.Area();
    p = s.Perimeter();
    cout<<"\nArea = "<<a;
    cout<<"\nPerimeter = "<<p;
    cout<<endl;
    return 0;
}
                                      

OUTPUT

Enter the Length of the side of a Square: 8
Area = 64
Perimeter = 32