请帮我写一下宠物管理系统的代码
时间: 2023-08-28 15:27:58 浏览: 146
好的,宠物管理系统的代码如下:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Pet {
public:
Pet(string name, string breed, int age, string color) {
this->name = name;
this->breed = breed;
this->age = age;
this->color = color;
}
string getName() {
return name;
}
string getBreed() {
return breed;
}
int getAge() {
return age;
}
string getColor() {
return color;
}
private:
string name;
string breed;
int age;
string color;
};
class PetStore {
public:
void addPet(Pet pet) {
pets.push_back(pet);
}
void removePet(int index) {
pets.erase(pets.begin() + index);
}
void printPets() {
for (int i = 0; i < pets.size(); i++) {
cout << "Name: " << pets[i].getName() << endl;
cout << "Breed: " << pets[i].getBreed() << endl;
cout << "Age: " << pets[i].getAge() << endl;
cout << "Color: " << pets[i].getColor() << endl;
cout << endl;
}
}
private:
vector<Pet> pets;
};
int main() {
PetStore petStore;
Pet pet1("Max", "Dog", 2, "Brown");
Pet pet2("Kitty", "Cat", 1, "Black");
Pet pet3("Charlie", "Rabbit", 3, "White");
petStore.addPet(pet1);
petStore.addPet(pet2);
petStore.addPet(pet3);
petStore.printPets();
petStore.removePet(1);
petStore.printPets();
return 0;
}
```
这个宠物管理系统包含了PetStore和Pet两个类,Pet类表示一个宠物,PetStore类表示一个宠物商店,可以添加和删除宠物,以及打印宠物列表。在main函数中,我们创建了一些宠物,并将它们添加到PetStore中,然后打印出宠物列表。然后我们删除了一个宠物,并再次打印宠物列表,以验证删除操作。
阅读全文