DEVC++ 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:21 浏览: 76
Here is a possible implementation of the Pet, Dog, and Rock classes in C++:
```cpp
#include <iostream>
#include <string>
class Pet {
public:
Pet(const std::string& name, int age, double weight)
: name(name), age(age), weight(weight) {}
virtual ~Pet() {}
const std::string& getName() const { return name; }
int getAge() const { return age; }
double getWeight() const { return weight; }
void setName(const std::string& newName) { name = newName; }
void setAge(int newAge) { age = newAge; }
void setWeight(double newWeight) { weight = newWeight; }
virtual std::string getLifespan() const { return "unknown lifespan"; }
private:
std::string name;
int age;
double weight;
};
class Dog : public Pet {
public:
Dog(const std::string& name, int age, double weight, const std::string& breed)
: Pet(name, age, weight), breed(breed) {}
const std::string& getBreed() const { return breed; }
void setBreed(const std::string& newBreed) { breed = newBreed; }
virtual std::string getLifespan() const override {
if (getWeight() > 100) {
return "Approximately 7 years";
} else {
return "Approximately 13 years";
}
}
private:
std::string breed;
};
class Rock : public Pet {
public:
Rock(const std::string& name, int age, double weight)
: Pet(name, age, weight) {}
virtual std::string getLifespan() const override {
return "Thousands of years";
}
};
int main() {
Dog dog("Fido", 5, 80, "Golden Retriever");
std::cout << dog.getName() << " is a " << dog.getBreed()
<< " and lives for " << dog.getLifespan() << std::endl;
Rock rock("Pebbles", 1000, 0.1);
std::cout << rock.getName() << " is a rock and lives for "
<< rock.getLifespan() << std::endl;
return 0;
}
```
In this implementation, the Pet class stores the basic information about a pet, such as its name, age, and weight, and provides accessor and mutator functions for these variables. The getLifespan function is virtual and returns a default value of "unknown lifespan". The Dog class adds a private member variable for the breed of the dog, as well as mutator and accessor functions for it. It also redefines the getLifespan function to return a different value depending on the weight of the dog. The Rock class is a special case of a pet that has a very long lifespan, so it redefines the getLifespan function accordingly.
The main function creates instances of a dog and a rock, and calls their member functions to exercise their behavior. The output of the program should be:
```
Fido is a Golden Retriever and lives for Approximately 13 years
Pebbles is a rock and lives for Thousands of years
```
阅读全文