C++学习

C++预处理命令:宏定义define;文件包含<>和“”;条件编译

类型转换

自动转换:char→short→int→uint→long→ulong→float→double

强行转换:static_cast;dynamic_cast;const_cast;reinterpret_cast

函数转换:atoi;atol;atof;sprintf;…

数据库连接

Windows:可用ADO方式:

Linux:连接Mysql可用Mysql++或mysqlclient;连接Oracle可用occi;

构造函数与析构函数(string类):

String(const char *str){

 if(str==NULL){

  m_data = new char[1];

  *m_data = '\o';

 } else {

  int length = strlen(str);

  m_data = new char[length+1];

  strcpy(m_data,str);

 }

}

String(const String &other){

 int length = strlen(other.m_data);

 m_data = new char[length+1];

 strcpy(m_data,other.m_data);

}

~String(void){

 delete[] m_data;

}

String &operate=(const String &other)

{

 if(this == &other)

 return *this;

 delete [] m_data;

 int length = strlen(other.m_data);

 m_data = new char[length+1];

 strcpy(m_data,other.m_data);

 return *this;

}

char *strcpy(char *strDest,const char *strSrc)

{

 assert(strDest!=NULL && strSrc!=NULL);

 char *address = strDest;

 while((*strDest++ = *strSrc++) != '\o')

 NULL;

 return address;

}

对象的生存期

  1. 局部(当对象被定义时调用构造函数,该对象被创建,当程序退出定义该对象所在的函数体或程序块时,调用析构函数,释放该对象)
  2. 静态(当程序第一次执行所定义的静态对象时,该对象被创建;当程序再次运行到该对象定义所在时,直接跳过;当程序结束时,该对象被释放)
  3. 全局(当程序开始时,调用构造函数创建该对象,当程序结束时调用析构函数释放该对象)。

例如:

#include <iostream.h>

#include <string.h>

class A{

public:

 A(int sma) { cout<<"A"<<"  "<<sma<<endl; }

};

void fn(int n){

  static A  sm(n);

  cout<<"fn"<<"  "<<n<<endl;

}

void  main( ){

 fn(10);

 fn(20);

}

输出结果为:

A  10   //对象sm在构造时,由构造函数输出

fn  10 

fn  20  //对象sm 是静态对象,因此再次调用fn时sm已存在,不再初始化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值