Advertisement
Guest User

seminar10_1040

a guest
Dec 10th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class VehiculAcvatic
  6. {
  7. private:
  8. int greutate;
  9.  
  10. protected:
  11. string nume;
  12.  
  13. public:
  14. int getGreutate()
  15. {
  16. return greutate;
  17. }
  18.  
  19. VehiculAcvatic()
  20. {
  21. greutate = 0;
  22. }
  23.  
  24. VehiculAcvatic(int greutate)
  25. {
  26. if (greutate >= 0)
  27. {
  28. this->greutate = greutate;
  29. }
  30. else
  31. {
  32. throw 505;
  33. }
  34. }
  35. };
  36.  
  37. class Bicicleta
  38. {
  39. private:
  40. string producator;
  41.  
  42. protected:
  43. string nume;
  44.  
  45. public:
  46. Bicicleta()
  47. {
  48. producator = "Necunoscut";
  49. }
  50.  
  51.  
  52. Bicicleta(string producator)
  53. {
  54. this->producator = producator;
  55. }
  56.  
  57. string getProducator()
  58. {
  59. if (producator.length() > 0)
  60. {
  61. return producator;
  62. }
  63.  
  64. throw exception("producator este invalid!");
  65. }
  66.  
  67. virtual void Start()
  68. {
  69. cout << "Bicicleta s-a pus in miscare" << endl;
  70. }
  71. };
  72.  
  73. class Hidrobicicleta : public VehiculAcvatic, public Bicicleta
  74. {
  75. public:
  76. Hidrobicicleta()
  77. {
  78.  
  79. }
  80.  
  81. Hidrobicicleta(string producator, int greutate) : Bicicleta(producator), VehiculAcvatic(greutate)
  82. {
  83. VehiculAcvatic::nume = "hidrobicicleta";
  84. }
  85.  
  86. void Start() // nu trb sa mai pun virtual deoarece in baza ei este virtual (in bicicleta)
  87. {
  88. cout << "Hidrobicicleta s-a pus in miscare" << endl;
  89. }
  90. };
  91.  
  92. int main()
  93. {
  94. Hidrobicicleta h1;
  95. cout << h1.getProducator() << endl;
  96.  
  97. Hidrobicicleta h2("Explorer", 50);
  98. cout << h2.getGreutate() << endl;
  99.  
  100. Bicicleta b1;
  101. b1.Start();
  102.  
  103. h1.Start();
  104.  
  105. b1 = h1;
  106. b1.Start();
  107.  
  108. Bicicleta *pb = &h1;
  109. pb->Start();
  110.  
  111. Bicicleta b2("");
  112. try
  113. {
  114. VehiculAcvatic v(-2);
  115. b2.getProducator();
  116. }
  117. catch (exception e)
  118. {
  119. cout << e.what();
  120. }
  121. catch (int x)
  122. {
  123. cout << x;
  124. }
  125. catch (...)
  126. {
  127.  
  128. }
  129. }
  130.  
  131. /* THE DIAMOND OF DEATH
  132.  
  133. vehicul
  134. /\
  135. / \
  136. / \
  137. / \
  138. / \
  139. / \
  140. / \
  141. / \
  142. / \
  143. / \
  144. / \
  145. / \
  146. vehicul acvatic bicicleta
  147. \ /
  148. \ /
  149. \ /
  150. \ /
  151. \ /
  152. \ /
  153. \ /
  154. \ /
  155. \ /
  156. \ /
  157. \ /
  158. \/
  159. hidrobicicleta
  160. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement