So I am a beginner C++ student and I have been working on this assignment for about a week now.
Listed below is the question given to us by the professor.
Your task on this assignment is to create a food ADT using the C++ features that we are currently learning in Chapters 11 and 12, which includes member functions and friend functions, overloaded operators and separate compilation.
The individual objects will contain the names of the foods, so you will need to include a string member capable of holding this. Most important for our purposes will be the nutritional information about the food. You should have members to protein, fiber, fat and calories. Your class should include a constructor (default numeric values will be set to zero), a set of accessor functions, mutator functions and a full set of operators.
Now as we use the various foods in our cooking we would like to be able to add their nutrition together through the use of an overloaded + operator.
Likewise we may use more than one serving size of a food, or a partial serving of a food, in our recipe so it makes sense to allow multiplication of the foods nutrition by doubles. This needs to be implemented as an overloaded * operator. (Multiplication of foods with other foods makes no sense, so don’t try to include that.)
It would also be good to be able to compare caloric content with one or more Boolean operators, such as < and/or >, ==, etc. (Look at the application below to figure out which ones you think you will need.
Subtraction is an interesting proposition, since it will also involve removing the food item from the string that represents a cumulative food. Remember that you can use the string member functions, find, substr, and erase in doing this.
Input and output should be done with overloaded >> and << operators, which you will overload as well.
The application that you write should declare a Food object that will serve as a sum, your lunch, which will be initialized by the default constructor, two pre-made food items, initialized by the constructor that takes arguments (you, as programmer, get to pick these), and one tmp food item that the user will be filling in. Also, declare a Food object for yesterday’s lunch.
The program then offers the user a menu inside a loop which will:
Let them view the contents of their lunch so far
Let them view yesterday’s lunch and compare it with today’s lunch
Let them add your food item # 1 (It will be the one that you have created with the constructor, e.g. Potato Chips)
Ask them how many servings they would like of this item
Let them add your food item # 2(Again, something you have created, e.g. Taco)
Ask them how many servings they would like of this item
Let them enter the info for a food of their choosing and add it to the lunch (Food data will be enter via the >> operator, and then added to the lunch with the + operator.)
Ask them how many servings they would like
{Over}
Remove an item from their lunch, either Food # 1, or Food # 2, or an item which they enter the data. (To work the name of the item needs to appear in the lunch. This will involve the use of the subtraction operator.
Quit
Once they quit Today’s lunch will be saved in the YesterdayLunch file, so that it can be viewed and compared tomorrow. It would be good to have member functions for read_from_file(ifstream&), and write_to_file(ifstream&) to do this.
Separate Compilation: Once you have written this program all in one file use copy-paste to divide your code into three files, a food.h, containing the class declaration, a food.cc, containing the implementation of the member functions, and a main.cc, containing the application, menu and file-opening stuff. Be sure to put all of these files into a single working directory. Compile your code again with g++ food.cc main.cc. Test run it one more time. Then submit all three program files and yesterday’s lunch using the command:
Here is what I have. This is my h file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <cstring>
// resubmit with a -f
//~jedsubm/bin/2400submit -p8
// fill spaces with something so it will store easy
// read in the lines into an output file first
// then the function reads in the same txt file and replaces all the spaces with //
// store in a file
// note each function does one simple thing dont try to make one function to complex
using namespace std;
class Food
{
public:
//1)
Food ();
// sets the values of protein fat fiber and etc to zero
//2)
Food(double p, double fib, double f, double cal , string food);
// this functions adds the parameter passed by value to protein fiber fat calories and the string food_item to the list of lunch
//3)
friend double operator +(const Food& item1, const Food& item2);
// overloads the + operator by adding food item 1 to food item 2
//4)
friend double operator -(const Food& iteam1, const Food& item2);
//overloads the - operator and subtracts food iteam 1 from food item 2
//5)
friend double operator *(const Food& item1, double servings);
// overloads the * operator and multiples food item1 by servings
//6)
friend ostream& operator << (ostream &output, Food& item);
// overloads the << operator and outputs a food item
//7)
friend istream& operator >> (istream& input, Food& item);
// overloads the >> operator and inputs a food item
//8)
friend bool Food operator > (const Food& item1, const Food& item2);
// overloads the > operator returns a bool
//9)
friend bool Food operator ==( const Food& item1, const Food& item2);
// overloads the operator == returns a bool
//10)
void save_lunch (const Food& iteam);
// saves yesturday`s lunch
//11)
void load_file(istream& lunch);
//loads in the file from yesturdays lunch
// not sure if I need a const or not
//12)
double get_protein();
//13)
double get_fiber();
//14)
double get_fat();
//15)
double get_calories();
//16)
string food_item();
private:
string food_item;
double protein, fiber, fat, calories;
};
//1)
Food::Food()
{// initializes everything to zero
protein =0;
fiber = 0;
fat = 0;
calories = 0;
}
//2)
Food::Food(double p, double fib, double f, double cal , string food)
{// this allows me to initialize a predefined iteam
protein = p;
fiber = fib;
fat=f;
calories =cal;
food_item = food)
}
//3)
Food operator +(const Food& item1, const Food& item2)
{// overloads the + operator
Food hold;
hold.protein = item1.protein + item2.protein;
// adds proteins together
hold.fiber = item1.fiber + item2.fiber;
// adds fibers together
hold.fat = item1.fat +iteam2.fat;
// adds fat together
hold.calories = item1.calories + iteam2.calories;
// adds calories together
}
//4)
double operator -(const Food& iteam1, const Food& item2)
{// i am absolutely lost here i was thinking about using an array here but in class you said not to and when i googled this i got a lot of weird stuff
}
//5)
double operator *(const Food& item1, double servings)
{
}
//6)
ostream& operator << (ostream &output, Food& item)
{
Food lunch;
cout << lunch.food_item;
cout << "Protein: " << lunch.protein << endl;
cout << "Fiber: " << lunch.fiber << endl;
cout << "Fat" << lunch.fiber << endl;
cout << "Calories" << lunch.calories << endl;
}
//7)
istream& operator >> (istream& input, Food& item)
{
}
//8)
bool Food operator > (const Food& item1, const Food& item2)
{
// here there is a little confusion here I was thinking about doing a return item1 > item2;
// however I am not getting anything back when I do this
}
//9)
bool Food operator ==( const Food& item1, const Food& item2)
{
// here is the same case as the number 8 funtion
}
//10)
void save_lunch (const Food& iteam)
{
// i was told to open a file golbally by an upperclassman but i think i could do the same thing if i go by reference
// i am also wondering if i should be passing in another parameter here
}
//11)
void load_file(istream& lunch)
{// I was curious if this is what you ment by the load function
istream lunch;
lunch.open();
if(lunch.fail())
{
cout << "file did not open correctly";
return 0;
}
}
//12)
double get_protein()
{
return protein;
}
//13)
double get_fiber()
{
return fiber;
}
//14)
double get_fat()
{
return fat;
}
//15)
double get_calories()
{
return calories;
}
//16)
string food_item()
{
return food_item;
}
|