Last Updated: 22 Feb, 2021

Area of a Rectangle

Easy

Problem statement

Design a class called Rectangle. It contains two data members as length(L) and breadth(B); and a member function getArea(). The member function computes the area of the given rectangle and returns it to the caller.

Note:

Area of a rectangle = length x breadth
Data Members:
1. length: Length of the rectangle
2. breadth: Breadth of the rectangle 
Member Functions:
1. getArea(): that calculates the area of the rectangle and returns the value.

Note:

You do not have to take input, just create the Class and set the name of the Data Members and function as given in the above discription.
Input Format:
The only line of input contains two integers, both separated by a single space denoting the length and breadth respectively.
Output Format:
The only line of output prints the area of the rectangle.
Constraints:
0 <= L, B <= 100

Approaches

01 Approach

  • First, define a rectangle class that creates two variables, length and breadth, that has public as access specifier.
  • Then create a getArea method of this class which calculates the rectangle area and returns it.
  • Now in the main function, create an object of Rectangle class, take two inputs into two variables, i.e., length and breadth.
  • Then set this value using (.) and then call the function getArea() that calculates and returns the area, and then print the returned value.