Ciao a tutti! Ho un problema abbastanza serio, ovvero mercoledì ho l'esame di programmazione in C++ e ho appena scoperto di avere problemi coi puntatori
per favore potreste darmi una mano? Vi copio un programma che ho scritto tratto da un esame vecchio del mio prof:
Scrivere un programma per la gestione delle carte SIM ricaricabili emesse da un gestore di telefonia ai suoi clienti che possano essere usate solo per l'invio di SMS, ridefinendo l'operatore <<. Suggerisce il professore di creare una classe per il gestore di telefonia avente un archivio di sms e sim e una classe SIM ed sms. Ometto la classe del gestore perchè per ora non mi interessa, vi copio le classi sim ed sms ed il main:
codice:
//In file "Sim.h"
#ifndef SIM_H
#define SIM_H
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
#include <cstring>
using std::string;
class Sim{
friend ostream& operator<<(ostream&, const Sim&);
private:
string numero, pin, fiscale;
double residuo;
public:
Sim(string="", string="", string="", double=0);
const double getresiduo();
void ricarica(const double);
void setnumero(const string);
void setpin(const string);
void setfiscale(const string);
void setresiduo(const double);
const string getnumero();
const string getpin();
const string getfiscale();
};
#endif
codice:
//In file "Sim.cpp"
#include "Sim.h"
Sim::Sim(string a, string b, string c, double d)
{
numero = a;
pin = b;
fiscale = c;
residuo = d;
}
const double Sim::getresiduo()
{
return residuo;
}
void Sim::ricarica(const double a)
{
residuo+=a;
}
void Sim::setnumero(const string a)
{
numero = a;
}
void Sim::setpin(const string a)
{
pin = a;
}
void Sim::setfiscale(const string a)
{
fiscale = a;
}
void Sim::setresiduo(const double a)
{
residuo = a;
}
const string Sim::getnumero()
{
return numero;
}
const string Sim::getpin()
{
return pin;
}
const string Sim::getfiscale()
{
return fiscale;
}
ostream& operator<<(ostream& out, const Sim& a)
{
out << endl << "Dati della SIM: "
<< endl << "Numero: " << a.numero
<< endl << "PIN: " << a.pin
<< endl << "CF: " << a.fiscale
<< endl << "Credito: " << a.residuo
<< endl << endl;
return out;
}
codice:
//In file "Sms.h"
#ifndef SMS_H
#define SMS_H
#include "Sim.h"
class Sms{
friend ostream& operator <<(ostream&, const Sms&);
private:
double costo;
Sim *mittente;
string destinatario, data, testo;
public:
Sms(Sim a, double=0.15, string="", string="", string="");
Sms(double=0.15, string="", string="", string="");
void settesto(const string);
void setcosto(const double);
void setdestinatario(const string);
void setdata(const string);
void setmittente(string, string, string, double );
const string getdestinatario();
const string getdata();
const string gettesto();
const double getcosto();
};
#endif
codice:
//In file "Sms.cpp"
#include "Sms.h"
Sms::Sms(Sim e, double a, string b, string c, string d)
{
string h, f, g;
double i;
setcosto(a);
setdestinatario(b);
setdata(c);
settesto(d);
h = e.getnumero();
f = e.getpin();
g = e.getfiscale();
i = e.getresiduo();
setmittente(h,f,g,i);
}
Sms::Sms(double a, string b, string c, string d)
{
setcosto(a);
setdestinatario(b);
setdata(c);
settesto(d);
mittente = NULL;
}
void Sms::settesto(const string a)
{
testo = a;
}
void Sms::setcosto(const double a)
{
costo = a;
}
void Sms::setdestinatario(const string a)
{
destinatario = a;
}
void Sms::setdata(const string a)
{
data = a;
}
void Sms::setmittente(string a, string b, string c, double d)
{
mittente = new Sim(a,b,c,d);
}
const double Sms::getcosto()
{
return costo;
}
ostream& operator <<(ostream& out, const Sms& a)
{
out << endl << "Dati dell'sms: "
<< endl << "Destinatario: " << a.destinatario
<< endl << "Data: " << a.data
<< endl << "Costo: " << a.costo
<< endl << "Testo: " << a.testo;
cout << endl << a.mittente << endl << endl;
return out;
}
codice:
//In file "main.cpp"
#include "Sim.h"
#include "Sms.h"
#include "GestoreSMS.h"
int main()
{
GestoreSMS a;
Sim b1("3234686493", "3454", "RETUIN91H03G273T", 2.12);
Sim b2("337654564", "8474", "POLRTM92H06G273H", 0.89);
Sms c1(b1,0.15, "092353943", "08/07/2012", "Prova");
Sms c2(b1,0.15, "821734629", "08/07/2012", "Tanto");
Sms c3(b2,0.15, "245245245", "08/07/2012", "Non");
Sms c4(b2,0.15, "342543636", "08/07/2012", "Funziona");
cout << b1 << b2;
cout << c1 << c2 << c3 << c4;
system("PAUSE");
return 0;
}
Il problema evidentemente riguarda l'inizializzazione degli oggetti Sms, non riesco a passargli decentemente un oggetto Sim, ho provato in vari modi, passandolo fisicamente, tramite puntatori, creandolo direttamente dal costruttore... Questa è l'ultima versione che ho provato, per disperazione!
Qualche buon'anima mi darebbe una mano?