Question: For the following C++ code, add a function update that takesas argument an object Animal and allow to change the age. Change the main and

For the following C++ code, add a function update that takesas argument an object Animal and allow to change the age. Change the main and eventually the class accordingly.

//include headers #include #include using namespace std; //define class Book class Animal { private: string type, name, origin; int age; public: //define constructor Animal(string type = "NA", string name = "NA", string origin = "NA", int age = 0){ this->type = type; this->name = name; this->origin = origin; this->age = age; } //define loadAnimal function void loadAnimal(Animal* ptr){ //request user input for type, name, origin, and age of animal cout << "Please enter the type of the animal (mammal, bird, fish, etc): " << endl; cin >> ptr->type; cout << "Please enter the name of the animal: " << endl; cin >> ptr->name; cout << "Please enter the origin of the animal: " << endl; cin >> ptr->origin; cout << "Please enter the age of the animal: " << endl; cin >> ptr->age; } //define showAnimals function void showAnimals(Animal* ptr, int num){ //display all Animals in array for(int i = 0; i < num; i++){ cout << "Animal " << i + 1 << endl; cout << "Type: " << ptr[i].type << endl; cout << "Name: " << ptr[i].name << endl; cout << "Origin: " << ptr[i].origin << endl; cout << "Age: " << ptr[i].age << endl; } } }; int main(){ //declare Animal array Animal animals[5]; //display message to user cout << "Please enter information about five animals." << endl; //call loadAnimal function for each element in array for(int i = 0; i < 5; i++){ animals[i].loadAnimal(&animals[i]); } //call showAnimals function animals[0].showAnimals(animals, 5); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!