【我的青春coding物语果然有问题!】23年第三次上机补题

今天继续打一份补题的复盘

感觉今天这份多态不难,基本上都是以前的老题
就是这样,但是有的题有一些新发现
下面看题目:

02:统计动物数量

描述

代码填空,使得程序能够自动统计当前各种动物的数量

#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

Solution

关于这题的解题思路我不想多说,因为前几天有讲过
现在就是讲一个细节
我们在定义了static int number这个静态成员变量之后没有赋初值
所以要在类外面加上这一行:

int Animal::number=0;
int Dog::number=0;
int Cat::number=0;

下面看代码:

#include <iostream>
using namespace std;
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;
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();
    system("pause");
    return 0;
}

08:输出指定结果2

描述

通过填空使得程序输出的结果符合下面的要求。

#include <iostream>
#include <map>
using namespace std;
// 在此处补充你的代码
int A::count = 0;
void func(B b) {  }
int main()
{
	A a1(5),a2;
	cout << A::count << endl;
	B b1(4);
	cout << A::count << endl;
	func(b1);
	cout << A::count << endl;
	A * pa = new B(4);
	cout << A::count << endl;
	delete pa;
	cout << A::count << endl;
	return 0;
}

输入

不需要输入。

输出

使得程序的输出结果是:

2
3
B::destructor
A::destructor
3
4
B::destructor
A::destructor
3
B::destructor
A::destructor
A::destructor
A::destructor

样例输入

不需要输入。

样例输出

2
3
B::destructor
A::destructor
3
4
B::destructor
A::destructor
3
B::destructor
A::destructor
A::destructor
A::destructor

提示

int A::count = 0; 这个变量是用来记录一共有多少个类A及类A的派生类的对象的。

Solution

我们的抖机灵解法暂且不提
看到了一种很神奇的解法
就是重载delete 很神奇
我们下面贴一下代码:

#include <iostream>
#include <map>
using namespace std;
// 在此处补充你的代码
class A {
public:
    static int count;
    A(){count++;}
    A(int a) {count++;}
    virtual ~A(){
        cout<<"A::destructor"<<endl;
    }
    void operator delete(void *a) {
        count--;
    }
};
class B:public A {
public:
    B():A(){}
    B(int b):A(){}
    B &operator=(B &b) {
        return b;
    }
    virtual ~B(){
        cout<<"B::destructor"<<endl;
    }
};
int A::count = 0;
void func(B b) {}
int main()
{
    A a1(5),a2;
    cout << A::count << endl;
    B b1(4);
    cout << A::count << endl;
    func(b1);
    cout << A::count << endl;
    A * pa = new B(4);
    cout << A::count << endl;
    delete pa;
    cout << A::count << endl;
    return 0;
}

原理还不是很理解,应该是如果在func里创建的临时对象是B类 不会收到delete的A*类的影响?改天问问豆包
后半学期,也请各位继续关注:
《我的青春线代物语果然有问题》
《高数女主养成计划》
《程设の旅》
《青春猪头少年不会梦到多智能体吃豆人》
《某Linux的开源软件》
还有——

《我的算法竞赛不可能这么可爱》

本期到此结束!

posted @ 2025-04-24 13:37  elainafan  阅读(20)  评论(0)    收藏  举报