- C++ 基础
- C++ 首页
- C++ 概述
- C++ 环境设置
- C++ 基本语法
- C++ 注释
- C++ Hello World
- C++ 省略命名空间
- C++ 常量/字面量
- C++ 关键字
- C++ 标识符
- C++ 数据类型
- C++ 数值数据类型
- C++ 字符数据类型
- C++ 布尔数据类型
- C++ 变量类型
- C++ 变量作用域
- C++ 多个变量
- C++ 基本输入/输出
- C++ 修饰符类型
- C++ 存储类
- C++ 运算符
- C++ 数字
- C++ 枚举
- C++ 引用
- C++ 日期和时间
- C++ 控制语句
- C++ 决策制定
- C++ if 语句
- C++ if else 语句
- C++ 嵌套 if 语句
- C++ switch 语句
- C++ 嵌套 switch 语句
- C++ 循环类型
- C++ while 循环
- C++ for 循环
- C++ do while 循环
- C++ foreach 循环
- C++ 嵌套循环
- C++ break 语句
- C++ continue 语句
- C++ goto 语句
- C++ 构造函数
- C++ 构造函数和析构函数
- C++ 复制构造函数
- C++ 文件处理
- C++ 文件和流
- C++ 从文件中读取
C++ 类构造函数和析构函数
类构造函数
类构造函数是类的特殊成员函数,每当我们创建该类的新的对象时都会执行。
构造函数的名称与类名完全相同,并且根本没有任何返回类型,甚至没有 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
广告