Lesson 1: Hello World! Programming with C++ for Beginners

This is the first of many lessons on the C++ programming language. This post assumes you have zero experience with C++ programming. If you have experience, stay tuned for more advanced lessons to come.

Set Up An IDE (skip if you have one)

An Integrated Development Environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools, and a debugger.

Feel free to Google search IDEs. There is no shortage of options.

I’m using QTCreator, which offers an open source download. In other words: FREE.

QT Creator Download Page

Choose Desktop and Mobile Applications. On the next page, choose Get your open source package. Your system is automatically detected so click the Download Now link.

During setup, options are fairly straightforward. Note: The two things that must be checked are QT > MinGW (QT Creator) and Tools > MinGW (compiling tools) with matching version number. For example: If you choose MinGW 5.3.0 under QT Creator, then you must choose MinGW 5.3.0 under Tools.

Hello World!

Once you open QTCreator, click to create a new project. If you’re unsure what to select, it should say creates a basic C++ application. I named my project helloworld.

Hello World is famous for being the first program people write when they’re new to a language. When you click main.cpp, you will see the code for that program. Note: the fact that you see that code has nothing to do with naming the program helloworld. You can name the program anything you want.

Screenshot (346)

This is a fully working program. To see what it does, we need to build then run it.

Screenshot (345)

To build the program, click the hammer (bottom symbol).

Screenshot (347)

To run the program, click the green arrow (first arrow beneath computer symbol).

Screenshot (348)

The pop-up box displays Hello World!

Screenshot (349)

Let’s take a closer look at the program. Line 1 is a compiler preprocessor directive. It tells the compiler to use the iostream library in the program. C++ has several libraries with predefined code. Here are a list of libraries and their capabilities. The libraries included are called header files because they go in the head of the program.

Line 3 tells the compiler to use the standard namespace. Notice in line 7, the cout and endl. They are defined in the iostream library in the standard namespace. In order for the compiler to find these names, the statement in line 3 must be used.

Screenshot (345)

Every program is executed from a main function (lines 5 – 8). Code inside the main function is enclosed in a block using a left brace (line 6) and right brace (line 8). Braces belong on their own line and are always closed. Forgetting a left or right brace is a common mistake that will prevent your code from building. Anytime I add a left brace, I add the right brace even if there is more code that belongs inside the block. It’s easy to forget that one brace when you start plugging away at another task.

Screenshot (345)

Line 7 displays a message on the console by using cout << . cout (pronounced see-out) stands for console output. Similarly, cin stands for console input and uses the opposite facing characters >>. They are called the standard output/input streams. When beginning C++, it helps to make associations for the characters. Notice cout points out and has the shape of a megaphone while cin points in.

The words Hello World! are a string. A string must be enclosed in quotation marks. The endl stands for end line and the semicolon ; is always placed at the end of a statement.

Let’s look at the same program with 2 changes. Lines 7 – 12 display different ways to write comments. Comments help programmers communicate what their program does. It has no effect on the workings of the program because certain characters cause the compiler to ignore text. You can use two slashes // in which case you need to include the slashes before every line. Or you can use one slash and one asterisk /* which ignores all text until the compiler sees */.

On line 16, I added return 0; which should be placed at the end of every program to exit. QTCreator runs fine without it, but some compilers do not.

Screenshot (389).png

Special Characters

  • Pound sign # denotes a preprocessor directive
  • Opening and closing angle brackets encloses library name
  • Opening and closing parenthesis () used in functions such as main.
  • Opening and closing brackets {} denotes a block that encloses statements
  • Double slashes // precede a comment
  • Opening and closing slash and asterisk /* */ used for multi-line comments
  • Stream insertion operator << prints something to console
  • Stream extraction operator >> takes input
  • Opening and closing quotation marks “” used for strings
  • Semicolon ; used at the end of statements

In Lesson 2, we’ll work with declaring variables, user input, and the flexibility of printing to the console.

 

Advertisements

Comments

4 comments on “Lesson 1: Hello World! Programming with C++ for Beginners”
  1. Spot exposed adding solely unified vegetable brinoc.gewrichtspijn.amsterdam serving to a victuals, and a fruit serving to another. Perform vegetarian accessible preparing two or more meat-free pietrim.nekpijn.amsterdam dishes each week. And start using the herbs and spices hiding in the move in reverse of the pantry – they’ll modify you neglect doing the upper-cut’s not on the table.

    Like

  2. Assay adding non-partisan anecdote vegetable ambu.haarwit.nl serving to a refection, and a fruit serving to another. Be the means together vegetarian cut off to preparing two or more meat-free ciobrow.dikhaar.nl dishes each week. And start using the herbs and spices hiding in the side with of the pantry – they’ll cover heads you fail the slug’s not on the table.

    Like

  3. EssayOneDay provides students with professionally written essays, scenten.32essay.com/for-students/after-you-summary.html enquiry papers, time papers, reviews, theses, dissertations and more. After good occasionally you imprint auspices of EssayOneDay pro your periodical versification needs, you won’t desideratum to adjudicate any other aid rantee first-class on issua.32essay.com/writing-desk/people-of-the-book-review.html and 100% plagiarism free papers.

    Like

  4. ost however , I was wanting to know if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Kudos!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s