Urgent Help, some doubts

P

Piotre Ugrumov

I have some problems and some doubts.
I have implemented a class hierachy. The base class Velivolo, from Velivolo
derive Militare and Civile, from militare derive Aereo and Elicottero, from
Civile derive Passeggero e Merce. In every class I have implemented the
overload of << and >>. In a class Simulator (not in hierachy) I have
implemented the overload of << in this way:
ostream &operator<<(ostream &out, const Simulatore &s){
out<<"Risultato simulazione"<<endl;
for(int i=0; i<static_cast<int>(s.v.size()); i++)
out<<*(s.v)<<endl; //v is a vector of Velivoli pointer, the vector
contains object of the class Elicottero, Merce, Passeggero etc..;
return out;
}
This overload work but It's call the overload << of the class Velivolo, how
can I implement a polymorphic behaviour for <<?


Another doubt. I have implemented the class Film and the class Film. In
these 2 classes I have defined the overload of << and >>. The overloads of
<< work correctly. The overload >> of Actor works correctly too, the
overload >> of film gives me problems.
I have implemented thsi last overload in this way:

istream & operator>>(istream &in, Film &f){
string name;
int anno;
Actor a;
in>>name;
in>>year;
in>>a; //I use the overload >> of Actor
f.setTitle(name);
f.setyear(year);
f.addActor(a);
return in;
}

In the main I write this:
Film f;
cin>>f;
cout<<f;
I have the problem during execution at the moment of cout<<f; at this moment
I see stranges symbols, letters and number. The program stop the execution.
Is corettect using the overload>> of a class in the overload>> of another
class?
If I would insert a film's title with 2 or more words How can I do it?
For insert a film I write this in the prompt:
FilmTitle(one word) ProductionYear(an int) Actor(Name Surname).
Thanks to all and excuse me.
 
B

Buster

I have some problems and some doubts.
I have implemented a class hierachy. The base class Velivolo, from Velivolo
derive Militare and Civile, from militare derive Aereo and Elicottero, from
Civile derive Passeggero e Merce. In every class I have implemented the
overload of << and >>. In a class Simulator (not in hierachy) I have
implemented the overload of << in this way:
ostream &operator<<(ostream &out, const Simulatore &s){
out<<"Risultato simulazione"<<endl;
for(int i=0; i<static_cast<int>(s.v.size()); i++)
out<<*(s.v)<<endl; //v is a vector of Velivoli pointer, the vector
contains object of the class Elicottero, Merce, Passeggero etc..;
return out;
}
This overload work but It's call the overload << of the class Velivolo, how
can I implement a polymorphic behaviour for <<?


You need to put a virtual function in the base class:
virtual std::eek:stream & put (std::eek:stream & stream) { ... }

and override it in each derived class. Then you only
need to overload operator<< for the base class:

std::eek:stream &
operator<< (std::eek:stream & stream, const Velivolo & x)
{ return x.put (stream); }
Another doubt. I have implemented the class Film and the class Film. In
these 2 classes I have defined the overload of << and >>. The overloads of
<< work correctly. The overload >> of Actor works correctly too, the
overload >> of film gives me problems.
I have implemented thsi last overload in this way:

istream & operator>>(istream &in, Film &f){
string name;
int anno;
Actor a;
in>>name;
in>>year;
in>>a; //I use the overload >> of Actor
f.setTitle(name);
f.setyear(year);
f.addActor(a);
return in;
}

You can't just assume that every >> has worked correctly.
Try this:

std::istream & operator>> (std::istream & in, Film & f)
{
std::string name;
int anno;
Actor a;

if (in >> name >> year >> a) // if any of these >>s fails, the
rest are no-ops
{
f.setTitle (name);
f.setYear (year);
f.addActor (a);
}

return in; // assume the caller will also check return value of >>
}


In the main I write this:
Film f;
cin>>f;
cout<<f;
I have the problem during execution at the moment of cout<<f; at this moment
I see stranges symbols, letters and number. The program stop the
execution.

Perhaps you are outputting an uninitialized Actor object.
With the change I made te operator>>, use:

int main ()
{
//...
Film f;
if (std::cin >> f) std::cout << f;
else std::cerr << "Input error.\n";
//...
}
Is corettect using the overload>> of a class in the overload>> of another
class?

No problem.
If I would insert a film's title with 2 or more words How can I do
it?

Trickier. You need a file format. The simplest way I can think of is
to use
a whole line of the text file for the film title and read it using
std::getline.
For insert a film I write this in the prompt:
FilmTitle(one word) ProductionYear(an int) Actor(Name Surname).
Thanks to all and excuse me.

Good luck,
Buster.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top