Rectangle Area in C++

Posted by Samath
  4548
February 03, 2015

 

Write a program to calculate the area of a rectangle. The program will ask the user to enter the width and length of a rectangle, and then display the area of the rectangle. The following functions should be used:

getLength( ) – inline function

This function should ask the user to enter the rectangle’s length and then return that value as a double

getWidth( ) – inline function

This function should ask the user to enter the rectangle’s width and then return that value as a double

getArea( ) –default argument function (length = 10, width = 20)

This function should accept the rectangle’s length and width as arguments, and return the rectangle’s area.

displayData( )- default argument function (length = 10, width = 20, area = 200)

This function should accept the rectangle’s length, width, and area as arguments, and display them in an appropriate message on the screen.

Code:

#include <iostream>
using namespace std;

double getLength();
double getWidth();
void displayData(double, double, double);
double getArea(double, double);


int main(int argc, char *argv[])
{
	double len;
	double wid;
	double area;


	len = getLength();
	wid = getWidth();
	area = getArea(len, wid);
	displayData(len, wid, area);

	
	return 0;	
}

double getLength()
{
	double rectLength;


	cout<<"Enter the length of the rectangle: ";
	cin>>rectLength;

	return rectLength;

}

double getWidth()
{
		double rectWidth;

		cout<<"Enter the width of the rectangle: ";
		cin>>rectWidth;

		return rectWidth;
}

void displayData(double length = 10, double width = 20, double area = 200)
{
	cout<<"Rectangle's Width: "<<width<<endl;
	cout<<"Rectangle's Length: "<<length<<endl;
	cout<<"Rectangle's Area: "<<area<<endl;
}


double getArea(double length = 10, double width = 20)
{
	return length * width;
}