473,659 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why <<,>> operators are overloded to perform input and output?

5 New Member
Why <<(),>>() operators are overloded to perform input and output,why not any function is preferred, like scanf and printf in c
Feb 3 '11 #1
1 1385
horace1
1,510 Recognized Expert Top Contributor
a major problem with the printf() and scanf() functions is getting the conversion specification corresponding to the parameters correct in particular when first learning C. e.g. %d for int
Expand|Select|Wrap|Line Numbers
  1.  
  2.     int data
  3.     printf("data %d\n", data);
  4.  
when you have a long list of parameters of different types it is very easy to get the conversion specifications wrong and it prints garbage.
Scanf also requires the address of operator & for input of simple types
Expand|Select|Wrap|Line Numbers
  1.  
  2. scanf("%d", &data);
lots of students forget the &

C++ has the put to operator >> and get from operators <<
Expand|Select|Wrap|Line Numbers
  1.     int data;
  2.     cin >> data;
  3.     cout << "data " << data << endl;
  4.  
C++ looks at the paramter type (int in this case) and calls the correct version of the overloaded functions operator<<() and operator>>(). Makes life much simpler.

To avoid the problems associated with scanf() and printf() some C books provide libraries of IO functions like printInt() and readInt(). I don't like them prefering to teach C using the C++ << and >> operators for IO. There are pros (IO is easier) and cons, i.e. sooner or later students have to lean printf() and scanf()
Feb 3 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
1468
by: Christopher Benson-Manica | last post by:
I know I keep asking similar questions, but I really want to do this at least sort of right. Not to mention I got no on-group replies to my previous post :( I desperately want an interface that can allow classes that implement it to act sort of like streams, but I just can't seem to get everything I want... class Writable { protected: ostringstream outbuf; // or should I use private inheritance?
2
2457
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine. i would forget overriding it but i have to do it because its a coursework. here is a simple version of the class #include <iostream> #include <string> #include <vector>
3
2472
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going wrong? I am using g++ (GCC) 3.3.5 on a Debian Sarge system. The compiler complains: //**************************** //**************************** Compiler output starts *********** cd /home/red/tmp/testprogs/
0
1112
by: Pedro Werneck | last post by:
Hi list I'm trying to implement a new type in a C extension and it must support some binary operators, like &, |, ^, << and >>. With &, | and ^, the method must receive another object of the same type, perform the operation with an attribute of both, create a new object with the result as the attribute and return it. Everything works perfectly with &, | and ^, but with << and >> I need
5
5098
by: Ian Lazarus | last post by:
Hello, My question is whether it is possible to avoid assignment on the left hand side of an overloaded operator << expression, as in the code below. Without the assignment, the compiler complains. class myclass
17
3029
by: Peter Bromley | last post by:
The following code snippet does not seem to work correcly unsigned __int64 result = 0xFFFFFFFFFFFFFFFF; result = result << 64; Debugger.WriteLine(System::String::Format(S"{0:X16}", __box(result)); result = 0xFFFFFFFFFFFFFFFF; result = result >> 64; Debugger.WriteLine(System::String::Format(S"{0:X16}", __box(result)); __int64 result = -1;
9
1679
by: vineoff | last post by:
Why do I have to declare my oper<< as friend in my class as follows: class A { public: friend std::ostream& operator<<(std::ostream& out, const A& a) { return out << "foo"; } };
3
11685
by: jparulan | last post by:
Hi All, I'm using SOAP3.0. I was able to successfully call a WSDL file and get a value properly. But when the WSDL changed to have a MULTIPLE <element name> it was failing. This code works BEFORE: strCurrency = oSOAP.eServeSearch(CSTR(Request.Form("T1"))) until the WSDL file changed to have more <element name> T1 is just a field that accepts a STRING
8
1597
by: markengley | last post by:
Hi everyone hope you are all ok. Just learning C++ (as you can tell from the noob question Im about to ask). One symbol/function/assignment/whatever really bothers me which is the "<<" one. I saw this line of code: #define IN_VALIDINPUT (1 << 16)
4
5924
by: PulsarSL | last post by:
Hi I'm working on improving the "Simpletron" computer described in Deitel & Deitel's "C++ How to Program" 3rd edition. Check out http://www.lehigh.edu/~ejk0/smlspec.pas for a description if you're curious. I'm trying to add my own opcodes for string input/output using the string type from string.h
0
8851
Oralloy
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...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8535
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,...
0
8629
tracyyun
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...
0
4176
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...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.