Advertisement
Tucancitto

Pb08. Componente

May 26th, 2021
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.77 KB | None | 0 0
  1. // ComputerPart.h
  2. #pragma once
  3. #include <string>
  4. class ComputerPart
  5. {
  6. protected:
  7.     float price;
  8.     std::string name;
  9. public:
  10.     ComputerPart();
  11.     ComputerPart(float price, std::string name);
  12.  
  13.     virtual float getPrice() = 0;
  14.     virtual std::string getName() = 0;
  15.  
  16.     ~ComputerPart();
  17. };
  18.  
  19. // ComputerPart.cpp
  20. #include "ComputerPart.h"
  21.  
  22. ComputerPart::ComputerPart() : price(0), name("") {}
  23. ComputerPart::ComputerPart(float price, std::string name) : price(price), name(name) {}
  24. ComputerPart::~ComputerPart() {}
  25.  
  26. // HasButons.h
  27. #pragma once
  28. class HasButtons
  29. {
  30. protected:
  31.     int numberOfButtons;
  32. public:
  33.     HasButtons();
  34.     HasButtons(int);
  35.  
  36.     virtual int getNumberOfButtons();
  37. };
  38.  
  39. // HasButtons.cpp
  40. #include "HasButtons.h"
  41.  
  42. HasButtons::HasButtons() : HasButtons(0) {}
  43.  
  44. HasButtons::HasButtons(int numberOfButtons) :
  45.     numberOfButtons(numberOfButtons) {}
  46.  
  47. int HasButtons::getNumberOfButtons()
  48. {
  49.     return numberOfButtons;
  50. }
  51.  
  52. // HasMemory.h
  53. #pragma once
  54. class HasMemory
  55. {
  56. protected:
  57.     float capacity, frequency;
  58. public:
  59.     HasMemory();
  60.     HasMemory(float, float);
  61.  
  62.     virtual float getCapacity();
  63.     virtual float getFrequency();
  64. };
  65.  
  66. // HasMemory.cpp
  67. #include "HasMemory.h"
  68.  
  69. HasMemory::HasMemory() : HasMemory(0, 0) {}
  70.  
  71. HasMemory::HasMemory(float capacity, float frequency) :
  72.     capacity(capacity), frequency(frequency) {}
  73.  
  74. float HasMemory::getCapacity()
  75. {
  76.     return capacity;
  77. }
  78.  
  79. float HasMemory::getFrequency()
  80. {
  81.     return frequency;
  82. }
  83.  
  84. // HDD.h
  85. #pragma once
  86. #include "ComputerPart.h"
  87. #include "HasMemory.h"
  88.  
  89. class HDD : public ComputerPart, public HasMemory
  90. {
  91. public:
  92.     HDD();
  93.     HDD(float, std::string, float, float);
  94.  
  95.     float getPrice();
  96.     std::string getName();
  97.  
  98.     float getCapacity();
  99.     float getFrequency();
  100. };
  101.  
  102. // HDD.cpp
  103. #include "HDD.h"
  104.  
  105. HDD::HDD() : HDD(0, "", 0, 0) {}
  106.  
  107. HDD::HDD(float price, std::string name, float capacity, float frequency) :
  108.     ComputerPart(price, name), HasMemory(capacity, frequency) {}
  109.  
  110. float HDD::getPrice()
  111. {
  112.     return price;
  113. }
  114.  
  115. std::string HDD::getName()
  116. {
  117.     return name;
  118. }
  119.  
  120. float HDD::getCapacity()
  121. {
  122.     return capacity;
  123. }
  124.  
  125. float HDD::getFrequency()
  126. {
  127.     return frequency;
  128. }
  129.  
  130. // ExternalHDD.h
  131. #pragma once
  132. #include "ComputerPart.h"
  133. #include "HasMemory.h"
  134. #include "HasButtons.h"
  135.  
  136. class ExternalHDD : public ComputerPart, public HasMemory, public HasButtons
  137. {
  138. public:
  139.     ExternalHDD();
  140.     ExternalHDD(float, std::string, float, float, int);
  141.  
  142.     float getPrice();
  143.     std::string getName();
  144.  
  145.     float getCapacity();
  146.     float getFrequency();
  147.     int getNumberOfButtons();
  148. };
  149.  
  150. // ExternalHDD.cpp
  151. #include "ExternalHDD.h"
  152. #include "HDD.h"
  153.  
  154. ExternalHDD::ExternalHDD() : ExternalHDD(0, "", 0, 0, 0) {}
  155.  
  156. ExternalHDD::ExternalHDD(float price, std::string name, float capacity, float frequency, int numberOfButtons) :
  157.     ComputerPart(price, name), HasMemory(capacity, frequency), HasButtons(numberOfButtons) {}
  158.  
  159. float ExternalHDD::getPrice()
  160. {
  161.     return price;
  162. }
  163.  
  164. std::string ExternalHDD::getName()
  165. {
  166.     return name;
  167. }
  168.  
  169. float ExternalHDD::getCapacity()
  170. {
  171.     return HasMemory::getCapacity();
  172. }
  173.  
  174. float ExternalHDD::getFrequency()
  175. {
  176.     return HasMemory::getFrequency();
  177. }
  178.  
  179. int ExternalHDD::getNumberOfButtons()
  180. {
  181.     return HasButtons::getNumberOfButtons();
  182. }
  183.  
  184. // Display.h
  185. #pragma once
  186. #include "ComputerPart.h"
  187. #include "HasButtons.h"
  188.  
  189. class Display : public ComputerPart, public HasButtons
  190. {
  191. public:
  192.     Display();
  193.     Display(float, std::string, int);
  194.  
  195.     float getPrice();
  196.     std::string getName();
  197.  
  198.     int getNumberOfButtons();
  199. };
  200.  
  201. // Display.cpp
  202. #include "Display.h"
  203.  
  204. Display::Display() : Display(0, "", 0) {}
  205.  
  206. Display::Display(float price, std::string name, int numberOfButtons) :
  207.     ComputerPart(price, name), HasButtons(numberOfButtons) {}
  208.  
  209. float Display::getPrice()
  210. {
  211.     return price;
  212. }
  213.  
  214. std::string Display::getName()
  215. {
  216.     return name;
  217. }
  218.  
  219. int Display::getNumberOfButtons()
  220. {
  221.     return HasButtons::getNumberOfButtons();
  222. }
  223.  
  224. // Keyboard.h
  225. #pragma once
  226. #include "ComputerPart.h"
  227. #include "HasButtons.h"
  228.  
  229. class Keyboard : public ComputerPart, public HasButtons
  230. {
  231. public:
  232.     Keyboard();
  233.     Keyboard(float, std::string, int);
  234.  
  235.     float getPrice();
  236.     std::string getName();
  237.  
  238.     int getNumberOfButtons();
  239. };
  240.  
  241. // Keyboard.cpp
  242. #include "Keyboard.h"
  243.  
  244. Keyboard::Keyboard() : Keyboard(0, "", 0) {}
  245.  
  246. Keyboard::Keyboard(float price, std::string name, int numberOfButtons) :
  247.     ComputerPart(price, name), HasButtons(numberOfButtons) {}
  248.  
  249. float Keyboard::getPrice()
  250. {
  251.     return price;
  252. }
  253.  
  254. std::string Keyboard::getName()
  255. {
  256.     return name;
  257. }
  258.  
  259. int Keyboard::getNumberOfButtons()
  260. {
  261.     return HasButtons::getNumberOfButtons();
  262. }
  263.  
  264. // Mouse.h
  265. #pragma once
  266. #include "ComputerPart.h"
  267. #include "HasButtons.h"
  268.  
  269. class Mouse : public ComputerPart, public HasButtons
  270. {
  271. public:
  272.     Mouse();
  273.     Mouse(float, std::string, int);
  274.  
  275.     float getPrice();
  276.     std::string getName();
  277.  
  278.     int getNumberOfButtons();
  279. };
  280.  
  281. // Mouse.cpp
  282. #include "Mouse.h"
  283.  
  284. Mouse::Mouse() : Mouse(0, "", 0) {}
  285.  
  286. Mouse::Mouse(float price, std::string name, int numberOfButtons) :
  287.     ComputerPart(price, name), HasButtons(numberOfButtons) {}
  288.  
  289. float Mouse::getPrice()
  290. {
  291.     return price;
  292. }
  293.  
  294. std::string Mouse::getName()
  295. {
  296.     return name;
  297. }
  298.  
  299. int Mouse::getNumberOfButtons()
  300. {
  301.     return HasButtons::getNumberOfButtons();
  302. }
  303.  
  304. // main
  305. #include <iostream>
  306. #include "ComputerPart.h"
  307. #include "ExternalHDD.h"
  308. #include "HDD.h"
  309. using namespace std;
  310.  
  311. int main()
  312. {
  313.     ExternalHDD obj(500, "HDD", 512, 7700, 2);
  314.     cout << obj.getPrice()<<endl;
  315.     cout << obj.getName()<<endl;
  316.     cout << obj.getCapacity() << endl;
  317.     cout << obj.getFrequency() << endl;
  318.     cout << obj.getNumberOfButtons()<<endl<<endl;
  319.  
  320.  
  321.     HDD obj1(400, "HDD", 256, 7750);
  322.     cout << obj1.getPrice() << endl;
  323.     cout << obj1.getName() << endl;
  324.     cout << obj1.getCapacity() << endl;
  325.     cout << obj1.getFrequency();
  326.     return 0;
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement