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

This assignment will have you read information in from various files, perform ex

ID: 3590100 • Letter: T

Question

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 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 Formats:
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

This program is in C++ please help

Explanation / Answer

#include<iostream>

#include<stdlib.h>

#include<string.h>

#include<string>

#include<fstream>

using namespace std;

class Animal

{

string name;

string color;

public:

Animal(){

name = "";

color = "";

}

string getName(){

return name;

}

string getColor(){

return color;

}

void setName(string newName){

name = newName;

}

void setColor(string newColor){

color = newColor;

}

virtual void readfile(){

cout<< "Enter Name : ";

cin >> name;

cout<< "Enter Color : ";

cin >> color;

}

virtual void print(){

cout << "Name : " << name;

cout << "Color : " << color;

}

};

class Dog:public Animal

{

string breed;

int age;

float weight;

public:

Dog(){

breed = "";

age = 0;

weight = 0;

}

void print(){

cout << "Dog Info : "<<endl;

cout << "Name : " << getName() <<endl;

cout << "Breed : " << breed <<endl;

cout << "Color : " << getColor() <<endl;

cout << "Age : " << age << endl;

cout << "Weight : " << weight << endl;

}

void setBreed(string newBreed){

breed = newBreed;

}

void setAge(int newAge){

age = newAge;

}

void setWeight(float newWeight){

weight = newWeight;

}

string getBreed(){

return breed;

}

int getAge(){

return age;

}

float getWeight(){

return weight;

}

void subtract10(){

weight -= 10;

}

void readfile(){

ifstream in("Dog.txt");

string field;

int counter = 0;

if(!in){

cout<< "Dog.txt : Encountered error while opening the file" <<endl;

return;

}

try

{

while(getline(in,field,',')){

counter++;

  

switch(counter){

case 1:

setName(field);

break;

case 2:

breed = field;   

break;

case 3:

age = atoi(field.c_str());

//Check if age is all numerical, throw exception if not

if((age == 0) && (strlen(field.c_str()) > 1)){

throw 4;

}   

break;

case 4:

setColor(field);

break;

case 5:

weight = atof(field.c_str());

//Check if weight is all numerical, throw exception if not

if((weight == 0) && (strlen(field.c_str()) > 1)){

throw 5;

}   

break;

}

}

if(counter < 5){

throw 6;

}

}

catch(int exp){

switch(exp){

case 3:

cout<< "Error while reading age : format issue" << endl;

age = 0;

break;

case 5:

cout<< " Format isse while reading weight" <<endl;

weight = 0.0f;

break;

case 6:

cout<< "Insufficient number of values" << endl;

break;

}

}

catch(...){

cout << "Encountered error while reading the file" << endl;

}

in.close();

}

};

class fish: public Animal

{

bool freshwater;

string habitat;

bool predator;

public:

fish(){

freshwater = false;

habitat = "";

predator = false;

}

bool getFreshWater(){

return freshwater;

}

void setFreshWater(bool val){

freshwater = val;

}

string getHabitat(){

return habitat;

}

void setHabitat(string val){

habitat = val;

}

bool getPredator(){

return predator;

}

void setPredator(bool val){

predator = val;

}

void print(){

cout<< "Fish info : "<< endl;

cout<< "Name : " << getName() <<endl;

cout<< "Color : " << getColor() << endl;

cout<< "freshwater : " << freshwater << endl;

cout<< "habitat : " << habitat << endl;

cout<< "predator : " << predator << endl;

}

void readfile(){

ifstream in("Fish.txt");

string field;

int counter = 0;

if(!in){

cout<< "Fish.txt : Encountered error while opening the file" <<endl;

return;

}

try

{

while(getline(in,field,',')){

counter++;

  

switch(counter){

case 1:

setName(field);

break;

case 2:

setColor(field);

break;

case 3:

if(!strncmp(field,"y",1) || !strncmp(field,"Y",1)){

freshwater = true;

}

else{

freshwater = false;

}

break;

case 4:

habitat = field;

break;

case 5:

if(!strncmp(field,"y",1) || !strncmp(field,"Y",1)){

predator = true;

}

else{

predator = false;

}

}

}

if(counter < 5){

throw 6;

}

}

catch(int exp){

case 6:

cout<< "Insufficient number of values" << endl;

break;

}

catch(...){

cout << "Encountered error while reading the file" << endl;

}

in.close();

}

};

int main()

{

Dog d;

d.readfile();

d.print();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote