Java教程

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

这篇关于026 编程填空:统计动物数量的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!