This section teaches us the structures of a C++ program. C++ being a static language imposes some pretty strict rules on what you can do and not, so learning how a program is structured is pretty important.
These early videos don’t really do much coding, so there’s not really much to do for me here other than summarize them. Hopefully, there will be more coding later where I can show you my solution and my thoughts behind it.
Lecture 36: Section Overview
Again the section starts with a quick overview of what we will go through in later videos. Always nice to have some knowledge of what you are going to learn before you get the details.
Lecture 37: Overview of the Structure of a C++ Program
This lecture starts by showing us the keywords of C++. It has over 90(!) defined keywords, which is much more than many other languages. Luckily many of them are not used so you don’t have to remember all 90.
Then the video goes on to talk about identifiers and what’s the difference between them. It also gives you a quick overview of operators and points out some specific C++-operators. Lastly, we go into punctuation and show some examples of that.
All of these elements put together gives us the syntax of C++.
Lecture 38: #include Preprocessor Directive
Here the instructor gives us a brief overview of what the preprocessor is and what it does.
It’s a program that goes through the code before the compiler gets hold of it. It doesn’t do anything with the C++ code, nor does it understand it. It just goes through the source code, does some finessing with it and then spits it out and into the compiler.
Lecture 39: Comments
// This is a one-line comment.
/* These lines of code
are comments that go
of several lines, aka. a multi-line comment. */
I saw somewhere else that comments using // was the C++ style code, while /* */ was C-style. Don’t know if anyone can confirm or deny that?
Comments should just be used to explain complicated code. Clear to understand and obvious code doesn’t need comments.
The course also discourages the use of comments for version control. It’s much better to use Git, Subversion or other systems to get real version control of the source code.
BTW, one of the jobs of the preprocessor is to strip the source code of comments so the compiler doesn’t have to bother with them.
Lecture 40: The main() function
Now we’re coming to the main() course. 😀
The main()-function is an important part of a C++ program as each program must have one, and only one, main() function. This is where the program starts its execution, so whatever your program does, it all starts here. Most of the code is actually not in the main() function at all, but without it the program won’t do anything.
The course describes two different versions of main, depending on if the program accepts command line arguments or not, int main() or int main(int argc, char* argv).
In another course I take I see them using void main(…). After watching this lecture I wanted to find out why they do it differently, and the answer is that one does it right and the other one doesn’t.
The correct way to do it is to use int main(…). The using of void main(…) is something that Microsoft Visual Studio allows, but it is just wrong and should not be done. Trying it with g++, as is the compiler used in this course, will give you an error message.
Lecture 41: Namespaces
Namespaces are a way to avoid conflicts between different libraries or frameworks that might have functions with the same name. Using namespaces and the scope resolution operator (::), will avoid using the wrong function.
C++ has a “standard” namespace called std. Those three letters plus the two ::’s are something you will learn to type a lot.
You can use a directive to avoid typing the namespace names all the time, but it comes with some dangers. This is fine for small programs, but large and complex programs should avoid this to make sure you don’t use the wrong function due to a naming conflict.
Lecture 42: Basic Input and Output (I/O) using cin and cout
Most of the other videos in this section has been around the 3- to 4-minute mark, but this video comes in at a whopping 20+ minutes. Time to prepare for a HUGE chunk of info.
We learn about cin, cout, cerr and clog and more from iostream. This is really the first course I’ve taken that mentions cerr and clog. Probably because it’s mostly not used, but again this is a nice touch and shows that this course goes deeper than most.
Another nice point is that Frank, the instructor, talks about the difference between using \n and using endl when adding a line break. For the user, it looks the same, as the end result is that the next text that is written goes to the next line, but behind the scenes, there is a difference. Especially when working with files you will want to be aware of the difference.
The instructor also goes quickly through the pitfalls of using cin for reading inputs.
The lecture ends with examples of everything that’s talked about and again Frank does a pretty good job of explaining what happens and why you need to be careful when using cin. He actually says that usually, you don’t use cin to get user input without going into further explanation. I guess we will come back to that later.
Quiz 2: Section 5 Quiz
So no Section Challenge this section, but a nice little quiz. We didn’t really learn any new statements etc in this section, so a challenge would really just be a repeat of what we’ve already done in earlier sections.
The instructor sneaks some nice little traps into this quiz, so it’s important to read the questions and answer alternatives thoroughly before selecting your answer. I didn’t so I ended up with just 9 out of 10 correct on the first try. Was pretty easy to figure where I went wrong, but a nice touch from the course so you just don’t cruise through these.