不可在 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 }

 

转载于:https://www.cnblogs.com/borter/p/9413503.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值