Dayaan M. answered 2d
Computer Science Graduate with Computer Engineering Coursework
The hint you were given is the whole key to this one. Up in the class you only declared what the functions are called and what they return, you did not say what they do, so now you write the bodies underneath, and each one needs Rectangle:: in front of its name so the compiler knows it belongs to this class and not some loose function floating in the file.
Here is what goes between YOUR_CODE_BELOW and YOUR_CODE_ABOVE:
Rectangle::Rectangle(double theWidth, double theHeight) {
width = theWidth;
height = theHeight;
}
double Rectangle::getWidth() {
return width;
}
double Rectangle::getHeight() {
return height;
}
double Rectangle::getPerimeter() {
return 2 * width + 2 * height;
}
double Rectangle::getArea() {
return width * height;
}
void Rectangle::scale(double scaleFactor) {
width = width * scaleFactor;
height = height * scaleFactor;
}
The constructor is the one that looks strange at first, because the name appears twice as Rectangle::Rectangle and there is no return type in front of it. That is normal for a constructor, its job is just to fill in the object when you write Rectangle r1(10, 20), so it takes the two numbers that got passed in and copies them into the private variables width and height.
Notice that the two getters do not calculate anything, they just hand back the private variable. That is the reason they exist at all. width and height are private, so main cannot touch them directly, and getWidth() is the little window that lets main read the value without being able to change it.
For getPerimeter and getArea, those functions can see width and height even though main cannot, because they are members of the class, which is why you do not pass anything in to them.
The one that trips people up is scale. It is void, so it does not return a new rectangle, it changes the one you already have. That is why r1.scale(0.5) on its own line with no cout around it is correct, and why every getter after that line gives you the new smaller numbers.
I compiled and ran it to be sure, and with a width of 10 and a height of 20 it prints 10 20, then 60 for the perimeter and 200 for the area, and after scaling by 0.5 it prints 5 10, then 30 and 50, which is exactly what the comments in main are asking for.
One last thing, since both variables get multiplied by 0.5 the area drops to a quarter of what it was, from 200 down to 50, not to half. That catches a lot of people and it is worth knowing for geometry questions too.