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

In this example program, you will learn and get code on finding the area and perimeter of a square in C++ programming. Here we are using the approach using class and object to create the program.

What is the area and perimeter of a square?

The area of a two-dimensional shape is the space occupied by it. The area of a square is the product of the length of each side itself. That is,  Area A = s x s where s is the length of each side of the square. 

Perimeter is the distance around the edge of a shape The perimeter of a square is defined as the total length that its boundary covers. The perimeter can be calculated by finding the sum of all the sides. Since all four sides are equal, it is four times the length of each side of a square

Perimeter of square = s + s + s + s
The perimeter of the square = 4s, where "s" is the side length of the square.

Formula to find the area and perimeter of a square

To find the area of a square, use the following formula:

area = len*len

  • len indicates the length of the square. Because all the sides are of equal length in the square, therefore get the length of a side and square it. That will be the area

To find the perimeter of a square, use the following formula:
perimeter = 4*len

  • len indicates length.

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

Ask the user to enter the side length of the square. With this side length, apply the formula as given above and initialize its value to a variable say the area, and print its value as output.

Create a class with a named square. 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.

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

Define each function.

Getdata() to read the user-entered input into the variable side

cout<<"Enter the Length of Square:
cin>>side;

 

area() to compute the area of the square

return (side * side);

 

perimeter() to compute the perimeter of the square

return (4 * side);

 

within the main function create an object s for the class square.
Square s;
Declare two float type variables a and p to get the area and perimeter of the square.
Call the function to get the input. 
Call the function area with the object s and store the value into the variable a and call the function perimeter with the object s and store the value into the variable p. Display the result.

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.
             Ask the user to enter the length of the side
             Read the value to the variable side;

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 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