Write a complete class which represents a Rectangle object Y
Write a complete class which represents a Rectangle object. You should have an regimented constructor which sets both the length and widths of the rectangle (double values). As well, the rectangle class should have accessors and mutators for all variables needed. The mutator functions and constructor should ensure that the values being set arc valid (cannot be 0 or negative). Your class should also have a function get Area which calculates the area of the rectangle (length multiplied by width). Also write a tester class which tests ALL functionality of the class (you do not need to use user input).
Solution
#include<iostream>
#include<cassert>
using namespace std;
class Rectangle {
int l, w;
public:
Rectangle(int a, int b): l(a), w(b){}
int getArea(){
return l*w;
}
int getLength(){
return l;
}
int getWidth(){ return w; }
int setLength(int x){
l = x;
}
int setWidth (int x){
w = x;
}
};
int main(){
Rectangle rec(10,5);
assert(rec.getArea() == 50);
assert(rec.getLength() == 10);
assert(rec.getWidth() == 5);
rec.setLength(5);
assert(rec.getLength() == 5);
rec.setWidth(2);
assert(rec.getWidth() == 2);
assert(rec.getArea() == 10);
return 0;
}
