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
C231 - C and C based languages - Introduction to C++ [317] Programming languages - a comparison - (2005-05-20)
[318] Choosing a theme - (2005-05-20)
[336] Targetted Advertising - (2005-06-05)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[2004] Variable Scope in C++ - (2009-01-22)
[2169] When should I use OO techniques? - (2009-05-11)
[2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
[3052] Getting your C++ program to run - (2010-11-15)
[3053] Make - automating the commands for building and installing - (2010-11-16)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
[4466] Moving from C to C++ - Structured to Object Oriented - a lesson for engineers - (2015-03-28)
[4561] Hello World in C++ - a first program, with the process explained - (2015-10-30)
C235 - C and C based languages - I/O in C++ [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
[1675] Comparing Objects in C++ - (2008-06-13)
[3124] C++ - putting the language elements together into a program - (2011-01-08)
[3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
[3390] Printing objects in C++ - (2011-08-13)
[3807] Reading (and writing) files in C++ - (2012-07-18)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[4563] Formatting and outputting your own classes in C++ - (2015-10-30)
Some other Articles
C - why is slow to write and debug) but fast to run?Allocation of memory for objects in C++ - Stack v HeapPerl, PHP, Python, Lua, Tcl, C++, Ruby - final public courses for 2015Left 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++