#include<iostream> using namespace std; class TEST { int num; public: TEST( int num=0); void increment( ) ; ~TEST( ); }; TEST::TEST(int num) : num(num) { cout << num << endl; } void TEST::increment() { num++; } TEST::~TEST( ) { cout << num << endl; } int main( ) { TEST array[2]; array[0].increment(); array[1].increment(); return 0; }加上注释并且输出结果发给我
时间: 2024-01-16 21:01:50 浏览: 31
好的,以下是加上注释的代码:
```
#include <iostream>
using namespace std;
class TEST {
int num; // 私有成员变量
public:
TEST(int num = 0); // 构造函数声明
void increment(); // 成员函数声明
~TEST(); // 析构函数声明
};
// 构造函数定义
TEST::TEST(int num) : num(num) {
cout << num << endl; // 输出num的值
}
// 成员函数定义
void TEST::increment() {
num++; // 将num加1
}
// 析构函数定义
TEST::~TEST() {
cout << num << endl; // 输出num的值
}
// 主函数
int main() {
TEST array[2]; // 定义TEST类型数组,大小为2
array[0].increment(); // 调用数组中第一个元素的increment函数
array[1].increment(); // 调用数组中第二个元素的increment函数
return 0; // 程序结束
}
```
输出结果为:
```
0
0
1
1
```
以上代码创建了一个名为TEST的类,它包含一个私有成员变量num和三个公有成员函数:构造函数TEST()、成员函数increment()和析构函数~TEST()。在主函数中,定义了一个名为array的TEST类型数组,大小为2,然后分别调用了数组中两个元素的increment()函数将它们的num成员变量加1。最后,析构函数~TEST()被自动调用,输出了数组中两个元素的num成员变量的值。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)