pieniakoskar

Samochód (marka itd)

Mar 19th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. #define PASAZ 5
  9.  
  10. class Samochod
  11. {
  12. string _marka;
  13. string _model;
  14. int _pojem;
  15. int _masaw;
  16. int _pasaz[PASAZ];
  17.  
  18. public:
  19. Samochod() {};
  20. Samochod(const string Marka, const string Model, const int Pojemnosc, const int MasaWlasna);
  21. Samochod(Samochod &s);
  22.  
  23. bool operator==(Samochod &s);
  24. bool operator!=(Samochod &s);
  25. bool operator<(Samochod &s);
  26. bool operator>(Samochod &s);
  27. bool operator<=(Samochod &s);
  28. bool operator>=(Samochod &s);
  29.  
  30. operator string();
  31. friend ostream & operator<< (ostream &o, Samochod &s);
  32. operator int();
  33.  
  34. int & operator[] (int i) { return _pasaz[i-1]; }
  35.  
  36. };
  37.  
  38. Samochod::Samochod(const string Marka, const string Model, const int Pojemnosc, const int MasaWlasna):
  39. _marka(Marka), _model(Model), _pojem(Pojemnosc), _masaw(MasaWlasna)
  40. {
  41. for(unsigned int i=0; i<PASAZ; ++i)
  42. {
  43. _pasaz[i] = 0;
  44. }
  45. };
  46.  
  47. Samochod::Samochod(Samochod &s):
  48. _marka(s._marka), _model(s._model), _pojem(s._pojem), _masaw(s._masaw)
  49. {
  50. for(unsigned int i=0; i<PASAZ; ++i)
  51. {
  52. _pasaz[i] = s._pasaz[i];
  53. }
  54. };
  55.  
  56.  
  57.  
  58. bool Samochod::operator==(Samochod &s)
  59. {
  60. if( (_marka==s._marka) && (_model==s._model) && (_pojem==s._pojem) ) return 1;
  61. return 0;
  62. }
  63. bool Samochod::operator!=(Samochod &s)
  64. {
  65. if( (_marka!=s._marka) || (_model!=s._model) || (_pojem!=s._pojem) ) return 1;
  66. return 0;
  67. }
  68.  
  69. bool Samochod::operator<(Samochod &s){ return _masaw < s._masaw; }
  70. bool Samochod::operator>(Samochod &s){ return _masaw > s._masaw; }
  71. bool Samochod::operator<=(Samochod &s){ return _masaw <= s._masaw; }
  72. bool Samochod::operator>=(Samochod &s){ return _masaw >= s._masaw; }
  73.  
  74. Samochod::operator int()
  75. {
  76. int suma=_masaw;
  77. for(unsigned int i=0; i<PASAZ; ++i) suma+=_pasaz[i];
  78. return suma;
  79. }
  80.  
  81. Samochod::operator std::string()
  82. {
  83. char bufor[100];
  84. sprintf(bufor, "%s %s %d", _marka.c_str(), _model.c_str(), _pojem);
  85. return bufor;
  86. }
  87.  
  88. ostream & operator<< (ostream &o, Samochod &s)
  89. {
  90. return o << s._marka << " " << s._model << " " << s._pojem;
  91. }
  92.  
  93. int _tmain(int argc, _TCHAR* argv[])
  94. {
  95. Samochod s1("VW", "Golf", 1599, 1200);
  96. Samochod s2(s1);
  97.  
  98. cout << "s1: " << s1 << endl;
  99. cout << "s2: " << string(s2) << endl;
  100.  
  101. if(s1==s2) cout << "Auta s1 i s2 sa takie same. \n";
  102. if(s1!=s2) cout << "Auta s1 i s2 sa rozne. \n";
  103.  
  104. cout << "Waga s1: " << int(s1) << endl;
  105. cout << "s1[3] = " << s1[3] << endl;
  106. s1[3] = 100;
  107. cout << "s1[3] = " << s1[3] << endl;
  108. cout << "Waga s1: " << int(s1) << endl;
  109.  
  110. Samochod s3("VW", "Golf", 1999, 1300);
  111. if(s1>s3) cout << "Auto s1 jest wieksze od s3\n";
  112. if(s1<s3) cout << "Auto s3 jest wieksze od s1\n";
  113.  
  114. cin.get();
  115.  
  116. return 0;
  117. }
Add Comment
Please, Sign In to add comment