Given the following ADT class definition: class rectangle { public: rectangle();
ID: 3728192 • Letter: G
Question
Given the following ADT class definition:
class rectangle
{
public:
rectangle();
rectangle(double, double);
void setLength(double);
//Post: the length of the triangle is set to the parameter value
void setWidth(double);
//Post: the width of the triangle is set to the parameter value
double lengthIs();
//Post: return value is the length of the rectangle
double widthIs();
//Post: return value is the width
double area();
//Post: returns the area of the rectangle
private:
double length;
double width;
};
a) Assume that the default constructor sets the private data members to 0, and the other constructor sets the private data members to the parameters.
i) Write the implementation code for the default constructor.
ii) Write the implementation code for the second constructor.
iii) Show how client code would declare a rectangle using the first constructor.
iv) Show how client code would declare a rectangle using the second constructor.
b) Add a public member function that allows a user to compare two rectangles. Show the prototype, and explain where you would put it. Give the function definition as well. This public member function returns true if the two rectangles have exactly the same dimensions and false otherwise. (If one rectangle has width 6 and length 7 and the other has width 7 and length 6, they should still be equal.)
Explanation / Answer
i)
rectangle::rectangle() {
length = 0;
width = 0;
}
ii)
rectangle::rectangle(double l, double w){
length = l;
width = w;
}
iii)
rectangle r1; // default
rectangle r2(4,5); // parameterized
iv)
bool compare(const rectangle & oth);
class rectangle
{
public:
rectangle();
rectangle(double, double);
void setLength(double);
//Post: the length of the triangle is set to the parameter value
void setWidth(double);
//Post: the width of the triangle is set to the parameter value
double lengthIs();
//Post: return value is the length of the rectangle
double widthIs();
//Post: return value is the width
double area();
//Post: returns the area of the rectangle
bool compare(const rectangle & oth);
private:
double length;
double width;
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.