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<<(ost ream &out, const Simulatore &s){
out<<"Risultato simulazione"<<e ndl;
for(int i=0; i<static_cast<i nt>(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>>(istr eam &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. 1 1496
"Piotre Ugrumov" wrote 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<<(ost ream &out, const Simulatore &s){ out<<"Risultato simulazione"<<e ndl; for(int i=0; i<static_cast<i nt>(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 <<?
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); }
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>>(istr eam &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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: abbas reji |
last post by:
--0-599929911-1059996886=:4358
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Content-Id:
Content-Disposition: inline
________________________________________________________________________
|
by: CHRIS ALOZIE |
last post by:
NIGERIAN TELECOMMUNICATION LIMITED.
FEDERAL SECRETARIAT COMPLEX,
GARKI, ZONE II
ABUJA.
E-MAIL: chrisalozie@uboot.com
DATE:16th August 2003 REF:rf/ntl-fgn
|
by: Mike |
last post by:
Hello,
I have a few rather urgent questions that I hope someone can help with (I
need to figure this out prior to a meeting tomorrow.) First, a bit of
background: The company I work for is developing a web-based application,
one part of which involves allowing the user the ability to page through
transaction "history" information.
The _summary_ history table will have the following fields: ServiceName,
Date, User-Ref1, User-Ref2,...
|
by: ritesh |
last post by:
Hi,
I have been reading some text on C and C++ (i.e advanced books). One
of the books mentioned that C++ requires a runtime support whereas C
does not - what the author was trying to say was that once you compile
a C program the executable created is all that is needed whereas if you
compile a C++ program the executable created requires a C++ runtime
installed on your system to run the program.
Can someone please provide more...
|
by: Henry |
last post by:
Hi Everyone,
My name is Henry and I'm a Master of Computing student at Unitec New Zealand
(www.unitec.ac.nz). I'm currently doing my Thesis titled: "Utilizing
Newsgroups as Customer Support Tool in Software Development Companies". This
research is about whether or not Newsgroups are effective for providing
customer support with low cost.
This is **strictly** academic, non-profit research, only doing it to
complete my degree. You are...
| |
by: |
last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I
keep getting the following error:-
Error In:
http://www.mywebsite.org/WebResource.axd?d=5WvLfhnJp5Lc8WhQSD4gdA2&t=632614619884218750
--------------------------------------------------------------------------------
System.Security.Cryptography.CryptographicException: Padding is invalid and
cannot be removed. at...
|
by: Saps |
last post by:
Hi all. Can anyone help me here. I have loads of .sql files and i need
a way to call these from my asp page so the user can run them from the
browser. Meaning i have a page with a list of all scripts. each when
clicked i am able to run the script. so HOW and what do i do to call
and run the .sql
Thanks
|
by: johnnash |
last post by:
ok so i was just reading about #ifndef and header files. I have a few
doubts. It would be great help if someone can clear my doubts..
#ifndef A_H
#define A_H
....
code
.....
#endif
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| | |