读书人

C和C++语言学习总结(一)

发布时间: 2008-12-15 10:38:36 作者: liuhuituzi

知识结构:

  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 bParam;

  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,sum;

  int a[100][5];

  for(row=0;row <100;row++) 效率低于 for(col=0;col <5;col++)

  { {

  for(col=0;col <5;col++) for(row=0;row <100;row++)

  { {

  sum = sum+a[row][col]; sum = sum+a[row][col];

  } }

  } }

  int i;

  for(i=0;i   { {

  if(condition) for(i=0;i   DoSomething(); DoSomething();

  else }

  DoOtherthing(); else

  } {

  for(i=0;i   DoOtherthing();

  }

  for (int x=0;x <=N-1;x++) 直观性差于 for (int x=0;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;

  }

  }

  }

  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只能修饰输入参数,不能修饰输出参数)

  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 int& 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 Func(const A&a )const的好处

  第一个const表示返回的是个内部产生的对象,它不能被修改

  const A Func(...)

  {...}

  const A a=Func(...);//不能是A a=Func(...);

  第二个const表示输入的参数是引用传递,函数内部不会产生临时对象,而且这个对象不能被内部修改

  第三个const表示此函数内部的所涉及的数据成员不能修改

  class Stack

  {

  int m_num;

  int GetCount(void) const;

  int Pop(void);

  }

  int Stack::GetCount(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;

  }

  动态生成内存的代码

  正确代码:

  void GetMemory(char **p, int num)

  {

  *p = (char *)malloc(sizeof(char) * num);

  }

  char* GetMemory2(int num)

  {

  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); // 注意参数是&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||strSrc==NULL) return NULL;

  char* pStr=strDest;

  while((*strDest++=*strSrc++)!=’\0)

  NULL;

  return pStr;

  }

  复合表达式

  d = (a = b + c) + r ;

  该表达式既求a 值又求d 值.应该拆分为两个独立的语句:

  a = b + c;

  d = a + r;

  if (a < b < c) // a < b < c 是数学表达式而不是程序表达式



  并不表示

  if ((a   而是成了令人费解的

  if ( (a   memcpy代码

  void* memcpy(char* strDest,const char* strSrc,size_t size)

  {

  if(strDest==NULL||strSrc==NULL) return NULL;

  if(size <=0) return NULL;

  char* pStr=strDest;

  while(size-->0)

  *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*,int*,float*,double*)

  enum 4

  ii.在32位操作系统中,定义或函数中的大小

  char a[]="hello";

  char b[100];

  char *p=a;

  类型 字节长度

  sizeof(a) 6

  sizeof(b) 100

  sizeof(p) 4

  void Func(char a[100])

  {

  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 i[10],j,k;

  };

  };

  sizeof(A) //48

  #pragma pack(1)

  struct A

  {

  enum day{monring, moon, aftermoon};

  };

  sizeof(A) //1

  sizeof(A::day) //4



3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.net/exam/

读书人网 >复习指导

热点推荐