程序填空,使其输出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 12:25:55 浏览: 90
#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;
}
阅读全文