类构造函数(The Class Constructor)是一个特殊的类成员函数,当我们创建这个类的新对象时,会被执行。
一个构造函数将被确定与类名相同的名字,没有任何返回值,包括void。构造函数对于给某些类成员变量设置初始值非常有用。
例子:
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor
private:
double length;
};
// Member functions definitions including constructor
Line::Line(void) {
cout << "Object is being created" << endl;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}
// Main function for the program
int main( ) {
Line line;
// set line length
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); // This is the constructor
private:
double length;
};
// Member functions definitions including constructor
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;
}
// Main function for the program
int main( ) {
Line line(10.0);
// get initially set length.
cout << "Length of line : " << line.getLength() <<endl;
// set line length again
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) {
cout << "Object is being created, length = " << len << endl;
length = len;
}
如果一个类为C,有多个域X,Y,Z,…要初始化,可以使用如下句法:
C::C( double a, double b, double c): X(a), Y(b), Z(c) {
....
}
类析构函数
类析构函数是类中的一个特殊的成员函数,当相对应的一个对象超出了访问范围或者在任何情况下,delete 表达式释放对象内存时该函数将被执行。
类析构函数和对应类有相同的名字并在前面加~,它没有返回值,也没有任何参数。它在执行完程序前释放资源很有用,如关闭文件,释放内存,等。
例子:
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
// Member functions definitions including constructor
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;
}
// Main function for the program
int main( ) {
Line line;
// set line length
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
复制构造函数(The Copy Constructor)
复制构造函数是一个通过同一个类先创建的对象来初始化重新创建的对象的构造函数。经常被用在:
1. 用一个已定义的对象来初始化另一个对象;
2. 通过将一个对象作为一个函数的参数来复制这个对象;
3. 通过作为一个函数的返回值来复制一个对象。
如果一个复制构造函数没有在一个类中定义,编译器会定义。如果在类中有指针变量和一些动态内存分配,那么就必须有一个复制构造函数。最普通的形式:
classname (const classname &obj) {
// body of constructor
}
例子:
#include <iostream>
using namespace std;
class Line {
public:
int getLength( void );
Line( int len ); // simple constructor
Line( const Line &obj); // copy constructor
~Line(); // destructor
private:
int *ptr;
};
// Member functions definitions including constructor
Line::Line(int len) {
cout << "Normal constructor allocating ptr" << endl;
// allocate memory for the pointer;
ptr = new int;
*ptr = len;
}
Line::Line(const Line &obj) {
cout << "Copy constructor allocating ptr." << endl;
ptr = new int;
*ptr = *obj.ptr; // copy the value
}
Line::~Line(void) {
cout << "Freeing memory!" << endl;
delete ptr;
}
int Line::getLength( void ) {
return *ptr;
}
void display(Line obj) {
cout << "Length of line : " << obj.getLength() <<endl;
}
// Main function for the program
int main( ) {
Line line1(10);
Line line2 = line1; // This also calls copy constructor
display(line1);
display(line2);
return 0;
}
运行结果:
Normal constructor allocating ptr
Copy constructor allocating ptr.
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
Freeing memory!
注:When you pass an object to a function by value, a copy of the object is created on the stack.
将line对象传递给display时,需要复制对象,因此,会调用复制构造函数。