Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <iostream>
  2. #include "Box.h"
  3. int Box::nr = 0;
  4. int Box::nrkopii = 0;
  5.  
  6. int main()
  7. {
  8. cout << "*** Instancje tworzone dynamicznie:\n";
  9. const int ile = 10;
  10. Box* aBox[ile] = { 0 };
  11. aBox[0] = new Box;
  12. aBox[1] = new Box();
  13. aBox[2] = new Box("box2", 2.5, 1, .8);
  14. aBox[3] = new Box("box3", 3.1);
  15. aBox[4] = new Box("box4");
  16. aBox[5] = new Box(*aBox[1]);
  17. cout << "\n*** Instancje tworzone statycznie:\n";
  18. Box b6;
  19. Box b7;
  20. b7 = Box();
  21. Box b8("box8", 1.25, 1, 6.4);
  22. Box b9("box9", 9.1);
  23. Box b10("box10");
  24. Box b11 = b10;
  25. Box b12(b10);
  26. Box b13;
  27. b13 = Box("box13");
  28. b12 = b6;
  29. b13 = Box();
  30. cout << endl;
  31. for (int i = 0; i < ile; i++)
  32. {
  33. if (aBox[i])
  34. {
  35. aBox[i]->printVolume();
  36. }
  37. }
  38. cout << endl;
  39. b6.printVolume(); b7.printVolume();
  40. b8.printVolume(); b9.printVolume();
  41. b10.printVolume(); b11.printVolume();
  42. b12.printVolume(); b13.printVolume();
  43. cout << "\n*** Czyszczenie instancji dynamicznych:\n";
  44. for (int i = 0; i < ile; i++) if (aBox[i]) delete aBox[i];
  45. cout << "\n*** Koniec main()\n";
  46. }
  47.  
  48. /*
  49. 1) Ile razy uruchomiony został konstruktor klasy Box?
  50. 2) Ile razy uruchomiony został konstruktor kopiujący klasy Box?
  51. 3) Ile razy uruchomiona została funkcja printVolume() klasy Box?
  52. */
  53.  
  54. ///////////////////////////////////////////////////////////////////////////////////////////////
  55.  
  56. #pragma once
  57. #include <iostream>
  58. #include <string>
  59. #include <cstdlib>
  60.  
  61. using namespace std;
  62.  
  63. class Box
  64. {
  65. private:
  66. string name;
  67. float length;
  68. float width;
  69. float height;
  70. public:
  71. Box();
  72. Box(string name);
  73. Box(string name, float length);
  74. Box(string name, float length, float width, float heigth);
  75. Box(Box & box);
  76.  
  77. void printVolume();
  78. static int nr;
  79. static int nrkopii;
  80. };
  81.  
  82. //////////////////////////////////////////////////////////////
  83.  
  84. #include "Box.h"
  85.  
  86. Box::Box()
  87. {
  88. this->name = "i";
  89. this->name.append(to_string(nr));
  90. this->name.append("-");
  91. this->name.append("def");
  92. this->length = 1;
  93. this->width = 1;
  94. this->height = 1;
  95. cout << "K: tworze " << this->name << endl;
  96. nr++;
  97. }
  98.  
  99. Box::Box(string name)
  100. {
  101. this->name = "i";
  102. this->name.append(to_string(nr));
  103. this->name.append("-");
  104. this->name.append(name);
  105. this->length = 1;
  106. this->width = 1;
  107. this->height = 1;
  108. cout << "K: tworze " << this->name << endl;
  109. nr++;
  110. }
  111.  
  112. Box::Box(string name, float length)
  113. {
  114. this->name = "i";
  115. this->name.append(to_string(nr));
  116. this->name.append("-");
  117. this->name.append(name);
  118. this->length = length;
  119. this->width = 1;
  120. this->height = 1;
  121. cout << "K: tworze " << this->name << endl;
  122. nr++;
  123. }
  124.  
  125. Box::Box(string name, float length, float width, float height)
  126. {
  127. this->name = "i";
  128. this->name.append(to_string(nr));
  129. this->name.append("-");
  130. this->name.append(name);
  131. this->length = length;
  132. this->width = width;
  133. this->height = height;
  134. cout << "K: tworze " << this->name<< endl;
  135. nr++;
  136. }
  137.  
  138. Box::Box(Box & box)
  139. {
  140. this->name = "i";
  141. this->name.append(to_string(nr));
  142. this->name.append("-");
  143. this->name.append("kopia");
  144. this->name.append(to_string(nrkopii));
  145. this->name.append("_");
  146. this->name.append(box.name);
  147. this->length = box.length;
  148. this->width = box.width;
  149. this->height = box.height;
  150. cout << "KK: tworze " << name << endl;
  151. nrkopii++;
  152. }
  153.  
  154. void Box::printVolume()
  155. {
  156. cout <<"V(" << name << ")= " << length * width * height << endl;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement