Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Left shift operator on an output stream object - C++

When explaining "Hello World" in C++ (see here), delegates who are already familiar with programming in other languages pick up on the line
  cout << "Welcome - and enjoy your C++ course" << endl;
and ask for an explanation.

Yes - it is a bit different, isn't it?

The explanation and a full understanding come during the course though, and not necessarily at the early stage. But here goes:

The << operator is described (in C++ documentation) as the left shift operator, and indeed when used on an int data type, it takes the bit pattern that defines the integer and shifts it to the left by the number of bits given - for example:
  int value = 13;
  value = value << 2;

would take the internal format of the integer 13 (1101) and shift it 2 left (110100) - or 52 in decimal.

cout is an output stream object - and the concept of doing a "left shift" on a stream is a nonsense idea, so the authors of C++ used the operator which was going "spare" for something else - to send the contents of the expression to the right of the << to the stream named on its left.

The << operation on an output stream returns a reference to the stream, thus allowing further << operations to be concatanated onto the first one - allowing for a series out outputs in a single expression.

Full example of shifting in C++ [here] ... note that for an input stream (our example uses cin, the >> operator (right shift) is also overloaded, indicating that data from the input stream is sent to the named variable.
(written 2015-10-30)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C235 - C and C based languages - I/O in C++
  [4563] Formatting and outputting your own classes in C++ - (2015-10-30)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3807] Reading (and writing) files in C++ - (2012-07-18)
  [3390] Printing objects in C++ - (2011-08-13)
  [3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
  [3124] C++ - putting the language elements together into a program - (2011-01-08)
  [1675] Comparing Objects in C++ - (2008-06-13)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)

C231 - C and C based languages - Introduction to C++
  [4561] Hello World in C++ - a first program, with the process explained - (2015-10-30)
  [4466] Moving from C to C++ - Structured to Object Oriented - a lesson for engineers - (2015-03-28)
  [3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3053] Make - automating the commands for building and installing - (2010-11-16)
  [3052] Getting your C++ program to run - (2010-11-15)
  [2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2004] Variable Scope in C++ - (2009-01-22)
  [928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
  [336] Targetted Advertising - (2005-06-05)
  [318] Choosing a theme - (2005-05-20)
  [317] Programming languages - a comparison - (2005-05-20)


Back to
Hello World in C++ - a first program, with the process explained
Previous and next
or
Horse's mouth home
Forward to
Formatting and outputting your own classes in C++
Some other Articles
C - why is slow to write and debug) but fast to run?
Allocation of memory for objects in C++ - Stack v Heap
Perl, PHP, Python, Lua, Tcl, C++, Ruby - final public courses for 2015
Left shift operator on an output stream object - C++
Variables, Pointers and References - C and C++
When do I use the this keyword in C++?
Well House Consultants - Python courses / what's special.
Function prototype - what they are and why you should use them - C and C++
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4562_Lef ... ct-C-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat