Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std;
- class PuntoQ1 {
- public:
- PuntoQ1() = default; //per creare il punto (0,0)
- //costruttore 'standard' (configurazione parametri completa)
- //definito anche costruttore 'target' per le deleghe
- PuntoQ1(int x, int y):
- x_{x >= 0 ? x : throw std::invalid_argument("x negativo")},
- y_{y >= 0 ? y : throw std::invalid_argument("y negativo")}
- { }
- //costruttore che delega al precedente
- //per creare un punto con coordinate uguali (sulla bisettrice)
- PuntoQ1(int x) : PuntoQ1(x,x) {}
- //PuntoQ1(double x) : x_{x}, PuntoQ1(0,0) {} NO: o delega o inizializzazione
- //costruttore che accetta stringhe
- //esempio PuntoQ1( "(3, 6)" )
- //(delegante)
- PuntoQ1(const std::string& s) : PuntoQ1(
- std::stoi(s.substr(1, s.find(',') - 1)),
- std::stoi(s.substr(s.find(',') + 1)) //PuntoQ1(3,6)
- ) {}
- //tag
- struct Polare {};
- PuntoQ1(int raggio, int angolo_radianti, Polare p)
- : PuntoQ1(raggio* std::cos(angolo_radianti),
- raggio* std::sin(angolo_radianti)) {}
- //metodi getter / setter
- [[nodiscard]] auto x() const -> int {return x_;}
- [[nodiscard]] auto y() const -> int {return y_;}
- auto set_x(int x) -> void {
- if (x<0)
- throw std::invalid_argument("x negativo");
- x_ = x;
- }
- private:
- int x_{};
- int y_{};
- double x_{};
- };
- auto main() -> int
- {
- auto p1 = PuntoQ1{10, 34}; //OK
- std::println("{}", p1.x());
- std::string letta_da_file = "(12, 1)";
- auto p2 = PuntoQ1(letta_da_file);
- std::println("{} {}", p2.x(), p2.y());
- auto p3 = PuntoQ1(12, 1, PuntoQ1::Polare{});
- std::println("{} {}", p3.x(), p3.y());
- }
Advertisement
Add Comment
Please, Sign In to add comment