程序填空,使其输出4 5 1 #include <iostream> using namespace std; class Apple { // 在此处补充你的代码 static void PrintTotal() { cout << nTotalNumber << endl; } }; int Apple::nTotalNumber = 0; Apple Fun(const Apple & a) { a.PrintTotal(); return a; } int main() { Apple * p = new Apple[4]; Fun(p[2]); Apple p1,p2; Apple::PrintTotal (); delete [] p; p1.PrintTotal (); return 0; }
时间: 2024-04-26 08:25:55 浏览: 101
#include <iostream>
using namespace std;
class Apple {
public:
Apple() {
nTotalNumber++; // 每次创建对象时,总数加 1
}
static void PrintTotal() {
cout << nTotalNumber << endl;
}
private:
static int nTotalNumber; // 静态成员变量,记录总数
};
int Apple::nTotalNumber = 0;
Apple Fun(const Apple & a) {
a.PrintTotal();
return a;
}
int main() {
Apple * p = new Apple[4];
Fun(p[2]);
Apple p1,p2;
Apple::PrintTotal();
delete [] p;
p1.PrintTotal();
return 0;
}
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)