'Hello World' in C++ for the JavaScripter
Published: 2020-02-03 01:18:27

###JavaScript Recap

Declaring a namespace / variable
![](http://i.imgur.com/swfkzA9.png)
Function body
![](http://i.imgur.com/8hbtkDF.png)
Printing command
![](http://i.imgur.com/lyZsUaO.png)
Execution
![](http://i.imgur.com/APCog6s.png)

###C++ Intro

*Compiling script*
In JavaScript, our code is read by interpreters in the browser at run time. C++ is read by a compiler which converts the language into machine code before program run. But for the compiler to have everything it needs to interpret the given code. This first line `#include <iostream` is read by the preprocessor as a `preprocessor directive` which runs before actual compilation of the code and allows the preprocessor to paste in any needed files. 
![](http://i.imgur.com/UJhg0fK.png)

*Namespace declaration (sort of)*
Okay, here our namespace declaration contains a two parter. There's the namespace for the function itself, which is actually `main()` in this example and then there's the namespace declaration for any methods/other functions that will be utilized in our file. The `using namespace std;` as I understand it, is another kind of file that contains methods defined as prefixes, like a library in JavaScript, and we declare that we will be using that 'library' of functions. `cout` here exists within the `std` set of functions. If we needed a function that existed within a different 'file', we would include that corresponding namespace.
*There are two ways of doing this and this one is actually not best practice (more on that below)
![](http://i.imgur.com/KFCfMsC.png)

*Function body*
The meat of our function. The code that will be executed inside of our function. Still uses { and }.
![](http://i.imgur.com/LF69leE.png)

*Execution done by compiler*
We get our first glimpse at `main()`, which is a special function in all C++ programs. This is our actual function declaration. It is also our execution, because in C++ the main function, no matter where it is located in our file, will always be called first by the compiler. The `int` here is what defines the `type` of function. In JavaScript, we would have our variable declaration keyword either: 'let' or 'const'. Int in C++ is a 'fundamental variable type' It is short for integer and describes what the function will return. It's a little confusing when just printing something, but there's a 0 return that is usually included that gives a signal that no errors are included. I've left it out of these first examples, but you'll see it in the revised 'Hello World' solution further down.
![](http://i.imgur.com/ayBor27.png)

*Printing command*
This is the equivalent to our console log. `cout` tells our program to print and the `<<` gives direction to what will be printed. In our case, our dearly beloved: 'Hello World'.
![](http://i.imgur.com/MuPhTP8.png)
![](http://i.imgur.com/VJVt7SC.png)

*Input Output Visualization*
Back to `iostream` briefly, this is a visualization I found useful in considering what `standard input and output operations` are and how they're run in C++. I've heard the analogy of water and oil flowing through a pipe. I'll link the Quora response that explained this more in depth.^1
![](http://i.imgur.com/mBCGeYI.png)

Abstraction Scale
In my research, I've read C++ classified as both a high level language and a mid level language. What I understand is it's still high level, meaning that it uses a great deal of abstraction from machine code to let us use language syntax and variable names as words, etc. But it's a little lower level than JavaScript.
![](http://i.imgur.com/5mcoOMr.png)

What is C++ used for? 
*like everything*
- video games
- MySQL built with C++
- operating systems
- compilers
- 3d modeling
- adobe systems
- Curiosity rover's autonomy system

![](https://i.imgur.com/mM0HX1A.jpg)

*Another solution*
You'll notice the namespace `std` is included directly alongside the method that is pulled from that library. This is considered better practice, because you avoid ambiguity when including multiple libraries in a single file of code. This example also includes a `return 0` which I still have a lot of questions about myself, but I understand is kind of like an error check and ends the code block, similarly to the way returning in JavaScript defines a function and acts as a break point.
![](http://i.imgur.com/SsnPoJc.png)
![](http://i.imgur.com/N19m4V7.png)

*Side by side*
*double double toil and trouble*
![](http://i.imgur.com/TSivPwy.png)

*Thanks for reading!*
If anyone finds any errors I've made here or has suggestions, please let me know in the comments below. I'm trying to get a better grasp on C++ coming from a JavaScript world.
![](http://i.imgur.com/bVdzIOn.png)

^[1] [quora streams](https://www.quora.com/What-is-the-purpose-of-the-C-header-file-iostream)
***[and a great C++ resource](http://www.cplusplus.com/doc/tutorial/program_structure/)