Lesson 2: Writing Your First Program

After lesson 1 you should now have NetBeans installed. Of course if you know or prefer a different IDE and have that installed then that is perfectly fine. The coding is still the same but we’re using NetBeans for our examples on this website.

  1. Launch Netbeans and we will start our first C++ Project
    1. Click on Start a New Project.

      netbeans ide create project step 1

    2. Choose C/C++ Application from the Projects panel.

      netbeans ide create project step 2

    3. Name your Project. I call mine “MyFirstCppProject”
    4. Choose a location to store your NetBeans Project in. I normally create a central folder somewhere for all of my different C++ projects
    5. Leave everything else as it is. It should be something similar to this screenshot here. This is also where you will see the build host is set to localhost and the tool collection is set to the MinGW compiler we installed earlier. If this still isnt set for you then you need to go back and re do steps 6a – 6d In Lesson 1
    6. Uncheck the “Create Main File” check box. It is not needed for the first program here.
    7. Go ahead and click “Finish”.

    netbeans ide create project step 3

  2. Right click on “Source Files” > New > C++ Source File.

    NetBeans New C++ Source File
    NetBeans Naming New C++ File

  3. You will now see a brand new almost blank C++ file called helloworld.cpp. There is some default text added by NetBeans about the licence of the file / code. For now just remove it. We may re-visit this in the future.

    NetBeans C++ Source File Default Licence

  4. First thing we do is write a comment to explain what we’re going to do.
    Below is a block of code with comments explaining what each line of code is doing. There are 2 types of comments, // is a single line comment, and /* */ is a multi line comment.

    In this block of code you can see that I have written comments using both the single line and multi-line method.

    cout is the function name as mention in the comments.

    As said in the comment above this line of code. This informs the compiler that we are using the standard c++ library. This is not a necessary line of code in order to write this or indeed any C++ program. However if we did not define this namespace the you would have to call the namespace / library before each standard C++ function. For example; Take this line of code again:

    This would become:

    Now imagine a program hundreds of lines long with many function calls and each time you did that you had to define the namespace. No thanks. The rest I feel is all self explanatory.

    the word int is actually another datatype called an Integer. Which is just a number. The use of the word int here is to tell the compiler that our function called main will return an integer / number upon completion. This is also why we use the return 0; code at the end of the function.

    The round brackets, or parentheses for our American friends, after the word main are empty at this stage. This is where arguments / variables get passed into functions when they are called. For this small program they are not needed and so the brackets are empty.

    The curly brackets however, they define the boundaries of a function. Everything within those brackets is what will happen every time this function is called. Within the curly brackets that section of code is typically known as a “Block” of code. In our instance all we’re actually doing is outputting a String to the screen and the returning 0 to inform the compiler that the program has finised.

    Lets go ahead and run this now.

  5. Compile and Run the code. At the top of NetBeans you will see a blue hammer and green triangle / play button. After clicking play your NetBeans IDE will compile all of the code, you’ll see it doing so at the bottom of your window. Then the code will run and your output should be displayed in the window.

    NetBeans C++ Compile and Run
    NetBeans C++ Run Output
    NetBeans C++ Run Successful

Congratulations, You have just written, compiled and ran your own C++ program. Pretty simple eh? Follow on to the next lessons to find out some of the more complex but not scary things we can do in C++

Add a Comment

Your email address will not be published. Required fields are marked *