VC++6.0解题方法代码:
#include <iostream> #include <string> using namespace std; const double PI = 3.14159; class Circle { private: double radius; public: Circle(double r){ radius = r;} double getarea(){return radius * radius * PI;} }; class Table { private: double height; public: Table(double h){ height = h;} double getheight(){ return height; } }; class Roundtable:public Circle,public Table { private: char color[8]; public: Roundtable(double r,double h,char *c):Circle(r),Table(h) { strcpy(color,c);} char* getcolor(){return color;} }; int main() { Roundtable rt(10.0,1.8,"黑色"); cout << rt.getarea() << endl; cout << rt.getheight() << endl; cout << rt.getcolor() << endl; return 0; }输出结果:
314.159 1.8 黑色
VS code 解题方法代码:
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> using namespace std; const double PI = 3.14159; class Circle { private: double radius; public: Circle(double r){ radius = r;} double getarea(){return radius * radius * PI;} }; class Table { private: double height; public: Table(double h){ height = h;} double getheight(){ return height; } }; class Roundtable:public Circle,public Table { private: char color[8]; public: Roundtable(double r,double h,const char *c):Circle(r),Table(h) { strcpy(color,c);} char* getcolor(){return color;} }; int main() { Roundtable rt(10.0,1.8,"黑色"); cout << rt.getarea() << endl; cout << rt.getheight() << endl; cout << rt.getcolor() << endl; return 0; }VS 2017解题方法代码:
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> using namespace std; const double PI = 3.14159; class Circle { private: double radius; public: Circle(double r) { radius = r; } double getarea() { return radius * radius * PI; } }; class Table { private: double height; public: Table(double h) { height = h; } double getheight() { return height; } }; class Roundtable :public Circle, public Table { private: string color; public: Roundtable(double r, double h, string c) :Circle(r), Table(h) { color = c; } string getcolor() { return color; } friend ostream &operator << (ostream &,Roundtable); //使用string类需要运算符重载 }; ostream &operator << (ostream &stream,Roundtable shuchu) { stream << shuchu.color ; return stream; } int main() { Roundtable rt(10.0, 1.8, "黑色"); cout << rt.getarea() << endl; cout << rt.getheight() << endl; cout << rt.getcolor() << endl; return 0; }