Salve a tutti stavo sviluppando una classe, un esercizio semplice tratto da un libro di C++ che usiamo all'università.
Chiede di implementare la classe Address, formatat da civico, via, appartamente, città, stato e CAP. Ho buttato giù tutto, ho sviluppato la funzione print che lo stampa. Adesso chiede una particolare funzione. cito:
"Definire una funziona membro comes_before che stabilisca se un indirizzo viene prima o dopo un altro indirizzo, in base al codice postale di entrambi."
Per ora non son riuscito a trovare una soluzione sensata perchè non capisco come confrontare i 2 codici postali. Richiamare un qualsiasi elemento da "private" farà riferimento ad uno dei 2 indirizzi, ma non all'altro. Vi scrivo il codice e la non-funzionante soluzione che avevo "trovato".
Grazie in anticipo per le rispostecodice:#include <iostream> #include <string> using namespace std; class Address { public: Address(int civicn, string stre, string cit, string stat, int CAP); Address(int civicn, string stre, int app_num, string cit, string stat, int CAP); void print (); int comes_before(Address f); private: int civic; string street; int appartment; string city; string state; int PostalCode; }; Address::Address(int civicn, string stre, string cit, string stat, int CAP) { civic=civicn; street=stre; city=cit; state=stat; PostalCode=CAP; appartment=-1; } Address::Address(int civicn, string stre, int app_num, string cit, string stat, int CAP) { civic=civicn; street=stre; city=cit; state=stat; appartment=app_num; PostalCode=CAP; } void Address::print () { if (appartment=-1){ cout << endl << street << " n° " << civic << endl; cout << PostalCode << " " << city << " " << state << endl; } else { cout << endl << street << " n° " << civic << " Appartment " << appartment << endl; cout << PostalCode << " " << city << " " << state << endl; } } int Address::comes_before(Address f) { if (f.PostalCode > PostalCode) {return f.PostalCode;} return PostalCode; }![]()

Rispondi quotando