This homework is all about inheritance. In this assignment, you will be given a
ID: 3671078 • Letter: T
Question
This homework is all about inheritance. In this assignment, you will be given a class specification and implementation for a creature called a ProtoFish -- a simple fish-like creature -- and you will derive from that a more fleshed-out creature called a Fish. Then you'll use that as a basis for deriving a Shark. Finally, you'll test all your creatures to make sure you have the functionality right in your aquatic bestiary.
The starter code for this project is found in the class Google Drive folder SharksFish at https://drive.google.com/open?id=0B32rbCXq2_DpcTQwT2pwOVBzY0k (Links to an external site.). In that directory you will find a main.cpp test driver, a couple of basic helper files dir.h and dir.cpp, plus a ProtoFish specification and implementation called protofish.h and protofish.cpp. You can compile all these .cpp files together without modification to run a quick series of ProtoFish tests.
ProtoFish have an age, have a dir in which they facing (up, dn, lt, or rt in a 2D array), and are either dead or not dead. ProtoFish are constructed with an age already assigned to them, have accessor functions for their dir, age, and deadness, and have a toChar() function to tell the client what ascii character should represent them on the console (^, >, V, or <, depending on the dir). Additionally, ProtoFish have a void function point() that is used to allow the ProtoFish to change its dir to a random direction, and a timeUpdate() function by which the ProtoFish ages itself. ProtoFish live to the ripe old age of 100, at which time they invariable become dead.
Your first task is to create a Fish class that inherits from ProtoFish. Fish should have a constructor of the form
Fish(int gestationCountdown, int gestationPeriod, int startingAge);
Where the gestationCountdown indicates the time left before a baby is ready, the gestationPeriod is the reset value for gestationCount after having a baby, and the startingAge is passed to the ProtoFish constructor. Additionally, the Fish should have a function
bool haveBaby();
that simply returns true and resets the gestation counter if a baby is ready, and returns false otherwise. haveBaby() does nothing else. Fish should override the timeUpdate function in ProtoFish with
void timeUpdate()
that updates the fish parts and calls the ProtoFish timeUpdate() function. And it needs a function
void point(char sonarMap[5][5])
that sets the direction the fish is heading in. When point is called, it is given a 5x5 array of chars called a sonarMap that depicts the world as the fish sees it, with the fish at the center. A '.' in the sonarMap indicates an open space, while other chars in the sonarMap represent other creatures. Fish like to point toward empty spaces, and prefer spaces that are next to other fish, even diagonal to the intended open space. Other member variables and functions can be added to the protected area of Fish.
Finally, you should derive a Shark class from your Fish class. Shark constructors are of the form
Shark(int starvationCountdown, int starvationPeriod, int gestationCountdown, int gestationPeriod, int startingAge);
Where starvationCountdown indicates the time left before the Shark starves to death, starvationPeriod is the reset value for starvationCountdown after eating a Fishy meal, and the other arguments are as before, passed on to the Fish constructor.
Sharks implement the additional function
void eat();
That simply resets the starvationCountdown, overrides
char toChar();
to return u, d, l, or r as its character on the console. overrides
void timeUpdate();
to decrement the starvationCountdown and decides if the Shark has died, and finally overrides
void point(char sonarMap[5][5]);
to update the dir. Sharks like to point toward the direction of the nearest fish on the sonarMap.
You can test your sharks and fish by uncommenting portions of main.cpp which have thoughtfully been written for you.
Later in the semester, we'll put some sharks and fish in a virtual ocean to see what happens.
What to turn in:
Put your Fish specification in a file called fish.h, your Fish implementation in a file called fish.cpp, your Shark specification in a file called shark.h, and your Shark implementation in a file called shark.cpp. Modify main.cpp to run all the tests. Make sure it all compiles and runs together, then zip all nine files together (main.cpp, dir.h, dir.cpp, protofish.h, protofish.cpp, fish.h, fish.cpp, shark.h, and shark.cpp) into an archive and turn that in.
Code for protofish.h
#ifndef PROTOFISH_H
#define PROTOFISH_H
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include "dir.h"
using namespace std;
//a basic fish that changes direction and dies at age 100
class ProtoFish{
public:
//constructor
ProtoFish(int startingAge);
//direction accessor
Dir getDir(){return dir;}
//age accessor
int getAge(){return age;}
//deadness accessor
bool isDead(){return dead;}
//Another day passes.
void timeUpdate();
//return a character to represent me
char toChar();
//change direction in a random way
void point();
protected:
Dir dir; //where i am heading
int age; //how long i have lived
bool dead; //sadness
};
#endif
Code for protofish.cpp
#include "dir.h"
#include "protofish.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std;
//constructor
ProtoFish::ProtoFish(int startingAge):
age(startingAge){
dir=randDir();
dead=false;
}
//Another day passes.
void ProtoFish::timeUpdate(){
if(age<100){
age++;
}else{
dead=true;
}
}
//return a character to represent me
char ProtoFish::toChar(){
if(dead)return 'X';
if(dir==up)return '^';
if(dir==rt)return '>';
if(dir==dn)return 'v';
if(dir==lt)return '<';
exit(1);
}
//i will change direction in a random way
void ProtoFish::point(){
//just pick a random direction
dir=randDir();
}
Code for main.cpp
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include "dir.h"
#include "protofish.h"
/* TODO: uncomment to test fish and sharks
#include "fish.h"
#include "shark.h"
*/
using namespace std;
void printMap(ProtoFish f, char smap[5][5]){
smap[2][2]=f.toChar();
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
cout<<smap[i][j];
}
cout<<endl;
}
}
/* TODO: uncomment for the shark tests
void printMap(Shark s, char smap[5][5]){
smap[2][2]=s.toChar();
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
cout<<smap[i][j];
}
cout<<endl;
}
}
*/
int main(){
cout<<" ProtoFish Tests ";
ProtoFish nemo(96);
char line[1024];
char sonarMap1[5][5]={
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
};
for(int i=0;i<5;++i){
nemo.point();
nemo.timeUpdate();
printMap(nemo,sonarMap1);
if(i==4) cout<<"expected output: X ";
cout<<"====="<<endl;
}
/* TODO: uncomment to include fish tests
cout<<" Fish Tests ";
Fish guppy(2,5,90);
char sonarMap2[5][5]={
'.','.','.','.','.',
'.','.','.','.','.',
'^','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
};
guppy.point(sonarMap2);
guppy.timeUpdate();
printMap(guppy,sonarMap2);
if(guppy.haveBaby())cout<<"baby time. ";
cout<<"expected output: < ";
cout<<"====="<<endl;
char sonarMap3[5][5]={
'.','<','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
};
guppy.point(sonarMap3);
guppy.timeUpdate();
printMap(guppy,sonarMap3);
if(guppy.haveBaby())cout<<"baby time. ";
cout<<"expected output: ^ and baby time ";
cout<<"====="<<endl;
char sonarMap4[5][5]={
'.','<','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','v',
'.','.','.','.','.',
};
guppy.point(sonarMap4);
guppy.timeUpdate();
printMap(guppy,sonarMap4);
if(guppy.haveBaby())cout<<"baby time. ";
cout<<"expected output: ^ or > ";
cout<<"====="<<endl;
*/
/* TODO: uncomment to include shark tests
cout<<" Shark Tests ";
Shark mako(3,6,2,5,90);
char sonarMap5[5][5]={
'.','.','.','.','.',
'.','.','.','.','.',
'^','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
};
mako.point(sonarMap5);
mako.timeUpdate();
printMap(mako,sonarMap5);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: l ";
cout<<"====="<<endl;
char sonarMap6[5][5]={
'.','.','>','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
};
mako.point(sonarMap6);
mako.timeUpdate();
printMap(mako,sonarMap6);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: u and baby time ";
cout<<"====="<<endl;
char sonarMap7[5][5]={
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','^',
'.','.','.','.','.',
'.','.','.','.','.',
};
mako.eat();
mako.point(sonarMap7);
mako.timeUpdate();
printMap(mako,sonarMap7);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: r ";
cout<<"====="<<endl;
char sonarMap8[5][5]={
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','^','.','.',
};
mako.point(sonarMap8);
mako.timeUpdate();
printMap(mako,sonarMap8);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: d ";
cout<<"====="<<endl;
char sonarMap9[5][5]={
'>','.','.','.','.',
'.','.','>','.','.',
'.','.','.','.','.',
'.','>','.','.','.',
'.','.','^','>','.',
};
mako.point(sonarMap9);
mako.timeUpdate();
printMap(mako,sonarMap9);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: u ";
cout<<"====="<<endl;
mako.point(sonarMap9);
mako.timeUpdate();
printMap(mako,sonarMap9);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: u ";
cout<<"====="<<endl;
mako.point(sonarMap9);
mako.timeUpdate();
printMap(mako,sonarMap9);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: u and baby time ";
cout<<"====="<<endl;
mako.point(sonarMap9);
mako.timeUpdate();
printMap(mako,sonarMap9);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: X and dead ";
cout<<"====="<<endl;
*/
return 0;
}
Code for dir.h
#ifndef DIR_H
#define DIR_H
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std;
//there are four directions
enum Dir{up,rt,dn,lt};
//return a random direction
Dir randDir();
//given a direction, return the next direction in a clockwise fashion
Dir nextDir(Dir currentDir);
#endif
Code for dir.cpp
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include "dir.h"
using namespace std;
//return a random direction
Dir randDir(){
int i;
i=rand()%4;
switch(i){
case 0:return up;
case 1:return rt;
case 2:return dn;
case 3:return lt;
default: exit(1);
}
}
//given a direction, return the next direction in a clockwise fashion
Dir nextDir(Dir currentDir){
switch(currentDir){
case up:return rt;
case rt:return dn;
case dn:return lt;
case lt:return up;
}
}
Explanation / Answer
main.cpp
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include "dir.h"
#include "protofish.h"
/* TODO: uncomment to test fish and sharks
#include "fish.h"
#include "shark.h"
*/
#include "fish.h"
using namespace std;
void printMap(ProtoFish f, char smap[5][5]){
smap[2][2]=f.toChar();
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
cout<<smap[i][j];
}
cout<<endl;
}
}
/* TODO: uncomment for the shark tests
void printMap(Shark s, char smap[5][5]){
smap[2][2]=s.toChar();
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
cout<<smap[i][j];
}
cout<<endl;
}
}
*/
int main(){
cout<<" ProtoFish Tests ";
ProtoFish nemo(96);
char line[1024];
char sonarMap1[5][5]={
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
};
for(int i=0;i<5;++i){
nemo.point();
nemo.timeUpdate();
printMap(nemo,sonarMap1);
if(i==4) cout<<"expected output: X ";
cout<<"====="<<endl;
}
/* TODO: uncomment to include fish tests
cout<<" Fish Tests ";
Fish guppy(2,5,90);
char sonarMap2[5][5]={
'.','.','.','.','.',
'.','.','.','.','.',
'^','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
};
guppy.point(sonarMap2);
guppy.timeUpdate();
printMap(guppy,sonarMap2);
if(guppy.haveBaby())cout<<"baby time. ";
cout<<"expected output: < ";
cout<<"====="<<endl;
char sonarMap3[5][5]={
'.','<','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
};
guppy.point(sonarMap3);
guppy.timeUpdate();
printMap(guppy,sonarMap3);
if(guppy.haveBaby())cout<<"baby time. ";
cout<<"expected output: ^ and baby time ";
cout<<"====="<<endl;
char sonarMap4[5][5]={
'.','<','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','v',
'.','.','.','.','.',
};
guppy.point(sonarMap4);
guppy.timeUpdate();
printMap(guppy,sonarMap4);
if(guppy.haveBaby())cout<<"baby time. ";
cout<<"expected output: ^ or > ";
cout<<"====="<<endl;
*/
/* TODO: uncomment to include shark tests
cout<<" Shark Tests ";
Shark mako(3,6,2,5,90);
char sonarMap5[5][5]={
'.','.','.','.','.',
'.','.','.','.','.',
'^','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
};
mako.point(sonarMap5);
mako.timeUpdate();
printMap(mako,sonarMap5);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: l ";
cout<<"====="<<endl;
char sonarMap6[5][5]={
'.','.','>','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
};
mako.point(sonarMap6);
mako.timeUpdate();
printMap(mako,sonarMap6);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: u ";
cout<<"====="<<endl;
char sonarMap7[5][5]={
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','^',
'.','.','.','.','.',
'.','.','.','.','.',
};
mako.eat();
mako.point(sonarMap7);
mako.timeUpdate();
printMap(mako,sonarMap7);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: r and baby time ";
cout<<"====="<<endl;
char sonarMap8[5][5]={
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','.','.','.',
'.','.','^','.','.',
};
mako.point(sonarMap8);
mako.timeUpdate();
printMap(mako,sonarMap8);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: d ";
cout<<"====="<<endl;
char sonarMap9[5][5]={
'>','.','.','.','.',
'.','.','>','.','.',
'.','.','.','.','.',
'.','>','.','.','.',
'.','.','^','>','.',
};
mako.point(sonarMap9);
mako.timeUpdate();
printMap(mako,sonarMap9);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: u ";
cout<<"====="<<endl;
mako.point(sonarMap9);
mako.timeUpdate();
printMap(mako,sonarMap9);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: u ";
cout<<"====="<<endl;
mako.point(sonarMap9);
mako.timeUpdate();
printMap(mako,sonarMap9);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: u and baby time ";
cout<<"====="<<endl;
mako.point(sonarMap9);
mako.timeUpdate();
printMap(mako,sonarMap9);
if(mako.haveBaby())cout<<"baby time. ";
if(mako.isDead())cout<<"dead. ";
cout<<"expected output: X and dead ";
cout<<"====="<<endl;
*/
return 0;
}
dir.cpp
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include "dir.h"
using namespace std;
//return a random direction
Dir randDir(){
int i;
i=rand()%4;
switch(i){
case 0:return up;
case 1:return rt;
case 2:return dn;
case 3:return lt;
default: exit(1);
}
}
//given a direction, return the next direction in a clockwise fashion
Dir nextDir(Dir currentDir){
switch(currentDir){
case up:return rt;
case rt:return dn;
case dn:return lt;
case lt:return up;
}
}
dir.h
#ifndef DIR_H
#define DIR_H
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std;
//there are four directions
enum Dir{up,rt,dn,lt};
//return a random direction
Dir randDir();
//given a direction, return the next direction in a clockwise fashion
Dir nextDir(Dir currentDir);
#endif
fish.cpp
Fish::Fish(int gestationCountdown, int gestationPeriod, int startingAge) {
}
fish.h
#ifndef SHARKSFISH_FISH_H
#define SHARKSFISH_FISH_H
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include "dir.h"
class Fish{
public:
//fish constructor
Fish(int gestationCountdown, int gestationPeriod, int startingAge);
private:
};
#endif //SHARKSFISH_FISH_H
protofish.cpp
#include "dir.h"
#include "protofish.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std;
//constructor
ProtoFish::ProtoFish(int startingAge):
age(startingAge){
dir=randDir();
dead=false;
}
//Another day passes.
void ProtoFish::timeUpdate(){
if(age<100){
age++;
}else{
dead=true;
}
}
//return a character to represent me
char ProtoFish::toChar(){
if(dead)return 'X';
if(dir==up)return '^';
if(dir==rt)return '>';
if(dir==dn)return 'v';
if(dir==lt)return '<';
exit(1);
}
//i will change direction in a random way
void ProtoFish::point(){
//just pick a random direction
dir=randDir();
}
protofish.h
#include "dir.h"
#include "protofish.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std;
//constructor
ProtoFish::ProtoFish(int startingAge):
age(startingAge){
dir=randDir();
dead=false;
}
//Another day passes.
void ProtoFish::timeUpdate(){
if(age<100){
age++;
}else{
dead=true;
}
}
//return a character to represent me
char ProtoFish::toChar(){
if(dead)return 'X';
if(dir==up)return '^';
if(dir==rt)return '>';
if(dir==dn)return 'v';
if(dir==lt)return '<';
exit(1);
}
//i will change direction in a random way
void ProtoFish::point(){
//just pick a random direction
dir=randDir();
}
shark.cpp
Shark::Shark(int StarvationCountdown, int starvationPeriod, int gestationPeriod, int startingAge) {
}
shark.h
#ifndef SHARKSFISH_SHARK_H
#define SHARKSFISH_SHARK_H
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include "dir.h"
class Shark{
public:
Shark(int StarvationCountdown, int starvationPeriod, int gestationPeriod, int startingAge);
private:
};
#endif //SHARKSFISH_SHARK_H
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.