0.4 — Introduction to C++ development

Before we can write and execute our first C++ program, we need to understand in more detail how C++ programs get developed. Here is a graphic outlining a simplistic approach:

The software development process

Step 1: Define the problem that you would like to solve

This is the “what” step, where you figure out what problem you are intending to solve. Coming up with the initial idea for what you would like to program can be the easiest step, or the hardest. But conceptually, it is the simplest. All you need is an idea that can be well defined, and you’re ready for the next step.

Here are a few examples:

  • “I want to write a program that will allow me to enter many numbers, then calculates the average.”
  • “I want to write a program that generates a 2d maze and lets the user navigate through it. The user wins if they reach the end.”
  • “I want to write a program that reads in a file of stock prices and predicts whether the stock will go up or down.”

Step 2: Determine how you are going to solve the problem

This is the “how” step, where you determine how you are going to solve the problem you came up with in step 1. It is also the step that is most neglected in software development. The crux of the issue is that there are many ways to solve a problem -- however, some of these solutions are good and some of them are bad. Too often, a programmer will get an idea, sit down, and immediately start coding a solution. This often generates a solution that falls into the bad category.

Typically, good solutions have the following characteristics:

  • They are straightforward (not overly complicated or confusing).
  • They are well documented (especially around any assumptions being made or limitations).
  • They are built modularly, so parts can be reused or changed later without impacting other parts of the program.
  • They are robust, and can recover or give useful error messages when something unexpected happens.

When you sit down and start coding right away, you’re typically thinking “I want to do <something>”, so you implement the solution that gets you there the fastest. This can lead to programs that are fragile, hard to change or extend later, or have lots of bugs (technical defects).

As an aside...

The term bug was first used by Thomas Edison back in the 1870s! However, the term was popularized in the 1940s when engineers found an actual moth stuck in the hardware of an early computer, causing a short circuit. Both the log book in which the error was reported and the moth are now part of the Smithsonian Museum of American History. It can be viewed here.

Studies have shown that only 20% of a programmer’s time is actually spent writing the initial program. The other 80% is spent on maintenance, which can consist of debugging (removing bugs), updates to cope with changes in the environment (e.g. to run on a new OS version), enhancements (minor changes to improve usability or capability), or internal improvements (to increase reliability or maintainability).

Consequently, it’s worth your time to spend a little extra time up front (before you start coding) thinking about the best way to tackle a problem, what assumptions you are making, and how you might plan for the future, in order to save yourself a lot of time and trouble down the road.

We’ll talk more about how to effectively design solutions to problems in a future lesson.

Step 3: Write the program

In order to write the program, we need two things: First, we need knowledge of a programming language -- that’s what these tutorials are for! Second, we need an editor. It’s possible to write a program using any editor you want, even something as simple as Window’s notepad or Unix’s vi or pico. However, we strongly urge you to use an editor that is designed for coding. Don’t worry if you don’t have one yet. We’ll cover how to install a code editor shortly.

A typical editor designed for coding has a few features that make programming much easier, including:

1) Line numbering. Line numbering is useful when the compiler gives us an error, as a typical compiler error will state: some error code/message, line 64. Without an editor that shows line numbers, finding line 64 can be a real hassle.

2) Syntax highlighting and coloring. Syntax highlighting and coloring changes the color of various parts of your program to make it easier to identify the different components of your program. Here’s an example of a C++ program with both line numbering and syntax highlighting:

The examples we show in this tutorial will always have both line numbering and syntax highlighting to make them easier to follow.

3) An unambiguous font. Non-programming fonts often make it hard to distinguish between the number 0 and the letter O, or between the number 1, the letter l (lower case L), and the letter I (upper case i). A good programming font will differentiate these symbols in order to ensure one isn’t accidentally used in place of the other.

In C++ your programs will typically be called name.cpp, where name is replaced with the name of your choosing for the program (e.g. calculator, hi-lo, etc…). The .cpp extension tells the compiler (and you) that this is a C++ source code file that contains C++ instructions. Note that some people use the extension .cc instead of .cpp, but we recommend you use .cpp.

Best practice

Name your code files name.cpp, where name is a name of your choosing, and .cpp is the extension that indicates the file is a C++ source file.

Also note that many complex C++ programs have multiple .cpp files. Although most of the programs you will be creating initially will only have a single .cpp file, it is possible to write single programs that have tens or hundreds of .cpp files.

Once we’ve written our program, the next steps are to convert it into something that we can run, and then see whether it works! We’ll discuss those steps (4-7) in the next lesson.

0.5 -- Introduction to the compiler, linker, and libraries
0.3 -- Introduction to C/C++

153 comments to 0.4 — Introduction to C++ development

  • Ali Hasnain

    is this stuff outdated?

  • Ashish

    Hey, Alex
    I am Mechanical student 1st year.
    I don't know 1,2,3... of programming...
    Programming is recently added to our course (JAVA and C++), you must be knowing that how college teaching is ? Actually worse and self-study is only way.
    I searched over internet for learning basics of programming from ZERO level.... found this site of yours at the last
    ....i have read the first lesson.... it's really like i will learn basics of C ++ in 10 days if i will be persistent.
    Thanks to you BRO.

  • vex

    This site is just the way too great to be true. Well-explained for difficult concepts.

  • Can you replace OS X with macOS? ;)

  • James

    Hi i am new to this and use a windows os laptop,what are the softwares i need to get started

  • Awesome step wise explanation.

  • stefan

    Alex, This tutorial is awesome! So well paced and highly informative. Thank you very much! I understand the place I am at is only the beginning but getting here is encouraging, nonetheless.

    I'm learning slowly how to make use of and benefit from as many programming tools available; in particular, tools made for the Android platform. While I've had trouble with the C++ app which led me here to your website, I'm ever grateful to that app for including a link to reach this tutorial directly. With the aide of a MOOC and the app mentioned above, I started writing very rudimentary code to familiarise myself with the syntax of the C++ language. As more issues with the app surfaced, I dug in earnest for ways in which Termux - a terminal emulator and provider of a Linux environment on Android >=5.0 - could be of help in terms of compiling and building executables that I can run on my devices. I knew the requisite tools were available from Termux' repository. What I didn't know - and only know now because of this excellent tutorial - was how to use g++ for what it was designed, .

    Nano is my editor of choice, for now. It handles line-numbering, syntax highlighting, indentation rather well. The brief exposure I've had to g++ in these pages led me to compile and build my first executable C++ programme. An unsuccessful first attempt found me editing, paring down my trivial code for clarity (can't properly debug yet) and I was able to run the programme​ right from my prompt after a second compile, linker, build command - tiny programme; immensely satisfying!

    1 #include <iostream>
    2 using namespace std;
    4 int main()
    5 {
    6     int a;
    7     int b;
    8     cout << "Enter a number: n";       //Get user input
    9     cin >> a;
    10     cout << "Enter another number: n";  //Get input
    11     cin >> b;
    12     if (a > b) {
    13     cout << "a is greater than b." << endl;
    14     }
    15     else {
    16     cout << "b is greater than a." << endl;
    17     }
    19     return 0;
    20 }

    I have to say, my initial desire to learn the art, skill, and fundamentals of programming had some kindling added to it as of reaching this page. Again, many thanks!

  • Nizar

    Is this guide could make me develop programs on c++ language individually? :|

  • Jeremy

    I took a C++ course in college 1 1/2 years ago and made a "D" it was very discouraging. I forgot about programming altogether to focus on other course work.. I recently had the itch to relearn programming again. Thank you to the creators of this site.

  • Lemuel

    Thanks for the tutorial but I have one question....
    In the linking section you wrote -o prog for executing process.
    Meanwhile if you want to compile and execute them in the same time you still wrote -o prog.
    So my question is Why -o prog and not -c -o prog?
    Sorry for not using tags.....

    • Alex

      -c tells gcc not to link, just to compile. -o tells gcc what the output file is named. So we'd only use -c -o when we want to compile but not link to an object file with a specific name.

      • Freddy G

        Hi Alex. Just curious. This executable file named prog created by linking the other files will have automatically have an extension named "out"? prog.out?

        • Alex

          I don't think so? Generally if you don't supply an output filename g++ will create one for you (e.g. a.out). But if you supply one via the -o command, I think it should use that.

          My knowledge of g++ is rusty though.

  • Danny

    Im so excited. Thanks for making this guide.

  • Helpful info given. Moving towards ide

  • Quentin

    can you wright a C++ program in google docs?

  • Freeman

    Thanks am learning more

  • GEScott71

    Thanks, I'm learning more already.  Among other things I now understand what an object file is.

  • liviu balas

    this leason is for windows too?

  • Its interesting to learn what I have been failing to understand
    Thank you very much.

  • Jen

    Typo under Step 3 - there's an extra 'the' (currently starts with: "In order to the write the program")


    i like it very much

  • Ese Ogheneruro

    Thanks! This is so helpful.

  • Carlos

    Thank you for this great tutorial. Its really great!

  • Hafiz Furqan

    sir i am beginner please guide me how to learn c language because i am little bit knowledge of computer

  • Bearman

    Thanks for the tut alex, enabled ads just for you (adblock was up :) )

  • Isabella Zhou

    This is the best tutorial for programming I've ever seen! Thanks, Alex.

  • Ashley

    Thanks for clarifying, Alex.

  • Ashley

    Hi, Alex
    I'm a bit confused about something. You say above that all programs should have the .cpp extension. With "programs" do you mean the project file or the files inside the project? Whenever I start a new project with CodeBlocks they ask me for a project title. When I give a name it then adds this name in the project file name box below with an extension .cbp . When I change .cbp to .cpp it saves fine. Only problem is that when I try to open it again, CodeBlocks does not find it unless I set file type to C++ files. Then when I open it it does not open as a project but as some file with lots of things written in that I don't understand. I know that within a project I should give any new files I create the .cpp extension and then it gets grouped with the main.cpp file in the project. So what I need clarification on is whether I should use the .cpp extension on the project file when I start a fresh project(although I have a feeling this is not what you meant), or only to any new file I add to the existing project?

  • Hi Alex,
    I always question myself that how something like photoshop is written in C++ ?
    I encountered a question in web that was my question : << what languages would be used to create something like adobe photoshop? I've created about 2 programs using c++, but they're like those tutorial things where they run in DOS, and the most advanced one I did was adding two numbers. :(

    But How would you make a program where it opens outside of DOS, with it's own interface and things like that. Basically something like photoshop, or any program like excel or word >>
    C++ GUI libraries are not part of standard libraries, so I guess that they did not use them to write something like photoshop or Mozilla apps ...
    So How ? (this question is making me crazy!)

    • Alex

      You're correct, C++ GUI libraries aren't part of the C++ standard. I think the main reason is because GUI applications are operating-system specific. The C++ library tends to stick to things that are operating system agnostic.

      Fortunately, there are external libraries to fill in the gaps. QT is a popular library for making cross-platform GUI applications. If you don't mind being Windows specific, you can have your application interface with Windows directly and build a MFC application. There are plenty of other C++ GUI libraries as well. It's just a matter of finding the right one for whatever you're trying to do (and what you budget is). I'm not sure what library Photoshop uses (or whether Adobe writes their own code for each operating system).

      • Hello again,
        I still don't know what we can do with C++ ?!
        for example if we want to develop windows GUI applications, I think C# is a good choice.
        if we want to develop android applications Java is good.

        I mean nobody likes programs that runs in DOS or Terminal environment.
        what useful thing we can create with C++ (in DOS environment) ?

        I'm learning C++ but I'm not sure for continue learning ...

        every body says C/C++ are powerful languages that can directly associate with hardware and manage memory | what we can do with this ?!


        • Alex

          You can do just about anything with C++, including writing Windows GUI applications. C++ excels at high performance, low latency, and memory constrained (e.g. embedded) applications. It's used in 2d/3d games and simulations, finance, manufacturing, embedded systems, operating systems, compilers, music players, server applications, search engines, and everything in-between. Unlike Java or C#, C++ doesn't require a huge framework to be installed first (the JVM/.Net frameworks).

          Given a specific task, other languages may be easier or more suitable to use, so it really depends on what you specifically want to do, and what your performance requirements are.

          This tutorial teaches using the console environment because it's easy and cross-platform. Once you know the fundamentals of C++, learning how to write GUI apps is a lot simpler than trying to learn how to do C++ AND write GUI apps simultaneously.

          Also, once you know C++, moving to Java or C# (or vice-versa) is easier, because they have similar syntaxes, and a lot of fundamental programming concepts are language agnostic.

  • Hi Alex,
    What is your idea about Qt?
    Is that good?
    Is it very different with C++ console programming?
    Can you Compare Qt and C# ? which one is better?
    tnx ;)

    • Alex

      I like Qt. It's a solid, cross-platform library with a lot of useful functionality, including GUI components. I also like it because doesn't require a bunch of dependencies to be installed first. You can distribute everything with your executable, which makes installing your app easy. And it's efficient.

      I can't speak to C#, as I don't have much familiarity with it.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]