The following is an attempt to create a class to represent information about pets: class Pet { public: Pet(); void printDescription(); string name; int type; bool neuterSpayed; bool talks; }; Pet::Pet() : type(0), neuterSpayed(false), talks(false) { } void Pet::printDescription() { switch (type) { case 0: cout << "Dog named " << name << endl; cout << "Neuter/Spayed: " << neuterSpayed; break; case 1: cout << "Cat named " << name << endl; cout << "Neuter/Spayed: " << neuterSpayed; break; case 2: cout << "Bird named " << name << endl; cout << "Talks: " << talks << endl; break; } cout << endl; } Rewrite this code using inheritance. You should have a Pet class with subclasses for Dog, Cat, and Bird. Variables should be associated with the appropriate classes, defined as private when appropriate, and have appropriate functions to access the variables. Rewrite the printDescription function as a virtual function. There should no longer be the need for a switch statement or a type variable. Write a main function that creates a vector or array of pets that includes at least one bird, one dog, and one cat, and then loops through and outputs a description of each one.
时间: 2024-01-07 18:03:01 浏览: 28
Here's the updated code:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Pet {
public:
Pet(string name) : m_name(name) {}
virtual void printDescription() = 0; // make it pure virtual to make Pet an abstract class
protected:
string m_name;
};
class Dog : public Pet {
public:
Dog(string name, bool neutered) : Pet(name), m_neutered(neutered) {}
void printDescription() override {
cout << "Dog named " << m_name << endl;
cout << "Neuter/Spayed: " << m_neutered << endl;
}
private:
bool m_neutered;
};
class Cat : public Pet {
public:
Cat(string name, bool neutered) : Pet(name), m_neutered(neutered) {}
void printDescription() override {
cout << "Cat named " << m_name << endl;
cout << "Neuter/Spayed: " << m_neutered << endl;
}
private:
bool m_neutered;
};
class Bird : public Pet {
public:
Bird(string name, bool talks) : Pet(name), m_talks(talks) {}
void printDescription() override {
cout << "Bird named " << m_name << endl;
cout << "Talks: " << m_talks << endl;
}
private:
bool m_talks;
};
int main() {
vector<Pet*> pets;
pets.push_back(new Dog("Fido", true));
pets.push_back(new Cat("Whiskers", false));
pets.push_back(new Bird("Polly", true));
for (Pet* pet : pets) {
pet->printDescription();
}
// free memory
for (Pet* pet : pets) {
delete pet;
}
return 0;
}
```
In this updated code, we've used inheritance to create subclasses for Dog, Cat, and Bird. Each subclass has its own private variables `m_neutered` and `m_talks`, respectively. The `printDescription` function is now a virtual function in the `Pet` class, which means that each subclass can override it with its own implementation.
In the main function, we create a vector of `Pet` pointers and add instances of each subclass to it. We then loop through the vector and call the `printDescription` function on each `Pet` object, which automatically calls the appropriate subclass implementation of the function. Finally, we free the memory allocated for each `Pet` object using `delete`.