Abstract Base Class C Program Create an abstract base class
Abstract Base Class (C++ Program)
Create an abstract base class called Animal which has the following pure virtual member functions: talk( ), move( ) and the following attribute: animalType (which is a pointer to a dynamically allocated string.)
Then create the following derived classes (from the animal class): reptile, bird, and mammal.
Here are the concrete classes:
From the reptile class derive lizard and snake.
From bird derive eagle and chicken.
From mammal derive bear, hyena and lion.
Each concrete class needs to override the talk() and move() functions. Also each concrete class will have the operator<<( ) defined. The output of cout << smokey; (assuming smokey is a bear) will be the animalType , the sound he makes (talk( ) ) and how he moves ( move( )). Here is an example:
int main()
{
bear smokey; // using the concrete class
cout << smokey // prints out: bear, growl, walk
animal *aptr = new bear; // I could have used an animal pointer
cout << *aptr; // prints the same results: bear, growl, walk
return 0;
}
Create an array of animal pointers called zoo. Make it 10 in size. Here is what the menu should look like:
----Zoo Cages----
1 [empty]
2 [empty]
3 [empty]
4 [snake]
5 [eagle]
6 [bear]
7 [empty]
8 [empty]
9 [lion]
10 [hyena]
----Menu----
1) Add
2) Remove
3) Display all animals
4) Display animal count
5) Exit
If the user selects 1) then ask for a cage number and the animal he wishes to put in that cage. Be sure to check the cage for an existing animal first. The user cannot put an animal in a cage that is already occupied.
If the user selects 2) then ask for the cage number and remove the animal from that cage (use the destructor). If there is no animal in the cage, issue an error to the user.
If the user selects 3) then display the contents of each cage. The display function will show each cage number as well as the animalType of the animal, the sound it makes and how it moves, in other words, use the operator<<( ). All this information must be on a single screen. The output screen is up to you.
If the user selects 4) then display the count of the number of animals in your zoo. In the above example, you will have 5. You should use a static data member in the animal class to keep track of the number of animals. You should increase that count each time the animal constructor is called and decrease the count when the animal destructor is called.
***Use Virtual Functions and the animal base class MUST be abstract
Solution
main.cpp
#include <iostream> //input output
#include <string> //strings
#include <stdlib.h> //Randoms
#include <time.h> //Randoms
#include <sstream> //For input
#include \"lizard.h\"
#include \"snake.h\"
#include \"chicken.h\"
#include \"eagle.h\"
#include \"hyena.h\"
#include \"bear.h\"
#include \"lion.h\"
using namespace std;
//declare our static variable from the animal class
int animal::numAnimals;
int main()
{
//array of animal pointers
animal *zoo[10] =
{ NULL };
//initialize our static variable to zero
animal::numAnimals = 0;
//To test if cage is empty, it is null
//Begin the UI
bool menu = true;
while (menu)
{
//First print zoo cages
cout << \"---Zoo Cages---\ \";
for (int i = 0; i < 10; ++i)
{
//Set the string to animal type
string tempString;
if (zoo[i] != NULL)
{
tempString = zoo[i]->animalType;
}
else
{
tempString = \"empty\";
}
cout << (i + 1) << \" [\" << tempString << \"]\" << endl;
}
//Menu for options
cout << \"---MENU---\ \";
cout << \"1. Add\ \";
cout << \"2. Remove\ \";
cout << \"3. Display all animals\ \";
cout << \"4. Display animal count\ \";
cout << \"5. EXIT\ \";
cout << \"Please enter an option.\ \";
//Get the user option
string input;
//Getline easiest way to get string from console
getline(cin, input);
//String stream converts string to int
stringstream ss;
ss << input;
int option;
ss >> option;
if (option == 1)
{
//The user would like to add an animal to a cage
cout << \"Which cage would you like to add an animal to?\" << endl;
//Get the user option
string input;
//Getline easiest way to get string from console
getline(cin, input);
//String stream converts string to int
stringstream ss;
ss << input;
int cage;
ss >> cage;
//Check if the input is correct
if (!(cage >= 1 && cage <= 10))
{
cout
<< \"\ That is not a valid option, please enter a number 1 through 10.\ \"
<< \"Canceling add...\ \ \";
}
//Check if cage is filled, cage - 1 for index
else if (zoo[cage - 1] != NULL)
{
cout << \"\ There is already an animal in that cage...\ \"
<< \"Canceling add...\ \ \";
}
else
{
//Input is correct!
//Ask which animal
cout << \"Which animal would you like to add?\" << endl;
cout << \"1. lizard\" << endl;
cout << \"2. snake\" << endl;
cout << \"3. eagle\" << endl;
cout << \"4. chicken\" << endl;
cout << \"5. bear\" << endl;
cout << \"6. hyena\" << endl;
cout << \"7. lion\" << endl;
//Get the user option
string input;
//Getline easiest way to get string from console
getline(cin, input);
//String stream converts string to int
stringstream ss;
ss << input;
int animalType;
ss >> animalType;
//Check if the input is correct
if (!(animalType >= 1 && animalType <= 7))
{
cout
<< \"\ That is not a valid option, please enter a number 1 through 7.\ \"
<< \"Canceling add...\ \ \";
}
else
{
//add the animal to the cage
switch (animalType)
{
//Cage -1 for array index
case 1:
zoo[cage - 1] = new lizard;
break;
case 2:
zoo[cage - 1] = new snake;
break;
case 3:
zoo[cage - 1] = new eagle;
break;
case 4:
zoo[cage - 1] = new chicken;
break;
case 5:
zoo[cage - 1] = new bear;
break;
case 6:
zoo[cage - 1] = new hyena;
break;
case 7:
zoo[cage - 1] = new lion;
break;
}
}
}
}
else if (option == 2)
{
//The user would like to add an animal to a cage
cout << \"Which cage would you like to remove an animal?\" << endl;
//Get the user option
string input;
//Getline easiest way to get string from console
getline(cin, input);
//String stream converts string to int
stringstream ss;
ss << input;
int cage;
ss >> cage;
//Check if the input is correct
if (!(cage >= 1 && cage <= 10))
{
cout
<< \"\ That is not a valid option, please enter a number 1 through 10.\ \"
<< \"Canceling remove...\ \ \";
}
//Check if cage is filled, cage - 1 for index
else if (zoo[cage - 1] == NULL)
{
cout << \"\ There is not an animal in that cage...\ \"
<< \"Canceling remove...\ \ \";
}
else
{
//remove the animal with its constructor
delete zoo[cage - 1];
zoo[cage - 1] = NULL;
}
}
else if (option == 3)
{
//Display the contents of each cage
//go through a loop
for(int i = 0; i < 10; ++i)
{
if(zoo[i] != NULL)
{
cout << \"Cage \" << (i + 1) << \" outputs: \" << *zoo[i] << endl;
}
else cout << \"Cage \" << (i + 1) << \" is empty!\" << endl;
}
//end the line for formatting
cout << endl;
}
else if (option == 4)
{
//Display the number of animals in the zoo (Animals created)
cout << animal::numAnimals << \" animals in the zoo\" << endl << endl;
}
else if (option == 5)
{
//Exit the program
menu = false;
cout << \"\ Thank you, have a nice day!\ \";
}
else
{
//They did not enter a valid option
cout
<< \"\ That is not a valid option, please enter a number 1 through 5.\ \ \";
}
}
return 0;
}
animal.cpp
#include \"animal.h\"
using namespace std;
animal::animal()
{
numAnimals = numAnimals + 1;
}
//Overload our cout operator, to support printing our animal
ostream &operator<<(ostream &output, animal &lhs)
{
output << lhs.animalType << \", \" << lhs.talk() << \", \" << lhs.move();
return output;
}
animal::~animal()
{
--numAnimals;
}
animal.h
#ifndef ANIMAL_H_
#define ANIMAL_H_
#include <string>
#include <iostream>
using namespace std;
class animal
{
public:
animal();
virtual string talk()= 0;
virtual string move()= 0;
//Support << operator, outputs the animal type, talk, and move
friend ostream &operator<<(ostream &output, animal &lhs);
char *animalType;
static int numAnimals;
virtual ~animal() = 0;
};
#endif /* ANIMAL_H_ */
bear.cpp
#include \"bear.h\"
using namespace std;
bear::bear()
{
animalType = \"bear\";
}
string bear::talk()
{
return \"growl\";
}
string bear::move()
{
return \"crawl\";
}
//Overload our cout operator, to support printing our animal
ostream &operator<<(ostream &output, bear &lhs)
{
output << lhs.animalType << \", \" << lhs.talk() << \", \" << lhs.move();
return output;
}
bear::~bear()
{
// TODO Auto-generated destructor stub
}
bear.h
#ifndef BEAR_H_
#define BEAR_H_
#include\"mammal.h\"
using namespace std;
class bear: public mammal
{
public:
bear();
virtual string talk();
virtual string move();
virtual ~bear();
//Support << operator, outputs the animal type, talk, and move
friend ostream &operator<<(ostream &output, bear &lhs);
};
#endif /* BEAR_H_ */
bird.cpp
#include \"bird.h\"
using namespace std;
bird::bird()
{
//animalType = &type;
}
bird::~bird()
{
// TODO Auto-generated destructor stub
}
bird.h
#ifndef BIRD_H_
#define BIRD_H_
#include\"animal.h\"
using namespace std;
class bird: public animal
{
public:
bird();
virtual string talk() = 0;
virtual string move() = 0;
virtual ~bird();
};
#endif /* BIRD_H_ */
chicken.cpp
#include \"chicken.h\"
using namespace std;
chicken::chicken()
{
animalType = \"chicken\";
}
string chicken::talk()
{
return \"Bwak\";
}
string chicken::move()
{
return \"walk\";
}
//Overload our cout operator, to support printing our animal
ostream &operator<<(ostream &output, chicken &lhs)
{
output << lhs.animalType << \", \" << lhs.talk() << \", \" << lhs.move();;
return output;
}
chicken::~chicken()
{
// TODO Auto-generated destructor stub
}
chicken.h
#ifndef CHICKEN_H_
#define CHICKEN_H_
#include\"bird.h\"
using namespace std;
class chicken: public bird
{
public:
chicken();
string talk() ;
string move() ;
virtual ~chicken();
//Support << operator, outputs the animal type, talk, and move
friend ostream &operator<<(ostream &output, chicken &lhs);
};
#endif /* CHICKEN_H_ */
eagle.cpp
#include \"eagle.h\"
using namespace std;
eagle::eagle()
{
animalType = \"eagle\";
}
string eagle::talk()
{
return \"Screeeee\";
}
string eagle::move()
{
return \"Fly\";
}
//Overload our cout operator, to support printing our animal
ostream &operator<<(ostream &output, eagle &lhs)
{
output << lhs.animalType << \", \" << lhs.talk() << \", \" << lhs.move();;
return output;
}
eagle::~eagle()
{
// TODO Auto-generated destructor stub
}
eagle.h
#ifndef EAGLE_H_
#define EAGLE_H_
#include\"bird.h\"
using namespace std;
class eagle: public bird
{
public:
eagle();
string talk() ;
string move() ;
virtual ~eagle();
//Support << operator, outputs the animal type, talk, and move
friend ostream &operator<<(ostream &output, eagle &lhs);
};
#endif /* EAGLE_H_ */
hyena.cpp
#include \"hyena.h\"
using namespace std;
hyena::hyena()
{
animalType = \"hyena\";
}
string hyena::talk()
{
return \"hahahahaha\";
}
string hyena::move()
{
return \"walk\";
}
//Overload our cout operator, to support printing our animal
ostream &operator<<(ostream &output, hyena &lhs)
{
output << lhs.animalType << \", \" << lhs.talk() << \", \" << lhs.move();;
return output;
}
hyena::~hyena()
{
// TODO Auto-generated destructor stub
}
hyena.h
#ifndef HYENA_H_
#define HYENA_H_
#include\"mammal.h\"
using namespace std;
class hyena: public mammal
{
public:
hyena();
string talk() ;
string move() ;
virtual ~hyena();
//Support << operator, outputs the animal type, talk, and move
friend ostream &operator<<(ostream &output, hyena &lhs);
};
#endif /* HYENA_H_ */
lion.cpp
#include \"lion.h\"
using namespace std;
lion::lion()
{
animalType = \"Lion\";
}
string lion::talk()
{
return \"roar\";
}
string lion::move()
{
return \"crawl\";
}
//Overload our cout operator, to support printing our animal
ostream &operator<<(ostream &output, lion &lhs)
{
output << lhs.animalType << \", \" << lhs.talk() << \", \" << lhs.move();;
return output;
}
lion::~lion()
{
// TODO Auto-generated destructor stub
}
lion.h
#ifndef LION_H_
#define LION_H_
#include\"mammal.h\"
using namespace std;
class lion: public mammal
{
public:
lion();
string talk() ;
string move() ;
virtual ~lion();
//Support << operator, outputs the animal type, talk, and move
friend ostream &operator<<(ostream &output, lion &lhs);
};
#endif /* LION_H_ */
lizard.cpp
#include \"lizard.h\"
using namespace std;
lizard::lizard()
{
animalType = \"lizard\";
}
string lizard::talk()
{
return \"meep\";
}
string lizard::move()
{
return \"run\";
}
//Overload our cout operator, to support printing our animal
ostream &operator<<(ostream &output, lizard &lhs)
{
output << lhs.animalType << \", \" << lhs.talk() << \", \" << lhs.move();
return output;
}
lizard::~lizard()
{
// TODO Auto-generated destructor stub
}
lizard.h
#ifndef LIZARD_H_
#define LIZARD_H_
#include\"reptile.h\"
using namespace std;
class lizard : public reptile
{
public:
lizard();
string talk() ;
string move() ;
virtual ~lizard();
//Support << operator, outputs the animal type, talk, and move
friend ostream &operator<<(ostream &output, lizard &lhs);
};
#endif /* LIZARD_H_ */
mammal.cpp
#include \"mammal.h\"
using namespace std;
mammal::mammal()
{
}
mammal::~mammal()
{
// TODO Auto-generated destructor stub
}
mammal.h
#ifndef MAMMAL_H_
#define MAMMAL_H_
#include\"animal.h\"
using namespace std;
class mammal : public animal
{
public:
mammal();
virtual string talk() = 0;
virtual string move() = 0;
//Support << operator, outputs the animal type, talk, and move
friend ostream &operator<<(ostream &output, const mammal &lhs);
virtual ~mammal();
};
#endif /* MAMMAL_H_ */
reptile.cpp
#include \"reptile.h\"
using namespace std;
reptile::reptile()
{
}
reptile::~reptile()
{
// TODO Auto-generated destructor stub
}
reptile.h
#ifndef REPTILE_H_
#define REPTILE_H_
#include\"animal.h\"
using namespace std;
class reptile: public animal
{
public:
reptile();
virtual string talk() = 0;
virtual string move() = 0;
virtual ~reptile();
};
#endif /* REPTILE_H_ */
snake.cpp
#include \"snake.h\"
using namespace std;
snake::snake()
{
animalType = \"snake\";
}
string snake::talk()
{
return \"hisssss\";
}
string snake::move()
{
return \"slither\";
}
//Overload our cout operator, to support printing our animal
ostream &operator<<(ostream &output, snake &lhs)
{
output << lhs.animalType << \", \" << lhs.talk() << \", \" << lhs.move();
return output;
}
snake::~snake()
{
// TODO Auto-generated destructor stub
}
snake.h
#ifndef SNAKE_H_
#define SNAKE_H_
#include\"reptile.h\"
using namespace std;
class snake : public reptile
{
public:
snake();
string talk() ;
string move() ;
virtual ~snake();
//Support << operator, outputs the animal type, talk, and move
friend ostream &operator<<(ostream &output, snake &lhs);
};
#endif /* SNAKE_H_ */













