Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with C++. Please answer this question. Objectives: Distinguishing between the is a relationship of inheritance and the has a relationship of composition. Implementing

Need help with C++. Please answer this question.

Objectives:

  • Distinguishing between the is a relationship of inheritance and the has a relationship of composition.
  • Implementing base class and derived class functions.
  • Overriding a base class function in a derived class.
  • Writing classes that are composed of objects of other classes.

Instructions:

Download given code on Canvas. Create a new project and out all files into their supposed folders. Read and understand the given code. The classes and dependencies are shown in the included class diagram. There is a base class called Animal. A horse is an animal. Horse is derived from Animal. This relationship is inheritance. A bird is an animal. Bird is derived from Animal. This relationship is also inheritance. A Zoo has an animal (or several). In other words, the classes Horse and Bird are members of the Zoo class. This relationship is composition.

Part One: Inheritance

Please complete the following TODO tasks to implement the Animal base class, the Horse derived class, and the Bird derived class. It might look like a lot, but many of the functions are just getters and setters.

Part Two: Composition

Please complete the following TODO tasks to implement the Zoo class, which is composed of an object of the Horse class and an object of the Bird class.

Below are the given codes:

Zoo.h

#ifndef ZOO_H #define ZOO_H

#include #include #include "Animal.h" #include "Horse.h" #include "Bird.h"

class Zoo { public: Zoo(const Horse &h, const Bird &b); void printZooData() const;

private: Horse horse; Bird bird; };

#endif // !ZOO_H

zoo.cpp

#include "Zoo.h"

Zoo::Zoo(const Horse &h, const Bird &b) { // TODO: Implement this parameterized constructor to set the member // objects horse and bird according to the objects passed in the // parameter. }

void Zoo::printZooData() const { // Output the information about the two animals at this zoo. // Output the information about the horse. Then output the // information about the bird.

// Using the isOld function, also output information about // which of the two animals is older. }

Horse.h

#ifndef HORSE_H #define HORSE_H #include #include #include "Animal.h"

using namespace std;

class Horse : public Animal { public: Horse(); Horse(string, int, int, string); void setData(string, int, int, string); void setColor(string); string getColor() const; void printRecord() const;

private: string color; };

#endif // !HORSE_H

Horse.cpp

#include "Horse.h"

Horse::Horse() { // TODO: Use the setColor function to set this derived class member // variable color to "no-color". The base class content will be // initialized according to the base class's default constructor. }

Horse::Horse(string n, int a, int w, string b):Animal(n, a, w) // TODO: Before the opening brace, use the syntax from the example slides // to construct the base class using the base class parameterized // constructor. Pass the base class content (name, age and weight) // up to the base class. { // After the opening brace, call the setColor function to set the one // derived class member variable color according to the corresponding // parameter. }

void Horse::setData(string n, int a, int w, string b) { // TODO: The base class and derived classes both have setData functions. // The derived class setData function overrides the base class one. However, // we can invoke the base class version of the function here if we scope to it. // Since the member variables name, age, and weight are stored // in the base class, use the base class's setData function to set those // values according to the corresponding parameters.

// Then use the setColor function to set the remaining derived class variable. }

void Horse::setColor(string b) { // TODO: Set the member variable name according to the parameter. }

string Horse::getColor() const { // TODO: Return the member variable color. }

void Horse::printRecord() const { // TODO: The base class and derived classes both have printRecord functions. // The derived class printRecord function overrides the base class one. However, // we can invoke the base class version of the function here if we scope to it. // Since the member variables name, age, and weight are stored // in the base class, use the base class's printRecord function output those // base class values.

// Then output the derived class member variable directly. }

Bird.h

#ifndef BIRD_H #define BIRD_H #include #include #include "Animal.h"

using namespace std;

class Bird : public Animal { public: Bird(); Bird(string, int, int, double); void setData(string, int, int, double); void setWingspan(double); double getWingspan() const; void printRecord() const;

private: double wingSpan; };

#endif // !BIRD_H

Bird.cpp

#include "Bird.h"

Bird::Bird() { // TODO: Use the setWingspan function to set this derived class member // variable wingSpan to 0.0. The base class content will be // initialized according to the base class's default constructor. }

Bird::Bird(string n, int a, int w, double ws):Animal(n, a, w) // TODO: Before the opening brace, use the syntax from the example slides // to construct the base class using the base class parameterized // constructor. Pass the base class content (name, age and weight) // up to the base class. { // After the opening brace, call the setWingspan function to set the one // derived class member variable wingSpan according to the corresponding // parameter. }

void Bird::setData(string n, int a, int w, double ws) { // TODO: The base class and derived classes both have setData functions. // The derived class setData function overrides the base class one. However, // we can invoke the base class version of the function here if we scope to it. // Since the member variables name, age, and weight are stored // in the base class, use the base class's setData function to set those // values according to the corresponding parameters.

// Then use the setWingspan function to set the remaining derived class variable. }

void Bird::setWingspan(double ws) { // TODO: Provided that the parameter value is >= 0, set the member // variable wingSpan according to the parameter. Otherwise, set the // member variable wingSpan to 0.0. }

double Bird::getWingspan() const { // TODO: Return the member variable wingSpan. }

void Bird::printRecord() const { // TODO: The base class and derived classes both have printRecord functions. // The derived class printRecord function overrides the base class one. However, // we can invoke the base class version of the function here if we scope to it. // Since the member variables name, age, and weight are stored // in the base class, use the base class's printRecord function output those // base class values.

// Then output the derived class member variable directly. }

Animal.h

#ifndef ANIMAL_H #define ANIMAL_H #include #include

using namespace std;

class Animal { public: Animal(); Animal(string, int, int); void setData(string, int, int); void setName(string); void setAge(int); void setWeight(int); string getName() const; int getAge() const; int getWeight() const; void printRecord() const;

static bool isOlder(const Animal &first, const Animal &second);

private: string name; int age; int weight; };

#endif // ! ANIMAL_H

Animal.cpp

#include "Animal.h"

Animal::Animal() { // TODO: Use the setData function to set the member variable name // to "no-name", the member variable age to 0, and the member variable // weight to 0. }

Animal::Animal(string n, int a, int w) { // TODO: Use the setData function to set the member variables // according their corresponding parameters. }

void Animal::setData(string n, int a, int w) { // TODO: Use the setName, setAge, and setWeight functions to set // the member variables according to their corresponding parameters. }

void Animal::setName(string n) { // TODO: Set the member variable name according to the parameter. }

void Animal::setAge(int a) { // TODO: Provided that the parameter value is >= 0, set the member // variable age according to the parameter. Otherwise, set the // member variable age to 0. }

void Animal::setWeight(int w) { // TODO: Provided that the parameter value is >= 0, set the member // variable weight according to the parameter. Otherwise, set the // member variable weight to 0. }

string Animal::getName() const { // TODO: Return the member variable name. }

int Animal::getAge() const { // TODO: Return the member variable age. }

int Animal::getWeight() const { // TODO: Return the member variable weight. }

void Animal::printRecord() const { // TODO: Output the name, age, and weight. }

bool Animal::isOlder(const Animal &first, const Animal &second) { // TODO: Return true if first object passed in the parameter // has a greater age than the second object passed in the parameter. // Otherwise, false is returned. cout

client.cpp

#include #include

#include "Animal.h" #include "Horse.h" #include "Bird.h" #include "Zoo.h"

using namespace std;

int main() { Horse horse1("Runner", 9, 25, "White"); Bird bird1("Polly", 2, 2, 4.2);

Zoo myZoo(horse1, bird1);

myZoo.printZooData();

return 0; }

image text in transcribed

Sample Output: Name: Runner Age: 9 Weight: 25 Color: White Name: Polly Age: 2 Weight: 2 Yings: 4.2 Runner is older than Polly

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions

Question

=+What is the brand's character or personality?

Answered: 1 week ago