Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- class Bus{
- int numberBus_;//номер автобуса
- char*Fio_;//фамилия и инициалы водителя
- int n_;
- int numbertrip_;//номер маршрута
- public:
- Bus();//:numberBus_(0),Fio_("No Fio"),numbertrip_(0),_(0){}//конструктор без параметров
- Bus(int numberBus, char* Fio,int n,int numbertrip):numberBus_(numberBus),Fio_(Fio),n_(n),numbertrip_(numbertrip){} //конструктор с параметрами
- Bus(const Bus& ob);//конструктор копирования
- ~Bus(){//диструктор
- cout<<"Освобождение памяти\n";
- delete Fio_;
- };
- const Bus operator +(const Bus &other)const;//перегрузка бинарного оператора как метод класса
- friend Bus operator +( const Bus &other) ;//перегрузка бинарного оператора как дружественную функцию
- bool operator >(const Bus & ob1);//перегрузка оператора отношения как метод класса
- friend bool operator >(int x,Bus op4);//перегрузка оператора отношения дружественную функцию
- Bus& operator =(const Bus &ob);
- //Bus& operator =(const Bus & other);//перегрузка операторов присвоения
- Bus & operator ++();//перегрузка операторов префиксного
- friend Bus & operator ++(Bus& ob);//перегрузка пpeфиксного оператора через дружественную функцию
- Bus& operator ++ (int notUsed);
- //Bus operator ++( int );//перегрузка операторов постфиксного
- friend Bus& operator ++ (Bus &ob, int notUsed);
- //friend Bus operator ++ (Bus &ob, int notUsed);//перегрузка постфиксного оператора через дружественную функцию
- friend ostream &operator <<(ostream& out, Bus& ob);//перегрузка <<
- friend istream &operator >>(istream& in, Bus& ob);//перегрузка >>
- void show();//вывод на экран всех полей
- void fill(int ,char *,int );
- };
- # include <iostream>
- using namespace std;
- #include "Bus.h"
- /*Bus::Bus(){//определение конструктора
- numberBus=0;
- numbertrip=0;
- n=0;
- cout<<"конструктор работает";
- }
- Bus::Bus(int k,int t,int nn){//описание конструктора с параметром
- cout<<"конструктор работает";
- numberBus=k;
- numbertrip=t;
- Fio=new char [n=nn];
- }*/
- Bus::Bus( const Bus & ob){//конструктор копирования
- numberBus_=ob.numberBus_;
- Fio_=ob.Fio_;
- //n_=ob.n;
- numbertrip_=ob.numbertrip_;
- //cout<<"Выделение памяти\n";
- //Fio_=new char [n_];//не уверена что так???
- //*Fio_=*ob.Fio_;
- //delete Fio_;
- //cout<<"Вызван конструктор копии";
- }
- const Bus Bus::operator+(const Bus &other) const {
- // Bus result ; *this; // Make a copy of myself. Same as MyClass result(*this);
- // result += other; // Use += to add other to the copy.
- //return result; // All done!
- Bus temp;
- temp.numbertrip_=other.numbertrip_ +other.numbertrip_;
- //temp.numberBus=numberBus + op1.numberBus;
- return temp;
- }
- /*Bus Bus:: operator +(Bus op1,Bus op2){//перегрузка бинарного оператора как метод класса
- Bus temp;
- temp.numbertrip=Bus (op1.numbertrip + op2.numbertrip);
- //temp.numberBus=numberBus + op1.numberBus;
- return temp;
- }*/
- Bus operator +( const Bus &other) {//перегрузка бинарного оператора дружественную функцию
- Bus temp;
- temp.numbertrip_=other.numbertrip_+other.numbertrip_;
- //temp.numberBus_=op2.numberBus_+i;
- return temp;
- }
- bool Bus:: operator>(const Bus & ob1){//перегрузка оператора отношения как метод класса
- if(this->numbertrip_>ob1.numbertrip_)return true;
- return false;
- //return (op3.numberBus_>op4.numberBus_);
- }
- bool operator>(int x,Bus op4){//перегрузка оператора отношения дружественную функцию
- return (x>op4.numberBus_);
- }
- Bus &
- Bus::operator =(const Bus&ob)
- {
- if(&ob==this)return *this;
- Fio_=ob.Fio_;
- numberBus_=ob.numberBus_;
- numbertrip_=ob.numbertrip_;
- return *this;
- }
- /* Bus & Bus::operator = (const Bus & other)//перегрузка операторов присвоения
- {
- numberBus_=other.numberBus_;
- numbertrip_=other.numbertrip_;
- // 1: выделяем "новую" память и копируем элементы
- char * new_fio_ = new char[other.n_];
- copy(other.Fio_, other.Fio_ + other.n_, new_fio_);
- // 2: освобождаем "старую" память
- delete [] fio_;
- // 3: присваиваем значения в "новой" памяти объекту
- fio_ = new_fio_;
- n_ = other.n_;
- // по соглашению всегда возвращаем *this
- return *this;
- }*/
- Bus& Bus:: operator ++(){//перегрузка операторов префиксного
- numberBus_+=1;
- return *this;
- }
- Bus& operator ++(Bus &ob){//перегрузка пpeфиксного оператора через дружественную функцию
- ob.numberBus_+=1;
- return ob;
- }
- Bus&
- Bus::operator ++ (int notUsed){//перегрузка операторов постфиксног
- Bus& temp=*this;
- numberBus_+=1;
- return temp;
- }
- Bus& operator ++ (Bus &ob, int notUsed){//перегрузка операторов постфиксног
- Bus& temp=ob;
- ob.numberBus_+=1;
- return temp;
- }
- ostream &operator <<(ostream& out, Bus& ob)
- {return(out<<"numberBus:"<<ob.numberBus_);}
- /*ostream& operator<<(ostream& s, const Bus& t)
- {
- s << "NumberBus" << t.numberBus_ << " Numbertrip" << t.numbertrip_;
- return s;
- }*/
- /* istream & operator>>(ostream& s, const Bus& t)
- {
- s >>t.NumberBus_ << endl << t.numbertrip_;
- return s;
- }*/
- istream &operator >>(istream& in, Bus& ob)
- {return(in>>ob.numberBus_);}
- //вывод на экран всех полей
- void Bus:: show()
- {
- cout<<numberBus_<<" "<<Fio_<<" "<<n_<<" "<<numbertrip_<<" "<<"\n";
- }
- void Bus::fill (int k,char *s,int m){
- numberBus_=k;
- Fio_=s;
- numbertrip_=m;
- }
- void main(){
- cout<<"Create a bus";
- Bus ob;
- ob.fill (578,"Иванов А.М",196);
- Bus ob1;
- ob1.fill(555,"Халилов И.Н",388);
- Bus ob3;
- ob.show();
- ob1.show();
- ob3.show();
- ob3=ob1+ob;//использование бинарного оператора
- ob3.show();
- ob>ob1;//использования оператора отношения
- ob.show();
- ob3=ob=ob1;//множественное присвоение
- ob3.show();
- ob.show();
- ++ob;//пpeфиксного оператора
- ob.show();
- ob++;//постфиксного оператора
- ob.show();
- //a=++c;//Объект a получает значение объекта с после инкрементирования
- //a.show();
- //c.show();
- //a=c++;//Объект a получает значение объекта с до инкрементирования
- //a.show();
- //c.show();
- cin>>ob>>ob1>>ob3;//использование оператора >>
- cout<<ob;//использование оператора <<
- return ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement