当前位置: 首页 > 文档资料 > C++ 中文教程 >

构造函数和析构函数(Constructor & Destructor)

优质
小牛编辑
143浏览
2023-12-01

类构造函数

constructor是类的特殊成员函数,只要我们创建该类的新对象,就会执行该函数。

构造函数与类具有完全相同的名称,它根本没有任何返回类型,甚至无效。 构造函数对于为某些成员变量设置初始值非常有用。

下面的例子解释了构造函数的概念 -

#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) {
   ....
}

类析构函数

destructor是类的特殊成员函数,只要其类的对象超出作用域或每次将删除表达式应用于指向该类对象的指针时执行该类。

析构函数将与前缀为波浪号(〜)的类具有完全相同的名称,并且它既不能返回值也不能接受任何参数。 析构函数对于在退出程序之前释放资源非常有用,例如关闭文件,释放内存等。

以下示例解释了析构函数的概念 -

#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