构造函数是一种特殊的成员 函数,在创建 对象 时会自动调用。
在 C++ 中,构造函数与 类 的名称相同,并且没有返回类型。例如,
class Wall {
public:
// create a constructor
Wall() {
// code
}
};
这里,函数 Wall()
是 Wall
类的构造函数。请注意,构造函数
- 与类同名,
- 没有返回类型,并且
- 是
public
的
C++ 默认构造函数
没有参数的构造函数称为 **默认构造函数**。例如,
// C++ program to demonstrate the use of default constructor
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
public:
// default constructor to initialize variable
Wall()
: length{5.5} {
cout << "Creating a wall." << endl;
cout << "Length = " << length << endl;
}
};
int main() {
Wall wall1;
return 0;
}
输出
Creating a Wall Length = 5.5
这里,当创建 wall1 对象时,会调用 Wall()
构造函数。调用构造函数时会调用 length{5.5}
,并将对象的 length 变量 设置为 5.5
。
注意: 如果我们在类中没有定义任何构造函数、复制构造函数或移动构造函数,那么 C++ 编译器会自动创建一个不带参数且为空体的默认构造函数。
默认构造函数
当我们必须依赖默认构造函数来初始化类的成员变量时,我们应该通过以下方式显式地将构造函数标记为默认:
Wall() = default;
如果我们想设置默认值,那么我们应该使用值初始化。也就是说,我们在类成员变量声明中的花括号内包含默认值,如下所示:
double length {5.5};
我们来看一个例子
// C++ program to demonstrate the use of defaulted constructor
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length {5.5};
public:
// defaulted constructor to initialize variable
Wall() = default;
void print_length() {
cout << "length = " << length << endl;
}
};
int main() {
Wall wall1;
wall1.print_length();
return 0;
}
输出
length = 5.5
C++ 参数化构造函数
在 C++ 中,带有参数的构造函数称为参数化构造函数。这是初始化成员数据的首选方法。例如,
// C++ program to calculate the area of a wall
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
// parameterized constructor to initialize variables
Wall(double len, double hgt)
: length{len}
, height{hgt} {
}
double calculateArea() {
return length * height;
}
};
int main() {
// create object and initialize data members
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
输出
Area of Wall 1: 90.3 Area of Wall 2: 53.55
在这里,我们定义了一个参数化构造函数 Wall()
,它有两个参数:double len
和 double hgt
。这些参数中包含的值用于初始化成员变量 length 和 height。
: length{len}, height{hgt}
是成员初始化列表。
length{len}
使用参数 len 的值初始化成员变量 length。height{hgt}
使用参数 hgt 的值初始化成员变量 height。
当我们创建 Wall
类的对象时,我们将成员变量的值作为参数传递。代码如下:
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
在初始化成员变量之后,我们现在可以使用 calculateArea()
方法计算墙的面积。
注意:构造函数主要用于初始化对象。它们也用于在创建对象时运行默认代码。
C++ 成员初始化列表
考虑构造函数
Wall(double len, double hgt)
: length{len}
, height{hgt} {
}
这里,
: length{len}
, height{hgt}
是成员初始化列表。
成员初始化列表用于初始化类的成员变量。
成员变量的初始化顺序是按照它们在类中的声明顺序,而不是它们在成员初始化列表中的声明顺序。
由于成员变量在类中的声明顺序如下:
double length;
double height;
即使我们按以下方式定义构造函数,length 变量也将首先被初始化:
Wall(double hgt, double len)
: height{hgt}
, length{len} {
}
C++ 复制构造函数
C++ 中的复制构造函数用于将数据从一个对象复制到另一个对象。例如,
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
// initialize variables with parameterized constructor
Wall(double len, double hgt)
: length{len}
, height{hgt} {
}
// copy constructor with a Wall object as parameter
// copies data of the obj parameter
Wall(const Wall& obj)
: length{obj.length}
, height{obj.height} {
}
double calculateArea() {
return length * height;
}
};
int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);
// copy contents of wall1 to wall2
Wall wall2 = wall1;
// print areas of wall1 and wall2
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
输出
Area of Wall 1: 90.3 Area of Wall 2: 90.3
在此程序中,我们使用复制构造函数将 Wall
类的一个对象的内容复制到另一个对象。复制构造函数的代码如下:
Wall(const Wall& obj)
: length{obj.length}
, height{obj.height} {
}
请注意,此构造函数的参数具有 Wall
类对象的地址。
然后,我们将 obj 对象的变量值赋给对象的相应变量,从而调用复制构造函数。这就是对象内容被复制的方式。
在 main()
中,我们创建了两个对象 wall1 和 wall2,然后将 wall1 的内容复制到 wall2。
// copy contents of wall1 to wall2
Wall wall2 = wall1;
在这里,wall2 对象通过将其 wall1 对象的引用作为参数来调用其复制构造函数。
C++ 默认复制构造函数
如果我们没有在类中定义任何复制构造函数、移动构造函数或移动赋值运算符,那么 C++ 编译器会自动创建一个默认复制构造函数,该构造函数执行成员逐个复制赋值。在大多数情况下,这已经足够了。例如,
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
// initialize variables with parameterized constructor
Wall(double len, double hgt)
: length{len}
, height{hgt} {
}
double calculateArea() {
return length * height;
}
};
int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);
// copy contents of wall1 to wall2 by default copy constructor
Wall wall2 = wall1;
// print areas of wall1 and wall2
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
输出
Area of Wall 1: 90.3 Area of Wall 2: 90.3
在此程序中,我们没有定义复制构造函数。编译器使用默认复制构造函数将 Wall
类的一个对象的内容复制到另一个对象。
另请阅读