Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. #pragma once
  2.  
  3. class Bus{
  4. int numberBus_;//номер автобуса
  5. char*Fio_;//фамилия и инициалы водителя
  6. int n_;
  7. int numbertrip_;//номер маршрута
  8. public:
  9. Bus();//:numberBus_(0),Fio_("No Fio"),numbertrip_(0),_(0){}//конструктор без параметров
  10. Bus(int numberBus, char* Fio,int n,int numbertrip):numberBus_(numberBus),Fio_(Fio),n_(n),numbertrip_(numbertrip){} //конструктор с параметрами
  11. Bus(const Bus& ob);//конструктор копирования
  12. ~Bus(){//диструктор
  13. cout<<"Освобождение памяти\n";
  14. delete Fio_;
  15. };
  16. const Bus operator +(const Bus &other)const;//перегрузка бинарного оператора как метод класса
  17. friend Bus operator +( const Bus &other) ;//перегрузка бинарного оператора как дружественную функцию
  18. bool operator >(const Bus & ob1);//перегрузка оператора отношения как метод класса
  19. friend bool operator >(int x,Bus op4);//перегрузка оператора отношения дружественную функцию
  20. Bus& operator =(const Bus &ob);
  21. //Bus& operator =(const Bus & other);//перегрузка операторов присвоения
  22. Bus & operator ++();//перегрузка операторов префиксного
  23. friend Bus & operator ++(Bus& ob);//перегрузка пpeфиксного оператора через дружественную функцию
  24. Bus& operator ++ (int notUsed);
  25. //Bus operator ++( int );//перегрузка операторов постфиксного
  26. friend Bus& operator ++ (Bus &ob, int notUsed);
  27. //friend Bus operator ++ (Bus &ob, int notUsed);//перегрузка постфиксного оператора через дружественную функцию
  28. friend ostream &operator <<(ostream& out, Bus& ob);//перегрузка <<
  29. friend istream &operator >>(istream& in, Bus& ob);//перегрузка >>
  30. void show();//вывод на экран всех полей
  31. void fill(int ,char *,int );
  32. };
  33.  
  34.  
  35.  
  36.  
  37. # include <iostream>
  38. using namespace std;
  39.  
  40.  
  41. #include "Bus.h"
  42.  
  43.  
  44.  
  45. /*Bus::Bus(){//определение конструктора
  46. numberBus=0;
  47. numbertrip=0;
  48. n=0;
  49. cout<<"конструктор работает";
  50. }
  51. Bus::Bus(int k,int t,int nn){//описание конструктора с параметром
  52. cout<<"конструктор работает";
  53. numberBus=k;
  54. numbertrip=t;
  55. Fio=new char [n=nn];
  56. }*/
  57. Bus::Bus( const Bus & ob){//конструктор копирования
  58.  
  59. numberBus_=ob.numberBus_;
  60. Fio_=ob.Fio_;
  61. //n_=ob.n;
  62. numbertrip_=ob.numbertrip_;
  63. //cout<<"Выделение памяти\n";
  64. //Fio_=new char [n_];//не уверена что так???
  65. //*Fio_=*ob.Fio_;
  66. //delete Fio_;
  67. //cout<<"Вызван конструктор копии";
  68. }
  69. const Bus Bus::operator+(const Bus &other) const {
  70. // Bus result ; *this; // Make a copy of myself. Same as MyClass result(*this);
  71. // result += other; // Use += to add other to the copy.
  72. //return result; // All done!
  73. Bus temp;
  74. temp.numbertrip_=other.numbertrip_ +other.numbertrip_;
  75. //temp.numberBus=numberBus + op1.numberBus;
  76. return temp;
  77. }
  78.  
  79.  
  80. /*Bus Bus:: operator +(Bus op1,Bus op2){//перегрузка бинарного оператора как метод класса
  81. Bus temp;
  82. temp.numbertrip=Bus (op1.numbertrip + op2.numbertrip);
  83. //temp.numberBus=numberBus + op1.numberBus;
  84. return temp;
  85. }*/
  86. Bus operator +( const Bus &other) {//перегрузка бинарного оператора дружественную функцию
  87. Bus temp;
  88. temp.numbertrip_=other.numbertrip_+other.numbertrip_;
  89. //temp.numberBus_=op2.numberBus_+i;
  90. return temp;
  91. }
  92.  
  93.  
  94. bool Bus:: operator>(const Bus & ob1){//перегрузка оператора отношения как метод класса
  95. if(this->numbertrip_>ob1.numbertrip_)return true;
  96. return false;
  97. //return (op3.numberBus_>op4.numberBus_);
  98. }
  99. bool operator>(int x,Bus op4){//перегрузка оператора отношения дружественную функцию
  100. return (x>op4.numberBus_);
  101. }
  102. Bus &
  103. Bus::operator =(const Bus&ob)
  104. {
  105. if(&ob==this)return *this;
  106. Fio_=ob.Fio_;
  107. numberBus_=ob.numberBus_;
  108. numbertrip_=ob.numbertrip_;
  109.  
  110. return *this;
  111. }
  112. /* Bus & Bus::operator = (const Bus & other)//перегрузка операторов присвоения
  113. {
  114. numberBus_=other.numberBus_;
  115. numbertrip_=other.numbertrip_;
  116. // 1: выделяем "новую" память и копируем элементы
  117. char * new_fio_ = new char[other.n_];
  118. copy(other.Fio_, other.Fio_ + other.n_, new_fio_);
  119.  
  120. // 2: освобождаем "старую" память
  121. delete [] fio_;
  122.  
  123. // 3: присваиваем значения в "новой" памяти объекту
  124. fio_ = new_fio_;
  125. n_ = other.n_;
  126.  
  127. // по соглашению всегда возвращаем *this
  128. return *this;
  129. }*/
  130.  
  131. Bus& Bus:: operator ++(){//перегрузка операторов префиксного
  132. numberBus_+=1;
  133. return *this;
  134. }
  135. Bus& operator ++(Bus &ob){//перегрузка пpeфиксного оператора через дружественную функцию
  136. ob.numberBus_+=1;
  137. return ob;
  138. }
  139.  
  140.  
  141.  
  142.  
  143. Bus&
  144. Bus::operator ++ (int notUsed){//перегрузка операторов постфиксног
  145.  
  146. Bus& temp=*this;
  147. numberBus_+=1;
  148. return temp;
  149. }
  150. Bus& operator ++ (Bus &ob, int notUsed){//перегрузка операторов постфиксног
  151.  
  152.  
  153. Bus& temp=ob;
  154. ob.numberBus_+=1;
  155. return temp;
  156. }
  157.  
  158. ostream &operator <<(ostream& out, Bus& ob)
  159. {return(out<<"numberBus:"<<ob.numberBus_);}
  160.  
  161. /*ostream& operator<<(ostream& s, const Bus& t)
  162. {
  163.  
  164. s << "NumberBus" << t.numberBus_ << " Numbertrip" << t.numbertrip_;
  165. return s;
  166.  
  167. }*/
  168.  
  169. /* istream & operator>>(ostream& s, const Bus& t)
  170. {
  171. s >>t.NumberBus_ << endl << t.numbertrip_;
  172. return s;
  173. }*/
  174. istream &operator >>(istream& in, Bus& ob)
  175. {return(in>>ob.numberBus_);}
  176.  
  177.  
  178.  
  179.  
  180. //вывод на экран всех полей
  181. void Bus:: show()
  182. {
  183. cout<<numberBus_<<" "<<Fio_<<" "<<n_<<" "<<numbertrip_<<" "<<"\n";
  184. }
  185. void Bus::fill (int k,char *s,int m){
  186. numberBus_=k;
  187. Fio_=s;
  188. numbertrip_=m;
  189. }
  190.  
  191.  
  192. void main(){
  193. cout<<"Create a bus";
  194. Bus ob;
  195. ob.fill (578,"Иванов А.М",196);
  196.  
  197. Bus ob1;
  198. ob1.fill(555,"Халилов И.Н",388);
  199. Bus ob3;
  200.  
  201. ob.show();
  202. ob1.show();
  203. ob3.show();
  204. ob3=ob1+ob;//использование бинарного оператора
  205. ob3.show();
  206. ob>ob1;//использования оператора отношения
  207. ob.show();
  208. ob3=ob=ob1;//множественное присвоение
  209. ob3.show();
  210. ob.show();
  211. ++ob;//пpeфиксного оператора
  212. ob.show();
  213. ob++;//постфиксного оператора
  214. ob.show();
  215. //a=++c;//Объект a получает значение объекта с после инкрементирования
  216. //a.show();
  217. //c.show();
  218. //a=c++;//Объект a получает значение объекта с до инкрементирования
  219. //a.show();
  220. //c.show();
  221. cin>>ob>>ob1>>ob3;//использование оператора >>
  222. cout<<ob;//использование оператора <<
  223.  
  224. return ;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement