c++ Problem 1 Define a Pet class that stores the pet’s name, age, and weight. Add appropriate constructors, accessor, and mutator functions. Also define a function named getLifespan that returns a string with the value “unknown lifespan.” Next, define a Dog class that is derived from Pet. The Dog class should have a private member variable named breed that stores the breed of the dog. Add mutator and accessor functions for the breed variable and appropriate constructors. Redefine the getLifespan function to return "Approximately 7 years" if the dog’s weight is over 100 pounds and "Approximately 13 years" if the dog’s weight is under 100 pounds. Next, define a Rock class that is derived from Pet. Redefine the getLifespan function to return “Thousands of years”. Finally, write a test program that creates instances of pet rocks and pet dogs that exercise the inherited and redefined functions.
时间: 2024-03-25 18:40:07 浏览: 17
Here's an implementation which satisfies the requirements:
```c++
#include <iostream>
#include <string>
using namespace std;
class Pet {
private:
string name;
int age;
double weight;
public:
Pet(string n, int a, double w) {
name = n;
age = a;
weight = w;
}
string getName() {
return name;
}
void setName(string n) {
name = n;
}
int getAge() {
return age;
}
void setAge(int a) {
age = a;
}
double getWeight() {
return weight;
}
void setWeight(double w) {
weight = w;
}
virtual string getLifespan() {
return "unknown lifespan";
}
};
class Dog : public Pet {
private:
string breed;
public:
Dog(string n, int a, double w, string b) : Pet(n, a, w) {
breed = b;
}
string getBreed() {
return breed;
}
void setBreed(string b) {
breed = b;
}
virtual string getLifespan() {
if (getWeight() > 100) {
return "Approximately 7 years";
} else {
return "Approximately 13 years";
}
}
};
class Rock : public Pet {
public:
Rock(string n, int a, double w) : Pet(n, a, w) {}
virtual string getLifespan() {
return "Thousands of years";
}
};
int main() {
Pet *p1, *p2, *p3, *p4;
p1 = new Dog("Fluffy", 3, 120.5, "Golden Retriever");
p2 = new Dog("Buddy", 8, 60.2, "Labrador");
p3 = new Rock("Rocky", 1000, 10.0);
p4 = new Rock("Smokey", 5000, 20.0);
cout << p1->getName() << " (" << p1->getBreed() << "): " << p1->getLifespan() << endl;
cout << p2->getName() << " (" << p2->getBreed() << "): " << p2->getLifespan() << endl;
cout << p3->getName() << ": " << p3->getLifespan() << endl;
cout << p4->getName() << ": " << p4->getLifespan() << endl;
delete p1;
delete p2;
delete p3;
delete p4;
return 0;
}
```
The `Pet` class stores the pet's name, age, and weight, and has appropriate constructors, accessor, and mutator functions. The `Dog` class is derived from `Pet` and adds a private member variable `breed` along with mutator and accessor functions. The `Dog` class redefines the `getLifespan` function to return "Approximately 7 years" if the dog’s weight is over 100 pounds and "Approximately 13 years" if the dog’s weight is under 100 pounds. Similarly, the `Rock` class is derived from `Pet` and redefines the `getLifespan` function to return "Thousands of years". The test program creates instances of `Pet`, `Dog`, and `Rock` classes and tests the inherited and redefined functions.