在面向对象编程语言中,构造函数用于初始化将从类类型创建的对象。在C++中,构造函数会自动调用,以便以不同的方式初始化对象。
什么是构造函数?
构造函数是在类内部定义的,其名称与类相同。名称必须与类相同,因为它将自动标记为构造函数。构造函数应该是公共的,这意味着它应该是类的公共定义。构造函数名称也区分大小写,大小写应该与类名相同。 构造函数也具有与类相同的名称。
构造函数是如何工作的?
构造函数的工作方式类似于在对象初始化期间自动调用的函数。初始化可以通过以下不同的方式完成。
Point p = new Point();Point p();
施工人员类型
在一个类定义中,我们不必显式地创建一个构造函数,编译器将隐式地创建一个默认的构造函数,该构造函数只初始化对象。但是我们有不同的选项来创建如下构造函数:
- `默认构造函数`
- `参数化构造函数`
- `动态构造函数`
缺省构造
我们将从一个显式的默认构造函数开始。我们会把 Point()
下的构造函数定义 public:
部分,我们还将添加花括号 { }
创建默认构造函数代码块。我们将把在对象初始化或创建期间运行的代码放入默认构造函数代码块中。代码很简单,我们将x和y变量设置为0。
// C++ Example Application To Explain Constructors#includeusing namespace std;class Point {private: int x, y;public: // Default Constructor Point() { x = 0; y = 0; } int getX() { return x; } int getY() { return y; }};int main(){ // Constructor called with a new object initilization from the Point class Point p = Point(); // We can access values assigned by constructor cout << "p.x = " << p.getX() << ", p.y = " << p.getY(); return 0;}
参数化构造函数
在前面的示例中,我们刚刚调用了构造函数来初始化对象。但是我们没有为初始化提供任何值。我们还可以使用参数化构造函数来为初始化提供一些值,这些值将设置为创建的对象。参数化构造函数与默认构造函数非常相似,在默认构造函数中,我们将添加一些参数作为构造函数的参数。在下面的示例中,我们将通过将x和y作为值提供给构造函数来设置它们。
// C++ Example Application To Explain Constructors#includeusing namespace std;class Point {private: int x, y;public: // Default Constructor Point(int new_x, int new_y) { x = new_x; y = new_y; } int getX() { return x; } int getY() { return y; }};int main(){ // Constructor called with a new object initilization from the Point class Point p = Point( 10 , 15 ); // We can access values assigned by constructor cout << "p.x = " << p.getX() << ", p.y = " << p.getY(); return 0;}
构造函数重载
在前面的示例中,我们只定义了一个构造函数,但是C++提供了为不同用例定义多个构造函数的能力。为单个类定义多个构造函数称为构造函数重载。构造函数重载最重要的限制是同一个签名不能多次使用。这意味着相同的参数计数和类型不能多次使用。让我们看一个例子,我们将定义一个不需要参数的构造函数和第二个接受两个参数的构造函数。
相关文章: C++世界应用
我们将使用以下构造函数:
- `Point()`constructor不会获取任何参数,在初始化过程中,它会将x和y变量设置为0。
- `Point(int newu x,int newu y)`将获取’newu x’和’newu y’参数,并将它们的值设置为x和y。
// C++ Example Application To Explain Constructors#includeusing namespace std;class Point {private: int x, y;public: // Default Constructor Point() { x = 0; y = 0; } Point(int new_x, int new_y) { x = new_x; y = new_y; } int getX() { return x; } int getY() { return y; }};int main(){ //Initialize without parameter Point p1 = Point(); //Initialize with parameters Point p2 = Point( 10 , 15 ); // We can access values assigned by constructor cout << "p.x = " << p1.getX() << ", p.y = " << p1.getY(); cout << "p.x = " << p2.getX() << ", p.y = " << p2.getY(); return 0;}
复制构造函数
从一个类开始,可以创建和初始化多个对象。在某些情况下,我们可能需要在新创建的对象中使用已经创建的对象值。我们可以使用copy构造函数来实现这一点,它将获取现有的对象指针,并在构造函数内部初始化期间将现有的对象值复制到新创建的对象。
// C++ Example Application To Explain Constructors#includeusing namespace std;class Point {private: int x, y; char* c;public: // Default Constructor Point(int new_x, int new_y) { x = new_x; y = new_y; } Point(const Point &p2) { x = p2.x; y = p2.y; } int getX() { return x; } int getY() { return y; }};int main(){ // Constructor called with a new object initilization from the Point class Point p = Point(10,15); Point new_p = Point(p); // We can access values assigned by constructor cout << "p.x = " << new_p.getX() << ", p.y = " << new_p.getY(); return 0;}
动态构造函数
通常,在对象初始化过程中,内存分配大小是固定的。像int、char、object等每种类型都有特定的大小。但我们也可以在构造函数调用或初始化期间动态分配内存。在下面的示例中,我们将创建一个动态分配的char数组。分配char数组c内存,然后放入 point
到这个变量。
// C++ Example Application To Explain Constructors#includeusing namespace std;class Point {private: int x, y; char* c;public: // Default Constructor Point() { c = new char[6]; c = "point"; x = 0; y = 0; } int getX() { return x; } int getY() { return y; }};int main(){ // Constructor called with a new object initilization from the Point class Point p = Point(); // We can access values assigned by constructor cout << "p.x = " << p.getX() << ", p.y = " << p.getY(); return 0;}
构造函数与成员函数
由于构造函数的行为类似于类的成员函数,您可能会问自己,构造函数和成员函数之间有什么区别或相似之处。以下是其中一些最重要的。
- 构造函数没有返回值或类型,而成员函数有返回值或类型,即使它是空的。
- 构造函数可以根据显式调用成员函数的用法自动或显式调用。
- 构造函数与其类具有相同的名称,其中成员函数可以/应该具有与其类不同的名称
- 如果编译器不指定构造函数(成员函数不隐式创建),则会自动创建构造函数。
相关文章: C++世界应用