Advertisement
avr39-ripe

virtualOperatorFileIO

Jan 22nd, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <chrono>
  5.  
  6. class Parent
  7. {
  8. protected:
  9.     int id;
  10. public:
  11.     Parent(int val = 0) : id{ val } {};
  12.     virtual std::ostream& print(std::ostream& out)
  13.     {
  14.         if (typeid(out) == typeid(std::ofstream))
  15.         {
  16.             std::cout << "File I/O!!!\n";
  17.             return out << 'P' << '\n' << id << '\n';
  18.         }
  19.         return out << "Parent id:" << id << '\n';
  20.     };
  21.  
  22.     virtual std::istream& input(std::istream& in)
  23.     {
  24.         return in >> id;
  25.     }
  26.  
  27.     virtual ~Parent() {};
  28.     friend std::ostream& operator<<(std::ostream& out, Parent& obj);
  29.     friend std::istream& operator>>(std::istream& in, Parent& obj);
  30. };
  31.  
  32. std::ostream& operator<<(std::ostream& out, Parent& obj)
  33. {
  34.     return obj.print(out);
  35. };
  36.  
  37. std::istream& operator>>(std::istream& in, Parent& obj)
  38. {
  39.     return obj.input(in);
  40. };
  41.  
  42. class Child : public Parent
  43. {
  44.     std::string name;
  45. public:
  46.     Child(std::string str = "Child", int val = 0) : Parent{ val }, name{ str } {};
  47.     virtual std::ostream& print(std::ostream& out)
  48.     {
  49.         if (typeid(out) == typeid(std::ofstream))
  50.         {
  51.             std::cout << "File I/O!!!\n";
  52.             return out << 'C' << '\n' << id << '\n' << name << '\n';
  53.         }
  54.         return out << "Child id:" << id << " name:" << name << '\n';
  55.     };
  56.  
  57.     virtual std::istream& input(std::istream& in)
  58.     {
  59.         return in >> id >> name;
  60.     }
  61.  
  62.     void childWork() { std::cout << "Children " << name << " is working hard!\n"; };
  63.  
  64.     virtual ~Child() {};
  65. };
  66.  
  67. int main()
  68. {
  69.     srand(time(0));
  70.  
  71.     Parent* objects[4];
  72.  
  73.     for (auto& object : objects)
  74.     {
  75.         object = (rand() % 2 ? new Parent : new Child);
  76.     }
  77.  
  78.     for (auto& object : objects)
  79.     {
  80.         if (typeid(*object) == typeid(Child))
  81.         {
  82.             std::cout << "Input Child id and name\n";
  83.         }
  84.         else
  85.         {
  86.             std::cout << "Input Parent id\n";
  87.         }
  88.         std::cin >> *object;
  89.     }
  90.  
  91.     std::ofstream outf{ "file.txt" };
  92.     if (!outf.is_open())
  93.     {
  94.         std::cout << "Cannot create file! Exit now!\n";
  95.     }
  96.  
  97.     for (auto& object : objects)
  98.     {
  99.         std::cout << *object;
  100.         outf << *object;
  101.  
  102.         delete object;
  103.     }
  104.  
  105.     outf.close();
  106.  
  107.     std::cout << "Now read file!\n";
  108.     char buf[50];
  109.     std::ifstream inf{ "file.txt" };
  110.  
  111.     if (!inf.is_open())
  112.     {
  113.         std::cout << "Cannot open file! Exit now!\n";
  114.     }
  115.  
  116.     Parent fParent;
  117.     Child fChild;
  118.  
  119.     std::string str;
  120.     str.reserve(50);
  121.     while (!inf.eof())
  122.     {
  123.  
  124.         getline(inf, str);
  125.  
  126.         if (str == "P")
  127.         {
  128.             std::cout << "Got Parent!\n";
  129.             inf >> fParent;
  130.             std::cout << fParent << '\n';
  131.         }
  132.  
  133.         if (str == "C")
  134.         {
  135.             std::cout << "Got Child!\n";
  136.             inf >> fChild;
  137.             std::cout << fChild << '\n';
  138.         }
  139.  
  140.     }
  141.  
  142.  
  143.     inf.close();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement