/Animal.cpp is the main function file of the Animal class //CSIS 112/B16 #include using namespace std; //defining Animal class class Animal { public: //Name and age of class string name; int age; //default contstructor Animal() { name = ""; age = 0; cout << "Invoking Animal 2 argument constructor?" << endl; } //virtual function virtual void move() {} //getter and setter functions string getName() { return name; } void setName(string n) { name = n; } int getAge() { return age; } void setAge(int a) { age = a; } }; //Dog derives Animal publically class Dog : public Animal { public: //dogs constructor Dog(string n, int a) { name = n; age = a; cout << "Invoking Dog constructor" << endl; } //implementing virtual function virtual void move() { cout << "I run" << endl; } }; class Rabbit : public Animal { public: Rabbit(string n, int a) { name = n; age = a; cout << "Invoking Rabbit constructor," << endl; } ~Rabbit() { cout << "Invoking Rabbit destructor" << endl; } virtual void move() { cout << "I hop" << endl; } }; class Fish : public Animal { public: Fish(string n, int a) { name = n; age = a; cout << "Invoking Fish constructor" << endl; } ~Fish(){ cout << "Invoking Fish destructor" << endl; } virtual void move() { cout << "I swim" << endl; } }; class Snake : public Animal { public: Snake(string n, int a) { name = n; age = a; cout << "Invoking Snake constructor" << endl; } virtual void move() { cout << "I slither" << endl; } }; int main() { cout << ("Steven_Brightwell-Assignment 8\n"); int i = 0; Animal animals[4]; while (i < 3) { cout << endl << "Press (1) for dog\t(2)for rabbit\t(3) for fish\t(4) for snake" << endl; cout << "Choice:"; int choice; cin >> choice; switch (choice) { case 1: { int age = rand() % (20 - 1) + 1;; string name; cout << "Name:"; cin >> name; animals[i] = Dog(name, age); break; } case 2: { int age = rand() % (20 - 1) + 1; string name; cout << "Name:"; cin >> name; animals[i] = Rabbit(name, age); break; } case 3: { int age = rand() % (20 - 1) + 1; string name; cout << "Name:"; cin >> name; animals[i] = Fish(name, age); break; } case 4: { int age = rand() % (20 - 1) + 1; string name; cout << "Name:"; cin >> name; animals[i] = Snake(name, age); break; } } i++; } for (int i = 0; i < 5; i++) { cout << "hello" << endl;

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter2: Using Data
Section: Chapter Questions
Problem 1GZ
icon
Related questions
Question

How would I fix this to get it looking like output #2?

//Animal.cpp is the main function file of the Animal class
//CSIS 112/B16

#include <iostream>
using namespace std;
//defining Animal class
class Animal {
public:
//Name and age of class
string name;
int age;
//default contstructor
Animal() {
name = "";
age = 0;
cout << "Invoking Animal 2 argument constructor?" << endl;
}
//virtual function
virtual void move() {}
//getter and setter functions
string getName() {
return name;
}
void setName(string n) {
name = n;
}
int getAge() {
return age;
}
void setAge(int a) {
age = a;
}
};
//Dog derives Animal publically
class Dog : public Animal {
public:
//dogs constructor
Dog(string n, int a) {
name = n;
age = a;
cout << "Invoking Dog constructor" << endl;
}
//implementing virtual function
virtual void move() {
cout << "I run" << endl;
}
};
class Rabbit : public Animal {
public:
Rabbit(string n, int a) {
name = n;
age = a;
cout << "Invoking Rabbit constructor," << endl;
}
~Rabbit() {
cout << "Invoking Rabbit destructor" << endl;
}
virtual void move() {
cout << "I hop" << endl;
}
};
class Fish : public Animal {
public:
Fish(string n, int a) {
name = n;
age = a;
cout << "Invoking Fish constructor" << endl;
}
~Fish(){
cout << "Invoking Fish destructor" << endl;
}
virtual void move() {
cout << "I swim" << endl;
}
};
class Snake : public Animal {
public:
Snake(string n, int a) {
name = n;
age = a;
cout << "Invoking Snake constructor" << endl;
}
virtual void move() {
cout << "I slither" << endl;
}
};
int main() {
cout << ("Steven_Brightwell-Assignment 8\n");
int i = 0;
Animal animals[4];
while (i < 3) {
cout << endl << "Press (1) for dog\t(2)for rabbit\t(3) for fish\t(4) for snake" << endl;
cout << "Choice:";
int choice;
cin >> choice;
switch (choice) {
case 1: {
int age = rand() % (20 - 1) + 1;;
string name;
cout << "Name:";
cin >> name;
animals[i] = Dog(name, age);
break;
}
case 2: {
int age = rand() % (20 - 1) + 1;
string name;
cout << "Name:";
cin >> name;
animals[i] = Rabbit(name, age);
break;
}
case 3: {
int age = rand() % (20 - 1) + 1;
string name;
cout << "Name:";
cin >> name;
animals[i] = Fish(name, age);
break;
}
case 4: {
int age = rand() % (20 - 1) + 1;
string name;
cout << "Name:";
cin >> name;
animals[i] = Snake(name, age);
break;
}

}

i++;
}
for (int i = 0; i < 5; i++) {
cout << "hello" << endl;
animals[i].move();

Looks like this:

Steven_Brightwell-Assignment 8
Invoking Animal 2 argument constructor?
Invoking Animal 2 argument constructor?
Invoking Animal 2 argument constructor?
Invoking Animal 2 argument constructor?

Press (1) for dog (2)for rabbit (3) for fish (4) for snake
Choice:1
Name:dog
Invoking Animal 2 argument constructor?
Invoking Dog constructor

Press (1) for dog (2)for rabbit (3) for fish (4) for snake
Choice://Animal.cpp is the main function file of the Animal class

Press (1) for dog (2)for rabbit (3) for fish (4) for snake
Choice:hello
hello
hello
hello
hello

C:\Users\hpsha\OneDrive Liberty\OneDrive - Liberty University\CSIS 112\CSIS Assinment 8\ANIMAL\Debug\ANIMAL.exe (process 16532) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Needs to look like this:

PHOTO ATTACHED OF WHAT IT SHOULD LOOK LIKE. IT DOES NOT THOUGH

 

 

 

My name is Buttercup, and I am 6 years old.
I run.
My name is Hoppy, and I am 17 years old.
I hop.
My name is Nemo, and I am 16 years old.
I swim.
My name is Snakey, and I am 5 years old.
I slither.
Main Menu
1 - Dog
My name is Princess, and I am 5 years old.
I run.
2 - Rabbit
Invoking the Dog destructor
Invoking the Animal destructor
Invoking the Rabbit destructor
Invoking the Animal destructor
Invoking the Fish destructor
Invoking the Animal destructor
Invoking the Snake destructor
Invoking the Animal destructor
Invoking the Dog destructor
Invoking the Animal destructor
Press any key to continue
3 - Fish
4 - Snake
Enter selection:
D (Ct
As each animal is selected, you should see messages like the following:
Invoking Animal 2-argument constructor
Invoking Dog 2-argument constructor
Transcribed Image Text:My name is Buttercup, and I am 6 years old. I run. My name is Hoppy, and I am 17 years old. I hop. My name is Nemo, and I am 16 years old. I swim. My name is Snakey, and I am 5 years old. I slither. Main Menu 1 - Dog My name is Princess, and I am 5 years old. I run. 2 - Rabbit Invoking the Dog destructor Invoking the Animal destructor Invoking the Rabbit destructor Invoking the Animal destructor Invoking the Fish destructor Invoking the Animal destructor Invoking the Snake destructor Invoking the Animal destructor Invoking the Dog destructor Invoking the Animal destructor Press any key to continue 3 - Fish 4 - Snake Enter selection: D (Ct As each animal is selected, you should see messages like the following: Invoking Animal 2-argument constructor Invoking Dog 2-argument constructor
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT