Program to find the Area of a Rectangle using Class in C++

# Program to find the Area of a Rectangle using Class in C++:

To create a program for the area of a rectangle. We have to create a Rectangle class, In which we will have to private member variables of integer type like iHeight and iWidth for storing the height and width of the rectangle, then we have to create one member function to calculate the area of the rectangle.


Then in the main function, we create one object of Rectangle class and provide height and width of the rectangle then call the function area to know the area of that rectangle and then we print it using cout.

#include <iostream>

using namespace std;

class Rectangle
{
    int iHeight;
    int iWidth;

public:
    Rectangle()
    {

    }

    Rectangle(int height, int width) : iHeight(height), iWidth(width)
    {

    }

    int area()
    {
        int iArea;

        iArea = iHeight * iWidth;

        return iArea;
    }
};


int main()
{
    Rectangle objRect(15, 10);

    int iArea = objRect.area();

    cout << "The Area of Rectangle is: " << iArea << endl;

    return 0;
}

Comments