I am working on a code focusing on Inheritance in C++. I am getting some errors regarding the lines that read: void bat :: double travel_time (double distance, terrain_type t)
(lines 56, 68, 113, 125 in animal.h file)
The others that begin with void penguin, mammal, and bird have the same error. The errors read:
--expected unqualified-id before "double"
--expected init declarator before "double"
--expected ',' or ';' before "double"
These seem like simple errors...but it's driving me nuts, I can't seem to find what is wrong. I'm hoping that maybe some fresh eyes could find it? It's also possible I could have something completely wrong in there all together...
Below I have information on what needs to be accomplished in the program. The cpp file and the .h file. Hope this is enough info.
animal class is the base. animal is inherited by mammal and bird. mammal is inherited by cow and bat. bird is inherited by hawk and penguin.
Derived classes from mammal uses travel_time function unless it is overrode (by bat). Derived classes from bird will use the travel_time function unless overrode (by penquin).
There is a chart where the numbers come from in the calculations in the void functions. The math isn't my problem. I think it's the declarators themselves. The problem is in the animal.h file. I just pasted the cpp file for reference to where things will go once program is ran.
I appreciate any input or advice.
cpp file
#include <cstdlib>
#include <iostream>
using namespace std;
#include "animal.h"
char menu ();
int main(int argc, char *argv[])
{
animal* the_animal; // pointer to animal of a type selected by the user
double distance;
char animal;
char command;
double hours;
terrain_type terrain;
int terrain_number;
// pick a default animal
the_animal = new cow ();
command = menu ();
// continue executing until user enters a q to quit
while (command != 'q')
{
// select appropriate directory operation based on command
switch (command)
{
case 'a' : cout << "\nChoose an animal: ";
cout << "\nh : hawk";
cout << "\np : penguin";
cout << "\nc : cow";
cout << "\nb : bat";
cout << "\n\nChoice: ";
cin >> animal;
// create the selected animal. Return old animal object
// to free memory
switch (animal)
{
case 'h' : delete the_animal;
the_animal = new hawk ();
break;
case 'p' : delete the_animal;
the_animal = new penguin ();
break;
case 'c' : delete the_animal;
the_animal = new cow ();
break;
case 'b' : delete the_animal;
the_animal = new bat ();
break;
default: cout << "\nIllegal choice, no animal set!";
}
break;
case 'p' : cout << "\nCurrent animal is:";
// print animal with public method
the_animal -> print ();
break;
case 't' : cout << "\nEnter the distance: ";
cin >> distance;
cout << "Enter the terrain (0:PLAIN, 1:HILL, 2:MOUNTAIN): ";
cin >> terrain_number;
// change from integer input to terrain type
terrain = (terrain_type) terrain_number;
// compute travel time with public method
hours = the_animal -> travel_time (distance, terrain);
cout << "\nThe trip would take " << hours << " hours!";
break;
default: cout << "\nlegal command entered!";
} // switch
command = menu ();
} // while
system("PAUSE");
return EXIT_SUCCESS;
}
// prints the menu and returns user entered character command
char menu ()
{
char ch;
cout << "\n\n Animal Menu";
cout << "\n\na: choose an animal";
cout << "\np: print current animal";
cout << "\nt: execute a trip";
cout << "\nq: quit from the program";
cout << "\n\nEnter command: ";
cin >> ch;
return ch;
}
animal.h
enum terrain_type {PLAIN, HILL, MOUNTAIN};
// animal class
class animal
{
protected:
char* kind; // name of animal
double airspeed; // speed for animal flying
double landspeed; // speed for animal walking
public:
void print (); // prints animal data
// virtual function to compute travel time given a distance and
// terrain type. Not defined in animal but in derived classes.
virtual double travel_time (double distance, terrain_type t) = 0;
};
// prints an animal's data (inherited by derived classes)
void animal::print ()
{
cout << "\n\nKind: " << kind;
cout << "\nAir Speed: " << airspeed;
cout << "\nLand Speed: " << landspeed;
}
class mammal : public animal
{
public:
double travel_time (double distance, terrain_type t);
};
class cow : public mammal
{
public:
cow();
};
//constructor sets a default animal type, cow
cow :: cow ()
{
kind = "Cow";
airspeed = 0;
landspeed = 3;
}
class bat : public mammal
{
public:
bat();
double travel_time (double distance, terrain_type t);
};
bat :: bat ()
{
kind = "Bat";
airspeed = 15;
landspeed = 1;
}
void bat :: double travel_time (double distance, terrain_type t)
{
switch (t)
{
case PLAIN: travel_time = (distance/airspeed)
break;
case HILL: travel_time = (distance/airspeed)
break;
case MOUNTAIN: travel_time = (distance/airspeed)
}
}
void mammal :: double travel_time (double distance, terrain_type t)
{
switch (t)
{
case PLAIN: travel_time = ((distance/landspeed)*1)
break;
case HILL: travel_time = ((distance/landspeed)*2)
break;
case MOUNTAIN: travel_time = ((distance/landspeed)*3)
}
}
class bird : public animal
{
public:
double travel_time (double distance, terrain_type t);
};
class hawk : public bird
{
public:
hawk();
};
hawk :: hawk ()
{
kind = "Hawk";
airspeed = 30;
landspeed = 4;
}
class penguin : public bird
{
public:
penguin();
double travel_time (double distance, terrain_type t);
};
penguin :: penguin ()
{
kind = "Penguin";
airspeed = 0;
landspeed = 2;
}
void penguin :: double travel_time (double distance, terrain_type t)
{
switch (t)
{
case PLAIN: travel_time = ((distance/landspeed)*1)
break;
case HILL: travel_time = ((distance/landspeed)*2)
break;
case MOUNTAIN: travel_time = ((distance/landspeed)*3)
}
}
void bird :: double travel_time (double distance, terrain_type t)
{
switch (t)
{
case PLAIN: travel_time = ((distance/landspeed)*1)
break;
case HILL: travel_time = ((distance/landspeed)*2)
break;
case MOUNTAIN: travel_time = ((distance/landspeed)*3)
}
}