Slavik9510

Untitled

Dec 15th, 2022
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. main.cpp
  2. //--------------------------------
  3. #include "parentClass.h"
  4. #include "ChildClass.h"
  5. #include <vector>
  6. int main()
  7. {
  8. std::vector<ParentClass*> objVector;
  9. for (int i = 0; i < 3; i++)
  10. {
  11. objVector.push_back(new ParentClass(i));
  12. }
  13.  
  14. for (int i = 0; i < 2; i++)
  15. {
  16. objVector.push_back(new ChildClass(0, i + 3));
  17. }
  18. int choise;
  19. std::cout << "Enter id: ";
  20. std::cin >> choise;
  21. if (choise < 0 || choise >4)
  22. {
  23. exit(1);
  24. }
  25.  
  26. for (auto it = objVector.begin(); it != objVector.end(); it++)
  27. {
  28. if ((*it)->GetId() == choise) //пошук в векторі
  29. {
  30. (*it)->ReadFromFile("pathToName.txt"); //зчитуємо імя з файлу
  31. (*it)->Print();
  32. std::cout << "Enter new name: ";
  33. std::string tmpName;
  34. std::cin >> tmpName;
  35. (*it)->SetName(tmpName); //міняємо його
  36. }
  37. }
  38. std::ofstream outfile;
  39. outfile.open("results.txt");
  40. for (auto& it : objVector) //вивід в файл і на екран
  41. {
  42. it->Print();
  43. outfile << *it;
  44. }
  45. outfile.close();
  46. return 0;
  47. }
  48. //--------------------------------
  49. ChildClass.h
  50. //--------------------------------
  51. #pragma once
  52. #include "parentClass.h"
  53. #include <fstream>
  54. #include <iostream>
  55. class ChildClass : public ParentClass
  56. {
  57. protected:
  58. int newField;//нове поле відносно батьківського
  59. public:
  60. ChildClass()
  61. {
  62. newField = 0;
  63. }
  64. ChildClass(int value, int id)
  65. {
  66. name = "Empty";
  67. this->id = new int(id);
  68. newField = value;
  69. }
  70. ChildClass(const ChildClass& that)
  71. {
  72. this->name = that.name;
  73. this->id = new int(*that.id);//id вказіник і шоб скопіювати значення а не адресу робимо так
  74. this->newField = that.newField;
  75. }
  76. ~ChildClass()
  77. {
  78. delete id;
  79. std::cout << "Object " << name << " has been destructed\n";
  80. }
  81. void Print()
  82. {
  83. std::cout << "Name: " << name << " ID: " << *id << "\tSometring new..." << std::endl;
  84. }
  85. friend std::ostream& operator << (std::ostream& os, const ChildClass& child)
  86. {
  87. //метод відображення обєкту в файл + перегружений оператор(бінарний)
  88. return os << "Name: " << child.name << " ID: " << *child.id << "\tSometring new..." << std::endl;;
  89. }
  90.  
  91. //Методи редагування змісту обєкту???
  92. void SetNewField(int value)
  93. {
  94. newField = value;
  95. }
  96. int GetNewField() //новий метод відносно батьківстького
  97. {
  98. return newField;
  99. }
  100. };
  101. //--------------------------------
  102. parentClass.h
  103. //--------------------------------
  104. #pragma once
  105. #include "abstractClass.h"
  106. #include <string>
  107. #include <fstream>
  108. #include <iostream>
  109. class ParentClass :public AbstractClass
  110. {
  111. protected:
  112. std::string name; //назва обєкту
  113. int* id; //код обєкту
  114. public:
  115. ParentClass()
  116. {
  117. id = new int(0);
  118. }
  119. ParentClass(int value)
  120. {
  121. name = "Empty";
  122. id = new int(value);
  123. }
  124. ParentClass(const ParentClass& that)
  125. {
  126. this->name = that.name;
  127. this->id = new int(*that.id);//id вказіник і шоб скопіювати значення а не адресу робимо так
  128. }
  129. ~ParentClass()
  130. {
  131. delete id;
  132. std::cout << "Object " << name << " has been destructed\n";
  133. }
  134. void ReadFromFile(std::string path) override
  135. {
  136. std::ifstream inFile;
  137. inFile.open(path);
  138. if (inFile.is_open())
  139. {
  140. inFile >> name >> *id;
  141. inFile.close();
  142. }
  143. }
  144. void Print()//метод відображення обєкту на екран
  145. {
  146. std::cout << "Name: " << name << " ID: " << *id << std::endl;
  147. }
  148. friend std::ostream& operator << (std::ostream& os, const ParentClass& parent)
  149. {
  150. //метод відображення обєкту в файл + перегружений оператор(бінарний)
  151. return os << "Name: " << parent.name << " ID: " << *parent.id << std::endl;;
  152. }
  153.  
  154. //Методи редагування змісту обєкту???
  155. void SetName(std::string name)
  156. {
  157. this->name = name;
  158. }
  159. std::string GetName()
  160. {
  161. return this->name;
  162. }
  163. void SetId(int id)
  164. {
  165. *(this->id) = id;
  166. }
  167. int GetId()
  168. {
  169. return *id;
  170. }
  171. };
  172. //--------------------------------
  173. abstractClass.h
  174. //--------------------------------
  175. #pragma once
  176. #include <string>
  177. class AbstractClass
  178. {
  179. public:
  180. virtual void ReadFromFile(std::string path) = 0; //чисто віртуальна функція
  181. };
  182.  
  183.  
Add Comment
Please, Sign In to add comment