Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. //============================================================================
  2. // Name : 1stt.cpp
  3. // Author :
  4. // Version :
  5. // Copyright : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <string>
  11. using namespace std;
  12. class C
  13. {
  14. public:
  15. virtual void show_value()=0;
  16. virtual ~C(){};
  17. };
  18. class B : virtual public C
  19. {
  20. char x;
  21. public:
  22. B(){x='s';}
  23. char get_x(){
  24. return x;
  25. }
  26.  
  27. void show_value()
  28. {
  29. cout << this->x << endl;
  30. }
  31.  
  32. };
  33. class A : virtual public C
  34. {
  35. private:
  36. const int a;
  37. string str;
  38.  
  39. static B * ptr;
  40. public:
  41. A(): a(2), str("string"){};
  42. static void b_create()
  43. {
  44. ptr = new B();
  45. }
  46. static void show()
  47. {
  48. cout << "Metoda statyczna: " << ptr-> get_x() << endl;
  49. }
  50. static void b_delete()
  51. {
  52. delete ptr;
  53. }
  54. int get_a() const
  55. {
  56. return a;
  57. }
  58. string get_str() const
  59. {
  60. return str;
  61. }
  62.  
  63. void set_a(int b)
  64. {
  65. // a=b;
  66. }
  67.  
  68. void set_str(string s)
  69. {
  70. str = s;
  71. }
  72.  
  73. void show_value()
  74. {
  75. cout << this->a << ", " << this->str << endl;
  76. }
  77. };
  78.  
  79. class X: public A, public B
  80. {
  81. int x;
  82. public:
  83. X(){x = 255;}
  84. void show_value()
  85. {
  86. cout <<endl<< this->x << endl;
  87. }
  88. };
  89. void set(A **&tab, int size)
  90. {
  91. for(int i=0; i<size; i++)
  92. {
  93. tab[i]->set_str("nowy string");
  94. }
  95. }
  96.  
  97. void set(A tab[], int size)
  98. {
  99. for(int i=0; i<size; i++)
  100. {
  101. tab[i].set_str("stat tablica");
  102. }
  103. }
  104.  
  105. void create(A** &tab, int size)
  106. {
  107. tab = new A*[size];
  108. for(int i=0; i<size; i++)
  109. {
  110. tab[i] = new A();
  111. }
  112. }
  113. void show(A**&tab, int size)
  114. {
  115. for(int i=0; i<size; i++)
  116. {
  117. cout << tab[i]->get_a() << endl;
  118. cout << tab[i]->get_str() << endl;
  119. }
  120. }
  121.  
  122. void show(A tab[], int size)
  123. {
  124. for(int i=0; i<size; i++)
  125. {
  126. cout << tab[i].get_str() << endl;
  127. cout << tab[i].get_a() << endl;
  128. }
  129. }
  130.  
  131. void remove(A ** &tab, int size)
  132. {
  133. for(int i=0; i<size; i++)
  134. {
  135. delete tab[i];
  136. }
  137. delete [] tab;
  138. }
  139. B *A::ptr = NULL;
  140. int main() {
  141. int size = 3;
  142. A::b_create();
  143. //
  144. A::show();
  145.  
  146. A::b_delete();
  147.  
  148. A ** a;
  149. A tab[size];
  150. create(a, size);
  151.  
  152. show(tab,size);
  153. show(a,size);
  154.  
  155. set(a, size);
  156. show(a,size);
  157.  
  158. set(tab,size);
  159. show(tab,size);
  160. cout << "poliformfizm" << endl << endl;
  161. C **tb = new C*[3];
  162.  
  163. tb[0] = new A();
  164. tb[1] = new B();
  165. tb[2] = new X();
  166.  
  167.  
  168. for(int i=0;i<size;i++)
  169. {
  170. tb[i]->show_value();
  171. }
  172. cout << "poliformfizm" << endl << endl;
  173. C* pointer = NULL;
  174. pointer = new A();
  175. pointer->show_value();
  176. pointer = new B();
  177. pointer->show_value();
  178. pointer = new X();
  179. pointer->show_value();
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement