Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1.  
  2. // Program name: Lab3a
  3. //
  4. // Description: This program prints macro nutrional data.
  5. //
  6. // What's on your mind about this lab? This exercise gives me an
  7. // opportunity to learn how to use classes.
  8. //
  9. // Author: Aaron Rosales
  10. //
  11. // Date: 02/19/2020
  12. //
  13. // IDE Used: visual studio 2019
  14. //
  15.  
  16. // Note: DO NOT remove these include statements.
  17.  
  18. #include <iostream>
  19. #include <iomanip>
  20. #include <string>
  21.  
  22. using namespace std;
  23.  
  24. class NutritionData {
  25. private:
  26. string foodName;
  27. int servingSize;
  28. double calFromCarb;
  29. double calFromFat;
  30. double calfromProtein;
  31. public:
  32. NutritionData();
  33. // Gets the food name and sets it to a string.
  34. void setFoodName(string);
  35. // Gets the serving size and sets it to a int.
  36. void setServingSize(int);
  37. // Gets the calories from carbs and sets it to a double.
  38. void setCalFromCarb(double);
  39. // Gets the calories from fat and sets it to a double.
  40. void setCalFromFat(double);
  41. // Gets the calories from protein and sets it to a double.
  42. void setCalFromProtein(double);
  43.  
  44. // Returns the food name.
  45. const string getFoodName();
  46. // Returns the serving size.
  47. const int getServingSize();
  48. // Returns the calories from carbs.
  49. const double getCalFromCarb();
  50. // Returns the calories from fat.
  51. const double getCalFromFat();
  52. // Returns the calories from protein.
  53. const double getCalFromProtein();
  54. // Returns the total calories per serving.
  55. const double getCaloriesPerServing();
  56.  
  57. };
  58.  
  59. NutritionData::NutritionData() {
  60. // Initializes all of variables that may be used in the class.
  61. string foodName = "";
  62. int servingSize = 0;
  63. double calFromCarb = 0.0;
  64. double calFromFat = 0.0;
  65. double calfromProtein = 0.0;
  66. }
  67. // Sets the food name.
  68. void NutritionData::setFoodName(string userFoodName) {
  69. foodName = userFoodName;
  70. }
  71. // Gets the serving size and sets it to an int.
  72. void NutritionData::setServingSize(int size) {
  73. servingSize = size;
  74. }
  75. // Gets the calories from carbs and sets it to a double.
  76. void NutritionData::setCalFromCarb(double carbMacro) {
  77. calFromCarb = carbMacro;
  78. }
  79. // Gets the calories from fat and sets it to a double.
  80. void NutritionData::setCalFromFat(double fatMacro) {
  81. calFromFat = fatMacro;
  82. }
  83. // Gets the calories from protein and sets it to a double.
  84. void NutritionData::setCalFromProtein(double bodyBuildersBestFriend) {
  85. calFromCarb = bodyBuildersBestFriend;
  86. }
  87.  
  88. // Returns the food name.
  89. const string NutritionData::getFoodName() {
  90. return foodName;
  91. }
  92. // Returns the serving size.
  93. const int NutritionData::getServingSize() {
  94. return servingSize;
  95. }
  96. // Returns the calories from carbs.
  97. const double NutritionData::getCalFromCarb() {
  98. return calFromCarb;
  99. }
  100. // Returns the calories from fat.
  101. const double NutritionData::getCalFromFat() {
  102. return calFromFat;
  103. }
  104. // Returns the calories from protein.
  105. const double NutritionData::getCalFromProtein() {
  106. return calfromProtein;
  107. }
  108. // Returns the total calories per serving.
  109. const double NutritionData::getCaloriesPerServing() {
  110. return calFromCarb + calFromFat + calfromProtein;
  111. }
  112.  
  113. // Prototype for a function that prints out the data.
  114. void printNutritionData(const NutritionData&);
  115.  
  116. int main() {
  117. // Next 5 lines would be the "user" input.
  118. string foodName = "Bread pita whole wheat";
  119. int servingSize = 64;
  120. double calFromCarb = 134.0;
  121. double calFromFat = 14.0;
  122. double calfromProtein = 22.6;
  123. // Defines an instance of the class.
  124. NutritionData pita;
  125. // Calls upon the functions that set up the pita bread nutrition facts.
  126. pita.setFoodName(foodName);
  127. pita.setServingSize(servingSize);
  128. pita.setCalFromCarb(calFromCarb);
  129. pita.setCalFromFat(calFromFat);
  130. pita.setCalFromProtein(calfromProtein);
  131. // Calls upon the function to print out the data.
  132. printNutritionData(pita);
  133. return 0;
  134. }
  135.  
  136. // Remove the function template below for the labs not requiring functions.
  137. //************************************************************************
  138. //* Function name: printNutritionData
  139. //*
  140. //* This function prints out the nutrion data.
  141. //*
  142. //* Parameters:
  143. //* printableData - is a pass through reference that accesses the data.
  144. //*
  145. //* Returns:
  146. //*
  147. //* void
  148. //*
  149. //************************************************************************
  150.  
  151.  
  152. void printNutritionData(const NutritionData& userData) {
  153. cout << fixed << setprecision(1);
  154. cout << "Food Name: "
  155. << userData.getFoodName() << endl;
  156. cout << "Serving Size: "
  157. << userData.getServingSize()
  158. << " grams." << endl;
  159. cout << "Calories Per Serving: "
  160. << userData.getCaloriesPerServing()
  161. << " grams." << endl;
  162. cout << "Calories From Carb: "
  163. << userData.getCalFromCarb()
  164. << " grams." << endl;
  165. cout << "Calories From Fat: "
  166. << userData.getCalFromProtein()
  167. << " grams." << endl;
  168. cout << "Calories From Protein: "
  169. << userData.getCaloriesPerServing()
  170. << " grams." << endl;
  171. }
  172.  
  173. /*
  174. Copy output of this program below this line.
  175. --------------------------------------------
  176.  
  177. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement