C++学习笔记_two_day

  1. 函数的高级特性(2

  1. 重载函数 overload c++特有

定义:具有相同函数名 但参数列表不同 的一类函数

参数列表不同:

  1. 参数的个数不同

  2. 参数的数据类型不同

  3. 参数的顺序不同

void show();

void show( int i );

void show(double d); //类型不同

void show(int i,int j); //个数不同

void show(int j, int i) ; //顺序不同

 

什么时候使用重载函数:

当函数的功能一样但参数不同的时候

C语言实现不同数据的交换

swap_int( int ,int );

swap_double( double,double);

C++:

swap(int ,int );

swap(double,double);

char(char,char); 

例如:overload.cc

 1 //overload.cc
 2 #include <iostream>
 3 using namespace std;
 4 
 5 void show()
 6 {
 7     cout << " show() " << endl;
 8     return ;
 9 }
10 
11 void show(int i)
12 {
13     cout << " show(int) " << endl;
14     cout << " i = " << i << endl;
15     return ;
16 }
17 
18 
19 int main(int argc,char **argv)
20 {
21     show();
22     show(123456);
23 
24     return 0;
25 }

 

  1. 带形参默认值的函数 C++特有

void add(int i=10);

优 点:

可以频繁调用的时候,减少参数的传入

注意点:

带形参默认值函数的声明和定义分开的时候要注意默认值存放的位置

易错点:

当有多个参数的时候,只能依次从右向左写默认值

//error 3.14 赋值给i

void show(int i=10 , double d , char c=’a’);//错误的

show(3.14);//3.14不会赋值double d 是赋值给哪个了和系统相关

//void show(int i , double d , char c=’a’);//正确的

例如:default_param.cc

 1 //default_param.cc
 2 #include<iostream>
 3 
 4 using namespace std;
 5 
 6 //补全函数的声明 - 分配空间
 7 
 8 //int j是一个局部变量
 9 
10 void test(int j=20);
11 
12 int main(int argc,char *argv[])
13 
14 {
15 
16 //不传入值则使用默认值
17 
18 test();
19 
20 //也可以当作一般函数使用
21 
22 test(120);
23 
24 return 0;
25 
26 }
27 
28 //定义test函数 - 将代码放在指定(函数)的位置
29 
30 void test(int j)////注意
31 
32 {
33 
34 cout<<"j="<<j<<endl;
35 
36 return;
37 
38 }
  1. 类和对象

哲学题:

地球是由什么组成的?

物理学家:物质

生物学家:环境 +

化 学 家:元素

数 学 家:数字

普 通 人:.........

C + +:类组成

类 种类

具有相似特征的事物

生活举例:

类:抽象

猴子(yes) 孙悟空(no) 动物(yes) 小狗(yes) 小白(no) 蟑螂(yes) 小强(yes

C++如何描述一个类的

c语言写一个时钟, hour/minute/second 显示时间(show/走动(tick

例如:class_clock.cc

 1 //class_clock.cc
 2 #include <unistd.h>
 3 #include<stdlib.h>
 4 
 5 #include <iostream>
 6 using namespace std;
 7 
 8 //typedef struct _STCLOCK
 9 
10 // 定义一个类
11 
12 // 表   <----->  人
13 // 身高 体重 年龄
14 // 小时 分钟  秒钟(属性)  
15 // 吃 喝 玩 乐
16 //  显示时间  /  走动时间  / 运行(行为)
17 
18 class STCLOCK
19 {
20 public:  //访问修饰符 让数据在外部被访问
21     int h;   //小时
22     int m;   //分钟
23     int s;   //秒钟
24 
25     void set(int t_h,int t_m,int t_s)
26     {
27         h = t_h;
28         m = t_m;
29         s = t_s;
30         return ;
31     }
32 
33     void show()
34     {
35         // 11:57:23
36         if (h < 10)
37         cout << 0;
38         cout << h << ":";
39 
40         if (m < 10)
41         cout << 0;
42         cout << m << ":";
43 
44         if (s < 10)
45         cout << 0;
46         cout << s << endl;
47         return ;
48     }
49 
50     void tick()
51     {
52         sleep(1);
53         if(++s > 59)
54         {
55             s = 0;
56             if(++m > 59)
57             {
58                 m = 0;
59                 if(++h > 23)
60                 h = 0;
61             }
62         }    
63         return ;
64     }
65 
66     void run()
67     {
68         while(1)
69         {
70             system("clear");
71              show();
72              tick();
73         }
74         return ;
75     }
76 
77 };
78 //  home work 
79 
80 // 用 类 写一个定时炸弹
81 
82 //  设定一个时间
83 //  运行定时炸弹 cout << " bomb ~~~~~" << endl;
84 int main(int argc,char **argv)
85 {
86     STCLOCK stClock;
87     stClock.set(23,59,55);
88     stClock.run();
89     return 0;
90 }

 

// home work

// 用 类 写一个定时炸弹

// 设定一个时间

// 运行定时炸弹 cout << " bomb ~~~~~" << endl;

int main(int argc,char **argv)

{

  STCLOCK stClock;

  stClock.set(23,59,55);

  stClock.run();

  return 0;

}



C++的类是由C中的struct演变而来

操作struct中的成员数据 函数 也 包含在struct内部,这类函数内部、、可以自由的操作struct中的数据。

Struct只是包含数据

C++ 中的类是一种结构体 包含 数据和函数

定义一个类 class


Public访问修饰符 让数据在外部被访问


作业:用类来写一个定时炸弹 设定时间 运行定时炸弹 cout<<”bomb----”<<endl;

解答:bomb.cc

 

 1 //bomb.cc
 2 #include<stdlib.h>
 3 #include<unistd.h>
 4 
 5 #include<iostream>
 6 using namespace std;
 7 
 8 class STCLOCK
 9 {
10 public:
11     int m;
12     int s;
13 
14     void set()
15     {
16         cout<<"m> "<<endl;
17         cin>>m;
18         cout<<"s> "<<endl;
19         cin>>s;
20         return ;
21     }
22 
23     void show()
24     {
25         cout<<"00"<<":";
26 
27         if(m<10)
28             cout<<0;
29         cout<<m<<":";
30         if(s<10)
31             cout<<0;
32         cout<<s<<endl;
33         if(0==m && 0==s)
34         {
35             sleep(1);
36             system("rhythmbox 风情万种.mp3");
37             cout<<endl;
38             cout<<" bomb --"<<endl;
39             exit(0);
40         }
41         return ;
42     }
43 
44     void tick()
45     {
46         sleep(1);
47         if(--s<0)
48         {
49             s=59;
50             if(--m<0)
51             {
52                 m=59;
53             }
54         }
55         return ;
56     }
57 
58     void run()
59     {
60         while(1)
61         {
62             system("clear");
63             show();
64             tick();
65         }
66         return;
67     }
68 };
69 
70 int main()
71 {
72     STCLOCK stCLOCK;
73     stCLOCK.set();
74     stCLOCK.run();
75     
76     return 0;
77 }

 

 

    by   吴尚奇 Devil_box 2014/05

 

posted @ 2014-05-22 22:17  一页书_Devil_box  阅读(137)  评论(0编辑  收藏  举报