Advertisement
bazmikel

wata fak maza fak

Nov 8th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 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) const{ // przeciazanie operatora sumowania iteracyjnego
  61.         return K2(p1[0], p1[1], p2 + value);
  62.     }*/
  63.  
  64.     friend ostream & operator <<(ostream &, const K2&);
  65.     friend K2 operator +=(const K2 &, const double &);
  66.  
  67. };
  68.  
  69.     K2 operator += (const K2 & object, const double & value){
  70.         return K2(object.p1[0], object.p1[1], value + object.p2);
  71.     }
  72.  
  73.     ostream & operator <<(ostream & out, const K2& object){
  74.     return out << object.p1[0] << " " << object.p1[1] << " " << object.p2 << endl;
  75. }
  76.  
  77. int main() {
  78.     K2 ob1, ob2;
  79.     const K2* wsk1 = new K2("kawa", "z mlekiem", 4.50);
  80.     const K2 ob3(*wsk1);
  81.     delete wsk1;
  82.     wsk1 = 0;
  83.  
  84.     const K2* wsk2 = new K2(ob3);
  85.     ob2 = *wsk2;
  86.     cout << ob1 << *wsk2;
  87.     delete wsk2;
  88.     wsk2 = 0;
  89.  
  90.     cout << ob2;
  91.     cout << ob2 - 1.25;
  92.  
  93.     cout << "******** 3 **********" << endl;
  94.  
  95.     K2 tab[4];
  96.     ifstream plik("data.txt");
  97.     try{
  98.         if(!plik) throw string("Cannot open the file");
  99.         for(size_t i = 0; i < 4; ++i)
  100.             tab[i].czytajLinie(plik);
  101.     }
  102.     catch(const string & err){cout << err << endl;}
  103.     catch(...){cout << "Contact with developers" << endl;}
  104.  
  105.  
  106.     /*for(size_t i = 0; i < 4; i++){
  107.         tab[i] += 1; //???
  108.         cout << tab[i];
  109.     }*/
  110.     tab[1] += 1;
  111.     cout << tab[1];
  112.  
  113.     cout << "******** 4 **********" << endl;
  114.  
  115.  
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement