Struct with Methods
Completed
Give struct Rect { double w, h; } methods area(), perimeter() and isSquare(). Read rectangles and print all three (2 decimals, yes/no).
Input
n, then n lines w h.
Output
n lines.
Example
Input
2 3 4 2.5 2.5
Output
12.00 14.00 no 6.25 10.00 yes
Editor
C++
Hints
const after a method means it doesn't change the object.
Solution explanation
Marking read-only methods const lets them be called on const objects.
Show solution
#include <iostream>
#include <iomanip>
using namespace std;
struct Rect {
double w, h;
double area() const { return w * h; }
double perimeter() const { return 2 * (w + h); }
bool isSquare() const { return w == h; }
};
int main() {
int n;
cin >> n;
cout << fixed << setprecision(2);
for (int i = 0; i < n; i++) {
Rect r;
cin >> r.w >> r.h;
cout << r.area() << " " << r.perimeter() << " " << (r.isSquare() ? "yes" : "no") << "\n";
}
return 0;
}