有两个类:XClass和YClass, xClass有公有成员西数XIntArrBubbleSort(实现整数数组的冒泡排序)和XIntArrDoSum(实现整数数组元素求和); vClass有公有成员函数VIntArrBubbleSort(实现整牧数组的冒泡排序)和VIntArrDoSum(实现整数数组元素求和)。其中XIntArrBubbleSort委托 VintArrBubbleSort实现整数数组的冒泡排序;YintATrDoSum委托XIntAr-Dosum实现整数数组元素求和。请买用多文件设计实现两个类,并在主西数中用 XClass对一个整数数组进行冒泡排序,用YClass对 个整数数组进行元素求和。 四个成员函数的原型如下: 21220523 212 void XClass :: XIntArrBubbleSort (YClass &ery,int Arr[ lint n); int XClass :: XIntArrDoSum (int Arr[ lint n); void Class :: YIntArrBubbleSort (int Arr[ ],int n); int Class :: VIntArrDoSum (XClass 8rx, Arr 1,int n);
时间: 2024-02-24 17:55:49 浏览: 22
好的,根据你的要求,我将不会透露你的任何要求。关于你的问题,我可以提供一个多文件设计的实现方案。
首先,我们在一个名为XClass.h的头文件中定义XClass类和它的成员函数。其中,XIntArrBubbleSort函数委托了YClass的VIntArrBubbleSort函数实现整数数组的冒泡排序。代码如下:
```c++
#ifndef XCLASS_H
#define XCLASS_H
#include "YClass.h"
class XClass
{
public:
void XIntArrBubbleSort(YClass &y, int Arr[], int n);
int XIntArrDoSum(int Arr[], int n);
};
#endif
```
接下来,在一个名为XClass.cpp的源文件中实现XClass类的成员函数。在XIntArrBubbleSort函数中,我们将YClass的VIntArrBubbleSort函数作为参数传递,并调用它实现整数数组的冒泡排序。在XIntArrDoSum函数中,我们直接实现整数数组元素求和。代码如下:
```c++
#include "XClass.h"
void XClass::XIntArrBubbleSort(YClass &y, int Arr[], int n)
{
y.VIntArrBubbleSort(Arr, n);
}
int XClass::XIntArrDoSum(int Arr[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += Arr[i];
}
return sum;
}
```
然后,我们在一个名为YClass.h的头文件中定义YClass类和它的成员函数。其中,YIntArrDoSum函数委托了XClass的XIntArrDoSum函数实现整数数组元素求和。代码如下:
```c++
#ifndef YCLASS_H
#define YCLASS_H
#include "XClass.h"
class YClass
{
public:
void VIntArrBubbleSort(int Arr[], int n);
int YIntArrDoSum(XClass &x, int Arr[], int n);
};
#endif
```
最后,在一个名为YClass.cpp的源文件中实现YClass类的成员函数。在VIntArrBubbleSort函数中,我们直接实现整数数组的冒泡排序。在YIntArrDoSum函数中,我们将XClass的XIntArrDoSum函数作为参数传递,并调用它实现整数数组元素求和。代码如下:
```c++
#include "YClass.h"
void YClass::VIntArrBubbleSort(int 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])
{
int temp = Arr[j];
Arr[j] = Arr[j + 1];
Arr[j + 1] = temp;
}
}
}
}
int YClass::YIntArrDoSum(XClass &x, int Arr[], int n)
{
return x.XIntArrDoSum(Arr, n);
}
```
最后,在主函数中,我们可以创建XClass和YClass的实例,并分别调用它们的成员函数实现整数数组的冒泡排序和元素求和。代码如下:
```c++
#include "XClass.h"
#include "YClass.h"
int main()
{
int Arr[] = {3, 2, 1, 5, 4};
int n = sizeof(Arr) / sizeof(Arr[0]);
XClass x;
YClass y;
x.XIntArrBubbleSort(y, Arr, n);
int sum = y.YIntArrDoSum(x, Arr, n);
return 0;
}
```
这样,我们就用多文件设计实现了两个类XClass和YClass,并在主函数中调用了它们的成员函数实现整数数组的冒泡排序和元素求和。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)