#include #include using namespace std; class Food { public: // ------------------------------ Food ------------------------------ /* Default constructor "this" Food is created with a serving size of 114 grams, as well as 3 grams of fats, 13 grams of carbos, and 3 grams of protein. */ Food(); // ------------------------------ Food ----------------------------- /* Constructor: "this" Food is created. The following facts are checked: -- serving_weight, fat_weight, carbo_weight, and protein_weight are all non-negative -- the sum of fat_weight plus carbo_weight plus protein_weight is less than serving_weight If *all* of the above facts are true, then "this" food is created with a serving size of serving_weight, as well as fat_weight grams of fat, carbo_weight grams of carbos, and protein_weight grams of protein Otherwise, there was clearly something wrong with the information given, and so we create "this" food with a serving size of 114 grams, as well as 3 grams of fats, 13 grams of carbos, and 3 grams of protein */ Food(int serving_weight, int fat_weight, int carbo_weight, int protein_weight); // ------------------------ print -------------------------------------- /* Prints out information about "this" food Example 1: Food with 114g serving size, 2g fats, 13g carbos, 3g protein *** Info: s: 114, f: 2, c: 13, p: 3 Example 2: Food with 200g serving size, 1g fats, 8g carbos, 0g protein *** Info: s: 200, f: 1, c: 8, p: 0 */ void print(); // -------------------------- fat_free ---------------------------------- /* Returns true if "this" food contains no fats Otherwise returns false */ bool fat_free(); // ------------------------ high_protein ----------------------------- /* Returns true if "this" food is high in protein (has at least 10 grams of protein in one serving) Otherwise returns false. */ bool high_protein(); // ------------------------- fortify ---------------------------- /* If added_protein is negative, the function does nothing. If added_protein is 0 or positive, the function adds added_protein grams of protein to "this", */ void fortify(int added_protein); // -------------------------- calories ----------------------- /* Returns the number of calories in "this" Food. This can be computed by adding together: fat*9, protein*4, carbos*4 So, for example, if this food has 3g of fat, 2g of protein, and 4g of carbos, then it has: (3*9) + (2*4) + (4*4) calories, i.e. 51 calories (Since the nutrition facts labels allow the manufacturer to round things, the numbers don't always work out right, but for this assignment, you should just assume that it works exactly when you follow the procedure above) */ int calories(); // ------------------------- calories_per_100_grams ------------------------------ /* This function returns the amount of calories "this" food would have in a 100 gram serving. It computes that as follows: 100 number of calories in a regular serving * -------------------- regular_serving_size So, for example, if we have a food that has 51 calories in a 25g serving size then it has: 100 51 * ----- i.e. 204 calories in 100 grams 25 NOTE: You should use your calories function inside of this function. If you don't, 5 points will be deducted from your score */ int calories_per_100_grams(); // ------------------------ combine ------------------------------------ /* Combine two foods to produce a third. e.g. if "this" food has a serving size of 10g, 3g fat, 2g carbos, 1g protein and "other_food" has a serving size of 8g, 2g fat, 4g carbos, 1g protein then the function returns a food that has a serving size of 18g, 5g fat, 6g carbos, 2g protein. NOTE: After performing a combine, "this" and other_food's contents will not have not changed. */ Food combine (Food other_food) const; // ------------------------high_carb------------------------------------- /* Returns true when more than half of the calories in "this" food come from carbohydrates, and false otherwise. */ bool high_carb(void); private: int j_serving_size; int j_total_fat; int j_total_carbs; int j_protein; }; // DO NOT CHANGE THE CODE BELOW THIS LINE (EXCEPT WHERE NOTED FOR EXTRA CREDIT // DO NOT CHANGE THE CODE BELOW THIS LINE (EXCEPT WHERE NOTED FOR EXTRA CREDIT // DO NOT CHANGE THE CODE BELOW THIS LINE (EXCEPT WHERE NOTED FOR EXTRA CREDIT // DO NOT CHANGE THE CODE BELOW THIS LINE (EXCEPT WHERE NOTED FOR EXTRA CREDIT // DO NOT CHANGE THE CODE BELOW THIS LINE (EXCEPT WHERE NOTED FOR EXTRA CREDIT //-------------------------- regular prototypes --------------------- bool should_i_continue(); /* asks user whether to continue or quit */ void success_message(int num); /*prints out how many points you may get */ void yes_or_no (bool x); /* prints yes if x is true, no if x is false */ // EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 // EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 // EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 // EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 // You may write here and below main (look for comment) for extra credit 2 /* This function that two arguments of type Food and returns the one that has more calories per 100 grams. */ Food more_cals_per_100(Food first_food, Food second_food); // --------------------- main ----------------------------- int main() { cout << " ------------------- Part 1 ---------------------" << endl; Food food1; cout << "\nJust created a new food" << endl; cout << "Here's food1\n" << endl; food1.print(); success_message(20); if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Part 2 ---------------------" << endl; Food tuna(56, 1, 0, 15); Food pepsi(355, 0, 41, 0); Food pasta(56, 1, 42, 7); Food food2(53, -2, 4, 8); Food food3(3, 4, 0, 1); cout << "Created 5 new Foods:" << endl << endl; cout << "Tuna: "; tuna.print(); cout << endl; cout << "Pepsi: "; pepsi.print(); cout << endl; cout << "Pasta: "; pasta.print(); cout << endl; cout << "Food 2: "; food2.print(); cout << endl; cout << "Food 3: "; food3.print(); cout << endl; success_message(30); if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Part 3 ---------------------" << endl; cout << "Is Pepsi fat free? "; bool answer; answer = pepsi.fat_free(); yes_or_no (answer); cout << "Is Tuna fat free? "; answer = tuna.fat_free(); yes_or_no (answer); success_message(40); if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Part 4 ---------------------" << endl; cout << "Pepsi: "; pepsi.print(); cout << "Is regular Pepsi high protein? "; answer = pepsi.high_protein(); yes_or_no (answer); cout << endl; cout << "Let's try and fortify the Pepsi: "; pepsi.fortify(5); pepsi.print(); cout << "Is it high protein now?? "; answer = pepsi.high_protein(); yes_or_no (answer); cout << endl; cout << "Let's try and fortify it some more: "; pepsi.fortify(-2); pepsi.print(); cout << "Is it high protein now?? "; answer = pepsi.high_protein(); yes_or_no (answer); cout << endl; cout << "Tuna: "; tuna.print(); cout << "Is the tuna high protein? "; answer = tuna.high_protein(); yes_or_no (answer); cout << endl; success_message(60); if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Part 5 ---------------------" << endl; int cals; cals = pepsi.calories(); cout << cals << " calories in a serving of pepsi."; cout << endl; cals = tuna.calories(); cout << cals << " calories in a serving of tuna."; cout << endl< pasta_cals) cout << "Tuna!"; else cout << "Pasta!"; cout << endl< food5_cals) cout << "Pasta!"; else cout << "food5!"; cout << endl; success_message(80); if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Part 6 ---------------------" << endl; cout << "Let's combine pasta and tuna and see what we get." << endl; Food mac_and_tuna = pasta.combine(tuna); cout << "Mac & Tuna: "; mac_and_tuna.print(); cout << endl << " it has " << mac_and_tuna.calories_per_100_grams() << " calories in 100 grams!\n" << endl; success_message(100); // Remove the begin comment symbol on the previous line to attempt // extra credit 1 if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Extra Credit 1 ---------------------" << endl; cout << "Is Pepsi high in carbos? "; answer = pepsi.high_carb(); yes_or_no(answer); cout << "Is tuna high in carbos? "; answer = tuna.high_carb(); yes_or_no(answer); // Remove the end comment symbol on the next line to attempt // extra credit 1 // Remove the begin comment symbol on the previous line to attempt // extra credit 2 if (should_i_continue() == false) return (EXIT_SUCCESS); cout << " ------------------- Extra Credit 2 ---------------------" << endl; cout << "Printing the thing that has more calories per 100 grams: "; food1 = more_cals_per_100(tuna, pepsi); food1.print(); Food food6(56, 15, 0, 1); cout << endl << "Here's another\n"; food1 = more_cals_per_100(food6, tuna); food1.print(); // Remove the end comment symbol on the next line to attempt // extra credit 2 return (EXIT_SUCCESS); } // Normal function implementations start here // EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 // EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 // EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 // EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 EXTRA CREDIT 2 // You may write here and above main (look for comment) for extra credit 2 /* This function that two arguments of type Food and returns the one that has more calories per 100 grams. */ Food more_cals_per_100(Food first_food, Food second_food) { if ( first_food.calories_per_100_grams() > second_food.calories_per_100_grams() ) return first_food; else return second_food; } //----------------------------- should_i_continue ------------------- /* asks the user whether they want to continue or to quit */ bool should_i_continue() { cout << "\nDo you want to continue (type y for yes)? "; char ans; cin >> ans; cout << endl << endl << endl; if ((ans == 'y') || (ans == 'Y')) return (true); else return (false); } // ------------------ success_message ---------------------- /*prints out how many points you may get */ void success_message(int num) { cout << "\n\n-- If you successfully complete the program to here\n" << "-- You are eligible for a total of " << num << " points." << endl; } // ------------------- yes_or_no --------------------------------- /* prints yes if x is true, no if x is false */ void yes_or_no (bool x) { if (x) cout << "Yes!"; else cout << "No!"; cout << endl; } /* DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT DO NOT CHANGE THE CODE ABOVE THIS LINE EXCEPT FOR EXTRA CREDIT */ // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // YOU WRITE THE CODE IMPLEMENTING THE CLASS FUNCTIONS BELOW THIS LINE // ------------------------------ Food ------------------------------ /* Default constructor "this" Food is created with a serving size of 114 grams, as well as 3 grams of fats, 13 grams of carbos, and 3 grams of protein. */ Food::Food() { j_serving_size = 114; j_total_fat = 3; j_total_carbs = 13; j_protein = 3; } // ------------------------------ Food ----------------------------- /* Constructor: "this" Food is created. The following facts are checked: -- serving_weight, fat_weight, carbo_weight, and protein_weight are all non-negative -- the sum of fat_weight plus carbo_weight plus protein_weight is less than serving_weight If *all* of the above facts are true, then "this" food is created with a serving size of serving_weight, as well as fat_weight grams of fat, carbo_weight grams of carbos, and protein_weight grams of protein Otherwise, there was clearly something wrong with the information given, and so we create "this" food with a serving size of 114 grams, as well as 3 grams of fats, 13 grams of carbos, and 3 grams of protein */ Food::Food(int serving_weight, int fat_weight, int carbo_weight, int protein_weight) { if ( (serving_weight >= 0) && (fat_weight >= 0) && (carbo_weight >= 0) && (protein_weight >= 0) && (serving_weight > (fat_weight + carbo_weight + protein_weight) ) ) { j_serving_size = serving_weight; j_total_fat = fat_weight; j_total_carbs = carbo_weight; j_protein = protein_weight; } else { j_serving_size = 114; j_total_fat = 3; j_total_carbs = 13; j_protein = 3; } } // ------------------------ print -------------------------------------- /* Prints out information about "this" food Example 1: Food with 114g serving size, 2g fats, 13g carbos, 3g protein *** Info: s: 114, f: 2, c: 13, p: 3 Example 2: Food with 200g serving size, 1g fats, 8g carbos, 0g protein *** Info: s: 200, f: 1, c: 8, p: 0 */ void Food::print() { cout<<"Info: s: " <= 10) return true; else return false; } // ------------------------- fortify ---------------------------- /* If added_protein is negative, the function does nothing. If added_protein is 0 or positive, the function adds added_protein grams of protein to "this", */ void Food::fortify(int added_protein) { if(added_protein >= 0) j_protein += added_protein; } // -------------------------- calories ----------------------- /* Returns the number of calories in "this" Food. This can be computed by adding together: fat*9, protein*4, carbos*4 So, for example, if this food has 3g of fat, 2g of protein, and 4g of carbos, then it has: (3*9) + (2*4) + (4*4) calories, i.e. 51 calories (Since the nutrition facts labels allow the manufacturer to round things, the numbers don't always work out right, but for this assignment, you should just assume that it works exactly when you follow the procedure above) */ int Food::calories() { return( (j_total_fat*9) + (j_protein*4) + (j_total_carbs*4) ); } // ------------------------- calories_per_100_grams ------------------------------ /* This function returns the amount of calories "this" food would have in a 100 gram serving. It computes that as follows: 100 number of calories in a regular serving * -------------------- regular_serving_size So, for example, if we have a food that has 51 calories in a 25g serving size then it has: 100 51 * ----- i.e. 204 calories in 100 grams 25 NOTE: You should use your calories function inside of this function. If you don't, 5 points will be deducted from your score */ int Food::calories_per_100_grams() { return ( calories() * (100/double(j_serving_size)) ); } // ------------------------ combine ------------------------------------ /* Combine two foods to produce a third. e.g. if "this" food has a serving size of 10g, 3g fat, 2g carbos, 1g protein and "other_food" has a serving size of 8g, 2g fat, 4g carbos, 1g protein then the function returns a food that has a serving size of 18g, 5g fat, 6g carbos, 2g protein. NOTE: After performing a combine, "this" and other_food's contents will not have not changed. */ Food Food::combine (Food other_food) const { Food bla ( (j_serving_size + other_food.j_serving_size), (j_total_fat + other_food.j_total_fat), (j_total_carbs + other_food.j_total_carbs), (j_protein + other_food.j_protein) ); return bla; } // ------------------------high_carb------------------------------------- /* Returns true when more than half of the calories in "this" food come from carbohydrates, and false otherwise. */ bool Food::high_carb(void) { if ( (j_total_carbs*4) > (calories()/double(2)) ) return true; else return false; }