Lab 5a (15 points) Create a class named Animals. (5 total points) Create the imp
ID: 3730076 • Letter: L
Question
Lab 5a (15 points) Create a class named Animals. (5 total points) Create the implementation file Animals.cpp 1. a. The class will have the following members Animal name Animals Age- You must validate the age being input (use the code from the previous lab). 1. 2. Create an Animal header file Animal.h (5 points) This file acts as a prototype. 2. 3. Create the program Lab05a.cpp which uses the class Animal to Implement the following a. The program will allow the user to (4 points total) 1. 2. 3. 4. Add Animals (1 point) Delete Animals (1 points) Display Animals searching by name (1 points) Display a list of all Animals (1 points) 4. On exit; print to the screen "exxX> says bye" where is the first animal in the list. If there are no animals in the list, print "There are no animals in the Zoo". (1 point) IDExplanation / Answer
Animals.cpp
#include<iostream>
#include<string>
using namespace std;
class Animals
{
private:
string name;
int age;
public:
void setname(string s)
{
name =s;
}
void setage(int a)
{
age=a;
}
string getname()
{
return name;
}
int age()
{
return age;
}
};
Lab05.cpp
#include "Animal.h"
class List:public Animals
{
private:
Animals list[100];
int num;
public:
List()
{
num=0;
}
void add(Animals x)
{
list[num]=x;
num++;
}
void del(string s)
{
Animals x;
if(num==0)
{
cout<<"No animal present ";
return;
}
int i=0,j;
for(i=0;i<num;i++)
{
if(list[i].getname()==s)
{
cout<<list[i].getname()<<" age"<<list[i].get_age()<<" ";
num--;
for(j=i;j<num;j++)
{
list[j]=list[j+1];
}
return;
}
}
cout<<"Animal not present in list ";
return;
}
void display()
{
int i;
for(i=0;i<num;i++)
{
cout<<list[i].getname()<<" "<<list[i].get_age()<<endl;
}
}
void display_by_name(string s)
{
int i;
for(i=0;i<num;i++)
{
if(list[i].getname()==s)
{
cout<<list[i].getname()<<" "<<list[i].get_age()<<endl;
return;
}
}
cout<<"Animal "<<s<<" not present in list ";
}
int isEmpty()
{
return num;
}
string getfirst()
{
return list[0].getname();
}
};
int main()
{
Animals x;
List l;
string name;
int age,tmp;
while(1)
{
cout<<"Enter animal's name ";
cin>>name;
cout<<"Enter animal's age ";
cin>>age;
x.setname(name);
x.setage(age);
l.add(x);
cout<<"Enter another animal? (1/0): ";
cin>>tmp;
if(tmp==0)
break;
}
l.display();
cout<<"Enter name of the animal to be deleted: ";
cin>>name;
l.del(name);
cout<<"Want to search animal? (1/0): ";
cin>>tmp;
if(tmp)
{
cout<<"Enter the name of animal to be searched: ";
l.display_by_name(name);
}
l.display();
if(l.isEmpty())
cout<<"There are no animals in the zoo ";
else
{
cout<<l.getfirst()<<" says bye ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.