fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Watch {
  6. public:
  7. void SetHours(int watchHours) {
  8. hours = watchHours;
  9. }
  10.  
  11. void SetMins(int watchMins) {
  12. mins = watchMins;
  13. }
  14.  
  15. virtual void PrintItem() {
  16. cout << hours << ":" << mins << endl;
  17. }
  18.  
  19. protected:
  20. int hours;
  21. int mins;
  22. };
  23.  
  24. class SmartWatch : public Watch {
  25. public:
  26. void SetPercentage(int watchPercentage) {
  27. batteryPercentage = watchPercentage;
  28. }
  29.  
  30. void PrintItem() {
  31. cout << hours << ":" << mins << " " << batteryPercentage << "%" << endl;
  32. }
  33.  
  34. private:
  35. int batteryPercentage;
  36. };
  37.  
  38. int main() {
  39. SmartWatch* watch1;
  40. SmartWatch* watch2;
  41. Watch* watch3;
  42.  
  43. vector<Watch*> watchList;
  44. unsigned int i;
  45.  
  46. watch1 = new SmartWatch();
  47. watch1->SetHours(3);
  48. watch1->SetMins(50);
  49. watch1->SetPercentage(65);
  50.  
  51. watch2 = new SmartWatch();
  52. watch2->SetHours(1);
  53. watch2->SetMins(21);
  54. watch2->SetPercentage(96);
  55.  
  56. watch3 = new Watch();
  57. watch3->SetHours(2);
  58. watch3->SetMins(12);
  59.  
  60. watchList.push_back(watch2);
  61. watchList.push_back(watch1);
  62. watchList.push_back(watch3);
  63.  
  64. for (i = 0; i < watchList.size(); ++i) {
  65. watchList.at(i)->PrintItem();
  66. }
  67.  
  68. return 0;
  69. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
1:21 96%
3:50 65%
2:12