Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. //Wprowadzenie:
  8. class A {
  9. private:
  10. int x_A;
  11.  
  12. protected:
  13. int y_A;
  14.  
  15. public:
  16. int z_A;
  17.  
  18. A() : x_A(0), y_A(0), z_A(0)
  19. {
  20. cout << "Lista inicjalizacyjna A zostala wywolana" << endl;
  21. }
  22.  
  23. ~A()
  24. {
  25. cout << "Destruktor A zostal wywolany" << endl;
  26. }
  27.  
  28. void print_values_A()
  29. {
  30. cout << x_A << "\t" << y_A << "\t" << z_A << endl;
  31. }
  32. };
  33. class B : public A
  34. {
  35. private:
  36. int x_B;
  37.  
  38. protected:
  39. int y_B;
  40.  
  41. public:
  42. int z_B;
  43.  
  44. B(int x) : A(), x_B(x), y_B(0), z_B(0)
  45. {
  46. cout << "Lista inicjalizacyjna B zostala wywolana" << endl;
  47. }
  48. ~B()
  49. {
  50. cout << "Destruktor B zostal wywolany" << endl;
  51. }
  52. void print_values_B()
  53. {
  54. cout << y_A << "\t" << z_A << "\t" << x_B << "\t" << y_B << "\t" << z_B << endl;
  55. }
  56. };
  57. class C : protected B
  58. {
  59. private:
  60. int x_C;
  61.  
  62. protected:
  63. int y_C;
  64.  
  65. public:
  66. int z_C;
  67.  
  68. C(int x) : B(x), x_C(x), y_C(0), z_C(1)
  69. {
  70. cout << "Konstruktor C zostal wywolany" << endl;
  71. }
  72. ~C()
  73. {
  74. cout << "Destruktor C zostal wywolany" << endl;
  75. }
  76.  
  77. void print_values_C()
  78. {
  79. cout << y_B << "\t" << z_A << "\t" << y_B << "\t" << z_B << "\t" << x_C << "\t" << y_C << "\t" << z_C << endl;
  80. }
  81. };
  82. class AA
  83. {
  84. protected:
  85. double x, y;
  86.  
  87. public:
  88. AA(double x, double y) : x(x), y(y) { cout << "Konstruktor AA zostal wywolany" << endl; }
  89.  
  90. virtual double compute()
  91. {
  92. return x + y;
  93. }
  94. AA() {}
  95. virtual ~AA() { cout << "Wirtualny destruktor AA zostal wywolany" << endl; }
  96. };
  97. class BB : public AA
  98. {
  99. public:
  100. BB(double x, double y) : AA(x, y) { cout << "Konstruktor BB zostal wywolany" << endl; }
  101.  
  102. virtual double compute()
  103. {
  104. return x * y;
  105. }
  106. BB() {}
  107. virtual ~BB() { cout << "Wirtualny destruktor BB zostal wywolany" << endl; }
  108. };
  109. //Zadania:
  110. class Shape
  111. {
  112. protected:
  113. string name;
  114.  
  115. public:
  116. Shape(string n)
  117. {
  118. this->name = n;
  119. cout << "Konstruktor shape zostal wywolany" << endl;
  120. }
  121. Shape()
  122. {
  123. this->name = "";
  124. cout << "Konstruktor domyslny shape zostal wywolany" << endl;
  125. }
  126. //Shape(string name) : name(name) { cout << "Konstruktor shape zostal wywolany" << endl; }
  127. ~Shape() { cout << "Destruktor shape zostal wywolany" << endl; }
  128. void setName(string name)
  129. {
  130. this->name = name;
  131. }
  132. string getName()
  133. {
  134. return name;
  135. }
  136. virtual double area()
  137. {
  138. return 0.00;
  139. }
  140. };
  141. class Rectangle : public Shape
  142. {
  143. protected:
  144. double width;
  145. double height;
  146.  
  147. public:
  148. Rectangle()
  149. {
  150. this->name = "";
  151. this->width = 0;
  152. this->height = 0;
  153. }
  154. Rectangle(string n, double w, double h)
  155. {
  156. this->name = n;
  157. this->width = w;
  158. this->height = h;
  159. }
  160. ~Rectangle() { cout << "Destruktor rectangle zostal wywolany" << endl; }
  161. double area()
  162. {
  163. return width * height;
  164. }
  165. };
  166. class Square : public Rectangle
  167. {
  168. public:
  169. Square(string n, double w)
  170. {
  171. cout << "Konstruktor square zostal wywolany" << endl;
  172. this->name = n;
  173. this->width = w;
  174. }
  175. virtual double area()
  176. {
  177. return width * width;
  178. }
  179. ~Square() { cout << "Destruktor square zostal wywolany" << endl; }
  180. };
  181.  
  182.  
  183. void Wprowadzenie()
  184. {
  185. //1
  186. /*A a;
  187. a.print_values_A();
  188. B b(5);
  189. b.print_values_B();
  190. C c(7);
  191. c.print_values_C();*/
  192. //2
  193. /*A a1;
  194. B b1(5);
  195. C c1(2);
  196. cout << "Zwykle A" << endl;
  197. a1.print_values_A();
  198. b1.print_values_A();
  199. b1.print_values_B();
  200. c1.print_values_C();
  201.  
  202. A &a2 = b1;
  203. a2.print_values_A();
  204.  
  205. A *a3 = &b1;
  206. cout << "Wskaznik A" << endl;
  207. a3->print_values_A();*/
  208. //3
  209. /*AA aa1(5, 4);
  210. cout << aa1.compute() << endl;
  211. BB bb1(5, 4);
  212. cout << bb1.compute() << endl;
  213. AA& aa2_bb1 = bb1;
  214. cout << aa2_bb1.compute() << endl;
  215. AA* aa3_bb1 = &bb1;
  216. cout << aa3_bb1->compute() << endl;
  217. BB* pbb = new BB(3,1);
  218. AA* paa_bb = new AA(3,1);
  219. cout << "Wskaznik do wirtualnej metody: \n" << pbb->compute() << endl;
  220. cout << paa_bb->compute() << endl;*/
  221. }
  222.  
  223. void Zad1()
  224. {
  225. Shape shape("Kwadrat");
  226. cout << shape.getName() << endl;
  227. //shape.setName("Trojkat");
  228.  
  229.  
  230. Rectangle a("Prostokat",5,8);
  231. Square b1("Kwadrat", 9);
  232. Square *b2 = &b1;
  233. b2->setName("Kwadrat2");
  234. cout << b2->getName() << " : " << b2->area() << endl;
  235. }
  236.  
  237. int main()
  238. {
  239. Wprowadzenie();
  240. Zad1();
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement