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

Components of a C++ Program

Discussion in 'C++' started by MinalS, Nov 29, 2015.

  1. Different components of C++ programming

    The various essential components used in C++ programming are as explained below:
    1. Class: The class is a template used for demonstrating the behavior supported by the object
    2. Object: The object in C++ consists of states and behaviors. An object is an instance of the class
    3. Methods: The behavior of the class is defined using the method. The code to be executed is added inside the methods
    4. Instance variables: There is a set of instant variables associated with the objects.
    Program structure

    In C++, the program contains various parts which are necessary for the user to learn.
    Consider the following sample code used to represent the program for displaying the message.
    #include <iostream>
    using namespace std;
    int main()
        cout << "Welcome User";
        return 0;
    In the above code snippet, the various parts are:
    1. The headers are used to define the information used in the program. The <iostream> header contains of input output stream files
    2. The standard namespace is added through the line using namespace std; in the code
    3. The main() is the entry point for the code execution
    4. The message is shown on the screen using the cout statement
    5. The main() function is terminated using the return 0; statement
    Compiling and executing the program

    There are some steps that user needs to follow for compilation and execution of the program.
    1. In the text editor, add the program code to be executed
    2. Once the code is added, save the file using file1.cpp
    3. In the command prompt window, navigate to the location where the file is saved
    4. Add the code g++ file1.cpp and hit the enter key for code compilation
    5. An executable file is generated when the code is compiled
    6. The result is displayed as the output
    Identifiers and keywords

    An identifier is name assigned to a class, module, variable, function, etc. It letter from A to Z, a to z, digits from 0 to 9, an underscore following more than one letter. The characters like $, %, @ are not allowed as identifiers. The language is case sensitive.

    Some of the valid identifiers useful in C++ programming are as listed below:

    Xyz12, demo45, name, _age, data2

    The keywords in C++ are reserved. User cannot add them as a variable or an identifier.
    Some of the keywords used in C++ programing are as listed below:

    else, auto, new, asm, this, enum, operator, bool, throw, private, protected, public, catch, case, try, using, goto, if, else, do, while, etc.

    Blocks and semicolons in C++

    The statements defined inside the set of curly braces is known as a block in C++.

    int main()
        cout<< "Welcome";
        return 0;
    The statements are terminated using a semicolon. The end of the entity is defined.

    a = 10;
    b = 20;
    c = b – a;
    Trigraphs and whitespaces in C++

    The three character representation is defined using a trigraph. It is used as an alternate representation for some characters. The sequence is preceded by two question marks. A single character is defined. User can add the trigraphs in string literals, character literals, directives, and comments.

    The list of trigraphs and their replacement are as mentioned below:
    1. ??/ is replaced using \
    2. ??= is replaced using #
    3. ??’ is replaced using ^
    4. ??( is replaced using [
    5. ??) is replaced using ]
    6. ??> is replaced using }
    7. ??< is replaced using {
    8. ??- is replaced using ~
    Whitespaces in C++ is defined as a line consisting of only whitespace which is known as blank line. It is ignored by the compiler. User can describe blanks, newline characters, and comments using the whitespaces. It is used for dividing a part of the statement from others. The compiler can easily identify the element in the statement as ends, int, begins, etc.

    There must be whitespace between the declarations of an element. Consider user needs to declare the element as:

    int marks;
    In the above code, there is a whitespace between the integer and character declaration.

    Comments in C++

    The comments provide additional information about the code. It is beneficial for the user to add the comments for better understanding. There are two types of comments supported by C++. They are single line and multi -line comments. The comments are defined inside the /* and */ tags.

    Consider an example to demonstrate the use of comments in C++.

    #include <iostream>
    using namespace std;
        cout<< "Welcome User"; //shows the message Welcome User
        return 0;

Share This Page