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;
}
对象的生存期
- 局部(当对象被定义时调用构造函数,该对象被创建,当程序退出定义该对象所在的函数体或程序块时,调用析构函数,释放该对象)
- 静态(当程序第一次执行所定义的静态对象时,该对象被创建;当程序再次运行到该对象定义所在时,直接跳过;当程序结束时,该对象被释放)
- 全局(当程序开始时,调用构造函数创建该对象,当程序结束时调用析构函数释放该对象)。
例如:
#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已存在,不再初始化