C++面向对象程序设计 026:编程填空:统计动物数量 ---- (北大Mooc)
发布日期:2021-06-30 22:27:42 浏览次数:2 分类:技术文章

本文共 845 字,大约阅读时间需要 2 分钟。

文章目录


专题博客链接


原题题目

在这里插入图片描述

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

代码实现

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

转载地址:https://love6.blog.csdn.net/article/details/114956774 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C++面向对象程序设计 027:简单的SumArray ---- (北大Mooc) (麻了 今天一天嗯是上课没跑成)
下一篇:C++面向对象程序设计 025:怎么又是Fun和Do ---- (北大Mooc)

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月16日 14时31分18秒