c++总结

上传人:xzh****18 文档编号:46772148 上传时间:2018-06-27 格式:PDF 页数:33 大小:601.04KB
返回 下载 相关 举报
c++总结_第1页
第1页 / 共33页
c++总结_第2页
第2页 / 共33页
c++总结_第3页
第3页 / 共33页
c++总结_第4页
第4页 / 共33页
c++总结_第5页
第5页 / 共33页
点击查看更多>>
资源描述

《c++总结》由会员分享,可在线阅读,更多相关《c++总结(33页珍藏版)》请在金锄头文库上搜索。

1、1一、一、C+基础:基础:/基于 Win32 Console Application 空工程:cpp1、第一个、第一个 C+程序程序/新建一个 Win32 Console Application 空工程 #include using namespace std;/使用命名空间 stdstruct point int x; int y; ;/不能漏了 分号int main() point pt; pt.x=10; pt.y=20; cout using namespace std;/使用命名空间 stdstruct point int x; int y; void output()/C+中结构体

2、可以定义函数 coutclass point public: int x; int y; void output() cout3class point public: int x; int y; void init() x=3; y=7; void output() coutclass point public: int x; int y; point()/构造函数连 void 也不能有 4x=5; y=8; void output() coutclass point public: int x; int y; point()/连 void 也不能有 coutclass point publi

3、c: int x; int y; point()/连 void 也不能有 coutclass point public: int x; int y; point()/连 void 也不能有 coutx=x;7this-y=y; void output() cout 指向,运行结果:8、类的继承类的继承/新建一个 Win32 Console Application 空工程 #include class animal public: void eat()8 coutclass animal 9public: animal() coutclass animal public: animal() co

4、utclass class1 public: void output1() coutclass animal public: animal() coutbreathe(); int main() animal *pan; fish fh; pan=/pan 指向 fh 对象, /但编译器认为 fh 是 animal 对象的地址,所以调用 animal 的 breath fn(pan); return 0; 13、virtual 虚函数虚函数/新建一个 Win32 Console Application 空工程 #include class animal public:15animal() co

5、utbreathe(); int main() animal *pan;16fish fh; pan=/pan 指向 fh 对象,用 virtual 声明会根据实际类型来调用相应函数 fn(pan); return 0; 14、纯虚函数,抽象类纯虚函数,抽象类/新建一个 Win32 Console Application 空工程 #include class animal public: animal() coutbreathe(); int main() animal *pan; fish fh; pan=/pan 指向 fh 对象, /但编译器认为 fh 是 animal 对象的地址,所以

6、调用 animal 的 breath fn(pan); return 0; 15、函数的覆盖函数的覆盖/新建一个 Win32 Console Application 空工程 #include class animal public: animal() coutbreathe(); int main() fish fh; fh.sleep();19return 0; 16、函数的隐藏函数的隐藏/新建一个 Win32 Console Application 空工程 #include class animal public: animal() coutbreathe(); int main() fi

7、sh fh; fh.sleep(); return 0; 17、引用引用#include int main() int tk=199; int /用 delete pan;/删除对象 return 0; 21、对象复制对象复制/新建一个 Win32 Console Application 空工程 #include class animal public: int year; int month; int day;24animal() coutclass animal public: int year; int month; int day;animal() coutclass animal p

8、rivate: int year; int month; int day;friend void today(animal an); /把该函数声明为类内的友元函数,否则不能访问类内私有 成员,若为公有成员可以不用友元public: animal(int y,int m,int d) coutclass today; /对 today 类的提前引用声明class time /定义 Time 类 private: int hour; int minute; int sec;public: time(int h,int m,int s) hour=h; minute=m; sec=s; void

9、distoday(today day);/在 time 类中访问 today 私有成员,使用友元28;class today /声明 today 类 private: friend void time:distoday(today day);/声明为类 today 中的友元成员函数,使 time 类中能访问 today 类中的私有成员int month; int day; int year;public: today(int y,int m,int d) year=y; month=m; day=d; ;/*要在外面(today 类的后面)实现该函数*/ void time:distoday(

10、today day) coutclass time;/先声明 time 类class today private: int month; int day; int year;public: today(int y,int m,int d) year=y; month=m; day=d; friend time;/在 today 类中友元 time 类,使 today 类中的私有成员能被 time 类访 问 ;class time private: int hour; int min; int sec;public: time(int h,int m,int s) hour=h; min=m;

11、sec=s; void show(today day) couttemplate/类模板定义 class sum public: sum(datatype a,datatype b,datatype c) cout sum1(2,3,4); /定义 sum 对象,数据类型为 int,求和 sum sum2(2.6,2.9,9.4);/定义 sum 对象,数据类型为 float,求和 return 0; 3127、重载运算符重载运算符#include struct data int sum; int dt; int mul; int div; ; data operator * (data d1

12、,data d2) d1.sum=d1.sum+d2.sum; d1.dt=d1.dt-d2.dt; d1.mul=d1.mul*d2.mul; d1.div=d1.div/d2.div; return d1; /*/ data operator += (data d1,data d2) d1.sum=d1.sum+d2.sum; d1.dt=d1.dt+d2.dt; d1.mul=d1.mul+d2.mul; d1.div=d1.div+d2.div; return d1; /*/ int main( ) data d1=1,2,3,4; data d2=11,12,13,14; data

13、d=d1 * d2;/重载运算符函数的调用 coutint main( ) try/检查异常 char *s; s=“Hello Word!“; while(*s) if(*s=o)/是 o throw *s;/抛出异常 else coutint main( ) try/检查异常 char *s; s=“Hello Word!“; while(*s) if(*s= )/是 空格 throw *s;/抛出异常 else cout*s; s+; coutendl; catch(.)/捕获异常,任何数据类型异常 coutendl“异常“endl; ;/分号可用,可不用return 0; 2014 年 2 月 13 日星期四 誻譏勇

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

当前位置:首页 > 办公文档 > 总结/报告

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