Advertisement
bazmikel

Untitled

Nov 8th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1.  
  2.  
  3. class K1 {
  4.     string* p1;
  5. public:
  6.     K1() : p1(new string[2]) {p1[0] = "Nothing"; p1[1] = "Nothing";};
  7.     K1(const string& one, const string& two) { p1 = new string[2]; p1[0] = one; p1[1] = two; };
  8.     ~K1() { delete p1; };
  9.     friend ostream & operator <<(ostream &, const K1&);
  10.     string & operator[](size_t i) const { // indexing operator
  11.         return p1[i];
  12.     }
  13.  
  14. };
  15.  
  16.  
  17. ostream & operator <<(ostream & out, const K1 & object) {
  18.     return out << object.p1[0] << " " << object.p1[1] << endl;
  19. }
  20.  
  21. class K2 {
  22.     K1 p1;
  23.     double p2;
  24. public:
  25.     K2(): p2(0){};
  26.     K2(const string & one, const string & two, const double & price): p1(one, two), p2(price){};
  27.     K2(const K2 & object){ // konstruktor kopiacy
  28.         p1 = object.p1;
  29.         p2 = object.p2;
  30.     }
  31.  
  32.     K2 & operator=(const K2 & object){ // operator przypisania
  33.         if(this != &object){
  34.             p1 = object.p1;
  35.             p2 = object.p2;
  36.         }
  37.         return *this;
  38.     }
  39.  
  40.     const K2 operator-(const double & value) const {
  41.         return K2(p1[0], p1[1], p2 - value);
  42.     }
  43.  
  44.  
  45.     friend ostream & operator <<(ostream &, const K2&);
  46. };
  47.  
  48.  
  49.  
  50.     ostream & operator <<(ostream & out, const K2& object){
  51.     return out << object.p1[0] << " " << object.p1[1] << " " << object.p2 << endl;
  52.  
  53. }
  54.  
  55. int main() {
  56.     K2 ob1, ob2;
  57.     const K2* wsk1 = new K2("kawa", "z mlekiem", 4.50);
  58.     const K2 ob3(*wsk1);
  59.     delete wsk1;
  60.     wsk1 = 0;
  61.  
  62.     const K2* wsk2 = new K2(ob3);
  63.     ob2 = *wsk2;
  64.     cout << ob1 << *wsk2;
  65.     delete wsk2;
  66.     wsk2 = 0;
  67.  
  68.     cout << ob2;
  69.     cout << ob2 - 1.25;
  70.  
  71.     system("PAUSE");
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement