Connecting Tech Pros Worldwide Forums | Help | Site Map

Urgent Help, some doubts

Piotre Ugrumov
Guest
 
Posts: n/a
#1: Jul 22 '05
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[i])<<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.




Buster
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Urgent Help, some doubts


"Piotre Ugrumov" wrote
[color=blue]
> I have some problems and some doubts.
> I have implemented a class hierachy. The base class Velivolo, from[/color]
Velivolo[color=blue]
> derive Militare and Civile, from militare derive Aereo and[/color]
Elicottero, from[color=blue]
> Civile derive Passeggero e Merce. In every class I have[/color]
implemented the[color=blue]
> overload of << and >>. In a class Simulator (not in hierachy) I[/color]
have[color=blue]
> 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[i])<<endl; //v is a vector of Velivoli pointer, the[/color]
vector[color=blue]
> contains object of the class Elicottero, Merce, Passeggero etc..;
> return out;
> }
> This overload work but It's call the overload << of the class[/color]
Velivolo, how[color=blue]
> can I implement a polymorphic behaviour for <<?[/color]

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

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

std::ostream &
operator<< (std::ostream & stream, const Velivolo & x)
{ return x.put (stream); }
[color=blue]
> Another doubt. I have implemented the class Film and the class[/color]
Film. In[color=blue]
> these 2 classes I have defined the overload of << and >>. The[/color]
overloads of[color=blue]
> << work correctly. The overload >> of Actor works correctly too,[/color]
the[color=blue]
> 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;
> }[/color]

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 >>
}


[color=blue]
>
> 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[/color]
this moment[color=blue]
> I see stranges symbols, letters and number. The program stop the[/color]
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";
//...
}
[color=blue]
> Is corettect using the overload>> of a class in the overload>> of[/color]
another[color=blue]
> class?[/color]

No problem.
[color=blue]
> If I would insert a film's title with 2 or more words How can I do[/color]
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.
[color=blue]
> 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.[/color]

Good luck,
Buster.


Closed Thread