|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <iostream>
- #include <cmath>
- using uint = unsigned int;
- class shape
- {
- private:
- unsigned int high;
- unsigned int bottom;
- public:
- shape(uint l = 0, uint w = 0) : high(l), bottom(w) {}
- shape(shape &sh) : high(sh.high), bottom(sh.bottom) {}
- virtual double area() { return high * bottom; }
- virtual uint girth() { return 2 * (high + bottom); }
- virtual ~shape() { std::cout << "基类" << std::endl; }
- };
- class parallelogram : public shape
- {
- public:
- parallelogram(uint pl = 0, uint pw = 0) : shape(pl, pw) {}
- ~parallelogram() { std::cout << "四边形" << std::endl; }
- };
- class triangle : public shape
- {
- private:
- uint A, B, C;
- public:
- triangle(uint a = 0, uint b = 0, uint c = 0) : A(a), B(b), C(c) {}
- triangle(triangle &t) : A(t.A), B(t.B), C(t.C) {}
- bool isEffTri()
- {
- bool t = true;
- if (A + B <= C || A + C <= B || B + C <= A)
- {
- t = false;
- }
- return t;
- }
- virtual uint girth()
- {
- return A + B + C;
- }
- virtual double area()
- {
- double t = this->girth() / 2;
- return sqrt(t * (t - A) * (t - B) * (t - C));
- }
- ~triangle() { std::cout << "三角形" << std::endl; }
- };
- int main(int argc, char const *argv[])
- {
- parallelogram a = {5, 6};
- triangle c = {3, 4, 5};
- shape &b = a;
- shape &d = c;
- std::cout << b.area() << " " << b.girth() << std::endl;
- std::cout << std::boolalpha << c.isEffTri() << "\n"
- << d.area() << " " << d.girth() << std::endl;
- return 0;
- }
复制代码
=============================================
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。
E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-xi5u5r2f.pvc --stdout=Microsoft-MIEngine-Out-f0v3iqur.5cy --stderr=Microsoft-MIEngine-Error-jarwxi4b.f01 --pid=Microsoft-MIEngine-Pid-za2qqxtf.z4l --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
30 22
true
6 12
三角形
基类
四边形
基类
E:\Users\admin\Documents\VScode\Code> |
|