In: Computer Science
C++ // Rectangle.cpp is the Rectangle class function implementation file. #include "Rectangle.h" /******************************************************************* * Rectangle::setLength * * If the argument passed to the setLength function is zero or * * greater, it is copied into the member variable length, and true * * is returned. If the argument is negative, the value of length * * remains unchanged and false is returned. * *******************************************************************/ bool Rectangle::setLength(double len) { bool validData = true; if (len >= 0) // If the len is valid length = len; // copy it to length else validData = false; // else leave length unchanged return validData; } /****************************************************************** * Rectangle::setWidth * * If the argument passed to the setWidth function is zero or * * greater, it is copied into the member variable width, and true * * is returned. If the argument is negative, the value of width * * remains unchanged and false is returned. * ******************************************************************/ bool Rectangle::setWidth(double w) { bool validData = true; if (w >= 0) // If w is valid width = w; // copy it to width else validData = false; // else leave width unchanged return validData; } /************************************************************** * Rectangle::getLength * * This function returns the value in member variable length. * **************************************************************/ double Rectangle::getLength() { return length; } /************************************************************** * Rectangle::getWidth * * This function returns the value in member variable width. * **************************************************************/ double Rectangle::getWidth() { return width; } /******************************************************************* * Rectangle::getArea * * This function calculates and returns the area of the rectangle. * *******************************************************************/ double Rectangle::getArea() { return length * width; }
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
public:
Rectangle();
bool setLength(double len);
bool setWidth(double w);
double getLength();
double getWidth();
double getArea();
private:
// Declaring variables
double length;
double width;
};
#endif
================================
// Rectangle.cpp
#include <iostream>
using namespace std;
#include "Rectangle.h"
Rectangle::Rectangle()
{
this->length=0;
this->width=0;
}
/*******************************************************************
* Rectangle::setLength *
* If the argument passed to the setLength function is zero or
*
* greater, it is copied into the member variable length, and true
*
* is returned. If the argument is negative, the value of length
*
* remains unchanged and false is returned. *
*******************************************************************/
bool Rectangle::setLength(double len)
{
bool validData = true;
if (len >= 0) // If the len is valid
length = len; // copy it to length
else
validData = false; // else leave length unchanged
return validData;
}
/******************************************************************
* Rectangle::setWidth *
* If the argument passed to the setWidth function is zero or
*
* greater, it is copied into the member variable width, and true
*
* is returned. If the argument is negative, the value of width
*
* remains unchanged and false is returned. *
******************************************************************/
bool Rectangle::setWidth(double w)
{
bool validData = true;
if (w >= 0) // If w is valid
width = w; // copy it to width
else
validData = false; // else leave width unchanged
return validData;
}
/**************************************************************
* Rectangle::getLength *
* This function returns the value in member variable length.
*
**************************************************************/
double Rectangle::getLength()
{
return length;
}
/**************************************************************
* Rectangle::getWidth *
* This function returns the value in member variable width. *
**************************************************************/
double Rectangle::getWidth()
{
return width;
}
/*******************************************************************
* Rectangle::getArea *
* This function calculates and returns the area of the rectangle.
*
*******************************************************************/
double Rectangle::getArea()
{
return length * width;
}
==============================
// main.cpp
#include <iostream>
using namespace std;
#include "Rectangle.h"
int main()
{
double length,width;
Rectangle r;
while(true)
{
cout<<"Enter Length :";
cin>>length;
if(!r.setLength(length))
{
cout<<"** Invalid.Must be >=0 **"<<endl;
}
else
{
break;
}
}
while(true)
{
cout<<"Enter Width :";
cin>>width;
if(!r.setWidth(width))
{
cout<<"** Invalid.Must be >=0 **"<<endl;
}
else
{
break;
}
}
cout<<"Length "<<r.getLength()<<endl;
cout<<"Width "<<r.getWidth()<<endl;
cout<<"Area of Rectangle
:"<<r.getArea()<<endl;
return 0;
}
====================================
Output:
=====================Could you plz rate me well.Thank You