-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path动态联编示例
43 lines (36 loc) · 821 Bytes
/
动态联编示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
using namespace std;
class Shape {
public:
virtual double area() const = 0;
};
class Rectangle : public Shape {
private:
double width;
double height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
virtual double area() const {
return width * height;
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
virtual double area() const {
return radius * radius * 3.14159;
}
};
int main() {
Shape* shape;
Rectangle rec(3, 4);
Circle cir(2.5);
shape = &rec;
cout << "Rectangle 测试: area = " << shape->area() << endl;
shape = ○
cout << "Circle 测试: area = " << shape->area() << endl;
return 0;
}
//gpt