不可在 for 循环体内修改循环变量,防止 for 循环失去控制

不可在 for 循环体内修改循环变量,防止 for 循环失去控制。

 

 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 class First {
 7     int val1;
 8 protected:
 9     void SetVal1(int v) {
10         val1=v;
11     }
12 public:
13     show_First(void) {
14         cout<<"val1="<<val1<<endl;
15     }
16 };
17 //派生类
18 class Second:public First {   
19     int val2;
20 protected:
21     void SetVal2(int v) {
22         SetVal1(v);  //使用First 基类的保护成员
23         val2=v;
24     }
25 public:
26     show_Second(void) {
27         show_First();
28         cout<<"val2="<<val2<<endl;
29     }
30 };
31 //派生类
32 class Third:public Second {   
33     int val3;
34 public:
35     void SetVal3(int n) {
36          SetVal1(n);  //使用First 基类的保护成员
37          SetVal2(n);  //使用Second基类的保护成员
38          val3=n;
39     }
40     show_Third(void) {
41         show_Second();
42         cout<<"val3="<<val3<<endl;
43     }
44 };
45 //main()函数的定义
46 
47 int main(int argc, char** argv) {
48         First f1;
49     //f1.SetVal1(1);   不可访问
50 
51     Second s1;
52     //s1.SetVal1(1);   不可访问
53     //s1.SetVal2(2);   不可访问
54 
55     Third  t1;
56     //t1.SetVal1(1);   不可访问
57     //t1.SetVal2(2);   不可访问
58     t1.SetVal3(10);
59 
60     //显示t1对象的数据
61     cout<<"t1.show_Third();"<<endl;
62     t1.show_Third();
63     cout<<"t1.show_Second();"<<endl;
64     t1.show_Second();
65     cout<<"t1.show_First();"<<endl;
66     t1.show_First();
67     return 0;
68 }

 

posted @ 2018-08-03 12:40  borter  阅读(589)  评论(0编辑  收藏  举报