2022年C和C++语言学习总结 .pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2022年C和C++语言学习总结 .pdf》由会员分享,可在线阅读,更多相关《2022年C和C++语言学习总结 .pdf(13页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、C 和 C+语言学习总结(资料来自 林锐博士2001 年 7 月 24)知识结构:1、if,for,switch,goto 2、#define,const 3、文件拷贝的代码,动态生成内存,复合表达式,strcpy,memcpy,sizeof 4、函数参数传递,内存分配方式,内存错误表现,malloc 与 new 区别5、类重载、隐藏与覆盖区别,extern 问题,函数参数的缺省值问题,宏代码与内联函数区别6、构造和析构的次序,String 函数定义具体实现:1、if,for,switch,goto if:bool int float pointer char 变量的使用方法bool bPar
2、am;int iParam;float fParam;int*pParam;char cParam;if(bParam),if(!bParam);if(iParam=0),if(iParam!=0);if(fParam=-0.00001&fParam=0.00001);if(pParam=NULL),if(pParam!=NULL);if(cParam=0),if(cParam!=0);if/else/return 的使用方法if(condition)可以等价为return(condition?x:y);return x;else return y;for:执行效率问题:int row,col
3、,sum;int a1005;名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 13 页 -for(row=0;row100;row+)效率低于for(col=0;col5;col+)for(col=0;col5;col+)for(row=0;row100;row+)sum=sum+arowcol;sum=sum+arowcol;int i;for(i=0;iN;i+)效率低于if(condition)if(condition)for(i=0;iN;i+)DoSomething();DoSomething();else DoOtherthing();else for(i=0;iN;
4、i+)DoOtherthing();for(int x=0;x=N-1;x+)直观性差于for(int x=0;xN;x+)switch:switch(variable)case value1:.break;case value2:.break;default:.break;switch(c)中的 c 的数据类型可以是int,char,long,unsigned int,bool.variable 必须是整数或者强制为整数,由于 char 实际上是 ASCII 码,所以也可以.c 不可以是 double,float,char*.goto:goto 主要用于.goto error;名师资料总结-
5、精品资料欢迎下载-名师精心整理-第 2 页,共 13 页 -error:.2、#define,const#define 和 const 区别1、#define C 语言const C语言C+语言const常量有数据类型,编译器会进行类型安全检查,而#define 没有数据类型,const的常量可以进行调试,但宏常量不能进行调试.2、const 的使用方法在全局定义const float PI=3.1415926 在类中定义class A.A(int size);const int SIZE;A:A(int size):SIZE(size).对参数和函数的定义(const只能修饰输入参数,不能修
6、饰输出参数)const int x=1;表示 x 的值是 1,在程序中不能改变;const int*x;表示 x 代表的地址所指向的内容是不能改变得;int const*x;与 const int*x;的表示的意思一样;int*const x;表示 x 代表的地址是不能改变的;当是输入参数时,不需要是 void Func(const int i),void Func(const int&i),可以是 void Func(int i)因为输入参数采用值传递(const int i),由于函数将自动产生临时变量用于复制该参数,该输入参数本来就无需保护,所以不要加const修饰;不用 const i
7、nt&i 的原因在于内部数据类型的参数不存在构造、析构的过程,而复制也非常快,值传递 和引用传递 的效率几乎相当.当是输入参数时,不需要是 void Func(const A a),void Func(A a),可以是 void Func(A&a)或 void Func(const A&a)不用 const A a,A a 的原因是函数的效率比较低,因为函数体内将产生A 类型的临时对象用于复制参数a,而临时对象的构造、复制和析构过程都需要消耗时间最好用 const A&a 的原因是 A&a 中的 a可以被改变,A&a 和 const A&a 的好处在于都不会产生临时对象,效率高;const A
8、 Func(const A&a)const 的好处第一个 const 表示返回的是个内部产生的对象,它不能被修改const A Func(.)名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 13 页 -.const A a=Func(.);/不能是 A a=Func(.);第二个 const 表示输入的参数是引用传递,函数内部不会产生临时对象,而且这个对象不能被内部修改第三个 const 表示此函数内部的所涉及的数据成员不能修改class Stack int m_num;int GetCount(void)const;int Pop(void);int Stack:GetCoun
9、t(void)const m_num+;/编译错误,企图修改数据成员m_num;Pop();/编译错误,企图调用非const 函数 3、文件拷贝的代码#include int main(int argc,char*argv)printf(Hello World!n);FILE*in;FILE*out;in=fopen(d:1.txt,rb);out=fopen(d:2.txt,wb);char ch=fgetc(in);while(!feof(in)fputc(ch,out);ch=fgetc(in);fclose(in);fclose(out);return 0;动态生成内存的代码-正确代码
10、:void GetMemory(char*p,int num)*p=(char*)malloc(sizeof(char)*num);char*GetMemory2(int num)名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 13 页 -char*p=(char*)malloc(sizeof(char)*num);return p;-错误的代码:void GetMemory3(char*p,int num)p=(char*)malloc(sizeof(char)*num);-void Test(void)char*str=NULL;GetMemory(&str,100);/注意
11、参数是&str,而不是 str strcpy(str,hello);cout str endl;free(str);str=NULL;str=GetMemory2(100);strcpy(str,hello);cout str endl;free(str);str=NULL;GetMemory3(str,100);/str 仍然为 NULL strcpy(str,hello);/运行错误cout str endl;/运行错误free(str);/运行错误 strcpy 代码char*strcpy(char*strDest,const char*strSrc)if(strDest=NULL|st
12、rSrc=NULL)return NULL;char*pStr=strDest;while(*strDest+=*strSrc+)!=0)NULL;return pStr;复合表达式名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 13 页 -d=(a=b+c)+r;该表达式既求a 值又求 d 值.应该拆分为两个独立的语句:a=b+c;d=a+r;if(a b c)/a b c 是数学表达式而不是程序表达式并不表示if(ab)&(bc)而是成了令人费解的if(ab)c)memcpy 代码void*memcpy(char*strDest,const char*strSrc,size_
13、t size)if(strDest=NULL|strSrc=NULL)return NULL;if(size0)*strDest+=*strSrc+;return pStr;sizeof:i.在 32 位操作系统中,基本数据类型类型 字节长度char 1 short 2 short int 2 signed short 2 unsigned short 2 int 4 long int 4 signed int 4 unsigned int(unsigned)4 long 4 unsigned long 4 float 4 double 8 void*4(所有指针类型长度都一样)(char*,
14、int*,float*,double*)enum 4 ii.在 32 位操作系统中,定义或函数中的大小char a=hello;名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 13 页 -char b100;char*p=a;类型 字节长度sizeof(a)6 sizeof(b)100 sizeof(p)4 void Func(char a100)sizeof(a);/4#pragma pack(1)struct A int i;char j;sizeof(A)/5#pragma pack(1)struct A int o;int j;union int i10,j,k;size
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年C和C+语言学习总结 2022 C+ 语言 学习 总结
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内