Hello World in C++

Hello World in C++ Featured Image

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 WikipediaOpens in a new tab..

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 compilerOpens in a new tab..

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.

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts