亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C++ 构造函数

C++ 构造函数

在本教程中,我们将通过帮助示例了解 C++ 构造函数及其类型。

构造函数是一种特殊类型的成员函数,在创建对象时会自动调用。

在 C++ 中,构造函数与类同名,并且没有返回类型。例如,

class  Wall {
  public:
    // create a constructor
    Wall() {
      // code
    }
};

这里,函数 Wall() 是类 Wall 的构造函数 .注意构造函数


C++ 默认构造函数

没有参数的构造函数称为 默认构造函数 .在上面的例子中,Wall() 是默认构造函数。


示例 1: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 对象的变量 .

注意: 如果我们的类中没有定义构造函数,那么C++编译器会自动创建一个默认构造函数,代码为空,无参数。


C++ 参数化构造函数

在 C++ 中,带参数的构造函数称为参数化构造函数。这是初始化成员数据的首选方法。


示例 2: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() 有 2 个参数:double lendouble hgt .这些参数中包含的值用于初始化成员变量length高度 .

当我们创建Wall的对象时 类,我们将成员变量的值作为参数传递。代码如下:

Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);

有了这样初始化的成员变量,我们现在可以用 calculateArea() 计算墙的面积 功能。


C++ 复制构造函数

C++中的复制构造函数用于将一个对象的数据复制到另一个对象。


示例 3: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(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(Wall &obj) {
  length = obj.length;
  height = obj.height;
}

请注意,此构造函数的参数具有 Wall 的对象的地址 类。

然后我们为 obj 的变量赋值 对象到调用复制构造函数的对象的相应变量。这就是对象内容的复制方式。

main() ,然后我们创建两个对象 wall1wall2 然后复制 wall1 的内容 到 wall2

// copy contents of wall1 to wall2
Wall wall2 = wall1;

这里,wall2 对象通过传递 wall1 的地址来调用其复制构造函数 对象作为其参数,即 &obj = &wall1 .

注意 :构造函数主要用于初始化对象。它们还用于在创建对象时运行默认代码。


C语言

  1. C# 构造函数
  2. C# 构造函数重载
  3. C++ 数据类型
  4. C++ 运算符
  5. C++ 注释
  6. C++ 类模板
  7. Java 构造函数
  8. Java - 构造函数
  9. C++ 概述
  10. C++ 常量/文字
  11. C++ 中的运算符
  12. C++ 中的数字