在多重循环中,如果有可能,应当将最长的循环放在最内层

在多重循环中,如果有可能,应当将最长的循环放在最内层,最短的 循环放在最外层,以减少 CPU 跨切循环层的次数。

 

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 
 5 using namespace std;
 6 //定义基类First
 7 class First {
 8     int  num;
 9     float grade;
10 public:
11     //构造函数带参数
12     First(int n,float v ) : num(n),grade(v)
13     {
14         cout<<"The First initialized"<<endl;
15     }
16     DispFirst(void) {
17         cout<<"num="<<num<<endl;
18         cout<<"grade="<<grade<<endl;
19     }
20 };
21 
22 //定义派生类Second
23 class Second :public First {  
24     double val;
25 public:
26     //无参数构造函数,要为基类的构造函数设置参数
27     Second(void):First(10000,0) {
28         val=1.0;
29         cout<<"The Second initialized"<<endl;
30     }
31 
32     //带参数构造函数,为基类的构造函数设置参数
33     Second(int n,float x,double dx):First(n,x) {
34         val=dx;
35         cout<<"The Second initialized"<<endl;
36     }
37     Disp(char *name){
38         cout<<name<<".val="<<val<<endl;
39         DispFirst();
40     }
41 };
42 
43 //main()函数中创建和使用派生类对象
44 
45 int main(int argc, char** argv) {
46        //调用派生类的无参数构造函数
47     cout<<"Second s1;"<<endl;
48     Second s1;
49     cout<<"s1.Disp(\"s1\");"<<endl;
50     s1.Disp("s1");
51 
52     //调用派生类的有参数构造函数
53     cout<<"Second s2(10002,95.7,3.1415926); "<<endl;
54     Second s2(10002,95.7,3.1415926); 
55     cout<<"s2.Disp(\"s2\");"<<endl;    
56     s2.Disp("s2");
57     return 0;
58 }

 

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