misslovlee
07-29-2006, 07:25 AM
Here is my homework assignment:
"Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide member functions that calculate the perimeter and the area of the rectangle. Also, provide set and get functions for the length and width attributes. The set frunctions should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. "
-I have created 3 files, rectangle.h, rectangle.cpp,tester.cpp.
-I'm really mixed up on what to do with a floater.
-I also have no clue how to add a this pointer, which is what I'm supposed to do.
//.h file
#include <iomanip>
using namespace std;
class Rectangle
{
public:
Rectangle(float=1, float=1);
~Rectangle();
void setLength(float l);
void setWidth(float w);//need to verify that length and width are each floating-point numbers larger than 0.0 and less than 20.00
float getLength();
float getWidth();
float calcPerimeter();
float calcArea();
private:
float length, width;
};
#endif
#include <iostream>
using namespace std;
#include "Rectangle.h"
Rectangle::Rectangle (float l, float w)
{
length=1;
width=w;
/* setLength(l);
setWidth(w);
cout<<"The length is:"<<length<<endl;
cout<<"The width is:"<<width<<endl;*/
}
Rectangle::~Rectangle()
{
cout<<"Destructor runs."<<endl;
}
void Rectangle::setLength(float l)
{
if (l > 0.0 && l < 20.00)
{
setLength(l);
}
else
{
cout<<"Invalid value.\n";
}
}
void Rectangle::setWidth(float w)
{
if (w > 0.0 && w < 20.00)
{
setWidth(w);
}
else
{
cout<<"Invalid value.\n";
}
}
//Prints the length and height of the rectangle
float Rectangle::getLength()
{
return length;
}
float Rectangle::getWidth()
{
return width;
}
// Computes and returns the area of the rectangle
//Computes and returns the perimeter of the rectangle
float Rectangle::calcPerimeter()
{
return length *width;
}
float Rectangle::calcArea()
{
return (2*length) + (2*width) ;
}
#include "Rectangle.h" //#include file created by the programmer
#include <iostream>
using namespace std;
int main()
{
Rectangle room(4,10);
Rectangle t;
t.setLength(4);
t.setWidth(10);
cout<<t.getLength();
t = Rectangle(10, 5); // Rectangle(10, 5) creates a temporary
// object with length 10 and width 5
cout << t.calcArea() << endl;
}
"Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide member functions that calculate the perimeter and the area of the rectangle. Also, provide set and get functions for the length and width attributes. The set frunctions should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. "
-I have created 3 files, rectangle.h, rectangle.cpp,tester.cpp.
-I'm really mixed up on what to do with a floater.
-I also have no clue how to add a this pointer, which is what I'm supposed to do.
//.h file
#include <iomanip>
using namespace std;
class Rectangle
{
public:
Rectangle(float=1, float=1);
~Rectangle();
void setLength(float l);
void setWidth(float w);//need to verify that length and width are each floating-point numbers larger than 0.0 and less than 20.00
float getLength();
float getWidth();
float calcPerimeter();
float calcArea();
private:
float length, width;
};
#endif
#include <iostream>
using namespace std;
#include "Rectangle.h"
Rectangle::Rectangle (float l, float w)
{
length=1;
width=w;
/* setLength(l);
setWidth(w);
cout<<"The length is:"<<length<<endl;
cout<<"The width is:"<<width<<endl;*/
}
Rectangle::~Rectangle()
{
cout<<"Destructor runs."<<endl;
}
void Rectangle::setLength(float l)
{
if (l > 0.0 && l < 20.00)
{
setLength(l);
}
else
{
cout<<"Invalid value.\n";
}
}
void Rectangle::setWidth(float w)
{
if (w > 0.0 && w < 20.00)
{
setWidth(w);
}
else
{
cout<<"Invalid value.\n";
}
}
//Prints the length and height of the rectangle
float Rectangle::getLength()
{
return length;
}
float Rectangle::getWidth()
{
return width;
}
// Computes and returns the area of the rectangle
//Computes and returns the perimeter of the rectangle
float Rectangle::calcPerimeter()
{
return length *width;
}
float Rectangle::calcArea()
{
return (2*length) + (2*width) ;
}
#include "Rectangle.h" //#include file created by the programmer
#include <iostream>
using namespace std;
int main()
{
Rectangle room(4,10);
Rectangle t;
t.setLength(4);
t.setWidth(10);
cout<<t.getLength();
t = Rectangle(10, 5); // Rectangle(10, 5) creates a temporary
// object with length 10 and width 5
cout << t.calcArea() << endl;
}