1. Write the definition of two functions in the class Rectangle in following cod
ID: 3549609 • Letter: 1
Question
1. Write the definition of two functions in the class Rectangle in following code (15 points)
class Rectangle
{
public:
Rectangle(); // initialize width to 1.0 and length to 2.0
Rectangle(double x, double y); // initialize width as x and length as y
double get_perimeter(); // return the perimeter of the rectangle.
private:
double width;
double length;
};
a) Write the definition of the constructor without parameter, Rectangle(), which initializes width to 1.0 and length to 2.0. Write it as it written outside of the class declaration.
b) Write the definition of the constructor with two double parameters, Rectangle (double w, double l), using x to initialize width and y to initialize length. Write it as it written outside of the class declaration.
c) Write the definition of the method double get_perimeter(), which returns the perimeter of the rectangle. Write it as it written outside of the class declaration -- (hint: the perimeter of Rectangle is (width+length)*2 ).
Explanation / Answer
3. Write the definition of two functions in the class Rectangle in following code (15 points)
class Rectangle
{
public:
Rectangle(); // initialize width to 1.0 and length to 2.0
Rectangle(double x, double y); // initialize width as x and length as y
double get_perimeter(); // return the perimeter of the rectangle.
private:
double width;
double length;
};
a) Write the definition of the constructor without parameter, Rectangle(), which initializes width to 1.0 and length to 2.0.
Write it as it written outside of the class declaration.
Rectangle::Rectangle()
{
width = 1.0;
length= 2.0;
}
b) Write the definition of the constructor with two double parameters, Rectangle (double w, double l),
using x to initialize width and y to initialize length. Write it as it written outside of the class declaration.
Rectangle::Rectangle(double x, double y)
{
width = x;
length= y;
}
c) Write the definition of the method double get_perimeter(), which returns the perimeter of the rectangle.
Write it as it written outside of the class declaration -- (hint: the perimeter of Rectangle is (width+length)*2 ).
double Rectangle::get_perimeter()
{
return (width+length)*2;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.