I need help with question 4 5a and 5b You can only answer on
I need help with question 4, 5a, and 5b. You can only answer one if you want I would appreciate any help, thank you!
// Q1a: Create Dog Class
// Part 1: Create a child class of the Pet class named \'Dog\'
// See the add function in hw10.cpp for proper use of this function.
// Part2: Declare constructor which accepts the same 3 parameters as the parent class Pet.
// Pass the 3 parameters to the super constructor in the Pet class.
// Part 3: Re-declare the method display (virtual method found inside of parent class Pet)
// Q2a: Define Display for Dog class
// Define the method display that you declared within the Dog class in the header file
// Information should be printed in the following format:
// Name:
// Breed:
// Type: Dog
// (See the print_all function in hw10.cpp for proper use of this function.)
// READ BEFORE YOU START:
// You are given a partially completed program that creates a list of pets.
// Each pet has the corresponding information: name, breed, and type.
// In the Pet.h file, you will find the definition for this enum \'type\'.
// Pets on the list can be 2 different \'types\' : either a dog or a cat.
// The classes Dog and Cat are subclasses of the Pet class (found in Pet.h).
// Both of these classes will have their own use of the virtual display method.
//
// To begin, you should trace through the given code and understand how it works.
// Please read the instructions above each required function and follow the directions carefully.
// If you modify any of the given code, the return types, or the parameters, you risk failing the automated test cases.
//
// You are to assume that all input is valid:
// Valid name: String containing alphabetical letters beginning with a capital letter
// Valid breed: String containing alphabetical letters beginning with a capital letter
// All input will be a valid length and no more than the allowed amount of memory will be used
#include \"Container.h\"
#include \"Pet.h\"
#include \"Dog.h\"
#include \"Cat.h\"
#include
#include
#include
using namespace std;
// forward declarations
void flush();
void branching(char);
void helper(char);
void add_pet(string, string, Type);
Pet* search_pet(string, string, Type);
void remove_pet(string, string, Type);
void remove_all();
void print_all();
void save(string); // 10 points
void load(string); // 10 points
Container* list = NULL; // global list
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); // Use to check for memory leaks in VS
load(\"Pets.txt\");
char ch = \'i\';
do {
cout << \"Please enter your selection\" << endl;
cout << \"\\ta: add a new pet to the list\" << endl;
cout << \"\\tc: change the breed of a pet\" << endl;
cout << \"\\tr: remove a pet from the list\" << endl;
cout << \"\\tp: print all pets on the list\" << endl;
cout << \"\\tq: quit and save list of pets\" << endl;
cin >> ch;
flush();
branching(ch);
} while (ch != \'q\');
save(\"Pets.txt\");
remove_all();
list = NULL;
return 0;
}
void flush()
{
int c;
do c = getchar(); while (c != \'\ \' && c != EOF);
}
void branching(char c)
{
switch (c) {
case \'a\':
case \'c\':
case \'r\':
case \'p\':
helper(c);
break;
case \'q\':
break;
default:
printf(\"\ Invalid input!\ \ \");
}
}
// The helper function is used to determine how much data is needed and which function to send that data to.
// It uses pointers and values that are returned from some functions to produce the correct ouput.
// There is no implementation needed here, but you should study this function and know how it works.
// It is always helpful to understand how the code works before implementing new features.
// Do not change anything in this function or you risk failing the automated test cases.
void helper(char c)
{
string name, breed;
Type type;
int type_check = -1;
if (c == \'p\')
print_all();
else
{
cout << endl << \"Please enter the pet\'s name: \" << endl;
cin >> name;
cout << \"Please enter the pet\'s breed: \" << endl;
cin >> breed;
while (!(type_check == 0 || type_check == 1))
{
cout << endl << \"Please select one of the following: \" << endl;
cout << \"0. Dog \" << endl;
cout << \"1. Cat\" << endl;
cin >> type_check;
}
type = (Type)type_check;
Pet* pet_result = search_pet(name, breed, type);
if (c == \'a\') // add pet
{
if (pet_result == NULL)
{
add_pet(name, breed, type);
cout << endl << \"Pet added.\" << endl << endl;
}
else
cout << endl << \"Pet already on list.\" << endl << endl;
}
else if (c == \'c\') // change pet breed
{
if (pet_result == NULL)
{
cout << endl << \"Pet not found.\" << endl << endl;
return;
}
cout << endl << \"Please enter the new breed for this pet: \" << endl;
cin >> breed; flush();
// Q3c: Call Change Breed Function
cout << endl << \"Pet\'s breed changed.\" << endl << endl;
}
else if (c == \'r\') // remove pet
{
if (pet_result == NULL)
{
cout << endl << \"Pet not found.\" << endl << endl;
return;
}
remove_pet(name, breed, type);
cout << endl << \"Pet removed from the list.\" << endl << endl;
}
}
}
// Q3b: Define Friend Function Change Breed
// Define the function changeBreed that is declared within the Pet.h file.
// This function sets the breed value of the Pet pointer to the value of the string parameter.
// Q4: Add Pet
// This function will be used to add a new pet to the tail of the global linked list.
// You will need to use the enum ëtypeí variable to determine which constructor to use.
// Remember that search is called before this function, therefore, the new pet is not on the list.
void add_pet(string name, string breed, Type type)
{
}
// No implementation needed here, however it may be helpful to review this function
Pet* search_pet(string name, string breed, Type type)
{
Container* container_traverser = list;
while (container_traverser != NULL)
{
if (container_traverser->pet->getName() == name
&& container_traverser->pet->getBreed() == breed
&& container_traverser->pet->getType() == type)
return container_traverser->pet;
container_traverser = container_traverser->next;
}
return NULL;
}
// No implementation needed here, however it may be helpful to review this function
void remove_pet(string name, string breed, Type type)
{
Container* to_be_removed;
if (list->pet->getName() == name
&& list->pet->getBreed() == breed
&& list->pet->getType() == type)
{
to_be_removed = list;
list = list->next;
delete to_be_removed->pet;
delete to_be_removed;
return;
}
Container* container_traverser = list->next;
Container* container_follower = list;
while (container_traverser != NULL)
{
if (container_traverser->pet->getName() == name
&& container_traverser->pet->getBreed() == breed
&& container_traverser->pet->getType() == type)
{
to_be_removed = container_traverser;
container_traverser = container_traverser->next;
container_follower->next = container_traverser;
delete to_be_removed->pet;
delete to_be_removed;
return;
}
container_follower = container_traverser;
container_traverser = container_traverser->next;
}
}
// No implementation needed here, however it may be helpful to review this function
void remove_all()
{
while (list != NULL)
{
Container* temp = list;
list = list->next;
delete temp->pet;
delete temp;
}
}
// This function uses the virtual display() method of the Dog and Cat classes to print all Pets in an oragnized format.
void print_all()
{
Container *container_traverser = list;
if (list == NULL)
cout << endl << \"List is empty!\" << endl << endl;
while (container_traverser != NULL)
{
container_traverser->pet->display();
container_traverser = container_traverser->next;
}
}
// Q5a: Save (5 points)
// Save the linked list of pets to a file using ofstream.
// You will need to come up with a way to store the amount of Containers in linked list.
// Hint: You may want to cast the enum \'type\' to an int before writing it to the file.
void save(string fileName)
{
}
// Q5b: Load (5 points)
// Load the linked list of pets from a file using ifstream.
// You will need to create the linked list in the same order that is was saved to a file.
// You will need to create a new node for the linked list, then add it to the tail of the list.
// Hint: If you casted the enum \'type\' to an int, you will need to cast it back to a \'Type\'.
// You will use the \'type\' variable read from the file to determine which constructor to use.
void load(string fileName)
{
}
Solution
Here is the updated code for the question. Since you have not pasted the code for the header files Container.h, Pet.h, Dog.h and Cat.h, I had to create my own versions of these files. But you may use your own files for those and just use the code for Q4, Q5a and Q5b i.e. add_pet(), load() and save() functions alone.
#include \"Container.h\"
 #include \"Pet.h\"
 #include \"Dog.h\"
 #include \"Cat.h\"
 #include <iostream>
 #include <fstream>
 //#include
 using namespace std;
 // forward declarations
 void flush();
 void branching(char);
 void helper(char);
 void add_pet(string, string, Type);
 Pet* search_pet(string, string, Type);
 void remove_pet(string, string, Type);
 void remove_all();
 void print_all();
 void save(string); // 10 points
 void load(string); // 10 points
 Container* list = NULL; // global list
 int main()
 {
 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); // Use to check for memory leaks in VS
 load(\"Pets.txt\");
 char ch = \'i\';
 do {
 cout << \"Please enter your selection\" << endl;
 cout << \"\\ta: add a new pet to the list\" << endl;
 cout << \"\\tc: change the breed of a pet\" << endl;
 cout << \"\\tr: remove a pet from the list\" << endl;
 cout << \"\\tp: print all pets on the list\" << endl;
 cout << \"\\tq: quit and save list of pets\" << endl;
 cin >> ch;
 flush();
 branching(ch);
 } while (ch != \'q\');
 save(\"Pets.txt\");
 remove_all();
 list = NULL;
 return 0;
 }
 void flush()
 {
 int c;
 do c = getchar(); while (c != \'\ \' && c != EOF);
 }
 void branching(char c)
 {
 switch (c) {
 case \'a\':
 case \'c\':
 case \'r\':
 case \'p\':
 helper(c);
 break;
 case \'q\':
 break;
 default:
 printf(\"\ Invalid input!\ \ \");
 }
 }
 // The helper function is used to determine how much data is needed and which function to send that data to.
 // It uses pointers and values that are returned from some functions to produce the correct ouput.
 // There is no implementation needed here, but you should study this function and know how it works.
 // It is always helpful to understand how the code works before implementing new features.
 // Do not change anything in this function or you risk failing the automated test cases.
 void helper(char c)
 {
 string name, breed;
 Type type;
 int type_check = -1;
 if (c == \'p\')
 print_all();
 else
 {
 cout << endl << \"Please enter the pet\'s name: \" << endl;
 cin >> name;
 cout << \"Please enter the pet\'s breed: \" << endl;
 cin >> breed;
 while (!(type_check == 0 || type_check == 1))
 {
 cout << endl << \"Please select one of the following: \" << endl;
 cout << \"0. Dog \" << endl;
 cout << \"1. Cat\" << endl;
 cin >> type_check;
 }
 type = (Type)type_check;
 Pet* pet_result = search_pet(name, breed, type);
 if (c == \'a\') // add pet
 {
 if (pet_result == NULL)
 {
 add_pet(name, breed, type);
 cout << endl << \"Pet added.\" << endl << endl;
 }
 else
 cout << endl << \"Pet already on list.\" << endl << endl;
 }
 else if (c == \'c\') // change pet breed
 {
 if (pet_result == NULL)
 {
 cout << endl << \"Pet not found.\" << endl << endl;
 return;
 }
 cout << endl << \"Please enter the new breed for this pet: \" << endl;
 cin >> breed; flush();
 // Q3c: Call Change Breed Function
 cout << endl << \"Pet\'s breed changed.\" << endl << endl;
 }
 else if (c == \'r\') // remove pet
 {
 if (pet_result == NULL)
 {
 cout << endl << \"Pet not found.\" << endl << endl;
 return;
 }
 remove_pet(name, breed, type);
 cout << endl << \"Pet removed from the list.\" << endl << endl;
 }
 }
 }
 // Q3b: Define Friend Function Change Breed
 // Define the function changeBreed that is declared within the Pet.h file.
 // This function sets the breed value of the Pet pointer to the value of the string parameter.
 // Q4: Add Pet
 // This function will be used to add a new pet to the tail of the global linked list.
 // You will need to use the enum ëtypeí variable to determine which constructor to use.
 // Remember that search is called before this function, therefore, the new pet is not on the list.
 void add_pet(string name, string breed, Type type)
 {
 Pet *p;
if(type == DOG)
 {
 p = new Dog(name, breed, type);
 }
 else //if not dog , its a cat
 {
 p = new Cat(name, breed, type);
 }
Container *new_container = new Container();
 new_container -> pet = p;
 new_container -> next = NULL;
if(list == NULL)
 {
 list = new_container;
 return ;
 }
Container* container_traverser = list;
 while (container_traverser->next != NULL)
 {
 container_traverser = container_traverser->next;
 }
container_traverser->next = new_container;
 }
 // No implementation needed here, however it may be helpful to review this function
 Pet* search_pet(string name, string breed, Type type)
 {
 Container* container_traverser = list;
 while (container_traverser != NULL)
 {
 if (container_traverser->pet->getName() == name
 && container_traverser->pet->getBreed() == breed
 && container_traverser->pet->getType() == type)
 return container_traverser->pet;
 container_traverser = container_traverser->next;
 }
 return NULL;
 }
 // No implementation needed here, however it may be helpful to review this function
 void remove_pet(string name, string breed, Type type)
 {
 Container* to_be_removed;
 if (list->pet->getName() == name
 && list->pet->getBreed() == breed
 && list->pet->getType() == type)
 {
 to_be_removed = list;
 list = list->next;
 delete to_be_removed->pet;
 delete to_be_removed;
 return;
 }
 Container* container_traverser = list->next;
 Container* container_follower = list;
 while (container_traverser != NULL)
 {
 if (container_traverser->pet->getName() == name
 && container_traverser->pet->getBreed() == breed
 && container_traverser->pet->getType() == type)
 {
 to_be_removed = container_traverser;
 container_traverser = container_traverser->next;
 container_follower->next = container_traverser;
 delete to_be_removed->pet;
 delete to_be_removed;
 return;
 }
 container_follower = container_traverser;
 container_traverser = container_traverser->next;
 }
 }
 // No implementation needed here, however it may be helpful to review this function
 void remove_all()
 {
 while (list != NULL)
 {
 Container* temp = list;
 list = list->next;
 delete temp->pet;
 delete temp;
 }
 }
 // This function uses the virtual display() method of the Dog and Cat classes to print all Pets in an oragnized format.
 void print_all()
 {
 Container *container_traverser = list;
 if (list == NULL)
 cout << endl << \"List is empty!\" << endl << endl;
 while (container_traverser != NULL)
 {
 container_traverser->pet->display();
 container_traverser = container_traverser->next;
 }
 }
 // Q5a: Save (5 points)
 // Save the linked list of pets to a file using ofstream.
 // You will need to come up with a way to store the amount of Containers in linked list.
 // Hint: You may want to cast the enum \'type\' to an int before writing it to the file.
 void save(string fileName)
 {
 //we will store the file in this format, first a line containing the type of pet is written
 //then next line has the pet\'s name, and then the next line has the pet\'s breed.
 //we choose to write each information on a separate line to allow the possiblity of having spaces
 //in pet name and breed. The 3 lines repeat for each pet.
 ofstream outfile(fileName.c_str());
 Container *container_traverser = list;
 if(!outfile.is_open())
 {
 cout<<\"Could not open output file for writing : \"<<fileName<<endl;
 return;
 }
 while(container_traverser != NULL)
 {
 outfile<<(int)(container_traverser -> pet ->getType())<<\" \";
 outfile<<container_traverser -> pet -> getName()<<\" \";
 outfile<<container_traverser -> pet -> getBreed()<<endl;
container_traverser = container_traverser -> next;
 }
 outfile.close();
 }
 // Q5b: Load (5 points)
 // Load the linked list of pets from a file using ifstream.
 // You will need to create the linked list in the same order that is was saved to a file.
 // You will need to create a new node for the linked list, then add it to the tail of the list.
 // Hint: If you casted the enum \'type\' to an int, you will need to cast it back to a \'Type\'.
 // You will use the \'type\' variable read from the file to determine which constructor to use.
 void load(string fileName)
 {
 string name, breed;
 int type;
 ifstream infile(fileName);
if(!infile.is_open())
 {
 cout<<\"Error opening input file : \"<<fileName<<endl;
 return;
 }
 int idx;
 while(infile>>type)
 {
 infile >> name >>breed;
 Type t=(Type)type;
 add_pet(name, breed, t);
 }
 infile.close();
 }
My version of other header files....
Pet.h
#ifndef PET_H
 #define PET_H
 #include <iostream>
 using namespace std;
 enum Type {DOG, CAT};
 class Pet
 {
 private:
 string name, breed;
 Type type;
 public:
 Pet(string n, string b, Type t):name(n),breed(b),type(t)
 {}
string getName()
 {
 return name;
 }
 string getBreed()
 {
 return breed;
 }
Type getType()
 {
 return type;
 }
virtual void display()
 {
}
 };
 #endif
Dog.h
#ifndef DOG_H
 #define DOG_H
 #include \"Pet.h\"
 class Dog : public Pet
 {
 public:
 Dog(string n, string b, Type t):Pet(n, b, t){};
 void display()
 {
 cout<<\"Name: \"<<getName() <<endl;
 cout<<\"Breed: \"<<getBreed() <<endl;
 cout<<\"Type: Dog\"<<endl;
 }
 };
 #endif
Cat.h
#ifndef CAT_H
 #define CAT_H
 #include \"Pet.h\"
 class Cat : public Pet
 {
 public:
 Cat(string n, string b, Type t):Pet(n, b, t){};
 void display()
 {
 cout<<\"Name: \"<<getName() <<endl;
 cout<<\"Breed: \"<<getBreed() <<endl;
 cout<<\"Type: Cat\"<<endl;
 }
 };
 #endif
Container.h
#ifndef CONTAINER_H
 #define CONTAINER_H
 #include \"Pet.h\"
class Container
 {
 public:
 Pet *pet;
 Container *next;
};
 #endif
output
Error opening input file : Pets.txt
 Please enter your selection
    a: add a new pet to the list
    c: change the breed of a pet
    r: remove a pet from the list
    p: print all pets on the list
    q: quit and save list of pets
 a
Please enter the pet\'s name:
 Sheru
 Please enter the pet\'s breed:
 Pug
Please select one of the following:
 0. Dog
 1. Cat
 0
Pet added.
Please enter your selection
    a: add a new pet to the list
    c: change the breed of a pet
    r: remove a pet from the list
    p: print all pets on the list
    q: quit and save list of pets
 a
Please enter the pet\'s name:
 Scisor
 Please enter the pet\'s breed:
 Bulldog
Please select one of the following:
 0. Dog
 1. Cat
 0
Pet added.
Please enter your selection
    a: add a new pet to the list
    c: change the breed of a pet
    r: remove a pet from the list
    p: print all pets on the list
    q: quit and save list of pets
 a
Please enter the pet\'s name:
 Sweety
 Please enter the pet\'s breed:
 Persian
Please select one of the following:
 0. Dog
 1. Cat
 1
Pet added.
Please enter your selection
    a: add a new pet to the list
    c: change the breed of a pet
    r: remove a pet from the list
    p: print all pets on the list
    q: quit and save list of pets
 p
 Name: Sheru
 Breed: Pug
 Type: Dog
 Name: Scisor
 Breed: Bulldog
 Type: Dog
 Name: Sweety
 Breed: Persian
 Type: Cat
 Please enter your selection
    a: add a new pet to the list
    c: change the breed of a pet
    r: remove a pet from the list
    p: print all pets on the list
    q: quit and save list of pets
 q
Initially Pets.txt did not exist and when program is quit, it is created.
Pets.txt
0 Sheru Pug
 0 Scisor Bulldog
 1 Sweety Persian
















