C++
引用
int add(int &b)
{
b=b+1;
return b;
}
int main()
{
int a=99;
printf("%d\n",add(a));
}
构造函数
#include<iostream>
using namespace std;
class Person{
private:
char *name;
int age;
char *work;
public:
Person(){cout<<"Person()"<<endl;}
Person(char *name){
cout<<"Person(char *name)"<<endl;
this->name=name;
}
Person(char *name,int age,char *work="none"){
cout<<"Person(char *name,int age)"<<endl;
this->name=name;
this->age=age;
this->work=work;
}
void printfInfo(void)
{
cout<<"name="<<name<<" age="<<age<<" work="<<work<<endl;
}
};
int main()
{
Person Per("chenmiaohong",16);
Person Per2;//调用无参数构造函数
Person Per3();//定义了一个函数Per3();
Person *per4=new Person;//调用无参数构造函数,创建对象的简写,与下面的写法相同
Person *per5=new Person();//调用无参数构造函数
Person *per6=new Person[2];
Person *per7=new Person("ls",18,"stu");
Person *per8=new Person("ww",16);
Per.printfInfo();
per7->printfInfo();
per8->printfInfo();
delete per4;
delete per5;
delete []per6;
delete per7;
delete per8;
return 0;
}
析构函数
#include<iostream>
#include<string.h>
using namespace std;
class Person{
private:
char *name;
int age;
char *work;
public:
Person(){cout<<"Person()"<<endl;
this->work=NULL;
this->name=NULL;
}
Person(char *name){
cout<<"Person(char *name)"<<endl;
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
this->work=NULL;
}
Person(char *name,int age,char *work="none"){
cout<<"Person(char *name,int age)"<<endl;
this->age=age;
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
this->work=new char[strlen(work)+1];
strcpy(this->work,work);
}
~Person()
{
cout<<"~Person()"<<endl;
if(this->name)
{
cout<<this->name<<"----"<<endl;
delete this->name;
}
if(this->work)
{
delete this->work;
}
}
void printfInfo(void)
{
cout<<"name="<<name<<" age="<<age<<" work="<<work<<endl;
}
};
int main()
{
Person Per("chenmiaohong",16);
Person Per2;
Person Per3();
Person *per4=new Person;///调用无参数构造函数
Person *per5=new Person();//调用无参数构造函数
Person *per6=new Person[2];
Person *per7=new Person("ls",18,"stu");
Person *per8=new Person("ww",16);
Per.printfInfo();
per7->printfInfo();
per8->printfInfo();
delete per4;
delete per5;
delete []per6;
delete per7;
delete per8;
}
析构函数在对象销毁前的瞬间被调用。
Person *per7=new Person(“ls”,18,”stu”);
delete per7;
new创建的对象,必须用delete函数销毁掉,负责不会调用析构函数。
Person Per(“chenmiaohong”,16);
Person *Per7=new Person(“ls”,18,”stu”);
delete Per7;
调用~Person的顺序为先new对象被释放,接着按构造顺序相反的顺序释放,先创建的后调用析构函数。
默认拷贝构造函数
Person per(“zhangsan”,18);
Person per2(per);
调用默认的拷贝构造函数将per的值传递给per2,但是这样引入问题因为per与per2指向内存的同一块地址,当对象被销毁时,都会去释放这块内存。
拷贝构造函数实现
Person(Person &per){
cout<<"(Person &per)"<<endl;
this->age=per.age;
this->name=new char[strlen(per.name)+1];
strcpy(this->name,per.name);
this->work=new char[strlen(per.work)+1];
strcpy(this->work,per.work);
}
int main()
{
Person per("zhangsan",18);
Person per2(per);
per2.printfInfo();
return 0;
}
类的静态成员
类成员必须通过类的对象来访问,不能通过类名直接访问。如果将类的成员定义为静态成员,则允许使用类名直接访问,静态成员使用static关键字访问
class Person{
public:
static unsigned int m_Price;//静态数据成员通常需要在类外部进行初始化
}
unsigned int Person::m_Price=10;
在一个类中类的静态数据成员被类中所有的类对象所共享。不论哪个对象对其值进行了修改。其他对象的静态数据成员也会被修改。
静态数据成员可以是当前类的类型,而其他数据成员只能是当前类的指针或者应用。
class Person{
public:
static unsigned int m_price;
Person per;//非法的定义,不允许在该类中定义所属类的对象
static Person per1;//正确,静态数据成员允许定义类的所属类对象
Person *per2;//正确,允许定义类的所属类型的指针型对象
}
静态数据成员可以作为成员函数的默认参数,其他数据成员不可以
class Person()
{
public:
static unsigned int m_per;
int m_page;
void OutputInfo(int data=m_per)//正确,静态数据成员作为函数的默认参数
{
cout<<data<<endl;
}
void OutputPage(int page=m_page)//错误
{
cout<<page<<endl;
}
}
类的静态成员函数只能访问静态数据成员,不能访问非静态数据成员。
static void Outputinfo()const;错误的定义,静态成员函数末尾不能使用const关键字
在类中定义的静态成员函数,在类外实现时不能添加static关键字
对象的构造顺序
构造顺序:按运行中定义对象的顺序调用构造函数,静态对象只调用一次构造函数; 全局对象在main函数执行前被构造
Person per_g("per_g",10);1
void fun()
{
Person per_func("per_func",11);4
static Person per_func_s("per_func_s",11);5
}
int main()
{
Person per_main("per_main",11);2
static Person per_main_s("per_main_s",11);3
for(int i=0;i<2;i++)
{
fun();
Person per_for("per_for",i);6
}
return 0;
}
析构函数调用顺序:
局部普通类对象,主函数内对象,局部static对象,main函数static对象,全局static对象
Person(char *name,int age,char *work="none"){
cout<<"Person(char*,int),name="<<name<<",age="<<age<<endl;
this->age=age;
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
this->work=new char[strlen(work)+1];
strcpy(this->work,work);
}
......
class Student{
private:
Person father;
Person mother;
int stu_id;
public :
Student()
{
cout<<"Student()"<<endl;
}
Student(int id,char *father,char *mother,int father_age=40,int mother_age=39):father(father,father_age),mother(mother,mother_age)使用父类的构造函数
{
cout<<"Student(int id,char *father,char *mother,int father_age=40,int mother_age=39)"<<endl;
}
};
int main()
{
Student s(10,"cath","lily");
return 0;
}
构造函数调用顺序是:先调用father、mother、stu
析构函数调用顺序是:先调用stu、mother、father