- [Instructor] The exercise files for this course
are mostly console applications.
And that means that they display output as text on a console
and take input from the keyboard.
The console may look different on different systems.
For Unix, it's usually a terminal.
For Windows, it's the command Window or PowerShell.
On a Mac in X code, it's that little console area
at the bottom of the screen.
In the terminology of the C plus plus standard library,
the standard output stream is how text is displayed
on the console.
There are several ways to send output
to the standard output stream.
So far in this chapter, we've used the puts
and print F functions.
C plus plus also has a library class for displaying output
on the console.
The C out class as part of the standard C plus plus library.
And it works quite a bit differently than puts or print F.
In fact, it works quite a bit differently
from other objects and classes or anything else
in C plus, plus.
Let's take a moment and look at C out in a bit more detail.
This is hello, dash C out dot CPP from chapter two
of the exercise files.
You'll notice first off
that we're including the IO stream header
instead of the C standard IO header.
This is the header that includes the C out class.
You'll also notice the STD colon colon before the C out
and NL identifiers.
This is the namespace for STL identifiers.
And we'll talk about namespace later in this course.
C out is an STL class.
STL is the standard template library.
C out is not an object, it's a class
and it's rarely used to create an object,
it's normally used as a class.
C out overloads the left bit wise shift operator
to send a stream of characters
to the standard output stream.
Yes, this is weird.
Nothing else in the language does anything like this,
but this is how C out works and you'll see it used a lot.
So when I build and run this,
you'll notice that the output is exactly the same
as our puts and print F versions.
C out can be very convenient
because you can do something like this.
I can add a space here
and another bit wise left shift operator.
And I can say something like two times seven,
which is an entirely different type
than the hello world string.
And then I can have another bit wise left shift
and another quote space and another string.
And when I build and run this,
you'll notice that it displays all of these things,
just very seamlessly.
It displays both of the strings
and the integer multiplication,
all very seamlessly without my having to do a lot of work.
I don't have to manipulate the placeholders,
like I do in print F.
I just string them all together
with these overloaded operators and everything just works.
So this can be very convenient.
If you have some experience with C plus plus,
you've probably noticed that I often use puts
or print F instead of C out.
So bear with me for a moment while I explain.
These are basically two different approaches
to the same problem.
Some people maintain the C out should be preferred
because it's more object oriented or more C plus plusish
then puts and I tend to disagree with this opinion.
From a purely language standpoint,
while C out is certainly a class,
it's not used in the traditionally object oriented way.
In fact, it's rarely if ever used to create an instance
of an object.
Instead, it's used as a class
by overloading the bit wise left shift operator
to do something that has nothing to do is shifting bits.
And this is very clever, but personally,
I don't like to overload operators in a way
that redefines their meaning.
When I see an operator,
I want to know what that operator does.
The bit wise left shift operator is supposed to shift bits
by redefining its meaning to send a string of characters
to the console.
In my view, it makes the code more confusing,
not more clear.
Now I need to look at this operator more closely everywhere
because a lot of classes use it to sport C out,
instead of shifting bits and I find this very messy.
The purpose of a programming language is to make it easy
for a human to read the code and understand what it's doing.
Puts is clear, C out is not.
And this seems obvious to me.
Of course, this is a semantic argument
and many people don't agree with me and that's fine.
So let's take a look at a more technical reason
and we'll compile some code in a terminal.
This is a command line interface for a Linux box
that I use for development.
We're using it here for its compiler,
which supports static libraries.
So we can see more clearly the impact of this code.
In this directory, I have three source files,
hello dash C out dot CPP.
Hello print F and hello put dot CPP.
And if we take a look at these source files,
you'll notice that they're very simple.
This is the hello C out.
This is exactly the same as the one that we were looking
at an X code before.
Here's a print F version of exactly the same thing.
Again, very simple, nothing unusual about it.
And here's the same thing with puts.
Instead of puts, we have print F and we have C out versions
of exactly the same hello world code.
So I have a script here that will compile these
and show us the difference in size.
So I'm just going to run that script, it's called compile
and there, it compiles each of these source files
and it puts the output in dot out files.
And these are executable cause down here,
I run them and you notice that they all do the same thing.
They display hello world on the console.
And here's a directory listing of them with their size.
And you notice that the puts versions are under a megabyte
and the C out version is two and a half times larger.
What's happening here is the C out class
is using a large portion of the STL,
which turns out to be a lot of code.
All that code takes up space, uses CPU cycles
and introduces additional vectors for bugs and abuse.
And all this buys me as acute bit of syntactic sugar,
clever sure, but not tremendously useful.
As a long-time programmer, I tend to be conscious
of how much a bit of code costs, in terms of performance
and memory space.
And this costs a lot and it gives me very little benefit
for that cost.
C plus plus is often used for systems programming
and other sensitive projects where every bit of performance
and every bit of memory space is vitally important.
And so for these reasons, I tend to be cautious
about using C out and I recommend
that you use caution as well.
For many purposes, C out is completely justified.
And if you're using the STL already,
probably doesn't cost much extra.
But for many other purposes,
C out is just more expensive than it needs to be.
And puts and print F is little or no more trouble to use.
C out does have one significant advantage
over print F and that is that it's inherently aware
of the type of its arguments.
So I can use strings, I can use integers,
I can use objects from classes that I've written myself
that are written to support C out and C out it doesn't care.
It'll use them seamlessly.
And this does have value.
In the next version of C plus plus,
expected to be C plus plus 20,
there'll be a new format library that's inspired
by the format library from Python three.
This is designed to offer the best of both worlds,
being small, efficient and type aware and type safe.
It's important to understand how C out works
because it's used a lot and I'll cover in more detail later
in this course and an even more detail
in the companion STL course.
It's always important to understand the impact
of your library choices, using existing libraries
will always mean trade-offs
usually in convenience versus performance.
And a general purpose library,
like the C plus plus standard library
is often very convenient, but it's not always efficient
and in my view, not always worth the cost.