Advertisement
bazmikel

Kolos z kawą

Nov 8th, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6.  
  7. class K1 {
  8.     string* p1;
  9. public:
  10.     K1() : p1(new string[2]) {p1[0] = "Nothing"; p1[1] = "Nothing";};
  11.     K1(const string& one, const string& two) { p1 = new string[2]; p1[0] = one; p1[1] = two; };
  12.     ~K1() { delete  p1; };
  13.     friend ostream & operator <<(ostream &, const K1&);
  14.     string & operator[](size_t i) const { // indexing operator
  15.         return p1[i];
  16.     }
  17.  
  18. };
  19.  
  20.  
  21. ostream & operator <<(ostream & out, const K1 & object) {
  22.     return out << object.p1[0] << " " << object.p1[1] << endl;
  23. }
  24.  
  25. class K2 {
  26.     K1 p1;
  27.     double p2;
  28. public:
  29.     K2(): p2(0){};
  30.     K2(const string & one, const string & two, const double & price): p1(one, two), p2(price){};
  31.     K2(const K2 & object){ // konstruktor kopiacy
  32.         p1 = object.p1;
  33.         p2 = object.p2;
  34.     }
  35.  
  36.     const K2 & operator=(const K2 & object){ // operator przypisania
  37.         if(this != &object){
  38.             p1 = object.p1;
  39.             p2 = object.p2;
  40.         }
  41.         return *this;
  42.     }
  43.  
  44.     const K2 operator-(const double & value) const {
  45.         return K2(p1[0], p1[1], p2 - value);
  46.     }
  47.  
  48.     void czytajLinie(ifstream & input){
  49.             if(!input) throw string("Problem ze strumieniem wczytywania");
  50.             input >> p1[0];
  51.             input >> p1[1];
  52.             input >> p2;
  53.  
  54.     }
  55.  
  56.     const K2 operator+(const double & value) const { // przeciazanie operatora sumy
  57.         return K2(p1[0], p1[1], p2 + value);
  58.     }
  59.  
  60.     const K2 operator +=(const double & value){
  61.         this->p2 = this->p2 + value;
  62.         return *this;
  63.     }
  64.  
  65.     friend ostream & operator <<(ostream &, const K2&);
  66.     friend const K2 operator +(const K2 & object, const string & value);
  67.     friend const K2 operator +(const string & value, const K2 & object);
  68.  
  69. };
  70.  
  71.     const K2 operator +(const string & value, const K2 & object){
  72.         return K2(value + object.p1[0], object.p1[1], object.p2);
  73.     }
  74.  
  75.     const K2 operator +(const K2 & object, const string & value){
  76.         return K2(object.p1[0], object.p1[1]  + value, object.p2);
  77.     }
  78.  
  79.     ostream & operator <<(ostream & out, const K2& object){
  80.     return out << object.p1[0] << " " << object.p1[1] << " " << object.p2 << endl;
  81. }
  82.  
  83. int main() {
  84.     K2 ob1, ob2;
  85.     const K2* wsk1 = new K2("kawa", "z mlekiem", 4.50);
  86.     const K2 ob3(*wsk1);
  87.     delete wsk1;
  88.     wsk1 = 0;
  89.  
  90.     const K2* wsk2 = new K2(ob3);
  91.     ob2 = *wsk2;
  92.     cout << ob1 << *wsk2;
  93.     delete wsk2;
  94.     wsk2 = 0;
  95.  
  96.     cout << ob2;
  97.     cout << ob2 - 1.25;
  98.  
  99.     cout << "******** 3 **********" << endl;
  100.  
  101.     K2 tab[4];
  102.     ifstream plik("data.txt");
  103.     try{
  104.         if(!plik) throw string("Cannot open the file");
  105.         for(size_t i = 0; i < 4; ++i)
  106.             tab[i].czytajLinie(plik);
  107.     }
  108.     catch(const string & err){cout << err << endl;}
  109.     catch(...){cout << "Contact with developers" << endl;}
  110.  
  111.  
  112.     for(size_t i = 0; i < 4; i++){
  113.         tab[i] += 1; //??? how to overload +=
  114.         cout << tab[i];
  115.     }
  116.  
  117.     cout << "******** 4 **********" << endl;
  118.  
  119.     tab[1] = tab[1] + " with sugar";
  120.     tab[3] = "hot " + tab[3];
  121.  
  122.     for(size_t i = 0; i < 4; i++)
  123.         cout << tab[i];
  124.  
  125.     cout << "******** 5 **********" << endl;
  126.  
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement