Loading... # 1 ```cpp include<iostream> #include<cmath> using namespace std; class container{ protected: double radius; public: container(double r) : radius(r) {}; virtual double surface_area() = 0; virtual double volume() = 0; }; class cube : public container{ public: cube(double r) : container(r) {}; double surface_area() override { return 6 * pow(radius, 2); } double volume() override { return pow(radius, 3); } }; class sphere : public container{ public: sphere(double r) : container(r) {}; double surface_area() override { return 4 * M_PI * pow(radius, 2); } double volume() override { return (4.0/3.0) * M_PI * pow(radius, 3); } }; class cylinder : public container{ private: double height; public: cylinder(double r, double h) : container(r), height(h) {}; double surface_area() override { return 2 * M_PI * pow(radius, 2) + 2 * M_PI * radius * height; } double volume() override { return M_PI * pow(radius, 2) * height; } }; int main(){ double r = 5.0, h = 6.0; cube C(r); sphere S(r); cylinder Y(r, h); cout << "The surface area of cube is " << C.surface_area() << endl; cout << "The volume of cube is " << C.volume() << endl; cout << "The surface area of sphere is " << S.surface_area() << endl; cout << "The volume of sphere is " << S.volume() << endl; cout << "The surface area of cylinder is " << Y.surface_area() << endl; cout << "The volume of cylinder is " << Y.volume() << endl; return 0; } ``` # # 2 ```cpp #include <iostream> #include <set> using namespace std; class Set{ private: set<int> data; public: Set(){}; Set(const set<int>& s){ data = s; } Set(const initializer_list<int>& lst){ for(auto x : lst){ data.insert(x); } } friend Set operator+(const Set& a, const Set& b){ set<int> s = a.data; for(auto x : b.data){ s.insert(x); } return Set(s); } friend Set operator-(const Set& a, const Set& b){ set<int> s = a.data; for(auto x : b.data){ s.erase(x); } return Set(s); } friend Set operator&(const Set& a, const Set& b){ set<int> s; for(auto x : a.data){ if(b.data.count(x)){ s.insert(x); } } return Set(s); } void print(){ cout << "{"; for(auto x : data){ cout << x << " "; } cout << "}" << endl; } }; int main(){ Set a = {9, 5, 5, 3, 6, 7}; Set b = {2, 4, 6, 9}; Set c = a + b; c.print(); Set d = a - b; d.print(); Set e = a & b; e.print(); return 0; } ``` 最后修改:2023 年 08 月 05 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏