合成默认函数,是程序自己给出的,只有类内变量都赋初值时适用。设置对象时不带参数
手动默认函数,就是给类内变量赋初值,
如Human();
...
Human::Human(){
name=“小花”;
age=18;
}
创建对象时自动调用的手动默认构造函数,就不会再去调用合成默认构造函数。
定义带参数的构造函数
如
Human(string name ,int age);
...
Human::Human(string name,int age)
{
this->name =name ;
this->age =age;
}
Int main()
{
Human xiaohua(“小花”,5);
}
拷贝构造函数
使用场合
Int main()
{
Human h2=xiaohua;
Human h3(xiaohua);
}
在类内的声明
Human(const Human &other);
拷贝构造函数的定义
Human::Human(const Human &other)
{
name = other.name;
Age =other.age;
}