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)
- explicit 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_{};
- };
- void vecchia_funzione(PuntoQ1 p) {
- //PuntoQ1 {n, 2*n};
- //ecc.
- }
- class NPC {
- private:
- inline static int id_counter = 0;
- int id_{}; //DEVE essere unico per ciascun npc
- public:
- // Costruttore normale
- explicit NPC()
- : id_(++id_counter) {}
- auto [[nodiscard]] id() const -> int { return id_; }
- //costruttore copia (copy constructor)
- //NPC(const NPC& npc_da_copiare): NPC() {
- //
- //}
- NPC(const NPC& npc_da_copiare) = delete;
- };
- auto fnpc(NPC n) -> void {}
- 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());
- //chiamata con vecchia firma
- //vecchia_funzione(5); NO: explicit
- // Frazione f = 5;
- auto p4 = PuntoQ1({3,4});
- auto npc1 = NPC{};
- auto npc2= NPC{};
- //auto npc2 = npc1; NO =delete
- //fnpc(npc1); NO =delete
- std::println("npc1 id: {} npc2 id {}", npc1.id(), npc2.id());
- }
Advertisement
Add Comment
Please, Sign In to add comment