# C++ program to find the perimeter of shapes using class

Submitted by Shubh Pachori, on September 04, 2022

## Problem statement

Learn, how can we find the perimeter of shapes using the class and object approach?

Example:

```Enter Circle's Radius :32
Circumference of Circle : 201.143

Enter Rectangle's Length :23
Perimeter of Rectangle : 132

Enter Square's Side :23
Perimeter of Square : 92
```

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

```#include <iostream>
using namespace std;

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

// class Rectangle inheriting class Shape
class Rectangle : public Shape {
// protected data members
protected:

// 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 :";
}

// Perimeter() function to calculate
// the perimeter of the class
double Perimeter() {
// calculating perimeter
perimeter = 2 * (length + breadth);

// returning perimeter
return perimeter;
}
};

// class Circle inheriting class Shape
class Circle : public Shape {
// protected data member
protected:

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

// Perimeter() function to calculate perimeter of the class
double Perimeter() {
// calculating perimeter
perimeter = (2 * 22 * radius) / 7;

// returning perimeter
return perimeter;
}
};

// 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;
}

// Perimeter() function to calculate perimeter of the class
double Perimeter() {
// calculating perimeter
perimeter = 4 * side;

// returning perimeter
return perimeter;
}
};

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

// float type variable to
// store perimeter
float circle;

// calling get() function to
C.get();

// calling Perimeter() function to calculate perimeter of circle
circle = C.Perimeter();

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

// create an object of Rectangle class
Rectangle R;

// float type variable to store perimeter
float rectangle;

// calling get() function to input length
R.get();

// calling Perimeter() function to
// calculate perimeter of rectangle
rectangle = R.Perimeter();

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

// create an object of Square class
Square S;

// float type variable to store perimeter
float square;

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

// calling Perimeter() function to
// calculate perimeter of square
square = S.Perimeter();

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

return 0;
}
```

### Output

```Enter Circle's Radius :12
Circumference of Circle : 75.4286

Enter Rectangle's Length :11
Perimeter of Rectangle : 48

Enter Square's Side :10
Perimeter of Square : 40
```

### Explanation

In the above code, we have created a parent class Shape, and a protected float type data member perimeter to calculate and store the perimeter 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 Perimeter(), to store attributes and to calculate the perimeter of the shape. Same as above there are two more base classes Circle and Square with two public member functions get() and Perimeter() to store the attributes and to calculate the perimeter 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 perimeters of particular shapes and then call functions to perform operations like input attributes and to calculate the perimeter of the shapes. In the end, returning results.