Advertisement
Jopa322

Laba 4 Update

Nov 12th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "iostream"
  3. using namespace std;
  4. const int Count = 6;
  5. class TMyClass
  6. {
  7. private:
  8. int c;
  9. unsigned int *d;
  10. float arr[Count];
  11. char *mass;
  12. int q;
  13.  
  14. public:
  15. TMyClass(int cc = 3, float arrarr = 3.1, char massmass = 'w', int qq = 5, unsigned int dd = 10);
  16. TMyClass(TMyClass &obj);
  17. ~TMyClass();
  18. void input();
  19. void output();
  20. friend void input_F(TMyClass&);
  21. };
  22.  
  23.  
  24. TMyClass::TMyClass(int cc, float arrarr, char massmass, int qq, unsigned int dd)
  25. {
  26. q = qq;
  27. mass = new char[q];
  28. for (int i = 0; i < q; i++)
  29. mass[i] = massmass;
  30. c = cc;
  31. d = new unsigned int;
  32. *d = dd;
  33. for (int i = 0; i < Count; i++)
  34. {
  35. arr[i] = arrarr;
  36. }
  37.  
  38. }
  39. TMyClass::TMyClass(TMyClass &obj)
  40. {
  41. q = obj.q;
  42. mass = new char[q];
  43. for (int i = 0; i < q; i++)
  44. mass[i] = obj.mass[i];
  45. c = obj.c;
  46. d = new unsigned int;
  47. *d = *obj.d;
  48. for (int i = 0; i < Count; i++)
  49. {
  50. arr[i] = obj.arr[i];
  51. }
  52.  
  53. }
  54. TMyClass::~TMyClass()
  55. {
  56.  
  57. delete[]mass;
  58. delete d;
  59. }
  60.  
  61. void TMyClass::input()
  62. {
  63. cout << "Введите переменную(int)" << endl;
  64. cin >> c;
  65. cout << "Введите статический массив (float)" << endl;
  66. for (int i = 0; i < Count; i++)
  67. {
  68. cout << "arr[" << i + 1 << "]=";
  69. cin >> arr[i];
  70. }
  71. cout << "Введите количество элементов дин.массива" << endl;
  72. cout << "q= ";
  73. cin >> q;
  74. delete[]mass;
  75. mass = new char[q];
  76. cout << "Введите элементы динамического массива (char)" << endl;
  77. for (int i = 0; i < q; i++)
  78. {
  79. cout << "mass[" << i + 1 << "]=";
  80. cin >> mass[i];
  81. }
  82. cout << endl;
  83. cout << "Введите указатель *b (unsigned int)" << endl;
  84. cin >> *d;
  85. }
  86.  
  87. void input_F(TMyClass&obj)
  88. {
  89. cout << "Введите переменную (int)" << endl;
  90. cin >> obj.c;
  91. cout << "Введите статический массив (float)" << endl;
  92. for (int i = 0; i < Count; i++)
  93. {
  94. cout << "arr[" << i + 1 << "]= ";
  95. cin >> obj.arr[i];
  96. }
  97. cout << "Введите количество элементов дин.массива" << endl;
  98. cout << "q= ";
  99. cin >> obj.q;
  100. delete[]obj.mass;
  101. obj.mass = new char[obj.q];
  102. cout << "Введите элементы динамического массива(char)" << endl;
  103. for (int i = 0; i < obj.q; i++)
  104. {
  105. cout << "mass[" << i + 1 << "]= ";
  106. cin >> obj.mass[i];
  107. }
  108. cout << endl;
  109. cout << "Введите указатель *b (unsigned int)" << endl;
  110. cin >> *obj.d;
  111. }
  112.  
  113. void TMyClass::output()
  114. {
  115. cout << "Вывод значений:" << endl;
  116. cout << "Переменная типа int:" << c << endl;
  117. cout << "Указатель *b:" << *d << endl;
  118. cout << "Статический массив c количеством элементов " << Count << endl;
  119. for (int i = 0; i < Count; i++)
  120. {
  121. cout << arr[i] << " ";
  122. }
  123. cout << endl;
  124. cout << "Дин.массив mass[" << q << "]" << endl;
  125. for (int i = 0; i < q; i++)
  126. {
  127. cout << mass[i] << " ";
  128. }
  129. cout << endl;
  130. }
  131.  
  132. class TChild :public TMyClass
  133. {
  134. public:
  135. int y;
  136. char *t;
  137. double m;
  138. TChild(int cc = 3, float arrarr = 3.1, char massmass = 'w', int qq = 5, unsigned int dd = 10, int yy = 4, char tt = 's', double mm = 100.23);
  139. TChild(TMyClass &obj, TChild &object);
  140. void output();
  141. };
  142.  
  143. TChild::TChild(int cc, float arrarr, char massmass, int qq, unsigned int dd, int yy, char tt, double mm) :TMyClass(cc, arrarr, massmass, qq, dd)
  144. {
  145. y = yy;
  146. m = mm;
  147. t = new char;
  148. *t = tt;
  149. }
  150.  
  151. TChild::TChild(TMyClass &obj, TChild &object) :TMyClass(obj)
  152. {
  153. y = object.y;
  154. t = new char;
  155. *t = *object.t;
  156. m = object.m;
  157. }
  158.  
  159. void TChild::output()
  160. {
  161. TMyClass::output();
  162. cout << "Переменная y типа int: ";
  163. cout << y << endl;
  164. cout << "Указатель t типа char: ";
  165. cout << *t << endl;
  166. cout << "Переменная m типа double: ";
  167. cout << m << endl;
  168. }
  169.  
  170.  
  171. int main()
  172. {
  173. setlocale(LC_ALL, "Russian");
  174. cout << "Объекты TMyClass" << endl;
  175. TMyClass a(3, 2.1, 'g', 4, 13);
  176. cout << "Объект a - оригинальный" << endl;
  177. a.output();
  178. system("pause");
  179. TMyClass b(a);
  180. TMyClass *c;
  181. c = new TMyClass;
  182. cout << "Объект b(a) - копия объекта a" << endl;
  183. b.output();
  184. system("pause");
  185. cout << "Объект c" << endl;
  186. c->output();
  187. system("pause");
  188. cout << endl;
  189. cout << "Объекты TChild" << endl;
  190. TChild d(3, 2.1, 'g', 4, 13, 15, 'j', 50.21);
  191. cout << "Объект d - оригинальный:" << endl;
  192. cout << "Cобственный метод вывода:" << endl;
  193. d.output();
  194. system("pause");
  195. cout << "Унаследованный метод вывода:" << endl;
  196. d.TMyClass::output();
  197. TChild f(d);
  198. cout << "Объект f(d) - копия объекта d:" << endl;
  199. cout << "Cобственный метод вывода:" << endl;
  200. f.output();
  201. system("pause");
  202. cout << "Унаследованный метод вывода:" << endl;
  203. f.TMyClass::output();
  204. TChild *g;
  205. g = new TChild;
  206. cout << "Объект c:" << endl;
  207. cout << "Cобственный метод вывода:" << endl;
  208. g->output();
  209. system("pause");
  210. cout << "Унаследованный метод вывода:" << endl;
  211. g->TMyClass::output();
  212. delete g;
  213. system("pause");
  214. cout << "Проверка наследования" << endl;
  215. TChild h;
  216. input_F(h);
  217. h.output();
  218.  
  219. system("pause");
  220. return 0;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement