类的构造函数
类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。
构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 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) { cout << "Debug 2" << endl; length = len; } double Line::getLength(void) { return length; } int main() { Line line; cout << "Debug 1" << endl; line.setLength(6.0); cout << "Length of line : " << line.getLength() << endl; return 0; }
编译输出:
kevin@kevin-virtual-machine:~/Documents/Test/C++/three$ ./constructor Object is being created Debug 1 Debug 2 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" << endl; length = len; } void Line::setLength(double len) { cout << "Debug 2" << endl; length = len; } double Line::getLength(void) { return length; } int main() { Line line(10.0); cout << "Length of default line : " << line.getLength() << endl;//获取默认设置的长度 line.setLength(6.0); cout << "Length of line : " << line.getLength() << endl; return 0; }
编译输出:
kevin@kevin-virtual-machine:~/Documents/Test/C++/three$ ./constructor_para Object is being created Length of default line : 10 Debug 2 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,具有多个字段 X、Y、Z 等需要进行初始化,同理地,您可以使用上面的语法,只需要在不同的字段使用逗号进行分隔,如下所示:
C::C( double a, double b, double c): X(a), Y(b), Z(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) { cout << "Debug 2" << endl; 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; }
编译输出:
kevin@kevin-virtual-machine:~/Documents/Test/C++/three$ ./deconstructor Object is being created Debug 2 Length of line : 6 Object is being deleted
构造函数应用举例:
#include <iostream> #include <string> using namespace std; class Student { public: string name; string number; char X; int year; Student(string, string, char, int); //构造函数声明 void display(void); }; Student::Student(string N, string n, char x, int y) //利用构造函数个类的成员赋值 { name = N; number = n; X = x; year = y; } void Student::display() //输出成员的值 { cout << name << endl; cout << number << endl; cout << X << endl; cout << year << endl; } int main() { cout << "输入姓名:"; string N; cin >> N; cout << "输入学号:"; string n; cin >> n; cout << "输入性别(M或W):"; char x; cin >> x; cout << "输入年龄:"; int y; cin >> y; Student S(N, n, x, y); //定义对象并对构造函数赋值 S.display(); //引用输出函数 return 0; }
编译输出:
kevin@kevin-virtual-machine:~/Documents/Test/C++/three$ ./cons_exam 输入姓名:kevin 输入学号:10 输入性别(M或W):M 输入年龄:20 kevin 10 M 20
初始化列表的成员初始化顺序:
C++ 初始化类成员时,是按照声明的顺序初始化的,而不是按照出现在初始化列表中的顺序。
class CMyClass { CMyClass(int x, int y); int m_x; int m_y; }; CMyClass::CMyClass(int x, int y) : m_y(y), m_x(m_y) { };
你可能以为上面的代码将会首先做 m_y=y,然后做 m_x=m_y,最后它们有相同的值。但是编译器先初始化 m_x,然后是 m_y,,因为它们是按这样的顺序声明的。结果是 m_x 将有一个不可预测的值。有两种方法避免它,一个是总是按照你希望它们被初始化的顺序声明成员,第二个是,如果你决定使用初始化列表,总是按照它们声明的顺序罗列这些成员。这将有助于消除混淆。