1 #include<iostream>
2 using namespace std;
3 class Rectangle {
4 public:
5 int j;
6 void area(int X = 0, int Y = 0, int A = 0, int B = 0);
7 private:
8 int x, y, a, b;
9 };
10 void Rectangle::area(int X, int Y, int A, int B) {
11 x = X; y = Y; a = A; b = B;
12 j = (a - x) * (b - y);
13 }
14 int main() {
15 int x, y, a, b;
16 Rectangle rectangle;
17 cout << "输入左下角坐标x和y" << endl;
18 cin >> x >> y;
19 cout << "输入右上角坐标为a和b" << endl;
20 cin >> a >> b;
21 rectangle.area(x, y, a, b);
22 cout << "该矩形面积为:" << rectangle.j << endl;
23 return 0;
24 }
1 #include <iostream>
2 using namespace std;
3 class rectangle {
4 public:
5 rectangle(double len, double wide) {
6 length = len;
7 widen = wide;
8 }
9 ~rectangle() {};
10 double getarea() { return length * widen; }
11 double getlength() { return length; }
12 double getwiden() { return widen; }
13 private:
14 double length;
15 double widen;
16 };
17 void main() {
18 double length, widen;
19 cout << "请输入矩形的长:";
20 cin >> length;
21 cout << "请输入矩形的宽:";
22 cin >> widen;
23 rectangle r(length, widen);
24 cout << "长为" << length << ",宽为" << widen << "的矩形面积是" << r.getarea() << endl;
25 }
26
1 #include <iostream>
2 using namespace std;
3 class circle {
4 public:
5 circle(double radius) {
6 r = radius;
7 }
8 ~circle() {};
9 double getarea() { return 3.14 * r * r; }
10 private:
11 double r;
12 };
13 void main() {
14 cout << "请输入圆的半径:";
15 double radius;
16 cin >> radius;
17 circle r(radius);
18 cout << "圆的面积是:" << r.getarea() << endl;
19 }