类的构造函数
- 类的构造函数是类的一种特殊的成员函数,它会在每次创建性的对象时执行
- 构造函数的名称与类的名称是完全相同的,并且不返回任何类型,也不返回void。构造函数可用于为某些成员变量设置初始值。
下面的实例有助于更好地理解构造函数的概念:
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // 这是构造函数
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line(void)
{
cout << "Object is being created" << endl;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
// 程序的主函数
int main( )
{
Line line;//创建对象时会执行构造函数
// 设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
>>Object is being created
Length of line : 6
带参数的构造函数
默认的构造函数没有任何参数,但如果需要,构造函数也可以带有参数。这样在创建对象时就会给对象赋初始值,如下面的例子所示:
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(double len); // 这是构造函数
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line( double len)
{
cout << "Object is being created, length = " << len << endl;
length = len;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
// 程序的主函数
int main( )
{
Line line(10.0);
// 获取默认设置的长度
cout << "Length of line : " << line.getLength() <<endl;
// 再次设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
>>Object is being created, length = 10
Length of line : 10
Length of line : 6
使用初始化列表来初始化字段
Line::Line( double len): length(len)
{
cout << "Object is being created, length = " << len << endl;
}
上面的语法等同于如下语法:
Line::Line( double len)
{
length = len;
cout << "Object is being created, length = " << len << endl;
}
假设有一个类 C,具有多个字段 length、width、height等需要进行初始化,同理地,您可以使用上面的语法,只需要在不同的字段使用逗号进行分隔,如下所示:
C::C( double a, double b, double c): length(a), width(b), height(c)
{
....
}
类的析构函数
类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。
析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。
下面的实例有助于更好地理解析构函数的概念:
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // 这是构造函数声明
~Line(); // 这是析构函数声明
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line(void)
{
cout << "Object is being created" << endl;
}
Line::~Line(void)
{
cout << "Object is being deleted" << endl;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
// 程序的主函数
int main( )
{
Line line;
// 设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
>>Object is being created
Length of line : 6
Object is being deleted
构造函数在实例化对象时调用,析构函数在函数运行结束后调用。
构造函数的分类:无参构造、有参构造、拷贝构造。
无参构造不能加括号Person p1(),错误,编译器会认为这是一个声明。
class Person
{
public:
Person() {
cout << "无参构造函数" << endl;
}
Person(int age) {
m_Age = age;
cout << "有参构造函数" << endl;
}
Person(const Person &p) {
m_Age = p.m_Age;
cout << "拷贝构造函数" << endl;
}
~Person() {
cout << "析构函数" << endl;
}
int m_Age;
};
拷贝构造函数什么时候会被调用:
- 用已经创建好的对象来初始化一个新对象;
- 值传递的方式给函数参数传值;
- 值方式返回局部对象;
// 用已经创建好的对象实例化一个新对象
int main()
{
Person p1(20);
Person p2(p1);
}
>>
有参构造函数 // p1构造函数
拷贝构造函数 // p2构造函数
析构函数 // p1析构函数
析构函数 // p2析构函数
void doWork1(Person p)
{
}
int main()
{
Person p1(20);
doWork1(p1);
}
>>
有参构造函数 // Person p1实例化对象时调无参构造
拷贝构造函数 // 值传递方式传给参数,会将实参p1赋给形参p,相当于Person p(p1);
析构函数 // dowork1函数结束后调p的析构函数
析构函数 // main函数结束调p1的析构函数
Person doWork2()
{
Person p;
return p;
}
int main()
{
Person p1(20);
Person p2 = doWork2();
}
>>
有参构造函数 // Person p1调有参构造
无参构造函数 // Person p调无参构造
拷贝构造函数 // Person p是局部变量会被程序自动释放,但return时会重新创建个新的对象返回,相当于Person p_temp(p)
析构函数 // doWork2函数结束调Person p的析构函数
析构函数 // main函数结束调p1的析构函数
析构函数 // main函数结束调p2的析构函数