Learning C/C++ - Tutorial

Discussion in 'Programming & Scripting' started by Fallen, Jun 14, 2012.

  1. GhostX

    GhostX Pandageist

    Joined:
    Nov 2, 2011
    Messages:
    5,598
    Likes Received:
    676
    Best Answers:
    0
    lazy ass excuse of a coder. Kef to you old timer. Kef to you!
     
  2. OmfgRegister

    OmfgRegister Member

    Joined:
    Mar 7, 2009
    Messages:
    151
    Likes Received:
    7
    Best Answers:
    0
    Since Fallen is done writing the turoials and I'm bored...

    10. C++ - Subclassing abstract classes and re-defining virtual functions.

    Lets take a class for example

    Code (Text):
    1.  
    2. class Orc
    3. {
    4. public:
    5.     int hp, maxHp;
    6.     int mana, maxMana;
    7.    
    8.     void fight()
    9.     {
    10.         cout << "Fighting!\n";
    11.     }
    12.     void run()
    13.     {
    14.         cout << "Running!\n";
    15.     }
    16.     void growl()
    17.     {
    18.         cout << "Growl\n";
    19.     }
    20. };
    21.  
    22. class Elf
    23. {
    24.     int hp, maxHp;
    25.     int mana, maxMana;
    26.    
    27.     void fight()
    28.     {
    29.         cout << "Fighting!\n";
    30.     }
    31.     void run()
    32.     {
    33.         cout << "Running!\n";
    34.     }
    35.     void speakElven()
    36.     {
    37.         cout << "El grath\n";
    38.     }
    39.  
    40. };
    41.  

    One of the big things in programming is to have as little duplicated code as possible (saves time)
    Looking at these two classes they look pretty similar (almsot identical)

    An abstract class is a class that others can inherit off of and use their functions and data


    Code (Text):
    1.  
    2. class Monster
    3. {
    4. public:
    5.     int hp, maxHp;
    6.     int mana, maxMana;
    7.    
    8.     void fight()
    9.     {
    10.         cout << "Fighting!\n";
    11.     }
    12.     void run()
    13.     {
    14.         cout << "Running!\n";
    15.     }
    16. };
    17.  
    18.  
    19. class Orc : public Monster
    20. {
    21. public:
    22.     void growl()
    23.     {
    24.         cout << "Growl\n";
    25.     }
    26. };
    27.  
    28. class Elf : public Monster
    29. {
    30. public:
    31.     void speakElven()
    32.     {
    33.         cout << "El grath\n";
    34.     }
    35. };
    36.  
    37.  
    the orc class and elf class now inherit all of the functions of monster
    Examples of code
    Code (Text):
    1.  
    2. Elf legolas;
    3. legolas.hp = 200;
    4. legolas.fight();
    5.  
    As you can tell the elf class functions in the exact same way as before


    Now lets say that an elf fights a little differently than a normal monster and needs it own fight function
    this is where virtual functions come in
    Code (Text):
    1.  
    2. class Monster
    3. {
    4. public:
    5.     int hp, maxHp;
    6.     int mana, maxMana;
    7.    
    8.     // by specifying virtual, it allows the fight function to be overriden in subclasses
    9.     virtual void fight()
    10.     {
    11.         cout << "Fighting!\n";
    12.     }
    13.     void run()
    14.     {
    15.         cout << "Running!\n";
    16.     }
    17. }
    18.  
    19.  
    20. class Orc : public Monster
    21. {
    22. public:
    23.     void growl()
    24.     {
    25.         cout << "Growl!\n";
    26.     }
    27. }
    28.  
    29. class Elf : public Monster
    30. {
    31. public:
    32.  
    33.     // overriden fight function from monster parent class
    34.     void fight()
    35.     {
    36.         // do eleven fight
    37.     }
    38.     void speakElven()
    39.     {
    40.         cout << "El Grath!\n";
    41.     }
    42. }
    43.  
    44.  
    now we can have an elf that call its own fight function instead of the default

    Lets move onto to pure virtual functions
    these are functions in a parent class that the sub classes must override

    Example:
    Code (Text):
    1.  
    2. class Monster
    3. {
    4.     int hp,maxHp;
    5.     int mana, maxMana;
    6.    
    7.     virtual void speak() = 0;
    8.     virtual void fight()
    9.     {
    10.         cout << "fighting!\n";
    11.     }
    12.     void run()
    13.     {
    14.         cout << "Running\n";
    15.     }
    16.    
    17. }
    18.  
    Notice the line
    virtual void speak() = 0;
    the = 0 means there is no implementation of the function
    and since the class now has pure virtual functions you can no longer create a monster class
    the following code is now illegal
    Code (Text):
    1.  
    2. Monster m;  // Error: cannot create an instance of an abstract class
    3. m.run();
    4.  
    However you can do:
    Code (Text):
    1.  
    2. Monster *m = new Elf();
    3. m.speak();
    4.  

    But remember the sub classes now must have a function speak() in them so our old elf and orc classes will no longer compile

    Lets fix those..
    Code (Text):
    1.  
    2. class Orc : public Monster
    3. {
    4. public:
    5.     void growl()
    6.     {
    7.         cout << "Growl!\n";
    8.     }
    9.    
    10.     void speak()
    11.     {
    12.         growl();
    13.     }
    14. }
    15.  
    16. class Elf : public Monster
    17. {
    18. public:
    19.  
    20.     // overriden fight function from monster parent class
    21.     void fight()
    22.     {
    23.         // do eleven fight
    24.     }
    25.     void speakElven()
    26.     {
    27.         cout << "El Grath!\n";
    28.     }
    29.    
    30.     void speak()
    31.     {
    32.         speakElven();
    33.     }
    34. }
    35.  
    36.  

    Polymorphism: feature that allows values of different data types to be handled using a uniform interface.
    Code (Text):
    1.  
    2. vector<Monster*> monstersOnMap;
    3. for (int i =0; i < monstersOnMap.size(); i++)
    4. {
    5.     monstersOnMap[i]->speak();  // since I know that a monster has a speak function I can call it.. doens't matter if its an elf or an orc or whatever
    6. }
    7.  


















    11. C++ - The meaning of a static variable/function.

    Static means something doesn't change
    And in some instances that there is only 1 of them

    lets look at a class Button:
    Code (Text):
    1.  
    2.  
    3. class Button
    4. {
    5.     Image buttonPicture; // picture is 50 kb
    6.    
    7.     std::string text;
    8.    
    9. public:
    10.     void pressed();
    11.     void setText(std::string text)();
    12.    
    13. };
    14.  
    15.  
    And lets look at some imaginary code(this won't compile):
    Code (Text):
    1.  
    2.  
    3. Form::Form() // the initlizer of a form class (Something that would have buttons)
    4. {
    5.     Button b1();
    6.     Button b2();
    7.     Button b3();
    8.     Button b4();
    9.    
    10.     add(b1);
    11.     add(b2);
    12.     add(b3);
    13.     add(b4);
    14.  
    15. }
    16.  
    17.  
    18.  

    so basicly i'm creating a form with 4 buttons
    every button has its own memory
    and its own buttonPicture
    so the memory consumption is 200 kb just for the images (which every button has the same of)

    If we rewrite the Button class using a static variable for buttonImage

    Code (Text):
    1.  
    2.  
    3. class Button
    4. {
    5.     static Image buttonPicture; // picture is 50 kb
    6.    
    7.     std::string text;
    8.    
    9. public:
    10.     void pressed();
    11.     void setText(std::string text)();
    12.    
    13. };
    14.  
    15.  
    It will now only have 1 instance of buttonPicture for the class
    and thus save memory

    lets look at mis-uses of static and why not to use it in some cases
    Code (Text):
    1.  
    2. class Monster{
    3. public:
    4.     static int hp;
    5.    
    6. };
    7.  
    Code (Text):
    1.  
    2.     Monster m;
    3.     m.hp = 100;
    4.    
    5.     Monster m2;
    6.     m2.hp =200;
    7.    
    8.     cout << m.hp << endl; // prints out 200 since hp is static
    9.  

    Static functions are a little different.. they are functions that are called without an instance of the class
    and can only use the classes static varaibles

    Code (Text):
    1.  
    2.  
    3. class ABC
    4. {
    5.     static int x;
    6.     int y;
    7.     static void random()
    8.     {
    9.         cout << x << endl;
    10.         cout << y; // Error because y is not static
    11.    
    12.     }
    13. }
    14.  
    15.  
    you can call static functions by:
    Code (Text):
    1.  
    2. ABC::random();
    3. // an instance of the class may also call them
    4.  
    5. ABC a;
    6. a.random();
    7.  
     
    Last edited: Jun 22, 2013
  3. Fallen

    Fallen Freelancer

    Joined:
    Aug 21, 2009
    Messages:
    3,714
    Likes Received:
    260
    Best Answers:
    0
    Updated first post with two logical operators, AND and OR. Later will be adding the rest.
     
  4. jn0619

    jn0619 I am too Handsome

    Joined:
    May 23, 2012
    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    You could use "#define" so that you won't have to type the whole thing. :p

    #define p printf
    #define s scanf

    Like this:

    Code (Text):
    1. #include<stdio.h>
    2. #define p printf
    3.  
    4.     main()
    5.    {
    6.     p("Hello World!\n");
    7.  
    8.     getch();
    9. }
    -JannoV
     
    Last edited: Jul 24, 2013
  5. pasiak12

    pasiak12 Member

    Joined:
    Jun 7, 2009
    Messages:
    142
    Likes Received:
    18
    Best Answers:
    2
    15. A Brief idea of how OT code work (we'll be using TFS). - does anyone know where can I find any informations about this?
     

Share This Page

Loading...