************Animal.h************ #pragma once #ifndef Animal_h #define Animal_h
ID: 3861593 • Letter: #
Question
************Animal.h************
#pragma once
#ifndef Animal_h
#define Animal_h
#endif // !Animal_h
class Animal
{
private:
char* name;
int age;
public:
void getName(char * name) const;
void setName(char * name);
void setAge(int a);
int getAge()const;
};
*******animals.cpp*******
#include "Animal.h"
#include
#include
using namespace std;
void Animal::setName(char * name)
{
//release the exisisting memory if there is any
if(this->name)
delete [] this->name;
//set new name
this->name = new char[strlen(name)+1];
strcpy(this->name, name);
}
void Animal::getName(char * name) const
{
//returns name
strcpy(name, this->name);
}
void Animal::setAge(int a)
{
age = a;
};
int Animal::getAge()const
{
return age;
};
Create a program which uses the class Animal to Implement the following
a. The program will allow the user to
1. Add Animals
2. Delete Animals
3. Display Animals searching by name
4. Display a list of all Animals
Explanation / Answer
Animal.h
#pragma once
#ifndef Animal_h
#define Animal_h
#endif // !Animal_h
class Animal
{
private:
char* name;
int age;
public:
char* getName() const;
void setName(char * name);
void setAge(int a);
int getAge() const;
};
I have made some changes to animal.h above
Animal.cpp
#include"Animal.h"
#include<iostream>
#include<stdio.h>
#include<string.h>
#include <vector>
using namespace std;
void Animal::setName(char * name)
{
//release the exisisting memory if there is any
if(this->name)
delete [] this->name;
//set new name
this->name = new char[strlen(name)+1];
strcpy(this->name, name);
}
char * Animal::getName() const
{
return this->name;
}
void Animal::setAge(int a)
{
this->age = a;
};
int Animal::getAge() const
{
return this->age;
};
void addAnimal(Animal *&a) {
int age;
char name[200];
cout << "Add Animal: Enter Name: ";
cin >> name;
cout << " Enter Age: ";
cin >> age;
(*a).setName(name);
(*a).setAge(age);
}
void printList(vector<Animal *> animals) {
cout << " List of Animals:" << endl;
for (vector<Animal*>::iterator i = animals.begin(); i != animals.end(); ++i) {
Animal* p = *i;
cout << "ANIMAL: " << p->getName() << ", AGE: " << p->getAge() << endl;
}
}
int showMenu() {
int ch=0;
while(ch <=0 || ch > 5) {
cout << " Select:" << endl;
cout << "1. Add Animal." << endl;
cout << "2. Delete Animals." << endl;
cout << "3. Display Animals Searching by name." << endl;
cout << "4. Display list of all animals." << endl;
cout << "5. Quit." << endl;
cin >> ch;
}
return ch;
}
int main()
{
vector<Animal *> animals;
int choice = 1;
while((choice = showMenu()) != 5) {
if(choice==1) {
Animal *a = new Animal();
addAnimal(a);
animals.push_back(a);
} else if(choice==2) {
cout << "Enter Animal number to be deleted: (1-" << animals.size() << ") only: ";
int index;
cin >> index;
while (getchar() != ' ');
animals.erase(animals.begin() + (index-1));
printList(animals);
} else if(choice==3) {
char search[100];
cout << "Enter name to be searched: ";
cin >> search;
for (vector<Animal*>::iterator i = animals.begin(); i != animals.end(); ++i) {
char * index;
Animal* p = *i;
index = strstr(p->getName(),search);
if(index != NULL) {
cout << " Found ANIMAL: " << p->getName() << ", AGE: " << p->getAge() << endl;
}
}
} else if(choice==4) {
cout << " List of Animals:" << endl;
printList(animals);
}
}
return 0;
}
Sample Output:
Select:
1. Add Animal.
2. Delete Animals.
3. Display Animals Searching by name.
4. Display list of all animals.
5. Quit.
1
Add Animal: Enter Name: Zebra
Enter Age: 2
Select:
1. Add Animal.
2. Delete Animals.
3. Display Animals Searching by name.
4. Display list of all animals.
5. Quit.
1
Add Animal: Enter Name: Elephant
Enter Age: 8
Select:
1. Add Animal.
2. Delete Animals.
3. Display Animals Searching by name.
4. Display list of all animals.
5. Quit.
3
Enter name to be searched: Eleph
Found ANIMAL: Elephant, AGE: 8
Select:
1. Add Animal.
2. Delete Animals.
3. Display Animals Searching by name.
4. Display list of all animals.
5. Quit.
4
List of Animals:
List of Animals:
ANIMAL: Zebra, AGE: 2
ANIMAL: Elephant, AGE: 8
Select:
1. Add Animal.
2. Delete Animals.
3. Display Animals Searching by name.
4. Display list of all animals.
5. Quit.
2
Enter Animal number to be deleted: (1-2) only: 1
List of Animals:
ANIMAL: Elephant, AGE: 8
Select:
1. Add Animal.
2. Delete Animals.
3. Display Animals Searching by name.
4. Display list of all animals.
5. Quit.
4
List of Animals:
List of Animals:
ANIMAL: Elephant, AGE: 8
Select:
1. Add Animal.
2. Delete Animals.
3. Display Animals Searching by name.
4. Display list of all animals.
5. Quit.
5
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.