Question
For the following C++ code, //include headers #include #include using namespace std; //define class Book class Animal { private: string type, name, origin; int age;
For the following C++ code,
//include headers #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);
} ------------------------------------------------------------------------------------------------------------------------------
Create a base class Person including protected members to describe name and age, and a public constructor. Create two derived classes Farmer, with additional members (role, years of service, salary) and Client, with additional members (client code, city). Each derived class will have a constructor that inherits the base class constructor. Base class and derived classes will use a public method Show()to show the related objects data, however this method will be implemented in a different way on base class and each derived class.Create a main() function to create objects of each class and call the function Show() for each object.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started