jaredec18

Untitled

Sep 25th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. Code to Copy:
  2.  
  3. ItemToPurchase.h:
  4.  
  5. #pragma once
  6.  
  7. #ifndef ITEMTOPURCHASE_H_INCLUDED
  8.  
  9. #define ITEMTOPURCHASE_H_INCLUDED
  10.  
  11. #include<string>
  12.  
  13. #include <iostream>
  14.  
  15. using namespace std;
  16.  
  17. class ItemToPurchase
  18.  
  19. {
  20.  
  21. public:
  22.  
  23. //Declaration of default constructor
  24.  
  25. ItemToPurchase();
  26.  
  27. //Declaration of SetName function
  28.  
  29. void SetName(string ItemName);
  30.  
  31. //Declaration of SetPrice function
  32.  
  33. void SetPrice(int itemPrice);
  34.  
  35. //Declaration of SetQuantity function
  36.  
  37. void SetQuantity(int itemQuantity);
  38.  
  39. //Declaration of GetName function
  40.  
  41. string GetName();
  42.  
  43. //Declaration of GetPrice function
  44.  
  45. int GetPrice();
  46.  
  47. //Declaration of GetQuantity function
  48.  
  49. int GetQuantity();
  50.  
  51. private:
  52.  
  53. //Declaration of itemName as
  54.  
  55. //type of string
  56.  
  57. string itemName;
  58.  
  59. //Declaration of itemPrice as
  60.  
  61. //type of integer
  62.  
  63. int itemPrice;
  64.  
  65. //Declaration of itemQuantity as
  66.  
  67. //type of integer
  68.  
  69. int itemQuantity;
  70.  
  71. };
  72.  
  73. #endif
  74.  
  75. ItemToPurchase.cpp:
  76.  
  77. #include <iostream>
  78.  
  79. #include <string>
  80.  
  81. #include "ItemToPurchase.h"
  82.  
  83. using namespace std;
  84.  
  85. //Implementation of default constructor
  86.  
  87. ItemToPurchase::ItemToPurchase()
  88.  
  89. {
  90.  
  91. itemName = "none";
  92.  
  93. itemPrice = 0;
  94.  
  95. itemQuantity = 0;
  96.  
  97. }
  98.  
  99. //Implementation of SetName function
  100.  
  101. void ItemToPurchase::SetName(string name)
  102.  
  103. {
  104.  
  105. itemName = name;
  106.  
  107. }
  108.  
  109. //Implementation of SetPrice function
  110.  
  111. void ItemToPurchase::SetPrice(int itemPrice)
  112.  
  113. {
  114.  
  115. this->itemPrice = itemPrice;
  116.  
  117. }
  118.  
  119. //Implementation of SetQuantity function
  120.  
  121. void ItemToPurchase::SetQuantity(int itemQuantity)
  122.  
  123. {
  124.  
  125. this->itemQuantity = itemQuantity;
  126.  
  127. }
  128.  
  129. //Implementation of GetName function
  130.  
  131. string ItemToPurchase::GetName()
  132.  
  133. {
  134.  
  135. return itemName;
  136.  
  137. }
  138.  
  139. //Implementation of GetPrice function
  140.  
  141. int ItemToPurchase::GetPrice()
  142.  
  143. {
  144.  
  145. return itemPrice;
  146.  
  147. }
  148.  
  149. //Implementation of GetQuantity function
  150.  
  151. int ItemToPurchase::GetQuantity()
  152.  
  153. {
  154.  
  155. return itemQuantity;
  156.  
  157. }
  158.  
  159. main.cpp:
  160.  
  161. #include<iostream>
  162.  
  163. #include<string>
  164.  
  165. #include "ItemToPurchase.h"
  166.  
  167. using namespace std;
  168.  
  169. int main()
  170.  
  171. {
  172.  
  173. //Declaration of ItemToPurchase class objects
  174.  
  175. ItemToPurchase item1Cart, item2Cart;
  176.  
  177. string itemName;
  178.  
  179. //create a variable names like itemPrice
  180.  
  181. //itemQuantity,totalCost as type of integer
  182.  
  183. int itemPrice;
  184.  
  185. int itemQuantity;
  186.  
  187. int totalCost = 0;
  188.  
  189. //Display statement for Item1
  190.  
  191. cout << "Item 1:" << endl;
  192.  
  193. cout << "Enter the item name : " << endl;
  194.  
  195. //call the getline function
  196.  
  197. getline(cin, itemName);
  198.  
  199. //Display statememt
  200.  
  201. cout << "Enter the item price : " << endl;
  202.  
  203. cin >> itemPrice;
  204.  
  205. cout << "Enter the item quantity : " << endl;
  206.  
  207. cin >> itemQuantity;
  208.  
  209. item1Cart.SetName(itemName);
  210.  
  211. item1Cart.SetPrice(itemPrice);
  212.  
  213. item1Cart.SetQuantity(itemQuantity);
  214.  
  215. //call cin.ignore() function
  216.  
  217. cin.ignore();
  218.  
  219. //Display statement for Item 2
  220.  
  221. cout << endl;
  222.  
  223. cout << "Item 2:" << endl;
  224.  
  225. cout << "Enter the item name : " << endl;
  226.  
  227. getline(cin, itemName);
  228.  
  229. cout << "Enter the item price : " << endl;
  230.  
  231. cin >> itemPrice;
  232.  
  233. cout << "Enter the item quantity : " << endl;
  234.  
  235. cin >> itemQuantity;
  236.  
  237. item2Cart.SetName(itemName);
  238.  
  239. item2Cart.SetPrice(itemPrice);
  240.  
  241. item2Cart.SetQuantity(itemQuantity);
  242.  
  243. //Display statement
  244.  
  245. cout << "TOTAL COST : " << endl;
  246.  
  247. cout << item1Cart.GetName() << " " << item1Cart.GetQuantity() << " @ $" << item1Cart.GetPrice() << " = " << (item1Cart.GetQuantity()*item1Cart.GetPrice()) << endl;
  248.  
  249. cout << item2Cart.GetName() << " " << item2Cart.GetQuantity() << " @ $" << item2Cart.GetPrice() << " = " << (item2Cart.GetQuantity()*item2Cart.GetPrice()) << endl;
  250.  
  251. totalCost = (item1Cart.GetQuantity()*item1Cart.GetPrice()) + (item2Cart.GetQuantity()*item2Cart.GetPrice());
  252.  
  253. cout << endl;
  254.  
  255. cout << "Total : $" << totalCost << endl;
  256.  
  257. return 0;
  258.  
  259. }
Advertisement
Add Comment
Please, Sign In to add comment