A Rectangle Class
// Rectangle class.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
using namespace std;
class rectangle
{
private:
double length;
double width;
public:
void setlength(double);
void setWidth(double);
double getLength();
double getWidth();
double getArea();
};
void rectangle::setlength(double len)
{
if (len >= 0)
length = len;
else
{ length = 1.0;
cout << "Invalid length. Using a default value of 1.\n";
}
}
void rectangle:: setWidth(double w)
{
if (w >= 0)
width= w;
else
{ width = 1.0;
cout << "Invalid Width. using a default value of 1.\n";
}
}
double rectangle:: getLength()
{
return length;
}
double rectangle::getWidth()
{
return width;
}
double rectangle::getArea()
{
return length * width;
}
int _tmain(int argc, _TCHAR* argv[])
{
rectangle box;
double boxLength, boxWidth;
cout << "this program will calculate the are of a rectangle\n";
cout << "What is the length? ";
cin >> boxLength;
cout << "What is the width? ";
cin >> boxWidth;
box.setlength(boxLength);
box.setWidth(boxWidth);
cout << "\n Here is the rectangle's data:\n";
cout << "Length: " << box.getLength() << endl;
cout << "Width: " << box.getWidth() << endl;
cout << "Area: " << box.getArea() << endl;
int x;
cin >> x;
return 0;
}