
C++编写程序,建立矩形圆形类。
类的所有数据成员设置为私有的访问属性,通过类的成员函数获取类的数据成员的值以及矩形和圆形的面积,类中必须有构造和析构函数。要求:(1)矩形类中定义重载的构造函数。(2)圆...
类的所有数据成员设置为私有的访问属性,通过类的成员函数获取类的数据成员的值以及矩形和圆形的面积,类中必须有构造和析构函数。要求:
(1)矩形类中定义重载的构造函数。
(2)圆形类中定义用带默认参数值的构造函数。
(3)设置合适的主函数对所定义的类进行测试。
谢啦~ 展开
(1)矩形类中定义重载的构造函数。
(2)圆形类中定义用带默认参数值的构造函数。
(3)设置合适的主函数对所定义的类进行测试。
谢啦~ 展开
展开全部
CTest1 #include <iostream>#include <cmath>using namespace std;int main(){ while (true) { cout << "1.圆形" << endl; cout << "2.长方形" << endl; cout << "3.直角三角形" << endl; cout << "4.退出" << endl; int choice; cin >> choice; system("cls"); if (choice == 4) break; switch (choice) { case 1:{ double r = 0; cout << "请输入圆形的半径:"; cin >> r; cout << "圆形的面积:" << 3.14 * r * r << endl << "周长:" << 3.14 * 2 * r; }break; case 2:{ double l = 0.0, w = 0.0; cout << "请输入长方形的长和宽"<<endl; cout << "长:"; cin >> l; cout << "宽:"; cin >> w; cout << "长方形的面积:" << l * w << endl << "周长:" << 2 * (l + w); }break; case 3:{ double b = 0.0, h = 0.0; cout << "请输入直角三角形的长和宽" << endl; cout << "底:"; cin >> b; cout << "高:"; cin >> h; cout << "长方形的面积:" << 0.5 * b * h << endl << "周长:" << (b + h + sqrt(b*b + h*h)); }break; default:break; } getchar(); getchar(); system("cls"); } //getchar();getchar(); system("pause"); return 0;} 执行结果: CTest2 #include <iostream>#include <cmath>using namespace std;class Circle{public: Circle(double r) : radius(r){} double area(){ return 3.14*radius*radius; } double girth(){ return 3.14 * 2 * radius; }private: double radius;};class Rect{public: Rect(double l, double w) : length(l), width(w){} double area(){ return length * width;} double girth(){ return 2 * (length + width); }private: double length; double width;};class Tri{public: Tri(double b, double h) : bottom(b), height(h){} double area(){ return 0.5*bottom*height; } double girth(){ return (bottom + height + sqrt(bottom*bottom + height*height)); }private: double bottom; double height;};int main(){ while (true) { cout << "1.圆形" << endl; cout << "2.长方形" << endl; cout << "3.直角三角形" << endl; cout << "4.退出" << endl; int choice; cin >> choice; system("cls"); if (choice == 4) break; switch (choice) { case 1:{ double r = 0; cout << "请输入圆形的半径:"; cin >> r; Circle circle(r); cout << "圆形的面积:" << circle.area() << endl << "周长:" << circle.girth(); }break; case 2:{ double l = 0.0, w = 0.0; cout << "请输入长方形的长和宽"<<endl; cout << "长:"; cin >> l; cout << "宽:"; cin >> w; Rect rect(l, w); cout << "长方形的面积:" << rect.area() << endl << "周长:" << rect.girth(); }break; case 3:{ double b = 0.0, h = 0.0; cout << "请输入直角三角形的长和宽" << endl; cout << "底:"; cin >> b; cout << "高:"; cin >> h; Tri tri(b, h); cout << "长方形的面积:" << tri.area() << endl << "周长:" << tri.girth(); }break; default:break; } getchar(); getchar(); system("cls"); } //getchar();getchar(); system("pause"); return 0;} 执行结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询