每个 case 语句的结尾不要忘了加 break,否则将导致多个分支重叠 (除非有意使多个分支重叠)。
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 //定义一个枚举类型 6 enum Color {Red,Yellow,Green,White}; 7 //圆类Circle的定义 8 class Circle { 9 float radius; 10 public: 11 Circle(float r) { 12 radius=r; 13 cout<<"Circle initialized!"<<endl; 14 } 15 ~Circle() { //析构函数 16 cout<<"Circle destroyed!"<<endl; 17 } 18 float Area() { 19 return 3.1416*radius*radius; 20 } 21 }; 22 //桌子类Table的定义 23 class Table { 24 float height; 25 public: 26 Table(float h) { 27 height=h; 28 cout<<"Table initialized!"<<endl; 29 } 30 ~Table() { //构造函数 31 cout<<"Table destroyed!"<<endl; 32 } 33 float Height() { 34 return height; 35 } 36 }; 37 //圆桌类RoundTable的定义 38 class RoundTable:public Table,public Circle { 39 Color color; 40 public: 41 RoundTable(float h,float r,Color c); //构造函数 42 int GetColor() { 43 return color; 44 } 45 ~RoundTable() { //构造函数 46 cout<<"RoundTable destroyed!"<<endl; 47 } 48 }; 49 //圆桌构造函数的定义 50 RoundTable::RoundTable(float h,float r,Color c):Table(h),Circle(r) 51 { 52 color=c; 53 cout<<"RoundTable initialized!"<<endl; 54 } 55 //测试多继承中构造函数和析构函数的执行方式 56 57 int main(int argc, char** argv) { 58 59 RoundTable cir_table(15.0,2.0,Yellow); 60 61 cout<<"The table properties are:"<<endl; 62 //调用Height类的成员函数 63 cout<<"Height="<<cir_table.Height()<<endl; 64 65 //调用circle类的成员函数 66 cout<<"Area="<<cir_table.Area()<<endl; 67 68 //调用RoundTable类的成员函数 69 cout<<"Color="<<cir_table.GetColor()<<endl; 70 return 0; 71 }