Dinosaurs have several basic characteristics: a species name, height and weight,
ID: 3540565 • Letter: D
Question
Dinosaurs have several basic characteristics: a species name, height and weight, movement speed, and diet. They also have several basic behaviors: eating, moving, and sleeping.
Your assignment is to define and implement a generic Dinosaur class that defines these characteristics and behaviors. The behaviors can be very basic, just printing out a line saying something like "sleeps." Declare all member fields as private, and make sure that you have accessor functions for all of the member fields.
Use the Dinosaur class as a base class and make classes to represent the following:
For each of these classes, come up with at least two additional pieces of information about that specific dinosaur type (eg, for a Tyrannosaurus, you might have the number of teeth the particular Tyrannosaur has). Also, customize at least one of the sleeping/eating/moving behaviors for each class as appropriate. You can make up any details you want if you don't want to bother looking up information about dinosaurs.
Once you have these classes, write a small program that:
Explanation / Answer
#include <string>
#include <iostream>
using namespace std;
class Dinosaur
{
private:
//characteristics
string name;
int height;
double weight;
string diet;
//behaviors
//string eat;
// string move;
//string sleep;
public:
Dinosaur(){};
Dinosaur(string n, int h, double w, string d);
void eat();
void move();
void sleep();
void display();
};
Dinosaur::Dinosaur(string n, int h, double w, string d)
{
name = n;
height = h;
weight = w;
diet = d;
}
void Dinosaur::display()
{
cout << name << endl;
cout << height << " inches" << endl;
cout << weight << " pounds" << endl;
cout << diet << endl;
}
void Dinosaur::eat()
{
cout << "eats food quietly" << endl;
}
void Dinosaur::move()
{
cout << "walks at a brisk pace" << endl;
}
void Dinosaur::sleep()
{
cout << "sleeps soundly" << endl << endl;
}
class Apatosaurus : public Dinosaur
{
private:
string favfood;
string fact;
public:
Apatosaurus(string n, int h, double w, string d);
void move();
void info();
};
Apatosaurus::Apatosaurus(string n, int h, double w, string d)
: Dinosaur(n, h, w, d)
{
favfood = "kelp";
fact = "Wonderful breath";
}
void Apatosaurus::move()
{
cout << "walks to the pond and knocks a few trees over" << endl;
}
void Apatosaurus::info()
{
cout << "Favorite food: " << favfood << endl;
cout << "Fact: " << fact << endl;
}
class TRex : public Dinosaur
{
private:
int teeth;
int speed;
public:
TRex(string n, int h, double w, string d);
void eat();
void info();
};
TRex::TRex(string n, int h, double w, string d)
: Dinosaur(n, h, w, d)
{
teeth = 68;
speed = 45;
}
void TRex::eat()
{
cout << "voraciously eats an animal then rips off its head" << endl;
}
void TRex::info()
{
cout << "Teeth: " << teeth << endl;
cout << "Top speed: "<< speed << " MPH" << endl;
}
class Hadrosaur : public Dinosaur
{
private:
string fav_food;
int speed;
public:
Hadrosaur(string n, int h, double w, string d);
void sleep();
void info();
void show_favfood();
void show_speed();
};
Hadrosaur::Hadrosaur(string n, int h, double w, string d)
: Dinosaur(n, h, w, d)
{
fav_food = "branches";
speed = 88;
}
void Hadrosaur::sleep()
{
cout << "Snores heavily" << endl;
}
void Hadrosaur::info()
{
cout << "Loves to eat " << fav_food << endl;
cout << "Top speed: " << speed << endl;
}
//SOURCE START
#include <string>
#include <iostream>
#include "Dinosaur.h"
using namespace std;
int main ()
{
Dinosaur dino;
Apatosaurus A ("Apatosaurus", 25, 18003.45, "herbivore");
TRex T("Tyrannosaurus Rex", 30, 4600, "Carnivore");
Hadrosaur H ("Hadrosaur", 12, 2304, "Herbivore");
A.display();
A.info();
A.eat();
A.move();
A.sleep();
T.display();
T.info();
T.eat();
T.move();
T.sleep();
H.display();
H.info();
H.eat();
H.move();
H.sleep();
system("Pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.