1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Introduction to C++

Discussion in 'C++' started by BiplabKamal, Apr 30, 2016.

  1. C++ is a programming language which has evolved from it’s ancestor C language. Bjarne Stroustrup is the creator of the C++ language. Before C++ language got proper shape the creator has created it’s previous version called ‘C with Classes’ in the duration of 1979 to 1983. It was actually a transition period which ultimately gave the birth of C++. The first international standard for C++ was published in 1998 and the version of C++ was named as C++98. After that new versions were released once in almost 4 years like C++03 and C++07. These versions were minor versions until C++11 was released in 2011 as a major version. Again C++14 a minor version is released in 2014 and C++17 is planned to release in 2017 to be a major version.

    I am keeping the new programmers in mind who may not have any programming experience. The rest of the topic will help a beginner to build his basic concept of programming and programming languages. You can skip this chapter if you are already familiar with C or any programming language but no harm in revisiting. Before going into the C++ programming language detail, let us answer following questions which will come in the mind of a new guy who dare to adventure in the computer programming.
    1. What is a programming language?
    2. What are the types of programing languages.
    3. Is C++, Java and PHP are same category of languages?
    Let us first try to answer those questions before going into the particular language details.

    What is Programming Language

    Programming language is used to create computer program or application which can be executed by the CPU. A program is a set of machine executable instructions and data residing in computer memory. Instructions are machine code in binary format. Writing programs in machine code is a very difficult task for human being. It may be possible to write small programs using machine code bu it is infeasible to create large and complex programs. Programming languages came into picture due to the effort of software development groups of earlier days to make the computer programming easier. Programming languages help in writing program logic in human readable forms and then get it converted to the targeted machine code. For these purpose converter programs had to be developed. These converter programs are called compiler or interpreter. So question arises how the first converter was developed. Of course the first version had to be written in machine code (Assembly language). We hear that C compiler was written in C. That is true but not all? The first version with minimal functionality has to be implemented either in assembly language(machine code) or in some other existing language. Once the first version is working that can be used to enhance the same compiler itself. This process is called ‘bootstrapping’. In fact C compiler was built using bootstrapping method. Programming language has a specification which describe the rules and syntax for writing a program in human readable text. There has been a lot of improvement in these area and many powerful languages have been evolved. Starting from C there has been a lot of evolution to reach the current state of C++.

    Different programming languages

    As we discussed that programs written in programming languages are converted to machine language by the compiler. These languages are called compiled languages. In addition to that there are also languages those are not compiled but interpreted and executed by some applications. The interpreter interprets the program step by step and take appropriate action. These languages are called interpreted languages. There are also a third group of languages which are first converted to an intermediate language understood by some application(called run-time). They are converted to machine code during execution by the run-time application. C, C++, Pascal are compiled languages and PHP, javascript are interpreted languages or scripting languages. Java, C# are run-time based languages. Script based languages are not independent languages because they are not creating independent programs which can run independently. The script bases programs will be interpreted and executed by another program, called interpreter or engine. Basically Scripting languages are used to extent the functionality of the interpreter or the engine application. To become expert in writing programs in interpreted languages you need not to know the operating system details but need to know the interpreter specification. On the other hand if you want to become an expert in a compiled language like C or C++ you should know the underlying platform. For example if you know PHP you can program for any platform as long as the PHP engine is supported in that platform. But learning only the C++ syntax will not make you an efficient programmer in any platform. You need to be aware of internals of the targeted platform. This is because PHP code does not interact with the OS but C++ code uses OS functionality. Java and C# are not interpreted and also not compiled languages. This are modern run-time based languages which gives the power of compiled languages but hide the platform details from the programmer. This programs are compiled in an intermediate level code by its compiler. The run-time application converts this intermediate level code to machine code before executing it. It is better to learn C or C++ as the first programming language to be familiar with low level concept of programming.

    C++ overview

    We already know that C++ is a compiled language. When a program is compiled it creates a binary module which consists of machine instructions and data. Instructions operate on data. How the instructions and data are arranged inside the binary module depends on the compiler. C++ language is evolved from C language. Basic structure of C++ program is based on C program structure. C programs have two basic constructs , functions and variables. Variables represents data whereas functions represent instructions . Both C and C++ program flows are controlled by a series of function calls and functions operates on data. So writing a program in C or C++ you have to create function definition and data definition. Functions contain the execution logic and variables (data) contains different types of data. You will also have to specify one function as the entry point from which the execution starts. Generally each function does a specific task. One function can call another function and wait for the execution of the called function before continuing. When functions are compiled the compiler generates a set of instructions and keeps it in a sequential memory block. While compiling a function the compiler also generates some extra code so that execution return to the point from where the function was invoked. Data or variables can be primarily of two types depending on it’s visibility. Some data are local to a function, means it can be only used inside a function. Other functions can not use that data. There is other type of data which is shared across functions and is called global data. So the global data will be created before any function gets executed while local data to a function will be created once the function is invoked. So a minimum C/C++ program will contain at least one function. While invoking a function the caller function can pass data to the called function as the input to the function . Function can also return data to the caller as the result of the execution. The structure of a program will look like the following layout-
    global data...
    Function (Entry point)
        local data ...
    Function 2
        local data ...
    more functions...
    So you have to learn how to define a function, how to define data, syntax of code inside a function, how to call a function from another function and how to specify the entry point. All these are mentioned in the language specification. We will discuss each one in detail in later chapters but give brief description here.

    Functions in C/C++

    A function has two parts, the signature and the body. The signature specify how the function is called and the body contains the code. The signature contains a name so that the function can be called by name. It specifies the list of data structure it can accept from the caller and the data structure it will pass back to the caller.

    Creation of function has two steps: declaration and definition. Declaration specify the signature and the definition implements the body. Here is a real example of a function deceleration and definition. We will discuss the detail in later chapters.
    int Add(int number1, int number2); // Deceleration
    int Add(int number1, int number2) //Definition
        int result = number1 + number2;
        return result;
    In the above code the deceleration line is redundant. If you remove the first line then the declaration will be derived from the definition. If you want to use the function before defining it or in a place where the definition is not visible then you have to declare before using. This is because of two stage compilation, first step looks for declaration and the 2nd step looks for definition. First step is called compilation and the 2nd step is called linking.


    Variable definition has two aspects: specifying the amount of space required to store it in the memory and specifying the meaning or the type of data. The type indicates the meaning of the bits in memory. Suppose you have a data of one byte size and the content of the byte is 01000001. Now if you treat this data as a positive integer the value will be number 65. But if you treat it as a symbol of the ASCII character set the value will be letter ‘A’. In C++ language there is two steps in creating data, one is deceleration and the other is definition. In the deceleration we specify the size and meaning of data and give a name to call it. In the definition we allocate the memory and initialize the content. Deceleration syntax has two parts- data type and data name. Data type is specified with language key words which are reserved and has fixed meaning or can be user defined data type. Data type say the size and meaning of the data while data name is given by the programmer. Here is a real example of data deceleration and definition of a 4 byte data use for storing an integer number. In C++ data declaration and definition are done together with a single statement. We are not going in details here and will be discussed in later chapters.
    int num = 0;
    You can have user defined data types like structures, classes and enumerators. Class data types are not supported in C but in C++. Class is basic item of object oriented programming which we will discuss in later point of time.

    In the next chapter we will see how to create different construct of a program.

Share This Page