Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std;
- class MyString {
- std::string testo;
- public:
- MyString(const std::string& str) : testo{ str } {}
- auto rimuovi_spazi() -> MyString& {
- std::erase(testo, ' ');
- return *this;
- }
- auto converti_maiuscolo() -> MyString& {
- for (char& c : testo) {
- c = std::toupper(static_cast<unsigned char>(c));
- }
- return *this;
- }
- auto sostituisci(const std::string& da_cercare, const std::string& rimpiazzo) -> MyString& {
- if (da_cercare.empty()) return *this;
- std::size_t pos = 0;
- while ((pos = testo.find(da_cercare, pos)) != std::string::npos) {
- testo.replace(pos, da_cercare.length(), rimpiazzo);
- pos += rimpiazzo.length();
- }
- return *this;
- }
- auto stampa() const -> void {
- std::println("{}", testo);
- }
- };
- class Mostro {
- public:
- auto attacco(Personaggio& p) -> int
- {
- //logica in base al personaggio
- //...
- return 100; //il danno inflitto dal mostro
- }
- };
- class Personaggio {
- public:
- Personaggio(int salute) : salute{salute} {}
- auto interagisci(Mostro m) -> void
- {
- //logica che in base al mostro decide se e come attaccare
- salute += m.attacco(*this);
- }
- auto set_salute(int salute) -> void
- {
- //(*this).salute = salute;
- this->salute = salute;
- }
- //deducing this / self (prorogato)
- private:
- int salute{1000};
- };
- int main() {
- MyString stringa{ " c i a o m ondo A A A " };
- MyString stringa2{ "seconda stringa" }, stringa3 {"terza stringa"};
- stringa2.rimuovi_spazi();
- // Utilizzo del method chaining
- stringa.rimuovi_spazi()
- .converti_maiuscolo()
- .sostituisci("A", "BBBBBB");
- stringa.stampa(); // Output atteso: CIBOMONDOBBB
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment