Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Subject Write a complete C++ program and create a class tree shown in Figure 1.

ID: 3700942 • Letter: S

Question

Subject Write a complete C++ program and create a class tree shown in Figure 1. For each class in the tree, first write code for the non-argument constructor that prints out the class name. Then add one private property for each nimal growO class and corresponding get and set methods. Then split the code for definition and code for wo implementation into a header file and a source file displayO respectively. Plus the additional source file TestCreature.cpp with main() function, there should be Creature growO displayO Plant displayO growo Grass Insect growO Pine growo Parsle Fly displayO 19 files display) Figure 1 In addition, follow the instruction in Figure 1 and add growO and display) methods to specified classes. For each growO, print with clear prompt text 1) Annual growth rate in unit of inch 2) Host class name For any display) method, print the host class name and the current value of its own property and the property of its every ancestor class. If an ancestor had display), call display. If an ancestor does not have displayO, call its property's get method. Note one property should only be printed once. In the main() function, create one object from each class in the above class tree. Then call both growO and display) methods from each object created. The code of creating object of a certain class should immediately proceed the code to call this class's two methods. In other words, the code of one object should be together. The order of creating and using object should be top-down and then left-to-right n one row The printout should confirm to following rules of dynamic binding or polymorphism 1) On the path from current class to the root (common ancestor) 2) Closest 3) Never go downward Otherwise, each incorrect printout of an grow) or display) method will be deducted 5 points

Explanation / Answer

/*
* Creature.h
*
* Created on: 06-Apr-2018
*      Author: tan
*/

#ifndef CREATURE_H_
#define CREATURE_H_

class Creature {
   double rate;
public:
   Creature();
   void setRate(double);
   double getRate();
   void grow();
   virtual void display();
};

#endif /* CREATURE_H_ */

/*
* Creature.cpp
*
* Created on: 06-Apr-2018
*      Author: tan
*/

#include "Creature.h"
#include <iostream>
using namespace std;
Creature::Creature() {
   // TODO Auto-generated constructor stub
   std::cout<<"Class Creature"<<std::endl;
   rate=10.0;
}
void Creature::grow(){
   cout<<"Annual growth rate in unit of inch: "<<rate<<endl;
   cout<<"Host class name: Creature"<<endl;
}
void Creature::display(){
   cout<<"Host class: Creature"<<endl;
   cout<<"Growth rate"<<rate<<endl;
}
void Creature::setRate(double r){
   rate=r;
}
double Creature::getRate(){
   return rate;
}

/*
* Plant.h
*
* Created on: 06-Apr-2018
*      Author: tan
*/
#include "Creature.h"
#ifndef PLANT_H_
#define PLANT_H_

class Plant:public Creature {
   double rate;
public:
   Plant();
   void setRate(double);
   double getRate();
   void display();
};

#endif /* PLANT_H_ */

/*
* Plant.cpp
*
* Created on: 06-Apr-2018
*      Author: tan
*/

#include "Plant.h"
#include "Creature.h"
#include <iostream>
using namespace std;
Plant::Plant() {
   // TODO Auto-generated constructor stub
   std::cout<<"Class Plant"<<std::endl;
   rate=10.0;
}

void Plant::display(){
   cout<<"Host class: Plant"<<endl;
   cout<<"Growth rate"<<rate<<endl;
   Creature::display();
}
void Plant::setRate(double r){
   rate=r;
}
double Plant::getRate(){
   return rate;
}

/*
* Plant.h
*
* Created on: 06-Apr-2018
*      Author: tan
*/
#include "Creature.h"
#ifndef ANIMAL_H
#define ANIMAL_H

class Animal:public Creature {
   double rate;
public:
   Animal();
   void setRate(double);
   double getRate();
   void grow();
};

#endif /* PLANT_H_ */

/*
* Plant.cpp
*
* Created on: 06-Apr-2018
*      Author: tan
*/

#include "Animal.h"
#include "Creature.h"
#include <iostream>
using namespace std;
Animal::Animal() {
   // TODO Auto-generated constructor stub
   std::cout<<"Class Animal"<<std::endl;
   rate=10.0;
}


void Animal::setRate(double r){
   rate=r;
}
double Animal::getRate(){
   return rate;
}
void Animal::grow(){
   cout<<"Anual growth rate in unit of inch: "<<rate<<endl;
   cout<<"Host class name"<<endl;
}

/*
* Tree.h
*
* Created on: 06-Apr-2018
*      Author: tan
*/
#include "Plant.h"
#ifndef TREE_H_
#define TREE_H_

class Tree:public Plant {
   double rate;
public:
   Tree();
   void setRate(double);
   double getRate();
   void grow();
};

#endif /* TREE */

/*
* Tree.cpp
*
* Created on: 06-Apr-2018
*      Author: tan
*/

#include "Plant.h"
#include "Tree.h"
#include <iostream>
using namespace std;
Tree::Tree() {
   // TODO Auto-generated constructor stub
   std::cout<<"Class Tree"<<std::endl;
   rate=10.0;
}

void Tree::grow(){
   cout<<"Host class: Tree"<<endl;
   cout<<"Annual Growth rate"<<rate<<endl;
   Plant::display();
}
void Tree::setRate(double r){
   rate=r;
}
double Tree::getRate(){
   return rate;
}

/*
* Grass.h
*
* Created on: 06-Apr-2018
*      Author: tan
*/
#include "Plant.h"
#ifndef GRASS_H
#define GRASS_H

class Grass:public Plant {
   double rate;
public:
   Grass();
   void setRate(double);
   double getRate();
   void grow();
};

#endif /* GRASS */

/*
* Grass.cpp
*
* Created on: 06-Apr-2018
*      Author: tan
*/

#include "Plant.h"
#include "Grass.h"
#include <iostream>
using namespace std;
Grass::Grass() {
   // TODO Auto-generated constructor stub
   std::cout<<"Class Grass"<<std::endl;
   rate=10.0;
}

void Grass::grow(){
   cout<<"Host class: Grass"<<endl;
   cout<<"Annual Growth rate"<<rate<<endl;
   Plant::display();
}
void Grass::setRate(double r){
   rate=r;
}
double Grass::getRate(){
   return rate;
}

/*
* Insect.h
*
* Created on: 06-Apr-2018
*      Author: tan
*/
#include "Animal.h"
#ifndef INSECT_H
#define INSECT_H

class Insect:public Animal {
   double rate;
public:
   Insect();
   void setRate(double);
   double getRate();
   void grow();
   void display();
};

#endif /* GRASS */

/*
* Insect.cpp
*
* Created on: 06-Apr-2018
*      Author: tan
*/

#include "Animal.h"
#include "Insect.h"
#include <iostream>
using namespace std;
Insect::Insect() {
   // TODO Auto-generated constructor stub
   std::cout<<"Class Insect"<<std::endl;
   rate=10.0;
}

void Insect::grow(){
   cout<<"Host class: Insect"<<endl;
   cout<<"Annual Growth rate"<<rate<<endl;
}
void Insect::display(){
   cout<<"Host class: Creature"<<endl;
   cout<<"Growth rate"<<rate<<endl;
   Animal::display();
}
void Insect::setRate(double r){
   rate=r;
}
double Insect::getRate(){
   return rate;
}

/*
* Pine.h
*
* Created on: 06-Apr-2018
*      Author: tan
*/
#include "Tree.h"
#ifndef PINE_H
#define PINE_H

class Pine:public Tree {
   double rate;
public:
   Pine();
   void setRate(double);
   double getRate();
   void grow();
   void display();
};

#endif /* TREE */

/*
* Pine.cpp
*
* Created on: 06-Apr-2018
*      Author: tan
*/

#include "Pine.h"
#include "Tree.h"
#include <iostream>
using namespace std;
Pine::Pine() {
   // TODO Auto-generated constructor stub
   std::cout<<"Class Pine"<<std::endl;
   rate=10.0;
}

void Pine::grow(){
   cout<<"Host class: Pine"<<endl;
   cout<<"Annual Growth rate"<<rate<<endl;
}
void Pine::display(){
   cout<<"Host class: Pine"<<endl;
   Tree::display();
}
void Pine::setRate(double r){
   rate=r;
}
double Pine::getRate(){
   return rate;
}

/*
* Parsley.h
*
* Created on: 06-Apr-2018
*      Author: tan
*/
#include "Grass.h"
#ifndef PARSLEY_H
#define PARSLEY_H

class Parsley:public Grass {
   double rate;
public:
   Parsley();
   void setRate(double);
   double getRate();

};

#endif /* TREE */

/*
* Parsley.cpp
*
* Created on: 06-Apr-2018
*      Author: tan
*/

#include "Parsley.h"
#include "Grass.h"
#include <iostream>
using namespace std;
Parsley::Parsley() {
   // TODO Auto-generated constructor stub
   std::cout<<"Class Parsley"<<std::endl;
   rate=10.0;
}


void Parsley::setRate(double r){
   rate=r;
}
double Parsley::getRate(){
   return rate;
}

/*
* Fly.h
*
* Created on: 06-Apr-2018
*      Author: tan
*/
#include "Insect.h"
#ifndef FLY_H
#define FLY)H

class Fly:public Insect {
   double rate;
public:
   Fly();
   void setRate(double);
   double getRate();
   void display();
};

#endif /* GRASS */

/*
* Fly.cpp
*
* Created on: 06-Apr-2018
*      Author: tan
*/

#include "Fly.h"
#include "Insect.h"
#include <iostream>
using namespace std;
Fly::Fly() {
   // TODO Auto-generated constructor stub
   std::cout<<"Class Fly"<<std::endl;
   rate=10.0;
}

void Fly::display(){
   cout<<"Host class: Creature"<<endl;
   cout<<"Growth rate"<<rate<<endl;
   Insect::display();
}
void Fly::setRate(double r){
   rate=r;
}
double Fly::getRate(){
   return rate;
}

//============================================================================
// Name        : TestCreature.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include "Creature.h"
#include "Plant.h"
#include "Animal.h"
#include "Tree.h"
#include "Grass.h"
#include "Insect.h"
#include "Pine.h"
#include "Parsley.h"
#include "Fly.h"
using namespace std;

int main() {
   cout << "Testing classess" << endl; // prints !!!Hello World!!!
   Creature crt;
   crt.setRate(12);
   crt.grow();
   crt.display();
   Plant plnt;
   plnt.setRate(15);
   plnt.display();
   Animal anml;
   anml.setRate(13);
   anml.grow();
   Tree tree;
   tree.setRate(10);
   tree.grow();
   Grass grass;
   grass.setRate(14);
   grass.grow();
   Insect inst;
   inst.setRate(34);
   inst.grow();
   inst.display();
   Pine pine;
   pine.setRate(20);
   pine.grow();
   pine.display();
   Parsley prsl;
   prsl.setRate(20);
   cout<<"Growth rate "<<prsl.getRate()<<endl;
   Fly fly;
   fly.setRate(23);
   fly.display();
   return 0;
}


/**********************/output

Testing classess
Class Creature
Annual growth rate in unit of inch: 12
Host class name: Creature
Host class: Creature
Growth rate12
Class Creature
Class Plant
Host class: Plant
Growth rate15
Host class: Creature
Growth rate10
Class Creature
Class Animal
Anual growth rate in unit of inch: 13
Host class name
Class Creature
Class Plant
Class Tree
Host class: Tree
Annual Growth rate10
Host class: Plant
Growth rate10
Host class: Creature
Growth rate10
Class Creature
Class Plant
Class Grass
Host class: Grass
Annual Growth rate14
Host class: Plant
Growth rate10
Host class: Creature
Growth rate10
Class Creature
Class Animal
Class Insect
Host class: Insect
Annual Growth rate34
Host class: Creature
Growth rate34
Host class: Creature
Growth rate10
Class Creature
Class Plant
Class Tree
Class Pine
Host class: Pine
Annual Growth rate20
Host class: Pine
Host class: Plant
Growth rate10
Host class: Creature
Growth rate10
Class Creature
Class Plant
Class Grass
Class Parsley
Growth rate 20
Class Creature
Class Animal
Class Insect
Class Fly
Host class: Creature
Growth rate23
Host class: Creature
Growth rate10
Host class: Creature
Growth rate10