Search

0.4 — Introduction to development

Before we can write and execute our first 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 you are going 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 numbers, then calculates the average.”
  • “I want to write a program that generates a 2d maze and lets the user navigate through it.”
  • “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.
  • They are well documented (especially any assumptions being made).
  • 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 _this_”, so you implement the solution that gets you there the fastest. This can lead to programs that are fragile, hard to change or extend, or have lots of bugs.

Studies have shown that only 20% of a programmer’s time is actually spent writing the initial program. The other 80% is spent debugging (fixing errors) or maintaining (adding features to) a program. 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 one soon.

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. A typical compiler error will state “error, 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 see the overall structure of your program.

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.

Your C++ programs should be called name.cpp, where name is replaced with the name of your program. 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.

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 if not hundreds of individual .cpp files.

Step 4: Compiling

In order to compile a program, we need a program called a compiler. The job of the compiler is twofold:

1) To check your program and make sure it follows the rules of the C++ language. If it does not, the compiler will give you an error to help pinpoint what needs fixing.

2) To convert each file of source code into a machine language file called an object file. Object files are typically named name.o or name.obj, where name is the same name as the .cpp file it was produced from. If your program had 5 .cpp files, the compiler would generate 5 object files.

The compilation process

For illustrative purposes only, most Linux and Mac OS X systems come with a C++ compiler called g++. To use g++ to compile a file from the command line, we would do this:

g++ -c file1.cpp file2.cpp file3.cpp

This would create file1.o, file2.o, and file3.o. The -c means “compile only”, which tells g++ to just produce .o (object) files.

Other compilers are available for Linux, Windows, and just about every other system. We will discuss installing a compiler in the next section, so there is no need to do so now.

For complex projects, some development environments use a makefile, which is a file that tells the compiler which files to compile. Makefiles are an advanced topic, and entire books have been written about them. Fortunately, you don’t need to worry about them, so we will not discuss them here.

Step 5: Linking

Linking is the process of taking all the object files generated by the compiler and combining them into a single executable program that you can run. This is done by a program called the linker.

The linking process

In addition to the object files for a program, the linker also includes files from the C++ standard library (or any other precompiled libraries you’re using, such as graphics or sound libraries). The C++ language itself is fairly small and simple. However, it comes with a large library of optional components that may be utilized by your programs, and these components live in the C++ standard library. For example, if you wanted to output something to the screen, your program would include a special command to tell the compiler that you wanted to use the I/O (input/output) routines from the C++ standard library.

Once the linker is finished linking all the object files (assuming all goes well), you will have an executable file.

Again, for illustrative purposes, to link the .o files we created above on a Linux or OS X machine, we can again use g++:

g++ -o prog file1.o file2.o file3.o

The -o tells g++ that we want an executable file named “prog” that is built from file1.o, file2.o, and file3.o

The compile and link steps can be combined together if desired:

g++ -o prog file1.cpp file2.cpp file3.cpp

Which will combine the compile and link steps together and directly produce an executable file named “prog”.

Step 6: Testing and Debugging

This is the fun part (hopefully)! You are able to run your executable and see whether it produces the output you were expecting. If not, then it’s time for some debugging. We will discuss debugging in more detail soon.

Note that steps 3, 4, 5, and 6 all involve software. While you can use separate programs for each of these functions, a software package known as an integrated development environment (IDE) bundles and integrates all of these features together. With a typical IDE, you get a code editor that does line numbering and syntax highlighting. You get a compiler and a linker. The IDE will automatically generate the parameters necessary to compile and link your program into an executable, even if it includes multiple files. And when you need to debug your program, you can use the integrated debugger. Furthermore, IDE’s typically bundle a number of other helpful editing features, such as integrated help, name completion, hierarchy browsers, and sometimes a version control system.

We will talk more about installing and using IDEs in the next section.

0.5 -- Installing an Integrated Development Environment (IDE)
Index
0.3 -- Introduction to C/C++

68 comments to 0.4 — Introduction to development

  • Grant

    getting closer to real programming…

  • Prabhat

    This site is really amazing….it rocks.

  • I actually am learning stuff from this tutorial I didn’t previously know. After being in the field(s) I specialize in for 7 years it’s been a long road. I am finally learning a few things I have always wondered about. Some of the way you explain things make it to where I don’t think I will forget what I am learning. Thanks again for the great tutorial.

  • limitless

    This tutorial seems to be good. i think it helps that i no alot of Visual basic, but this seems to be getting me through. hopefully i will know C++ properly by the end of next month XD…

  • Skkal

    Like the way author has explained concepts.

  • Jalin (G33KG1RL)

    This tutorial is great!(or as Data would say, “intriguing!”).

    I initially didn’t plan on getting into the programming scene, because I am a licensed PC Repair Technician, and my workload is outrageously huge. But my job expects me to know a couple programming languages (I know HTML and CSS, but they are not exactly software programming languages- oh and I know Binary haha), and this tutorial is actually enjoyable to read. And it’s perfect for people my age (16 years old) because it’s free (I KNOW!!!!!), and easy to understand.

    Thanks so much!

    I am totally up to learning more languages now :]

  • rin

    i’ve never touched any programming… unless you consider HTML as one. but this tutorial made it so simple for me to understand πŸ™‚ Thank you so much!

  • i really like how this tutorial was not very os picky. thanks for having instructions for mac too most people leave macs out. idk why?
    but overall this was easy to understand

  • Awesome Tutorial. Wish I had stuff like this back when I started learning VB.

  • Pakoschild

    Actually I dont have a problem to solve :/ but I agree that this is an awesome tutorial πŸ˜‰

  • sanjeev saju

    This is a great tutorial.I just started my C++ class in school (+1).But this site helps me to learn more which are not in my text book.Thanks Alex for this wonderful website.
    God Bless You!

  • black_ant

    this is the best site to learn cpp i’ve ever gotten… big thanks for all people behind this site…especially for Alex…

  • juee 36

    can we learn c++ without having any knowledge of c.??

  • Avocadoweirdo

    best site to learn c++ ever. It helps me so much to understand programming and C++. Thank you for all the people behind this site. You are heros!

  • Pretty good, I came here looking to learn a programing language(I already knew several scripting languages) and learn I did.

  • the

    the way you explain , very exciting

    thanks

  • Shahin

    I begin learning C++ programming but it will help me learn in this field. If God lets….
    Thnaks very much for this site.

  • ben

    is it possible for a 12 year old to learn c++ becuase i want to

    • Alex

      It’s certainly possible, depending on your comprehension abilities. I know of 13 year olds who have been successful.

      Best way to find out is to try it and determine for yourself. πŸ™‚

  • Sam

    Is this stuff outdated since it was made in 2007?

  • Batuhan

    nice tutorial

  • Megalegacy

    Hey, are you continuing these C++ tutorials?  Last one you posted was in 2011…. These tutorials are really good by the way.

    • Alex

      I’m in the process of writing v2.0 of this site, which includes both new material and touched-up existing material.

      After I’m done, I’ll think about additional tutorials, but I still have quite a ways to go before I’m there!

  • Ben

    Hi, thank you for this tutorial. Its very user friendly and easy to understand.

  • Fahad

    tHANK S TO ALEX. I THINK I WILL LEARN IT EASILY. THANKS AGAIN =D

  • Alfa

    Thanks a lot for nice tutorial πŸ™‚

  • Pedrowatt

    OooooHhhhhh Dear!!! I never expected i would learn from a cat!!!
    But Here we are! Great job by the Way!

  • Random Developer

    I’m learning this so that I can begin game development in Unreal Engine 4. Will the compiling thing still apply or not? Will Unreal do it for me or not?

  • Necrod

    Can you explain what a runtime support library is?

    • Alex

      As noted in the lesson, the runtime support library is a bunch of optional routines that you can include in your program. These routines assist with tasks such as input and output, string handling, searching and sorting data, etc… The runtime support library is pretty extensive, and most C++ programs use at least some functionality from it.

      Because the runtime support library doesn’t change with each program you write, it comes precompiled, so it only has to be linked into your program.

  • Uzo

    What are the various kind of programs,one can write using c++?

  • Enrique

    Fantastic… help to lovel PROGRAMMING

  • mubin

    thanxxxx….alex

  • 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.

  • 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 ?!

        Thanks.

        • 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.

  • 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?

  • Ashley

    Thanks for clarifying, Alex.

  • Isabella Zhou

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

  • Bearman

    Thanks for the tut alex, enabled ads just for you (adblock was up πŸ™‚ )

  • Hafiz Furqan

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

  • Carlos

    Thank you for this great tutorial. Its really great!

  • Ese Ogheneruro

    Thanks! This is so helpful.

  • M.SURYACHARAN

    i like it very much

  • Jen

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

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

  • liviu balas

    this leason is for windows too?

  • GEScott71

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

  • Freeman

    Thanks am learning more

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter