Results 1 to 6 of 6
  1. #1
    P0SEID0N's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    KFC
    Posts
    318
    Reputation
    10
    Thanks
    24
    My Mood
    Lurking

    Learn how to code C++ || Day 3 - Variables and Date Types

    Lesson #3 - Variables/Data Types

    The first thing i'm going to do is take a little experiment off here:
    I want you to remember:
    A = 6
    B = 9
    C = 3
    D = 3
    A + D = 9
    E = A + D
    C = E
    E - 6 = B

    Now what does A + B x C + E equal? Finding it a bit hard to remember? Thats why we have Variables.
    What you have just done with your memory is the same as what a computer can do with variables. The same process can be done in C++ like this:
    Code:
    A = 6
    B = 9
    C = 3
    D = 3
    E = A + D
    C = E
    E - 6 = B
    We can define a variable as a portion of memory to store a determined value. Think about it as a small block of memory, you can write on it, erase it, make it bigger or smaller, but all the time just calling it by its identifier.

    The identifiers in that example were all single letters, A, B, C, D and E. Spaces or punctuation marks or symbols cannot be part of an identifier. Only letters, digits and single underscore characters are valid. Variable identifiers always have to begin with a letter or an underscore.

    Something else that you have to consider when inventing your own identifiers is they cannot match any keyword of the C++ language or your compiler's specific ones, which are reserved keywords.
    Standard reserved keywords are:
    [php]
    asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while[/php]

    Additionally, alternative representations for some operators cannot be used as identifiers since they are reserved words under some circumstances:

    [php]and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq[/php]

    Remember C++ is a case sensitive language so Identifier is not the same as identifier or iDentifier.
    There are many data types that can store many different things. From integers(whole numbers) to Characters or Strings. Here is a list of some.
    [IMG]https://i755.photobucke*****m/albums/xx191/_-_-_JAMES_-_-_/DataTypes.jpg[/IMG]

    If we want to use a variable, we must first declare it. You do this by putting a valid data type followed by your identifier. For example:
    [php]int cat;
    char dog;[/php]

    If you are going to declare more than one variable of the same type, you can declare all of them in a single statement by separating their identifiers with commas. For example:
    [php]int a, b, c;[/php]
    You DON'T need to do
    [php]int a;
    int b;
    int c;[/php]

    The integer data types char, short, long and int can be either signed or unsigned depending on the need. Signed types can represent both positive and negative values, unsigned types can only represent positive values (and zero).If we do not specify either signed or unsigned most compiler settings will assume the type to be signed.

    An exception to this rule is the char type, which is considered a different data type from signed char and unsigned char. You should use either signed or unsigned if you intend to store numerical values in a char-sized variable.

    Short and long can be used alone as type specifiers. Short is equivalent to short int and long is equivalent to long int. The following two variable declarations are equivalent:
    [php]
    short cat;
    short int cat;
    [/php]

    Signed and unsigned may also be used in the same way.

    We will now look at the scope of variables.
    There are two types of variables we will look at Global and Local.
    Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration.
    The scope of local variables is limited to the block enclosed in braces where they are declared. If they are declared at the beginning of the body of a function their scope is between its declaration point and the end of that function. This means that if another function existed in addition to main, the local variables declared in main could not be accessed from the other function and the variables from the other function could not be accessed inside main.

    When declaring a regular local variable, its value is automatically undetermined. But you may want a variable to store a value at the same moment that it is declared. In order to do that, you can initialize the variable. There are two ways to do this in C++:
    [php]
    int a = 6;

    OR

    int a (6)
    [/php]



    We are now going to use all this in a program:
    Code:
    #include <iostream>
    using namespace std;
    //you can declare global variables here
    int rabbit = 1;
    int main()
    {
    //you can declare local variables here
    int dog = 3;
    int cat (4);
    cout << dog << endl;
    cout << cat << endl;
    cout << dog + cat << endl;
    dog = rabbit;
    cout << "we have now changed dog to rabbit" << endl;
    cout << dog;
    return 0;
    }
    The result should look like this:
    [php]
    3
    4
    7
    We have now changed dog to rabbit
    1
    [/php]

    Credits:
    www.cplusplus.com
    www.cprogramming.com
    Me
    Last edited by P0SEID0N; 04-15-2010 at 02:51 AM.

  2. The Following User Says Thank You to P0SEID0N For This Useful Post:

    why06 (04-15-2010)

  3. #2
    killashoota1's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    37
    Reputation
    10
    Thanks
    4
    Nice. I was watching this guy's tuts on youtube but i might just switch to this. And 1 suggestion: Can you add more things into 1 day until you get to the more complicated things? Just asking. U dont have to. But again, nice guide. Keep it up

  4. #3
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Good tutorial I would say, I learned something new. I did not know you could initialize like this:
    Code:
    int a (6)
    About the length: I would say this is a good size. because though you only talk about one thing u go into quite a bit of detail about it and I feel that is best. Keep up the good work.

    EDIT: Looking back I see most of this is partial copy and paste off of cplusplus.com, but you gave credit, and I think this is a good thing so no worries.
    Last edited by why06; 04-15-2010 at 09:29 PM.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  5. #4
    P0SEID0N's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    KFC
    Posts
    318
    Reputation
    10
    Thanks
    24
    My Mood
    Lurking
    Not really a straight copy and paste. Took me an hour to put this together and re-word it and then added some other stuff. I just stole their tutorial structure :]

  6. #5
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Edited post: changed "straight" to "partial" sry bout that, but only partially.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  7. #6
    P0SEID0N's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    KFC
    Posts
    318
    Reputation
    10
    Thanks
    24
    My Mood
    Lurking
    <3 Msg2Short.

Similar Threads

  1. Replies: 3
    Last Post: 04-16-2010, 10:29 PM
  2. Learn how to code C++ || Day 3 - Variables and Date Types
    By P0SEID0N in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 8
    Last Post: 04-16-2010, 09:26 PM
  3. Learn how to code C++ || Day 4 - Input and Modifying Variables
    By P0SEID0N in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 0
    Last Post: 04-15-2010, 08:49 PM
  4. Learn how to code C++ || Day 1 - Compiler
    By P0SEID0N in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 14
    Last Post: 04-15-2010, 02:42 AM
  5. Learn how to code C++ || Day 1 - Compiler
    By P0SEID0N in forum C++/C Programming
    Replies: 5
    Last Post: 04-13-2010, 06:21 PM