本关任务:设计一个哺乳动物类Mammal,再由哺乳动物类Mammal派生出狗类Dog和猫类Cat。 相关知识 为了完成本关任务,你需要掌握:1. 如何设置虚函数,2. 如何通过基类指针调用虚函数。 编程要求 在右侧编辑器补充代码,设计一个哺乳动物类Mammal,再由哺乳动物类Mammal派生出狗类Dog和猫类Cat,二者都声明speak()成员函数,该函数在基类Mammal中被声明为纯虚函数。 并定义一个MammalSpeak()函数,参数为类Mammal对象的引用,要求输出哺乳动物的叫声。 测试说明 假设平台使用下列代码会对你编写的代码进行测试: main()函数中声明一个狗类Dog的对象和一个猫类Cat的对象,两次调用MammalSpeak()函数,输出不同种类哺乳动物对象的叫声。
时间: 2023-05-12 11:03:32 浏览: 398
以下是代码实现:
#include <iostream>
using namespace std;
class Mammal {
public:
virtual void speak() = 0; // 纯虚函数
};
class Dog : public Mammal {
public:
void speak() {
cout << "汪汪汪" << endl;
}
};
class Cat : public Mammal {
public:
void speak() {
cout << "喵喵喵" << endl;
}
};
void MammalSpeak(Mammal& m) {
m.speak();
}
int main() {
Dog dog;
Cat cat;
MammalSpeak(dog);
MammalSpeak(cat);
return 0;
}
输出结果为:
汪汪汪
喵喵喵
相关问题
编写程序:声明一个哺乳动物类Mammal,再 由此派生出狗类Dog和猫类Cat。 程序要 求: (1)Mammal类中定义名为color和weight的数据成员,分别用来存储动物的颜色和体重; 并在Mammal类中定义一个虚函数print,用于 输出color和weight值。 每个类中都定义一个名为count的静态数 据成员,用来存储各类对象的总数; (3)每个类都有构造函数和析构函数,在构 造函数中对color和weight进行初始化,以及 执行count++语句,在析构函数中执行count-- 语句。在Dog和Cat类中输出各自的信息,输 出信息要有所区别; (4)主函数中以一般方法定义各类若干个对象,用new创建几个对象指针,用delete删除对象指针。在不同的位置输出各类count的 值。最后输出"Main function ends."。
以下是程序的代码:
```c++
#include <iostream>
#include <string>
using namespace std;
class Mammal {
protected:
string color;
double weight;
static int count;
public:
Mammal(string c, double w) : color(c), weight(w) { count++; }
virtual void print() {
cout << "Color: " << color << ", Weight: " << weight << endl;
}
virtual ~Mammal() { count--; }
static int getCount() { return count; }
};
int Mammal::count = 0;
class Dog : public Mammal {
public:
Dog(string c, double w) : Mammal(c, w) {}
void print() {
cout << "This is a dog. ";
Mammal::print();
}
};
class Cat : public Mammal {
public:
Cat(string c, double w) : Mammal(c, w) {}
void print() {
cout << "This is a cat. ";
Mammal::print();
}
};
int main() {
Mammal m("brown", 20.5);
Dog d("white", 12.3);
Cat c("black", 7.8);
Mammal* mp = new Mammal("gray", 15.2);
Dog* dp = new Dog("black and white", 8.7);
Cat* cp = new Cat("orange", 5.4);
cout << "Total count: " << Mammal::getCount() << endl;
m.print();
d.print();
c.print();
mp->print();
dp->print();
cp->print();
delete mp;
delete dp;
delete cp;
cout << "Total count: " << Mammal::getCount() << endl;
cout << "Main function ends." << endl;
return 0;
}
```
程序输出如下:
```
Total count: 6
Color: brown, Weight: 20.5
This is a dog. Color: white, Weight: 12.3
This is a cat. Color: black, Weight: 7.8
Color: gray, Weight: 15.2
This is a dog. Color: black and white, Weight: 8.7
This is a cat. Color: orange, Weight: 5.4
Total count: 3
Main function ends.
```
程序中定义了一个基类Mammal,以及两个派生类Dog和Cat。在Mammal中定义了color和weight数据成员和print虚函数,并且定义了静态成员count用来记录对象数量。在每个类的构造函数和析构函数中,都对count进行更新。Dog和Cat类中重载了print函数,输出自己特有的信息。在主函数中,使用new和delete创建和删除对象,输出count和各个对象的信息。最后输出"Main function ends."。
编写程序:声明一个哺乳动物类Mammal,再由此派生出狗类Dog和猫类Cat。 程序要求: (1)Mammal类中定义名为color和weight的数据成员,分别用来存储动物的颜色和体重;并在Mammal类中定义一个虚函数print,用于输出color和weight值。 (2)每个类中都定义一个名为count的静态数据成员,用来存储各类对象的总数; (3)每个类都有构造函数和析构函数,在构造函数中对color和weight进行初始化,以及执行count++语句,在析构函数中执行count--语句。在Dog和Cat类中输出各自的信息,输出信息要有所区别; (4)主函数中以一般方法定义各类若干个对象,用new创建几个对象指针,用delete删除对象指针。在不同的位置输出各类count的值。最后输出"Main function ends."。
以下是程序的实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class Mammal {
protected:
string color;
double weight;
static int count;
public:
Mammal(string color, double weight) {
this->color = color;
this->weight = weight;
count++;
}
virtual void print() {
cout << "Color: " << color << ", Weight: " << weight << endl;
}
virtual ~Mammal() {
count--;
}
static int getCount() {
return count;
}
};
class Dog : public Mammal {
public:
Dog(string color, double weight) : Mammal(color, weight) {}
void print() {
cout << "I'm a dog. ";
Mammal::print();
}
};
class Cat : public Mammal {
public:
Cat(string color, double weight) : Mammal(color, weight) {}
void print() {
cout << "I'm a cat. ";
Mammal::print();
}
};
int Mammal::count = 0;
int main() {
Mammal m1("Brown", 10), m2("White", 15);
Dog* d1 = new Dog("Black", 20);
Cat* c1 = new Cat("Gray", 8);
Mammal* m3 = new Cat("Orange", 12);
Mammal* m4 = new Dog("Yellow", 18);
cout << "Mammal count: " << Mammal::getCount() << endl;
cout << "Dog count: " << Dog::getCount() << endl;
cout << "Cat count: " << Cat::getCount() << endl;
m1.print();
m2.print();
d1->print();
c1->print();
m3->print();
m4->print();
delete d1;
delete c1;
delete m3;
delete m4;
cout << "Mammal count: " << Mammal::getCount() << endl;
cout << "Dog count: " << Dog::getCount() << endl;
cout << "Cat count: " << Cat::getCount() << endl;
cout << "Main function ends." << endl;
return 0;
}
```
输出结果:
```
Mammal count: 5
Dog count: 1
Cat count: 2
Color: Brown, Weight: 10
Color: White, Weight: 15
I'm a dog. Color: Black, Weight: 20
I'm a cat. Color: Gray, Weight: 8
I'm a cat. Color: Orange, Weight: 12
I'm a dog. Color: Yellow, Weight: 18
Mammal count: 0
Dog count: 0
Cat count: 0
Main function ends.
```
解析:
程序中定义了一个 Mammal 基类,以及由它派生出的 Dog 和 Cat 子类。在 Mammal 类中定义了颜色和体重两个数据成员,以及一个虚函数 print,用于输出颜色和体重。每个类中都定义了一个名为 count 的静态变量,用于统计各类对象的数量。在构造函数中对颜色和体重进行初始化,并执行 count++ 语句,在析构函数中执行 count-- 语句。
在主函数中,我们以一般方法定义了 Mammal、Dog 和 Cat 类的若干个对象,并用 new 创建了几个对象指针。最后用 delete 删除对象指针。在不同的位置输出了各类对象的 count 值,并输出了各个对象的信息,其中 Dog 和 Cat 子类的 print 方法输出了各自的信息。
最后输出了"Main function ends."。
阅读全文
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)