Programming in C++ – Lesson 0

Preparing the Environment

 

And here we go..! Welcome to first programming lesson in this series, today we will prepare our desktop/laptop environment in order to start coding, and we will write our first Hello World applications.

In this course, we will use a program called CodeBlocks (click here to download). its an IDE (Integrated Development Environment) which helps you create and compile (run) C++ programs, so download it from the link above, install it the way you install any program, and lets start working 🙂

CodeBlocks main window

My favorite way of learning any new program is to try it out, so lets try to create and compile our first C++ code:

Open CodeBlcoks

  1. Go to File -> New -> File..
  2. Chose  C/C++ source, then hit Go
  3. Click Next >
  4. Make sure that C++ option is selected, click Next >
  5. Click on the dots icon under (Filename with full Path) , create a new folder with the name (Tutorial 0), navigate to it, then write (helloWorld) inside the File Name field, and click Save
  6. Click on Finish

 

Writing & compiling your first code

You will perform these steps each time you want to create and run a new code, so get used to them. Next, copy and paste the following code:

#include <iostream>
using namespace std;
int main()
{
     cout << "Hello World" << endl;
     return 0;
}

Now its time to compile & run our first code..! You will find a small toolbar at the top   , the first icon is to compile the program, which means: build up your code, the second icon is to run it and see the result, and the last one, is to compile & run the code at the same time. So lets try to compile and run our code to see the results; click the third icon (compile and run):

 

Congratulations..! you have successfully configured your CodeBlocks, and compiled and run your first code..! Now its time to explain what did we write exactly so we understand the basics of programming.

 

Code Explanation

#include <iostream>
using namespace std;
int main()

This is the head start of any code you will write in the future. The #include<iostream> is the header file for input and output  standard C++ Library. Its like saying (OK C++, I will output some lines “Like the Hellow World statement we just had”, and I will let the user to enter some parameters “will come lately’, so be prepared.!”).

using namespce std; namespaces allows us to group a set of global classes, objects and/or functions under a name. If you specify using namespace std then you don’t have to put std:: throughout your code. The program will know to look in the std library to find the object. Namespace std contains all the classes, objects and functions of the standard C++ library, see how our code will have to look like if we didn’t put that statement on:

#include <iostream>
int main()
{
  std::cout << "Hello world!n";
  return 0;
}

See..! You will have to write std:: before every single step you perform, even if it was a simple input, output, whatever, who wants to do that :/

int main(), This is the main function prototype that you are going to use everywhere, its where you will put all of your code inside in order to make it run.

cout << "Hello World" << endl;

A couple of things you have to note and concentrate on here:

To print a value to the screen, write the word cout, followed by the insertion operator (<<), which you create by typing the less-than character (<) twice. Even though this is two characters, C++ treats it as one. Next, put the word you want to print between two quotations, this actually tells the compiler that you want to print whatever is between these two quotes. And finally, indicates that you want to print an “end line”, or a new line, after the statement.

 

Important notes:

  • When you want to code any program, try to think about the problem through out the process. Think about a logical solution for the problem, and try to implement it. And remember: you can NEVER find any two identical codes for one problem. There are always multiple solutions and each one of us tend to solve it the way he/she thinks that is the best.
  • Don’t forget the semi column after each statement, or you will never be able to compile the program. A semi column indicates that “here where the command cout will end, the next line will be a new command line”.
  • The curly brackets {  } says that: the function “int main()” code will start at the beginning of this bracket and end when its closed.

 

Well, that’s it for now, and I’ll be more than happy to read all of your comments or suggestions.

Comments 9

  1. when start a new file on code blocks mac edition it works fine but when i click ( built and run) the terminal doesn’t work can you help me.
    also when i open code blocks then hit (new project) the terminal works
    is there any deferent between both

  2. Assalamu Alaikum..
    thank you for the HUGE effort you’ve put in this first lesson 🙂 😀
    the way of explaining is very simple and easy to understand ,, and everything is organized very well .. ^^
    If i was taking programming this semester this would be very useful for me, but unfortunately I’m already done with the course ^^”

    keep it up and jazakaAllah alf khair 🙂

    1. Post
      Author

      Wa Alykom al Salam 🙂
      You are most welcome, and I’m happy that the efforts were recognized by someone who already finished the course al7amdulellah 🙂 , and congratulations at finishing the course..! you can actually do many things regarding stuff that are actually programming related, but now by choice, not forced to 🙂

      Thanks for passing by, and Jazakom Khairan Wa Eyyakom ya rab 🙂

  3. Great job bro 😀 ..
    As Mohammed said it is better to have an idea about what we are going to face and I can see that your tutorials are the best way to do that keep the hard work and waiting for the next tutorials ^^ 😀 ..

    1. Post
      Author
  4. Aslam 3likom
    I think reading every lecture you post before the lab will give me an advantage in the class. Having a background of what im going to study will give me straight A in the course . I’ll keep up with you class by class .
    Thanks is not enough , don’t know what to say.

    1. Post
      Author

      I’m so happy to read your comment, it gives me a great encouragement to continue with these tutorials, specially when you find someone who really benefit from it.. Thank you for passing by, and I hope to see you in next tutorials 🙂

      1. I agree with Mohammad Yousuf its so simple and organized and if it contains the main point in every class it will be helpful specially for the beginners.. Thanks a lot.. I really appreciate your d work! 🙂

        1. Post
          Author

          You are most welcome 🙂 .. Actually I’m not going to follow the labs. I prefer to describe the main ideas (the one which will be of benefit students in all classes) rather than to concentrate into a specific lab or point, in that case, a student will have enough knowledge to solve any of the labs provided 🙂

Leave a Reply

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