Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <iostream>
- #include <string>
- using namespace std;
- #define PASAZ 5
- class Samochod
- {
- string _marka;
- string _model;
- int _pojem;
- int _masaw;
- int _pasaz[PASAZ];
- public:
- Samochod() {};
- Samochod(const string Marka, const string Model, const int Pojemnosc, const int MasaWlasna);
- Samochod(Samochod &s);
- bool operator==(Samochod &s);
- bool operator!=(Samochod &s);
- bool operator<(Samochod &s);
- bool operator>(Samochod &s);
- bool operator<=(Samochod &s);
- bool operator>=(Samochod &s);
- operator string();
- friend ostream & operator<< (ostream &o, Samochod &s);
- operator int();
- int & operator[] (int i) { return _pasaz[i-1]; }
- };
- Samochod::Samochod(const string Marka, const string Model, const int Pojemnosc, const int MasaWlasna):
- _marka(Marka), _model(Model), _pojem(Pojemnosc), _masaw(MasaWlasna)
- {
- for(unsigned int i=0; i<PASAZ; ++i)
- {
- _pasaz[i] = 0;
- }
- };
- Samochod::Samochod(Samochod &s):
- _marka(s._marka), _model(s._model), _pojem(s._pojem), _masaw(s._masaw)
- {
- for(unsigned int i=0; i<PASAZ; ++i)
- {
- _pasaz[i] = s._pasaz[i];
- }
- };
- bool Samochod::operator==(Samochod &s)
- {
- if( (_marka==s._marka) && (_model==s._model) && (_pojem==s._pojem) ) return 1;
- return 0;
- }
- bool Samochod::operator!=(Samochod &s)
- {
- if( (_marka!=s._marka) || (_model!=s._model) || (_pojem!=s._pojem) ) return 1;
- return 0;
- }
- bool Samochod::operator<(Samochod &s){ return _masaw < s._masaw; }
- bool Samochod::operator>(Samochod &s){ return _masaw > s._masaw; }
- bool Samochod::operator<=(Samochod &s){ return _masaw <= s._masaw; }
- bool Samochod::operator>=(Samochod &s){ return _masaw >= s._masaw; }
- Samochod::operator int()
- {
- int suma=_masaw;
- for(unsigned int i=0; i<PASAZ; ++i) suma+=_pasaz[i];
- return suma;
- }
- Samochod::operator std::string()
- {
- char bufor[100];
- sprintf(bufor, "%s %s %d", _marka.c_str(), _model.c_str(), _pojem);
- return bufor;
- }
- ostream & operator<< (ostream &o, Samochod &s)
- {
- return o << s._marka << " " << s._model << " " << s._pojem;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- Samochod s1("VW", "Golf", 1599, 1200);
- Samochod s2(s1);
- cout << "s1: " << s1 << endl;
- cout << "s2: " << string(s2) << endl;
- if(s1==s2) cout << "Auta s1 i s2 sa takie same. \n";
- if(s1!=s2) cout << "Auta s1 i s2 sa rozne. \n";
- cout << "Waga s1: " << int(s1) << endl;
- cout << "s1[3] = " << s1[3] << endl;
- s1[3] = 100;
- cout << "s1[3] = " << s1[3] << endl;
- cout << "Waga s1: " << int(s1) << endl;
- Samochod s3("VW", "Golf", 1999, 1300);
- if(s1>s3) cout << "Auto s1 jest wieksze od s3\n";
- if(s1<s3) cout << "Auto s3 jest wieksze od s1\n";
- cin.get();
- return 0;
- }
Add Comment
Please, Sign In to add comment