fork download
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class employ
  9. {
  10. private:
  11. int m_id;
  12. string m_name;
  13. float m_salary; //m_ for memeber
  14. public:
  15. employ()
  16. {
  17. m_id = 0;
  18. m_name = "";
  19. m_salary = 0.0f;
  20. }
  21. ~employ()
  22. {}
  23.  
  24. void Input_Data()
  25. {
  26. cout << "Enter id: ";
  27. cin >> m_id;
  28. cout << "Enter name: ";
  29. cin >> m_name;
  30. cout << "Enter salary: ";
  31. cin >> m_salary;
  32. }
  33.  
  34.  
  35. friend ostream& operator<<(ostream& show, const employ& emp)
  36. {
  37. show << "Id = " << emp.m_id << endl;
  38. show << "Name = " << emp.m_name << endl;
  39. show << "Salary = " << emp.m_salary << endl;
  40. return(show);
  41. }
  42.  
  43. } ;
  44.  
  45.  
  46. template<class TYPE>
  47. class Node
  48. {
  49. public:
  50. TYPE m_Value;
  51. Node<TYPE>* m_NextElement;
  52.  
  53. };
  54.  
  55. template<class TYPE>
  56. class List
  57. {
  58. public:
  59. Node<TYPE>* m_Start;
  60. Node<TYPE>* m_Current;
  61. int m_Size;
  62.  
  63. List():m_Start(NULL),m_Current(NULL),m_Size(0)
  64. {
  65. m_Start = new Node<TYPE>;
  66. }
  67.  
  68. void Add(TYPE data)
  69. {
  70. if(Size() == 0)
  71. {
  72. m_Start->m_Value = data;
  73. m_Start->m_NextElement = new Node<TYPE>;
  74. m_Current = m_Start->m_NextElement;
  75. }
  76. else
  77. {
  78. m_Current->m_Value = data;
  79. m_Current->m_NextElement = new Node<TYPE>;
  80. m_Current = m_Current->m_NextElement;
  81. }
  82. m_Size++;
  83.  
  84. }
  85. int Size()
  86. {
  87. return(m_Size);
  88. }
  89. void Print()
  90. {
  91. Node<TYPE>* temp = m_Start;
  92. while(temp != m_Current)
  93. {
  94. cout << temp->m_Value << endl;
  95. temp = temp->m_NextElement;
  96. }
  97. }
  98.  
  99. void Save_To_File(string file_name)
  100. {
  101. ofstream Out(file_name.c_str(),ios_base::binary);
  102. Out.write( (char*)this,(sizeof(Node<employ>) * Size()));
  103. Out.close();
  104. }
  105. void Load_From_File(string file_name, int size)
  106. {
  107. if(size != 0)
  108. {
  109. m_Size = size;
  110. ifstream In(file_name.c_str(),ios_base::binary);
  111. In.read( (char*)this,sizeof(Node<employ>)*Size() );
  112. In.close();
  113. }
  114. }
  115. ~List()
  116. {
  117. Node<TYPE>* temp = m_Start;
  118. Node<TYPE>* del;
  119. while(temp != m_Current)
  120. {
  121. del = temp;
  122. temp = temp->m_NextElement;
  123. delete del;
  124. }
  125. m_Start = m_Current = 0;
  126. }
  127. };
  128.  
  129.  
  130.  
  131. int main()
  132. {
  133. List<employ> list1;
  134. employ tmp;
  135. char anwser = '?';
  136. do
  137. {
  138. tmp.Input_Data();
  139. list1.Add(tmp);
  140. cout << "Would You like to continue (Y/N) :";
  141. cin >> anwser;
  142. }
  143. while(anwser == 'y' || anwser == 'Y');//all other key exit the loop
  144. //List<employ> list2;
  145. list1.Print();
  146.  
  147. return 0;
  148. }
  149.  
  150.  
Success #stdin #stdout 0s 3436KB
stdin
1
Younes
1500
n
stdout
Enter id: Enter name: Enter salary: Would You like to continue (Y/N) :Id     =  1
Name   =  Younes
Salary =  1500