fcamuso

Corso C++ 40 - getter e setter

Jul 5th, 2026 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. import std;
  2.  
  3. class PuntoQ1 {
  4.  
  5.    public:
  6.       PuntoQ1() { }
  7.  
  8.       PuntoQ1(int x, int y, int sconto_fisso=0):
  9.         x_{x >= 0 ? x : throw std::invalid_argument("x negativo")},
  10.         y_{y >= 0 ? y : throw std::invalid_argument("y negativo")}
  11.       {   }
  12.  
  13.   //metodi getter / setter
  14.   [[nodiscard]]auto x() const -> int {return x_;}
  15.  
  16.   auto set_x(int x) -> void {
  17.  
  18.     if (x<0)
  19.         throw std::invalid_argument("x negativo");
  20.    
  21.     x_ = x;
  22.   }
  23.  
  24.     private:  
  25.       int x_{};
  26.       int y_{};
  27.    
  28. };
  29.  
  30. auto main() -> int
  31. {
  32.     auto p1 = PuntoQ1{10, 34}; //OK
  33.     std::println("{}", p1.x());
  34.  
  35.  
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment