描述
代码填空,使得程序能够自动统计当前各种动物的数量
#include <iostream>
using namespace std;
// 在此处补充你的代码
void print() {
cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}
int main() {
print();
Dog d1, d2;
Cat c1;
print();
Dog* d3 = new Dog();
Animal* c2 = new Cat;
Cat* c3 = new Cat;
print();
delete c3;
delete c2;
delete d3;
print();
}
输入
无
输出
0 animals in the zoo, 0 of them are dogs, 0 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
6 animals in the zoo, 3 of them are dogs, 3 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
样例输入
None
样例输出
0 animals in the zoo, 0 of them are dogs, 0 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
6 animals in the zoo, 3 of them are dogs, 3 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
class Animal
{
public:
static int number;
Animal() { number++; }
virtual ~Animal() { number--; }
};
class Dog : public Animal
{
public:
static int number;
Dog() { number++; }
~Dog() { number--; }
};
class Cat : public Animal
{
public:
static int number;
Cat() { number++; }
~Cat() { number--; }
};
int Animal::number = 0, Dog::number = 0, Cat::number = 0;
注意的是Animal* c2 = new Cat;delete c2;
由于此处是基类指针指向的派生类,那么在调用析构函数的时候,基类的析构函数必须是虚函数,因为这样才能保证先调用派生类的析构函数,然后调用基类的析构函数。
类内的静态成员变量需要在类外进行定义。详见https://blog.csdn.net/vict_wang/article/details/80994894