构造函数的 存在意义
- 在实例化的对象时,需要对对象中的数据进行初始化。
-
首先构造一个类
class Tank { private: int m_iPosX; int m_iPosY; public: void init() { m_iPosX = 0; m_iPosY = 0; } };
-
实例两个对象,并通过
void init()
初始化对象int main() { Tank t1; t1.init(); Tank t2; t2.init(); return 0; };
- 对象初始化类型:
- 有且仅有一次
- 根据条件初始化
- 构造函数 - 避免初始化函数误操作
- 构造函数在对象实例化时被自动调用
- 构造函数与类同名
- 构造函数没有返回值 ( 不用写void)
- 构造函数可以有多重载形式
- 实例化对象时仅用到一个构造函数
- 当用户没有定义构造函数时,编译器自动生成一个构造函数
构造函数的分类
构造函数:
- 普通构造函数:
- 无参构造函数 ——> 默认构造函数
- 有参构造函数:
- 参数带默认值 ——> 默认构造函数
- 参数无默认值
- 拷贝构造函数
无论是哪种构造函数,初始化列表都具有重要的作用
普通构造函数
- 无参构造函数
class Student { public: Student(){m_strName = "jim";} private: string m_strName; };
- 有参构造函数
class Student { public: Student(string name) {m_strName = name;} private: string m_strName; };
- 重载构造函数(需遵循重载函数的规则)
class Student { public: Student(){m_strName = "jim";} Student(string name) {m_strName = name;} private: string m_strName; };
- 默认构造函数: 不需要传递参数的构造函数
初始化列表
class Student
{
public:
Student(): m_strName("jim"), m_iAge(10){}
private:
string m_strName;
int m_iAge;
};
- 初始化列表的特点
- 初始化列表先于构造函数执行
- 初始化列表只能用于构造函数
- 初始话列表可以同时初始化多个数据成员
- 初始化列表存在的 大专栏 构造函数总结 constructor必要性
class Circle { public: Circle(){m_dPi = 3.14;} // 编译报错,不能给const第二次赋值 private: const double m_dPi; // 圆周率pi为固定值 };
class Circle { public: Circle():m_dPi(3.14){} // 编译通过,使用初始化列表初始化const private: const double m_dPi; };
另一种形式
class Circle { public: Circle(double pi):m_dPi(pi){} // 编译通过,传入的形参通过初始化列表进行赋值 private: const double m_dPi; };
拷贝构造函数
***** Teacher Header ****
include <iostream>
include <string>
using namespace std;
class Teacher
{
public:
Teacher(string name = "jim", int age = 1);
Teacher(const Teacher &tea);
void setName(string name);
string getName();
void setAge(int age);
int getAge();
private:
string m_strName;
int m_iAge;
};
*** Teacher Header end ***
***** Teacher Cpp ********
include <Teacher.h>
include <iostream>
include <string>
using namespace std;
Teacher::Teacher(string name, int age):m_strName(name), m_iAge(age)
{
cout << "Teacher(string name, int age)" << endl;
}
Teacher::Teacher(const Teacher& tea)
{
cout << "Teacher(const Teacher& tea) << endl;
}
void Teacher::setName(string name)
{
m_strName = name;
}
string Teacher::getName()
{
return m_strName;
}
void Teacher::setAge(int age)
{
m_iAge = age;
}
int Teacher::getAge()
{
return m_iAge;
}
***** Teacher Cpp end *****
********** test ***********
include <Teacher.h>
include <iostream>
include <string>
using namespace std;
void test(Teacher& t)
{
cout << t.getName << " " << t.getAge << endl;
}
int main()
{
Teacher t1;
Teacher t2(t1);
Teacher t3 = t1;
test(t1);
return 0;
}
******** test end *********
拷贝构造函数的特点
- 通过同类行的对象实例化另外的对象时,自动调用拷贝构造函数
- 实例化对象为传入参数时,触发了拷贝过程,自动调用拷贝构造函数
- 拷贝函数不能重载!!!!!