How to write your first c++ program in Code Blocks

I have created a new c++ program recently in eclipse but is not that satisfy with the eclipse performances when it comes to writing program in c++ and today I have heard from my friend about Code Blocks which is another free IDE that we can use to write c/c++ program so I have decided to install it on my computer. Without further delay I head towards the Code Blocks main page and then download the setup binary file and finally install Code Blocks IDE on my computer. If you have not yet installed Code Blocks then go ahead and download this free IDE from it’s website so you can start to write program while reading this tutorial, make sure you have downloaded the one comes together with GCC/G++ compiler because if not then you will have a hard time to point the path of the compiler which you have installed separately to the Code Blocks IDE.

Before we can create any c++ program in Code Blocks we need to create a new project first through File->New->Project.

select console application then press Go button
select console application then press Go button
click Next
click Next
select c++
select c++
enter details
enter details
click Finish
click Finish

Next we are going to create a new class in addition to the main.cpp file through File->New->Class. Enter below setting…

click Create
provide name to the new class and click Create

Now you will see these files under the project folder…

code blocks
code blocks

So far, are you enjoy? Because you can only become a good c++ programmer if you enjoy what you are doing.

Click on the SayHi.cpp file, delete the default content and enter below content into this file.

#include "SayHi.h"

#include <iostream>
#include <string>
using namespace std;

SayHi::SayHi(string message)
{
    setMessage(message);
}

void SayHi:: setMessage(string message)
{
    hello_message = message;
}

string SayHi :: getMessage() {
    return hello_message;
}

void SayHi :: showMessage() {
    cout << "Good morning " << getMessage() << endl;
}

The above file is the file which has implemented the SayHi class’s method. The SayHi class is under the SayHi.h file. Open up the SayHi.h file then delete everything and enter the below code into it.

#ifndef SAYHI_H
#define SAYHI_H

#include <string>
using namespace std;

class SayHi
{
    public:
        SayHi(string);
        void setMessage(string);
        string getMessage();
        void showMessage();

    private:
        string hello_message;
};

#endif // SAYHI_H

Next open up the main.cpp file and replace the default content with the new one below.

#include <iostream>

#include "SayHi.h"

using namespace std;

int main()
{
    string message;
    int quit;
    cout << "Enter greeting!" << endl;
    getline(cin, message);
    SayHi hello(message);
    hello.showMessage();

    cout << "enter -1 to quit" << endl; 
    cin >> quit;

    while(quit != -1) {
        cout << "enter -1 to quit" << endl; 
        cin >> quit;
    }
    return 0;
}

That is it, now you will need to debug your program in Code Blocks, press the red button and debug your code, if everything is alright then the program will start up after the debugging process.

debug button
debug button
enter a string to the program then press -1 to terminate the program
enter a string to the program then press -1 to terminate the program

Basically the program above is uses to receive a message from the user input and then print it out on the screen! C++ uses the header file to link the new class with the main c++ file.

There is nothing to worry about the program above if you don’t understand it because the main purpose of this tutorial is not about how to create a class in c++ but instead just to show you how Code Blocks can help you to create c++ project for commercial and your own use.

I feel comfortable with Code Blocks now and I particularly like it’s auto complete feature which really helps me to speed up my c++ program creation. When it comes to creating an application in c++ Code Blocks certainly is one of the best IDE in the market nowadays after Visual Studio!

Like this post? Kindly share!