Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Code: This assignment will have you read information in from various files,

ID: 3590072 • Letter: C

Question

C++ Code:

This assignment will have you read information in from various files, perform exception handling when reading those files, and create a base class and derived classes with virtual functions to read the various files, perform some computations on the inputs as described below, and print the file details.   (Modified versions of these classes could be used in the Final Project as an input method for the user to enter ship location information instead of prompting for input.)

For this assignment, create a base class called Animal. It should have as member variables any variables that are contained across all animals in this assignment along with any associated mutator and accessor member functions. You will create a derived class for each of the following: dog, fish, horse, monkey and lizard. The member functions to read and output the object’s information will be virtual functions. Each derived class must contain the additional member variables and member functions needed to include all fields in each input file.

Each input file will contain data about one object; you will not be required to read in information about multiple objects from one file.

I have included one sample file for each of the five animal types. When I am testing your code, I will be using additional files with errors to see how you have implemented exception handling and gracefully recover. You may want to create additional files for your testing.

In your main, create an object of each derived class type, read in the object information from the provided files to populate the objects using the derived virtual read function. For the dog, subtract 10 pounds from its weight; for the horse, add one hand to its height; for the monkey, change its endangered flag (if endangered, make it not endangered; if not endangered, make it endangered). Finally, use the virtual print function to print to the screen the information about each animal.

Your main should create an object of each of the derived class types, use the readfile function of each object to read the files, call the subtract10 function for the dog, the add1 function for the horse, and the changeEndangered function for the monkey, and then use the overridden print function of each derived class to output the information about each animal.

File contents:

Dog.txt: name, breed, age, color, weight

Fish.txt: name, color, freshwater?, habitat, predator?

Horse.txt: name, color (body color), maneColor, age, height

Monkey.txt: name, color, age, wild?, home, endangered?

Lizard.txt: name, color, habitat, protected?, weight

Explanation / Answer

#include<iostream>
#include <fstream>
#include <stdlib.h>
#include<string>
using namespace std;
//Class Animal definition
class Animal
{
//Data member declaration
string name;
string color;

public:
int numberOfRecords;
//Default constructor
Animal()
{
numberOfRecords = 0;
}//End of function
//Function to set name
void setName(string n)
{
name = n;
}//End of function
//Function to return name
string getName()
{
return name;
}//End of function
//Function to set color
void setColor(string c)
{
color = c;
}//End of function
//Function to return color
string getColor()
{
return color;
}//End of function
//Function to set number of records
void setNumberOfRecords(int c)
{
numberOfRecords = c;
}//End of function
//Function to return number of records
int getNumberOfRecords()
{
return numberOfRecords;
}//End of function
//Virtual function print definition
virtual void print()
{
cout<<" Name: "<<name;
cout<<" Color: "<<color;
}//End of function
};//End of class

//Class Dog derived from Animal class
class Dog : public Animal
{
//Data member declaration
string breed;
int age;
int weight;
public:
//Function to print Dog information
void print()
{
//Calls the base class print function
Animal::print();
//Displays Dog class information
cout<<" Bread: "<<breed;
cout<<" Age: "<<age;
cout<<" Weight: "<<weight;
}//End of function
//Function to set breed
void setBreed(string b)
{
breed = b;
}//End of function
//Function to set age
void setAge(int a)
{
age = a;
}//End of function
//Function to set weight
void setWeight(int w)
{
weight = w;
}//End of function
//Function to return breed
string getBreed()
{
return breed;
}//End of function
//Function to return age
int getAge()
{
return age;
}//End of function
//Function to return weight
int getWeight()
{
return weight;
}//End of function
};//End of class

//Class Fish derived from Animal class
class Fish : public Animal
{
//Data member declaration
bool freshwater;
string habitat;
bool predator;
public:
//Function to print Fish information
void print()
{
//Calls the base class print function
Animal::print();
//Displays Fish class information
if(freshwater)
cout<<" Freshwater: true";
else
cout<<" Freshwater: false";
cout<<" Habitat: "<<habitat;
if(predator)
cout<<" Predator: true";
else
cout<<" Predator: false";
}//End of function
//Function to set fresh water
void setFrehwater(bool fw)
{
freshwater = fw;
}//End of function
//Function to set habit
void setHabit(string h)
{
habitat = h;
}//End of function
//Function to set predator
void setPredator(bool p)
{
predator = p;
}//End of function
//Function to return fresh water
bool getFrehwater()
{
return freshwater;
}//End of function
//Function to return habit
string setHabit()
{
return habitat;
}//End of function
//Function to return predator
bool setPredator()
{
return predator;
}//End of function
};//End of class

//Class Horse derived from Animal class
class Horse : public Animal
{
//Data member declaration
string maneColor;
int age;
int height;
public:
//Function to print Horse information
void print()
{
//Calls the base class print function
Animal::print();
//Displays Horse class information
cout<<" Mane Color: "<<maneColor;
cout<<" Age: "<<age;
cout<<" Height: "<<height;
}//End of function
//Function to set mane color
void setManeColor(string mc)
{
maneColor = mc;
}//End of function
//Function to set age
void setAge(int a)
{
age = a;
}//End of function
//Function to set height
void setHeight(int h)
{
height = h;
}//End of function
//Function to return mane color
string getManeColor()
{
return maneColor;
}//End of function
//Function to return age
int getAge()
{
return age;
}//End of function
//Function to return height
int getHeight()
{
return height;
}//End of function
};//End of class

//Class Monkey derived from Animal class
class Monkey : public Animal
{
//Data member declaration
int age;
bool wild;
string home;
bool endangered;
public:
//Function to print Monkey information
void print()
{
//Calls the base class print function
Animal::print();
//Displays Monkey class information
cout<<" Age: "<<age;
if(wild)
cout<<" Wild: true";
else
cout<<" Wild: false";
cout<<" Home: "<<home;
if(endangered)
cout<<" Endangered: true";
else
cout<<" Endangered: false";
}//End of function
//Function to set age
void setAge(int a)
{
age = a;
}//End of function
//Function to set wild
void setWild(bool w)
{
wild = w;
}//End of function
//Function to set home
void setHome(string h)
{
home = h;
}//End of function
//Function to set endangered
void setEndangered(bool e)
{
endangered = e;
}//End of function
//Function to return age
int getAge()
{
return age;
}//End of function
//Function to return wild
bool getWild()
{
return wild;
}//End of function
//Function to return home
string getHome()
{
return home;
}//End of function
//Function to return endangered
bool getEndangered()
{
return endangered;
}//End of function
};//End of class

//Class Lizard derived from Animal class
class Lizard : public Animal
{
//Data member declaration
string habitat;
bool protect;
int weight;
public:
//Function to print Lizard information
void print()
{
//Calls the base class print function
Animal::print();
//Displays Lizard information
cout<<" Habitat: "<<habitat;
cout<<" Protect: "<<protect;
cout<<" Weight: "<<weight;
}//End of function
//Function to set habitat
void setHabitat(string a)
{
habitat = a;
}//End of function
//Function to set protect
void setProtect(bool p)
{
protect = p;
}//End of function
//Function to set weight
void setWeight(int w)
{
weight = w;
}//End of function
//Function to return habitat
string getHabitat()
{
return habitat;
}//End of function
//Function to return protect
bool getProtect()
{
return protect;
}//End of function
//Function to return weight
int getWeight()
{
return weight;
}//End of function
};//End of class

//Function to read data from file and store in the class respective data member
void readFile(Dog d[], Fish f[], Horse h[], Monkey m[], Lizard l[])
{
int co = 0;
string n;
int a;
bool b;
//Creates an object of ifstream
ifstream rFile;
//For Fish class
try
{
//Opens the file Dog.txt for reading
rFile.open("Dog.txt");
if(!rFile)
throw 11;
//Loops till end of file
while(!rFile.eof())
{
//Read the data from file and stores in data members
rFile>>n;
d[co].setName(n);
rFile>>n;
d[co].setBreed(n);
rFile>>a;
d[co].setAge(a);
rFile>>n;
d[co].setColor(n);
rFile>>a;
d[co].setWeight(a);
//Increase the counter by one
co++;
}//End of while
}//End of try block
//Catch block begins
catch(int e)
{
cout<<"Unable to open file: "<<endl;
}
//Close file
rFile.close();
//Assigns the counter value to the number of records
d[0].setNumberOfRecords(co);
//For Fish class
co = 0;
try
{
//Opens the file Fish.txt for reading
rFile.open("Fish.txt");
if(!rFile)
throw 11;
//Loops till end of file
while(!rFile.eof())
{
//Read the data from file and stores in data members
rFile>>n;
f[co].setName(n);
rFile>>n;
f[co].setColor(n);
rFile>>b;
f[co].setFrehwater(b);
rFile>>n;
f[co].setHabit(n);
rFile>>b;
f[co].setPredator(b);
//Increase the counter by one
co++;
}//End of while
}//End of try block
//Catch block begins
catch(int e)
{
cout<<"Unable to open file: "<<endl;
}
//Close file
rFile.close();
//Assigns the counter value to the number of records
f[0].setNumberOfRecords(co);

//For Horse class
co = 0;
try
{
//Opens the file Horse.txt for reading
rFile.open("Horse.txt");
if(!rFile)
throw 11;
//Loops till end of file
while(!rFile.eof())
{
//Read the data from file and stores in data members
rFile>>n;
h[co].setName(n);
rFile>>n;
h[co].setColor(n);
rFile>>n;
h[co].setManeColor(n);
rFile>>a;
h[co].setAge(a);
rFile>>a;
h[co].setHeight(a);
//Increase the counter by one
co++;
}//End of while
}//End of try block
//Catch block begins
catch(int e)
{
cout<<"Unable to open file: "<<endl;
}
//Close file
rFile.close();
//Assigns the counter value to the number of records
h[0].setNumberOfRecords(co);

//For Monkey class
co = 0;
try
{
//Opens the file Monkey.txt for reading
rFile.open("Monkey.txt");
if(!rFile)
throw 11;
//Loops till end of file
while(!rFile.eof())
{
//Read the data from file and stores in data members
rFile>>n;
m[co].setName(n);
rFile>>n;
m[co].setColor(n);
rFile>>a;
m[co].setAge(a);
rFile>>b;
m[co].setWild(b);
rFile>>n;
m[co].setHome(n);
rFile>>b;
m[co].setEndangered(b);
//Increase the counter by one
co++;
}//End of while
}//End of try block
//Catch block begins
catch(int e)
{
cout<<"Unable to open file: "<<endl;
}
//Close file
rFile.close();
//Assigns the counter value to the number of records
m[0].setNumberOfRecords(co);

//For Lizard class
co = 0;
try
{
//Opens the file Lizard.txt for reading
rFile.open("Lizard.txt");
//Checks if unable to open the file throws an exception
if(!rFile)
throw 11;
//Loops till end of file
while(!rFile.eof())
{
//Read the data from file and stores in data members
rFile>>n;
l[co].setName(n);
rFile>>n;
l[co].setColor(n);
rFile>>n;
l[co].setHabitat(n);
rFile>>b;
l[co].setProtect(b);
rFile>>a;
l[co].setWeight(a);
//Increase the counter by one
co++;
}//End of while
}//End of try block
//Catch block begins
catch(int e)
{
cout<<"Unable to open file: "<<endl;
}//End of catch
//Close file
rFile.close();
//Assigns the counter value to the number of records
l[0].setNumberOfRecords(co);

}//End of function

//Main function definition
int main()
{
Dog dd[10];
Fish ff[10];
Horse hh[10];
Monkey mm[10];
Lizard ll[10];
readFile(dd, ff, hh, mm, ll);
cout<<" *********************Dog*******************";
for(int c = 0; c < dd[0].getNumberOfRecords(); c++)
dd[c].print();
cout<<" ***************************Fish**************************";
for(int c = 0; c < ff[0].getNumberOfRecords(); c++)
ff[c].print();
cout<<" ***************************Horse**************************";
for(int c = 0; c < hh[0].getNumberOfRecords(); c++)
hh[c].print();
cout<<" ***************************Monkey**************************";
for(int c = 0; c < mm[0].getNumberOfRecords(); c++)
mm[c].print();
cout<<" ***************************Lizard**************************";
for(int c = 0; c < ll[0].getNumberOfRecords(); c++)
ll[c].print();
}//End of main function

Sample Run:


*********************Dog*******************
Name: Puppy
Color: red
Bread: pamorian
Age: 5
Weight: 12
Name: Ranu
Color: black
Bread: poli
Age: 3
Weight: 22

***************************Fish**************************
Name: dolphin
Color: black
Freshwater: true
Habitat: jump
Predator: false

***************************Horse**************************
Name: ramu
Color: white
Mane Color: brown
Age: 10
Height: 5

***************************Monkey**************************
Name: Dhanu
Color: white
Age: 10
Wild: true
Home: raju
Endangered: true

***************************Lizard**************************
Name: furu
Color: white
Habitat: eat
Protect: 1
Weight: 2