Learning C++: Revenge of the Code - Part 1 Intro

Discussion in 'Web Design & Programming' started by Addis, May 24, 2005.

  1. Addis

    Addis The King

    Likes Received:
    91
    Trophy Points:
    48
    Learning C++: Revenge of the Code Part 1 - Introduction

    What are computer programs?

    Computer programs are a series of instructions in machine code that a computer executes to perform tasks. It is step by step instructions on what the computer must do. We use computer almost everyday and they all use the basic concept of machine code (in binary) as a series of instructions. However, to make simple programs it takes a lot of knowledge and complex programming to make the smallest program using machine code.
    So, programming languages were invented to make writing computer programs much easier. After machine code, assembly code was created to allow programmers to use small and simple commands like:
    Code:
    MOV, ADD, AND
    Then more verbose programming languages like C and C++ was invented which resembled human language more so than assembly. As of today C++ is the most widely used language for creating applications and games.

    The History of C++

    C++ is a superset of C; that is the C programming language with more features and better syntax and more overall improvements. C is still used today for many applications however C++ is the dominant language used. C++ has many of the same syntax styles and functions but has its own features and functions as well.
    You may be thinking: If C++ is a superset of C, shouldn’t I learn C first? The simple answer is no. No prior knowledge of C is needed to learn C++ as if you know C++ you already know some C, but not the other way round.

    How a C++ program works

    It’s a well known tradition for programming guides to first start out with a simple “hello world” program to output the text to the screen. Well I feel like being a bit different so I’ll start differently.
    This is a simple C++ program that outputs some text to the screen:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    cout << "HWFs roxors!";
    cin.get();
    return 0;
    }
    
    That was the source code to a C++ program. Lets examine each of the components.

    #include <iostream> -- This is whats called an include directive. This command here inserts the source code of the iostream library (input/output stream) into the source code above everything else.
    What is a library? A standard library is a piece of source code that allows you as a programmer to do things such as output text to the screen with just a few lines of code. If it weren’t for the standard library iostream you would have to write the source code yourself to make the message appear on the screen and that would not be easy or good use of time. The standard library also has other functions which you can make use of without you having to write them yourself and lets you write programs quickly and relatively easy,

    int main() -- This is starts the main function body. All C++ programs first start with the main function. It is the first function to be executed and from it you can write code or call other functions. This is so that the computer knows from where to start executing code. A function is group of related commands usually to perform a single task.

    {} -- These curly braces are used to group commands together. They’re used to tell the computer that the commands in it are to be executed together. In this case it groups together the commands in the main function.

    cout << "HWFs roxors!"; -- As you can guess this command outputs the text to the screen. We didn’t need to write the cout object as it was already declared in the iostream library. The cout object (pronounced ‘c-out’ as in console out; but I say it as ‘chowt’ as its easier to say :rolleyes:) takes the text and prints it to the screen.
    The << used is called a stream insertion operator. It inserts data from its right into the object on the left. Similarly the stream extraction operator >> extracts data from an object. Remember it as the movement of data. The text data travelling to the cout object for example.
    The last point is the semicolon. The ; is the character used to terminate any statement in C++. The cout code is a statement so it is terminated with a semicolon. E.g. so would this
    Code:
    Dude = happy;
    
    That is a statement and thus is terminated with a semicolon. A lot of code is terminated with semicolons but some aren’t so its important to remember and know which ones.

    cin.get(); -- This command is optional. The cin.get() command is defined in the iostream library and is used to halt execution of the program until it receives user input. Its not needed for the program to run, but when I first started out on C++ my tutorials and book didn’t tell me this and so whenever I ran my programs they would quickly flash up in a console and then close. Since it has executed all of its command the program can exit. You don’t have to use this function if you run your program from the command line as when it exits you can still see the output from the program but if you run from a GUI shell or you want program to wait until user input then use this command.

    return 0; -- This is the return statement. Some or most functions in C++ return a value when they are executed. For example the function pow(10,10); takes the first number and returns the number to the power of the second argument. In this case the number 100. A return statement is used to return the result of a function to the function which called it. Not all functions have a return value.
    The return statement in main is used to tell the operating system that the program termianted successfully. It returns the integer 0 when exiting normally.

    using namespace std; -- This command basically saves you time. C++ uses namespaces to organize different names used in programs. Every name used in the iostream library is part of a namespace called std. So the real name of cout is actually std::cout. By using this command we no longer need to do that we simply use cout.

    Now what?

    You’re probably thinking ok I’ve seen the code but how do I turn this into a working executable like a .exe for instance?
    Well first you need to write your souce code. It can be done with any text editor as long as the editor saves it as plain text. C source code files have a .c extension while C++ code has a .cpp or .cp extension. Usually the former.

    Computers don’t understand C++, nor do they understand C, assembly or BASIC. This is because they only work with machine code with binary. Consequently we need a way to turn this source code into an executable file.
    C++ is a compiled language. That means that the source code is given to a program called a compiler (which has separate parts) which then produces an executable.
    A compiler is used with other programs called a preprocessor and a linker to produce the final executable. Because many compilers have built in preprocessors and linkers we use the collective term compiler for the whole thing.
    When you compile your program, it goes through a number of stages.

    1. The preprocessor scans the source code to find any include directives. Then it inserts the source code from the library into the source.

    2. Then the compiler translates the preprocessed code to machine language and puts it into an object file (.obj extension). There are many compilers for different languages but they all do essentially the same thing. The compiler is sensitive to grammatical errors and will alert you in the form of command line error messages if there was any errors or warnings during the compile.

    3. The linker then links the .obj files with run-time libraries (used to interact with the operating system) and finally outputs the final executable.

    Using an IDE

    So you know whats needed to create an executable. You need to write the souce code and then use the compiler package to turn it into an executable. But wouldn’t it be easier if you could use one program that does it all for you without having to type in the command line every time you want to compile? The solution is an IDE; or an Integrated Development Environment. An IDE houses a text editor for writing code, the compiler tools along with libraries to create your programs all under one roof. Also they are useful because they often have debugging tools and the text editor has intelligent code formatting depending on the code as well as project managing features.
    Good IDEs are Microsoft’s Visual C++ and Borland’s C++ Builder which you must pay for. However if you want a free one then choose Dev-C++ from Bloodshed at www.bloodshed.net. Its open-source and does a pretty good job.

    Using Dev-C++ as an example, install it and then run the program. Once its loaded go to File>New>Source File or click the appropriate icon. It will then create a new document in a tab and you write your source code into the editor. Once finished click Compile>Compile or Compile and Run to compile the code. If you make any changes simple save and recompile to see the changes.
    Note: I recommend that before you start writing your programs you make an organsied tree of folders for your C++ files. For every part or concept make a new folder. E.g for 'Variable' files make a folder called Variables. By doing this you don't have a huge collection of unrelated source code and executables.
    That’s it for this part of the tutorial!

    Questions welcome.
     
  2. Addis

    Addis The King

    Likes Received:
    91
    Trophy Points:
    48
    Setting up your IDE

    I'll use Dev-C++ as an example for this tutorial. You can use MS Visual C++ or Borland if you wish and they'll all have pretty similar layouts.

    To set up your IDE download the latest version of Dev-C++. Its a small file and shouldn't take long. Install it and then start the program. It'll as you to do a first time setup. Choose a theme to your liking and then it will ask you whether you want to enable code completion.
    Code completion is a feature of Dev-C++ in which it will give you small alternate text style popups showing the most likely code you're about to write. Its quite complicated and for the rest of the tutorial it won't be needed. If you want it then it will take a while to set up.

    Once setups compete you'll be met with a screen with a grey blank in the body of the winow. Click File>New>New source or the icon to start a new source code. You'll see a text editor come up and it will be the place where you write your code. do the example program earlier and then save it.

    C++ programs are generally given the extension cpp or rarely cp. Since C++ will be the language for this tutorial save is as the default .cpp. Once done you can compile your source code. Click Compile>Compile or Compile and run. If there are no compile-time errors you'll see the compile box go off and you'll see the console window of your program.

    You can of course change the environment window through Options>Environment options to change how the editor formats your code. do what you like here.
    Another setting in which you may want to tweak is the compiler options. Its something like Options>Compiler Option>Compiler tab>then find the option where it says generate debugging information. It may be under the code generation tree. Change the setting to no and your program will be around 250k instead of 500k.
    You can also enable strip executable option to further decrease file size.
     

Share This Page