编程语言
首页 > 编程语言> > 026 编程填空:统计动物数量

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();
}

标签:int,编程,number,Dog,026,Animal,填空,Cat,public
来源: https://www.cnblogs.com/icefield817/p/15914041.html