Last time, we implemented Hello World in C. Since the syntax is slightly different, I’ve decided to have a separate tutorial on how to implement Hello World in C++. Let’s dive in!
Table of Contents
C++ Background
As is tradition at this point, I rounded up some information on C++ from Wikipedia.
According to Wikipedia, C++ is a general-purpose programming language developed back in 1979 by Bjarne Stroustrup. Back then, C++ was called “C with Classes.” As you can probably imagine, the name was changed to C++ which is a play on the increment operator.
C++ is probably the most powerful language in existence as it supports both imperative and object-oriented programming features. In addition, it allows you to get extremely close to hardware, so it’s not uncommon to see C++ used in embedded environments and game development.
However, with great power comes great responsibility. As a result, I personally find C++ to be an extremely rough language to use. Unlike Python, which was designed with the “one-way-only” paradigm, C++ allows you to code up a solution in literally any way you want. This can lead to some truly unreadable and complex code.
Hello World in C++
Even though C++ was built on C, the implementation of Hello World in C++ is slightly different:
#include <iostream> using namespace std; int main() { cout << "Hello, World!"; return 0; }
It appears this implementation of Hello World may be competing with Java for hardest to learn. But at any rate, let’s break it down.
Once again, the first line is an include statement. Here, we’re including the IO stream code in our solution. This is how we gain access to the IO objects like cout.
Next, we gain access to the std namespace which basically allows us to shorten std::cout to cout. It’s really just a style thing that eliminates a lot of verbosity that you might get with other languages like Java. Of course, if another namespace also has a cout, we’ll run into problems.
After that, we make a main method as usual. This is exactly the same as C, so I won’t bother explaining the return type bit again.
Finally, we write our string to the cout stream. The syntax is a bit strange, but basically we can imagine that the Hello World string is inserted into the cout stream. In fact, the double-arrow operator is the insertion operator, and it has some fun properties. For instance, the operator can be chained together, but that’s a topic for another time.
How to Run the Solution
Perhaps the easiest way to run the solution is to leverage the online gdb compiler.
Alternatively, you can try to run the C++ code in a similar way described in the last article. Honestly, it’s pretty easy to write and run C/C++ code on most platforms:
gcc -o reverse-string reverse-string.cpp
Unfortunately, Windows pretty much requires the use of Visual Studios. So, instead of sharing platform specific directions, I’ll fallback on my online compiler recommendation. Let me know if you have questions otherwise in the comments.
Sample Programs in Every Language
At this point, we’ve covered Python, Java, C, and C++. Fortunately, there are still several general-purpose languages to tackle. Up next, we’ll learn how to implement Hello World in C#. After that, we’ll start looking into other popular languages like Ruby and JavaScript.
If you enjoyed this article and you want the series to continue, share your favorite languages in the comments. Also, make sure to share this article with your friends.
Recent Posts
Python has a cool feature that allows you to overload the operators. Let's talk about what that means and how you might use it!
This week, we're hitting another beginner topic: the assignment operator. While the idea is simple, the concept is rich in related ideas like scope, iterable unpacking, and augmented assignment.