命名空间-new-delete-引用-重载-内联-类-构造-this

上传人:kms****20 文档编号:41012728 上传时间:2018-05-28 格式:DOC 页数:21 大小:52KB
返回 下载 相关 举报
命名空间-new-delete-引用-重载-内联-类-构造-this_第1页
第1页 / 共21页
命名空间-new-delete-引用-重载-内联-类-构造-this_第2页
第2页 / 共21页
命名空间-new-delete-引用-重载-内联-类-构造-this_第3页
第3页 / 共21页
命名空间-new-delete-引用-重载-内联-类-构造-this_第4页
第4页 / 共21页
命名空间-new-delete-引用-重载-内联-类-构造-this_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《命名空间-new-delete-引用-重载-内联-类-构造-this》由会员分享,可在线阅读,更多相关《命名空间-new-delete-引用-重载-内联-类-构造-this(21页珍藏版)》请在金锄头文库上搜索。

1、命名空间命名空间-new-delete-new-delete-引用引用- -重载重载- -内联内联- -类类- -构造构造-this-this名字空间是一种描述逻辑分组的机制,如果有一些声明按照某种准则在逻辑上属于同一集团,就可以将它们放入同一个名字空间,以表明这个事实。防止命名冲突也是一个很重要的原因。一个人写的程序放到一个名字空间中也是一种可取的做法。成员可以在名字空间的定义里声明,而后采用 namespace-name:member-name 的形式去定义一个名字空间的成员必须采用如下的记法形式引入:namespace namespace-name/声明和定义-严格的类型检查-enum

2、在 c 中直接当整数,而 C+中是一种独立的类型C+中,调用函数不做类型提升C+增加了 bool 类型,true, false函数的参数表严格匹配,空参代表没有任何参数。函数参数表中的参数如果没有参数名,只有类型,称作为哑元,一般是为了兼容性的考虑。C 中的隐式声明在 C+中去掉了,返回类型 int 型的函数必须声明或定义。1 #include 2 using namespace std;3 enum Season SPRING, SUMMER, AUTUMN, WINTER;4 struct Person5 string name;6 int age;7 ;8 void f1()9 cout

3、 () 不会破坏数据或使数据丢失转换时做静态检查,即在编译时进行reinterprect_case()允许强转任何类型的指针把整数强转成指针,指针强转成整数const_cast()去掉 cv 限制dynamic_case()动态转换1 #include2 using namespace std;3 int main()4 5 double d=3.14;6 int i=(int)d;7 i=int(d);8 cout(d);/静态检查10 cout(pd);14 pi=reinterpret_cast(15 cout(19 *pci=200;20 cout2 #include3 using n

4、amespace std;4 int main()5 6 int *p=static_cast(malloc(sizeof(int)*10);7 for(int i=0;i2 using namespace std;3 void f1(int *p)4 *p=100;5 6 void f2(int 8 9 void swap(int 11 a=b;12 b=t;13 14 int 16 return x;17 18 int f4(int 20 return x;21 22 int 24 return x;/不要返回局部变量的引用,会警告25 26 struct Person27 string

5、name;28 int age;29 ;30 void f6(Person 32 p.age=30;33 34 int main()35 36 int i=10;37 int x=20;38 int y=30;39 f1(40 coutusing namespace std;void printa(int *a,int n=10, char c= )/默认 n=10,c= for(int i=0; iusing namespace std;struct Dateint year;int month;int day;void showmember(Date *a,int n, int Date:

6、* p)for(int i=0; i*p = 2 using namespace std;3 int main()4 5 string s;6 s=“abc“;7 coutstr2)=str2)2 using namespace std;3 class Sample4 int x;5 const int ci;6 int 7 public:8 /初始化列表只允许在构造函数中9 Sample(const int i,int 11 int main()12 13 int x=10;14 Sample s(10,x);15 -小时钟1 #include2 #include3 #include4 us

7、ing namespace std;5 class Time6 int h;7 int m;8 int s;9 public:10 Time()/默认构造函数11 /构造函数12 Time(int hour,int minute,int second)13 h=hour;14 m=minute;15 s=second;16 17 void show()18 cout=60)24 s=0;25 m+;26 27 if(m=60)28 m=0;29 h+;30 31 if(h=24)32 h=0;33 34 35 ;36 int main()37 38 time_t tt=time(NULL);3

8、9 tm *tmp=localtime(40 Time t(tmp-tm_hour,tmp-tm_min,tmp-tm_sec);41 /Time t(14,20,55);42 t.show();43 for(int i=0;i2 #include3 #include “time.h“4 using namespace std;5 6 / Time()/默认构造函数7 /构造函数8 Time:Time(int hour,int minute,int second)9 10 h = hour;11 m = minute;12 s = second;13 14 void Time: show()1

9、5 cout=60)21 s=0;22 m+;23 24 if(m=60)25 m=0;26 h+;27 28 if(h=24)29 h=0;30 31 ;main.cpp1 #include2 #include3 #include“time.h“4 using namespace std;5 int main()6 7 time_t tt=time(NULL);8 tm *tmp=localtime(9 Time t(tmp-tm_hour,tmp-tm_min,tmp-tm_sec);10 /Time t(14,20,55);11 for(;)12 13 t.step();14 sleep

10、(1);15 t.show();16 17 coutname = name;16 this-age = age;17 this-salary = salary;18 void showE(Employee *e);19 showE(this);20 21 void show()22 23 coutshow();34 35 int main()36 37 Employee e(1,“guodh“, 30, 10000);38 e.show();39 Employee e2(2,“tianzhen“, 18, 7000);40 e2.show();41 e2.grewup().grewup().grewup().show();42

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号