1)、构造一个类CalculateFun,公有部分包含构造、析构函数、外部接口函数void AllByOne()、和静态成员函数static void ShowSequence()。私有部分包含:void InsideCalculation()、void SetData()、int x、int y和静态成员变量static int snCount。 2)、构造函数CalculateFun()初始化x和y的值,私有成员函数InsideCalculation和SetData函数供AllByOne函数调用,而外部main函数中对于Calculation的任意对象仅仅调用AllByOne就可以实现从键盘上输入x、y的值,求出x和y的两数平方和结果并显示出来。其中SetData用于键盘输入x和y的值,InsideCalculation用于计算两数平方和并显示结果。 3)、建立void ObjectOperating(CalculateFun *pObject)普通函数,功能是对形参中的对象指针调用其接口函数AllByOne(),并且调用静态成员函数ShowSequence来显示是第几次调用对象。 4)、main函数中建立一个含三个元素的对象指针数组pArr,分别对其三个元素new出三个对象,并调用ObjectOperating函数。完了后用delete释放new出的内存。
时间: 2024-03-25 16:36:21 浏览: 54
好的,我明白了您的问题。以下是代码实现:
```cpp
#include <iostream>
using namespace std;
class CalculateFun {
public:
CalculateFun();
~CalculateFun();
void AllByOne();
static void ShowSequence();
private:
void InsideCalculation();
void SetData();
int x, y;
static int snCount;
};
int CalculateFun::snCount = 0;
CalculateFun::CalculateFun() {
x = y = 0;
}
CalculateFun::~CalculateFun() {
}
void CalculateFun::AllByOne() {
SetData();
InsideCalculation();
}
void CalculateFun::ShowSequence() {
cout << "This is the " << snCount << "th object operated." << endl;
}
void CalculateFun::InsideCalculation() {
int result = x * x + y * y;
cout << "The result is " << result << endl;
}
void CalculateFun::SetData() {
cout << "Please input x and y: " << endl;
cin >> x >> y;
snCount++;
}
void ObjectOperating(CalculateFun* pObject) {
pObject->AllByOne();
CalculateFun::ShowSequence();
}
int main() {
CalculateFun* pArr[3];
for (int i = 0; i < 3; i++) {
pArr[i] = new CalculateFun();
ObjectOperating(pArr[i]);
}
for (int i = 0; i < 3; i++) {
delete pArr[i];
}
return 0;
}
```
输出结果:
```
Please input x and y:
1 2
The result is 5
This is the 1th object operated.
Please input x and y:
3 4
The result is 25
This is the 2th object operated.
Please input x and y:
5 6
The result is 61
This is the 3th object operated.
```
阅读全文