程序填空题:动物和狗 - C/C++ 代码复用
请将后续代码补充完整,使其满足下述要求,并可得到期望的运行结果。
* 设计动物基类Animal,包括体重、脚的数量等数据成员;
* 从Animal扩展出子类Dog,添加名字等数据成员;
* 为两个类添加带有信息输出的构造函数以及析构函数,观察当一个Dog对象被创建时,子类与父类构造函数的执行顺序;
* 观察当一个Dog对象被析构时,子类与父类析构函数的执行顺序;
* 打印输出一个Dog对象的地址以及子类对象中父对象的地址,分析Dog对象的内存结构。
c++
#include <iostream>
using namespace std;
class Animal {
public:
int iFeetCount {0};
Animal(){
}
{
cout << "~Animal(), weight = " << iWeight << endl;
}
};
class Dog : {
public:
string sName;
Dog(const string name, int weight){
sName = name;
iFeetCount = 4;
cout <<"Dog(), name = " << name << ", weight = " << iWeight << endl;
}
{
cout <<"~Dog(), name = " << sName << ", weight = " << iWeight << endl;
}
};
int main()
{
Dog* d1 = new Dog("Bottle",3100);
delete d1;
cout << "---------------------------------------" << endl;
Dog* d2 = new Dog("Nauty",2312);
Animal* a2 = d2;
if (a2==d2){
cout << "Parent object is located at the beginning of sub object." << endl;
}
delete d2;
return 0;
}
期望的运行结果为:
Animal(), weight = 0
Dog(), name = Bottle, weight = 3100
~Dog(), name = Bottle, weight = 3100
~Animal(), weight = 3100
---------------------------------------
Animal(), weight = 0
Dog(), name = Nauty, weight = 2312
Parent object is located at the beginning of sub object.
~Dog(), name = Nauty, weight = 2312
~Animal(), weight = 2312
**说明**
当一个对象被创建时,其在评测机内存中的位置是不确定的,所以出题者无法在OJ中设定合适的测试用例来评价上述程序是否满足题目要求的最后一条。题目要求的最后一条建议答题者自行研究体会。
### 感觉不会? 那试着听听**免费的B站网课**
[简洁的C和C++ - 重庆大学在线课程](https://www.bilibili.com/video/BV1it411d7zx/)
[Python编程基础及应用 - 重庆大学在线课程](https://www.bilibili.com/video/BV1kt411R7uW/)
![image.png](~/6e79c9e3-cb7f-486d-ab78-36b5a8f655c0.png)
答案:
第1空:int iWeight {0};
第2空:cout << "Animal(), weight = " << iWeight << endl;
第3空:~Animal()
第4空:public Animal
第5空:iWeight = weight;
第6空:~Dog()
* 设计动物基类Animal,包括体重、脚的数量等数据成员;
* 从Animal扩展出子类Dog,添加名字等数据成员;
* 为两个类添加带有信息输出的构造函数以及析构函数,观察当一个Dog对象被创建时,子类与父类构造函数的执行顺序;
* 观察当一个Dog对象被析构时,子类与父类析构函数的执行顺序;
* 打印输出一个Dog对象的地址以及子类对象中父对象的地址,分析Dog对象的内存结构。
c++
#include <iostream>
using namespace std;
class Animal {
public:
int iFeetCount {0};
Animal(){
}
{
cout << "~Animal(), weight = " << iWeight << endl;
}
};
class Dog : {
public:
string sName;
Dog(const string name, int weight){
sName = name;
iFeetCount = 4;
cout <<"Dog(), name = " << name << ", weight = " << iWeight << endl;
}
{
cout <<"~Dog(), name = " << sName << ", weight = " << iWeight << endl;
}
};
int main()
{
Dog* d1 = new Dog("Bottle",3100);
delete d1;
cout << "---------------------------------------" << endl;
Dog* d2 = new Dog("Nauty",2312);
Animal* a2 = d2;
if (a2==d2){
cout << "Parent object is located at the beginning of sub object." << endl;
}
delete d2;
return 0;
}
期望的运行结果为:
Animal(), weight = 0
Dog(), name = Bottle, weight = 3100
~Dog(), name = Bottle, weight = 3100
~Animal(), weight = 3100
---------------------------------------
Animal(), weight = 0
Dog(), name = Nauty, weight = 2312
Parent object is located at the beginning of sub object.
~Dog(), name = Nauty, weight = 2312
~Animal(), weight = 2312
**说明**
当一个对象被创建时,其在评测机内存中的位置是不确定的,所以出题者无法在OJ中设定合适的测试用例来评价上述程序是否满足题目要求的最后一条。题目要求的最后一条建议答题者自行研究体会。
### 感觉不会? 那试着听听**免费的B站网课**
[简洁的C和C++ - 重庆大学在线课程](https://www.bilibili.com/video/BV1it411d7zx/)
[Python编程基础及应用 - 重庆大学在线课程](https://www.bilibili.com/video/BV1kt411R7uW/)
![image.png](~/6e79c9e3-cb7f-486d-ab78-36b5a8f655c0.png)
答案:
第1空:int iWeight {0};
第2空:cout << "Animal(), weight = " << iWeight << endl;
第3空:~Animal()
第4空:public Animal
第5空:iWeight = weight;
第6空:~Dog()