请使程序完整, 并使程序的输出为: 2, 1 4, 3 请填空。 #include <iostream> using namespace std; class A { int a; public: A(int i = 0) { a = i; } int Geta() { return a; } }; class B { A a; int b; public: B(int i = 0, int j = 0): ______ { b = i; } void display() { cout << a.Geta() << ',' << b << endl; } }; int main() { B b[2] = { B(1, 2), B(3, 4) }; for (int i = 0; i < 2; i++) ___; } 空1空2 标 记上一题下一题
时间: 2024-04-05 20:33:23 浏览: 15
空1:a(i);空2:b[i].display(); 完整程序如下:
#include <iostream>
using namespace std;
class A {
int a;
public:
A(int i = 0) {
a = i;
}
int Geta() {
return a;
}
};
class B {
A a;
int b;
public:
B(int i = 0, int j = 0): a(i) {
b = j;
}
void display() {
cout << a.Geta() << ',' << b << endl;
}
};
int main() {
B b[2] = {
B(1, 2),
B(3, 4)
};
for (int i = 0; i < 2; i++)
b[i].display();
}
相关问题
分析下列程序,根据输出结果完善填空。 程序运行结果为:10 20 30 #include <iostream > using namespace std; class A {
由于题目中给出的程序不完整,我在此做出一些假设来完善程序。假设程序如下:
```c++
#include <iostream>
using namespace std;
class A {
public:
A(int x) {
a = x;
}
virtual void f() {
cout << a << " ";
}
private:
int a;
};
class B : public A {
public:
B(int x, int y) : A(x) {
b = y;
}
void f() {
cout << b << " ";
}
private:
int b;
};
int main() {
A *p = new B(10, 20);
p->f();
delete p;
A a(30);
a.f();
return 0;
}
```
程序中定义了两个类 A 和 B,其中 B 继承自 A。类 A 中有一个 int 类型的成员变量 a 和一个虚函数 f(),该函数输出 a 的值。类 B 中有一个 int 类型的成员变量 b,并重写了类 A 中的虚函数 f(),该函数输出 b 的值。
在主函数中,首先定义了一个指向类 B 对象的指针 p,并调用 p 的 f() 函数输出 b 的值,然后使用 delete 操作符释放该对象的内存。接着定义了一个类 A 的对象 a,并调用 a 的 f() 函数输出 a 的值。最终程序输出了
```
20 30
```
的结果。
具体分析如下:
首先定义了指向类 B 对象的指针 p,该对象的成员变量 a 被初始化为 10,b 被初始化为 20。由于类 A 中的 f() 函数是虚函数,因此在调用 p 的 f() 函数时,会动态绑定到类 B 中的 f() 函数,输出 b 的值 20。
接着使用 delete 操作符释放指针 p 所指向的对象的内存。
然后定义了一个类 A 的对象 a,该对象的成员变量 a 被初始化为 30。调用 a 的 f() 函数时,由于 a 是类 A 的对象,因此会调用类 A 中的 f() 函数,输出 a 的值 30。
因此最终程序输出了 20 和 30 两个数,中间以空格隔开。
2. 下面程序的作用是定义一个冒泡排序的函数模板,并在主函数中调用,请按题目要求填空。(6分) #include <iostream>
```c++
using namespace std;
template<typename T>
void bubbleSort(T arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
int main() {
int a[5] = {3, 1, 4, 2, 5};
bubbleSort<int>(a, 5);
for (int i = 0; i < 5; i++) {
cout << a[i] << " ";
}
return 0;
}
```
程序的作用是定义一个冒泡排序的函数模板,并在主函数中调用,对一个整型数组进行排序。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)