Advertisement
tavy14t

bad guy C++

Jan 15th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. // Avem o clasa Punct in care nu avem voie sa avem valori negative!
  2. // Prin utilizarea pointerilor putem face un bypass la validare.
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. class Punct {
  7. private:
  8.     int X, Y;
  9. public:
  10.     Punct(int X, int Y)
  11.     {
  12.         if (X >= 0 && Y >= 0)
  13.         {
  14.             this->X = X;
  15.             this->Y = Y;
  16.         }
  17.         else throw "Nu poti pune numere negative";
  18.     }
  19.     int getX()
  20.     {
  21.         return X;
  22.     }
  23.     int getY()
  24.     {
  25.         return Y;
  26.     }
  27. };
  28.  
  29. int main()
  30. {
  31.     Punct P1(10, 20); // merge!
  32.     Punct P2(-5, 10); // crapa!
  33.  
  34.  
  35.     struct Point { int X, Y; };
  36.     Point str_P = { -5, 10 };
  37.  
  38.     Punct *pct = (Punct*) &str_P;
  39.  
  40.  
  41.     cout << pct->getX() << "  " << pct->getY() << endl; // Nu mai crapa! Acum putem avea valori negative
  42.  
  43.     cin.get();
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement