DEV Community

Cover image for 'Hello World' in C++ for the JavaScripter
addupe
addupe

Posted on • Updated on

'Hello World' in C++ for the JavaScripter

JavaScript Recap

Declaring a namespace / variable

Function body

Printing command

Execution

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.

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)

Function body
The meat of our function. The code that will be executed inside of our function. Still uses { and }.

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.

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'.

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

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.

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

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.

Side by side
double double toil and trouble

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.

^[1] quora streams
***and a great C++ resource

Top comments (0)