1.下面程序运行的结果 #include <iostream> using namespace std; class test{ private: int num; float f1; public: test(){cout<<"Initalizing default"<<endl;num=0;f1=0.0;} int getint(){return num;} float getfloat(){return f1;} ~test(){cout<<"Destructor is active"<<endl;} }; int main() { test array[2]; cout<<array[1].getint()<<" "<<array[1].getfloat()<<endl; return 0; }
时间: 2024-03-23 12:37:46 浏览: 31
程序的输出结果为:
```
Initalizing default
Initalizing default
0 0
Destructor is active
Destructor is active
```
程序中定义了一个名为`test`的类,包含一个默认的构造函数、一个获取整型成员变量的函数`getint()`、一个获取浮点型成员变量的函数`getfloat()`以及一个析构函数。
在`main()`函数中,定义了一个名为`array`的`test`类型的数组,数组的长度为2。因为没有给数组元素指定具体的值,所以会调用默认构造函数进行初始化,因此会输出两次"Initalizing default"。
接着,程序输出了`array[1]`的整型成员变量和浮点型成员变量,由于没有给数组元素指定具体的值,所以这两个成员变量的值都为0。
最后,`main()`函数结束,程序自动调用两次析构函数,输出"Destructor is active"。
相关问题
补全程序,并写出程序运行结果。 #include <iostream> using namespace std; class Class { public : Class ( int...
补全程序如下:
```
#include <iostream>
using namespace std;
class Class {
public:
Class(int a, int b, int c) {
x = a;
y = b;
z = c;
}
void print() {
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "z: " << z << endl;
}
private:
int x;
int y;
int z;
};
int main() {
Class obj(1, 2, 3);
obj.print();
return 0;
}
```
程序运行结果为:
```
x: 1
y: 2
z: 3
```
程序定义了一个名为 Class 的类,该类包括有参构造函数和一个 print() 函数,构造函数用于对类的私有成员变量进行初始化,print() 函数用于输出类的私有成员变量。在主函数 main() 中,创建了一个名为 obj 的 Class 类对象,构造函数传入三个整数参数 1、2 和 3。最后调用 obj 对象的 print() 函数,输出 obj 对象的私有成员变量 x、y 和 z 的值。
#include <iostream> #include<algorithm> #include<cmath> #include <queue> using namespace std;
#include <iostream>:这是C++标准库中的头文件,用于输入输出流操作,包括cin、cout等。
#include<algorithm>:这是C++标准库中的头文件,用于提供各种算法操作,如排序、查找等。
#include<cmath>:这是C++标准库中的头文件,用于提供数学函数操作,如绝对值、平方根等。
#include <queue>:这是C++标准库中的头文件,用于提供队列操作,包括入队、出队等。
using namespace std;:这是C++的命名空间声明,表示使用std命名空间,可以直接使用std中的函数和对象。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)