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

Tutorial: A small introduction to C++

Discussion in 'C, C++, C#' started by WinBoot, Oct 21, 2013.

  1. WinBoot

    WinBoot Registered Member

    Joined:
    Aug 25, 2010
    Messages:
    61
    Likes Received:
    14
    Tutorial: A small introduction to C++

    This tutorial will only cover some main knowledge. After you have fully read it
    you should be able to create your own small programs, know the basics of C++ coding,
    and be able to use that base to continue and experimenting further into the language.
    So i will definitely not go into detail, like about how many bytes a certain
    data type uses.

    To compile C++ code you will need a debugger, the most well known is Visual Studio C++.

    A trial version of Visual Studio C++ is available here:
    http://msdn.microsoft.com/vstudio/express/visualc/download/

    Code:
    Content table:
    1. Data types
    2. Statements
    2.1. If, else
    2.2. For-loop
    3. Functions
    4. Code example
    1. Data types

    Like any other programming language, C++ uses data types to store values and information in.
    Here you can find a small list of the most used data types.

    Code:
    Int (integer value)
    Float (decimal value)
    Char (a character, contains text)
    Bool (boolean, true or false)
    I won't discuss Vector and String here since there are endless possibilities with them and
    it would take quite some time to write all that down :).

    When you are coding and you want to store a value, you will have to know two things.
    First, what kind of data should the variable be able to handle? Second,
    what will the variable be called?

    This is how you declare those four data types.
    Code:
    int ThisIsAIntegerValue;
    float ThisIsADecimalValue;
    char ThisIsACharacterVariable;
    bool ThisIsABoolean;
    As you can see you first write the data type, after that, it's name, and then you
    have to finish the declaring with a ";".

    Now we have created a place we can put data in, but untill now we still haven't put any in.
    Code:
    int ThisIsAIntegerValue = 0;
    float ThisIsADecimalValue = 0.0;
    char ThisIsACharacterVariable = "test";
    bool ThisIsABoolean = false;
    2. Statements

    2.1 If, else

    In the next topic i will show you how to write your first program based on these two statements.

    Other then the else-statement, the if-statement can stand-alone and can only be used in one way.
    The else-statement can also be used in a combination of both statements. These are the two possibilities:

    Code:
    if ( ... )
    {
    }
    else
    {
    }
    Code:
    if ( ... )
    {
    }
    else if ( ... )
    {
    }
    else
    {
    }
    When you use the else-statement the two actions will never be executed at the same time.
    As you may have guessed, the code which should get executed when a certain action
    returns true has to be placed between the two brackets.

    So now we know where to put the action but not yet what we can do where the points are situated. This is an
    example of how you would use it with a boolean.

    Code:
    bool test=false;
    if ( test )
    {
        // test is true
        /* test is true */
    }
    else
    {
        // test is false
        /* test is false */
    }
    The "//" sign is used to place comments in your code, as you can see you can also use the "/ *" and "* /" sign.
    (Without the spaces) The above code is one way to check if a boolean is true or false. But to
    compare all data types there are built in signs you can use, here i give you a very smal list:

    Code:
    ==    equal to
    !=    not equal to
    >    bigger
    <    smaller
    >=    bigger or equal
    <=    smaller or equal
    2.2. For-loop

    You can use this loop when you want a certain action to get executed several times, or use it to loop
    trough several variables. This is an example of how you use it.

    Code:
    for (int i=0; i<=10; i++)
    {
    }
    As you can see, there are three main parts in the loop. The first one "int i=0" defines the variable
    that will change it's value. The second part "i<=10" is where you define how far it should loop, here it
    goes from 0 to 10 since we defined the variable as 0. The last one "i++" is the actual counting up, you
    can also use "i--" or any other action you would like.

    3. Functions

    Functions are very important for writing programs. My examplefunction will be called "main". I'm choosing
    this name because every program, whatever you write, needs a main function. Without it, you can not
    execute your program code. This is the example:

    Code:
    void main( )
    {
    }
    The first word written down is "void" it states that the function does not return any value.
    And as you may allready know, "main" is the name of the function. Now i'm going to show you an example
    which contains several new aspects.

    Code:
    int testFunction( int temp )
    {
        return temp;
    }
    Now the first word written down is "int", which refers to the integer value. It means that the function
    will retain an integer value. We do this by using the "return", and in this example we return "temp".
    "temp" is our input, which we get when the function is called. This is an example of a normal function
    call without a return value:

    Code:
    int test = 0;
    testXFunction( test );
    As you can see our input is now "test" which contains the value 0. Now when we would use a function
    with an integer return value and want to store the output of that function. Then our calling
    might look like this:

    Code:
    int test = 1;
    int output = testFunction( test );
    If testFunction is the function i posted above, then output will be set equal to test, which is 1.

    4. Code example

    Code:
    float sum( float input1, float input2 )
    {
        float temp = input1 + input2;
        return temp;
    }
    
    void main ( )
    {
        float var1, var2;
        float outputValue;
    
        cout << "Input float value 1:" << endl;
        cin >> var1;
    
        cout << "Input float value 2:" << endl;
        cin >> var2;
    
        outputValue = sum( var1, var2 );
    
        cout << "Sum: " << outputValue << endl;
    }
    You should be able to read and understand both functions. The only new things i haven't explained yet
    basically are "cout", "cin" and "endl". The cout stands for output, you can display text, variables
    and every other kind of data types with it. The second one (cin) is used to write input to variables.
    And the endl cmd just takes a new line after your text/variables have been displayed. You should
    mess around with some of those small codes to try understand the code better and learn from your
    own mistakes.

    I hope you have learned from this tutorial. I wrote it a long time ago but i'm sure it will help someone over here ;-)
     
    • Thanks Thanks x 4
  2. Anderai

    Anderai Newbie

    Joined:
    Oct 14, 2013
    Messages:
    14
    Likes Received:
    1
    Occupation:
    Copyeditor
    Location:
    The UK.
    As someone who's just starting out learning programming, this is pretty helpful. Thanks!
     
  3. Panther28

    Panther28 Elite Member

    Joined:
    May 2, 2010
    Messages:
    2,268
    Likes Received:
    3,405
    Occupation:
    Internet.
    Location:
    Internet.
    can you show us an example for something like this. It would be very helpful for people to see a real word example, not just whats in the toolbox.

    cheers.

    set #search_variable == blackhatworld.com

    load www.google.com in chrome
    if search box exists
    then enter #search_variable in search box
    click search
     
    • Thanks Thanks x 1
  4. WinBoot

    WinBoot Registered Member

    Joined:
    Aug 25, 2010
    Messages:
    61
    Likes Received:
    14
    That would be more like a bot, i honestly don't think C++ is the best language to write something like that. You'd be better of with PHP, maybe Perl.
     
  5. soulow

    soulow Registered Member

    Joined:
    Aug 3, 2013
    Messages:
    69
    Likes Received:
    44
    I think C++/C# are perfect for bots but not easy to do. All browsers are made using c++ or C# or language like that. So if I am doing a bot I will just load the target URL, parse the html and do the next work, you can use cookies, sessions and simulate that your application is just a browser like a firefox.
    So I really think the language is super for bots but it is not easy for creating.