026 编程填空:统计动物数量

#include <iostream>
using namespace std;

class Animal {
public:
    static int number;
    virtual ~Animal() {
    }
};
class Dog:public Animal {

public:
    static int number;
    Dog() {
        ++Animal::number;
        ++number;
    }
    ~Dog() {
        --Animal::number;
        --number;
    }
};
class Cat:public Animal {
public:
    static int number;
    Cat() {
        ++Animal::number;
        ++number;
    }
    virtual  ~Cat() {
        --Animal::number;
        --number;
    }
};

int Animal::number = 0;
int Dog::number = 0;
int Cat::number = 0;
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();
}

posted @ 2022-02-19 22:49  icefield817  阅读(92)  评论(0编辑  收藏  举报