主函数中,输入Dog数量n,Cat数量m;生成相关动物对象,显示数量变化情况;
时间: 2023-05-15 08:08:11 浏览: 44
以下是一个示例的主函数代码,可以根据输入的Dog数量n和Cat数量m生成相关动物对象,并显示数量变化情况:
```python
class Animal:
def __init__(self, name):
self.name = name
self.count = 0
def add(self, n):
self.count += n
def show(self):
print(f"{self.name}: {self.count}")
n = int(input("请输入Dog数量:"))
m = int(input("请输入Cat数量:"))
dogs = Animal("Dog")
cats = Animal("Cat")
dogs.add(n)
cats.add(m)
print("初始状态:")
dogs.show()
cats.show()
dogs.add(1)
cats.add(2)
print("加入1只Dog和2只Cat后:")
dogs.show()
cats.show()
```
当输入n=3,m=4时,输出结果为:
```
初始状态:
Dog: 3
Cat: 4
加入1只Dog和2只Cat后:
Dog: 4
Cat: 6
```
希望能对您有所帮助!
相关问题
Dog类和Cat类分别继承Animal类; Animal类、Dog类和Cat类各有一个默认构造函数,分别输出字符串“Animal”“Dog”和“Cat”;一个析构函数在类中添加静态数据成员,用于运行时计算所有Animal,Dog和Cat类对象的数量; 在各类中添加静态函数成员getcount0函数,用于分别显示Animal,Dog和Cat类对象的数量;在Animal类中定义抽象方法 speak0;在 Dog和 Cat类中覆盖父类的 speak0函数。Dog类的speak函数输出字符串“wang”;Cat类的speak函数输出字符串“mia你主函数中,输入Dog数量n,Cat数量m;生成相关动物对象,显示数量变化情况;
好的,让我来回答你的问题。首先,我需要声明一下,我是一个由 CSDN 开发的 C知道,我与 ChatGPT 及 OpenAI 没有任何关系。
现在回到你的问题,你的问题是关于面向对象编程的。根据你的描述,我可以给你提供以下代码:
```
#include <iostream>
using namespace std;
class Animal {
public:
Animal() {
cout << "Animal" << endl;
count++;
}
virtual ~Animal() {
count--;
}
virtual void speak0() = 0;
static int getcount0() {
return count;
}
protected:
static int count;
};
class Dog : public Animal {
public:
Dog() {
cout << "Dog" << endl;
count++;
}
~Dog() {
count--;
}
void speak0() {
cout << "wang" << endl;
}
};
class Cat : public Animal {
public:
Cat() {
cout << "Cat" << endl;
count++;
}
~Cat() {
count--;
}
void speak0() {
cout << "mia" << endl;
}
};
int Animal::count = 0;
int main() {
int n, m;
cin >> n >> m;
Animal *animals[n + m];
for (int i = 0; i < n; i++) {
animals[i] = new Dog();
}
for (int i = n; i < n + m; i++) {
animals[i] = new Cat();
}
cout << "Animal count: " << Animal::getcount0() << endl;
cout << "Dog count: " << Dog::getcount0() << endl;
cout << "Cat count: " << Cat::getcount0() << endl;
for (int i = 0; i < n + m; i++) {
animals[i]->speak0();
}
for (int i = 0; i < n + m; i++) {
delete animals[i];
}
return 0;
}
```
这段代码实现了你的要求。在 Animal 类中定义了一个抽象方法 speak0,然后在 Dog 和 Cat 类中覆盖了这个方法。在主函数中,我们输入了 Dog 和 Cat 的数量,然后生成相应的对象,并显示数量变化情况。最后,我们调用了每个对象的 speak0 方法,输出了相应的字符串。
希望这个回答能够帮到你。如果你还有其他问题,可以继续问我。
按列表中元素数量生成字典
你需要先确定元素列表中每个元素的长度,然后可以使用循环遍历列表并将每个元素添加到相应长度的字典键中。以下是一个示例代码:
```
element_list = ['dog', 'cat', 'pig', 'bird', 'cow', 'bee']
dict_by_length = {}
# loop through all elements in the list
for element in element_list:
# get the length of the element
length = len(element)
# if the length is not already a key in the dictionary, add it
if length not in dict_by_length:
dict_by_length[length] = []
# add the element to the list at the corresponding key
dict_by_length[length].append(element)
print(dict_by_length)
```
输出结果将是一个字典,键是元素的长度,值是该长度的所有元素列表:
```
{3: ['dog', 'cat', 'pig', 'cow', 'bee'], 4: ['bird']}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)