SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Inheritance
Exercise:
Programming Projects
Question:10 | ISBN:9780132846813 | Edition: 5

Question

Define a Pet class that stores the pet’s name, age, and weight. Add appropriate constructors, accessor functions, 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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header file section
#include <iostream>
#include <string>
using namespace std;
class Pet {
protected:
    string name;
    int age;
    double weight;

public:
    // Constructors
    Pet() : age(0), weight(0.0) {}
    Pet(const string& name, int age, double weight) : name(name), age(age), weight(weight) {}

    // Accessor functions
    string getName() const {
        return name;
    }

    int getAge() const {
        return age;
    }

    double getWeight() const {
        return weight;
    }

    // Mutator functions
    void setName(const string& new_name) {
        name = new_name;
    }

    void setAge(int new_age) {
        age = new_age;
    }

    void setWeight(double new_weight) {
        weight = new_weight;
    }

    // Function to get the lifespan (default value)
    virtual string getLifespan() const {
        return "Unknown lifespan.";
    }
};

class Dog : public Pet {
private:
    string breed;

public:
    // Constructors
    Dog() : Pet(), breed("Unknown breed") {}
    Dog(const string& name, int age, double weight, const string& breed)
        : Pet(name, age, weight), breed(breed) {}

    // Accessor function for breed
    string getBreed() const {
        return breed;
    }

    // Mutator function for breed
    void setBreed(const string& new_breed) {
        breed = new_breed;
    }

    // Redefined function to get the lifespan based on weight
    string getLifespan() const override {
        if (weight > 100) {
            return "Approximately 7 years.";
        } else {
            return "Approximately 13 years.";
        }
    }
};

class Rock : public Pet {
public:
    // Redefined function to get the lifespan for a rock
    string getLifespan() const override {
        return "Thousands of years.";
    }
};

int main() {
    Dog dog1("Rover", 5, 80.5, "Labrador");
    Dog dog2("Max", 3, 120.0, "Saint Bernard");
    Rock rock1("Rocky", 1000, 1.5);
    Rock rock2("Pebbles", 2000, 2.0);

    // Display dog information
    cout << "Dog 1 - Name: " << dog1.getName() << ", Age: " << dog1.getAge()
              << ", Weight: " << dog1.getWeight() << " lbs, Breed: " << dog1.getBreed()
              << ", Lifespan: " << dog1.getLifespan() << endl;

    cout << "Dog 2 - Name: " << dog2.getName() << ", Age: " << dog2.getAge()
              << ", Weight: " << dog2.getWeight() << " lbs, Breed: " << dog2.getBreed()
              << ", Lifespan: " << dog2.getLifespan() << endl;

    // Display rock information
    cout << "Rock 1 - Name: " << rock1.getName() << ", Age: " << rock1.getAge()
              << ", Weight: " << rock1.getWeight() << " lbs, Lifespan: " << rock1.getLifespan() << endl;

    cout << "Rock 2 - Name: " << rock2.getName

 

OUTPUT:

Dog 1 - Name: Rover, Age: 5, Weight: 80.5 lbs, Breed: Labrador, Lifespan: Approximately 13 years.
Dog 2 - Name: Max, Age: 3, Weight: 120 lbs, Breed: Saint Bernard, Lifespan: Approximately 7 years.
Rock 1 - Name: Rocky, Age: 1000, Weight: 1.5 lbs, Lifespan: Thousands of years.
Rock 2 - Name: Pebbles, Age: 2000, Weight: 2 lbs, Lifespan: Thousands of years.

 

0 0

Discussions

Post the discussion to improve the above solution.