Abstract Base Class (C++ Program)Create an abstract base class cal.pdf
calderoncasto9163
18 views
20 slides
Jul 05, 2023
Slide 1 of 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
About This Presentation
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 an...
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 //input output
#include //strings
#include //Randoms
#include //Randoms
#include //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 nu.
Size: 25.2 KB
Language: en
Added: Jul 05, 2023
Slides: 20 pages
Slide Content
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]
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
#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