Results 1 to 15 of 15
  1. #1
    schim's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    My chair
    Posts
    367
    Reputation
    10
    Thanks
    114
    My Mood
    Twisted

    [TUT/Theory] A look at classes! (and structs...ohh and unions too)

    I was bored (bored close to death actually) so as boring as this may sound, I was exited to write some boring theory down, in order to help some of you that currently are clueless about classes/structs and unions


    Table of contents:

    1. Structures
    • [Theory on objects]
    • [What's a structure?]
    • [How do I use a structure?]
    • [Unions]
    2. Classes

    • [What's a class?]
    • [How do use a class?]
    • [What are friend classes?]


    1. Structures

    Theory on objects

    C++ is all about object orientated programming. So what exactly is an object?
    This is a pretty easy question to answer, because an object can be virtually anything you want it to be. But there are a few more thing to pay attention to. Most programs are all about solving problems (in game hacks this being the problem: how can I cheat?) often various things are involved in these problems so why not just describe (and fix) the problem using these things?

    Let me give you an example:
    Say we are making a hack for a game called...er...SCHiM game
    You've studied SCHiM game for quite a while now, and you've figured many things out, the hp addres, ammo adders, x/y/z pos and much more

    If you didn't have any knowledge on classes your hack would probably look verry sloppy since you have many, many addresses to keep track of, so you pack them in a neat little stucture to keep track of all your addresses, functions, other structures and everything else you may need.

    Let me show you how a structure looks:

    Code:
    struct GameHack{
    bool hackson;
    DWORD address1;
    DWORD address2;
    DWORD address3;
    void HackFunction();
    void LogFunction();
    }
    You see how nice and tight it looks in contrast with this:

    Code:
    bool hackon
    /*
    crap that often lays about being totaly crappy and sloppy
    crap that often lays about being totaly crappy and sloppy
    crap that often lays about being totaly crappy and sloppy
    */
    DWORD addres1;
    //more crap
    DWORD addres2;
    Now the difference is not that big, maybe this is a wrong example to give
    Imagine having a HUGE databasse (like the gouverment has that keeps track of all people in the country) You could easly sture all data about 1 people inside 2 or 3 structures:

    Code:
    struct work{
    int salary;
    char* company name;
    int phone number;
    char* company description;
    }; // don't forget the semicolon!!
    
    struct person{
    int age;
    char* name;
    work job;
    int phone number;
    person familly //This'll link to other people close to our 'person'
    };
    As you can see, you can include structures into structures into structures which are structures that are included within structures included into infinity
    Now try adding an infinite number of basic datatypes

    Now some people who are still following me after the structures into infinity part will note that, while it looks tight and neat, you still can only describe one person. To these people I say: good job on noticing that but wrong, because with a few slight changes to the current person code I can initialize whole arrays of persons.

    The world holds 6 billion people? ok, no problem:

    Code:
    person AllPeopleOfTheWorld[6.000.000.000];
    Now I've succesfully created the means to describe every living person on earth (gathering all the data is a completely different thing though )

    What's a structure?

    A structure in C++ is a leftover from the language C, essentially a structure was meant to create a named aggregate of data items of different data types (it means that you can give a name to a buch of different data types and that way keep them klose knit, and within reach)

    In C++ you'll mostly use the structure the same way as in C, however C++ added some neat functions that were developed for classes, in fact, a class can functionally replace a C++ structure (hence why I called a structure a leftover since it's renderd obsolite by classes)

    So why should you spend tie and effort to study something that's a leftover?
    That's because classes and structures are different from each other, and because it's there- part of the C++ language- And because your efforts to study structures will not be wasted, since the same rules apply to classes

    So that's what a stucture is:

    A structure is a data type that defines a particular kind of object of your chosing

    How do I use a structure?

    Now that's an easy question to answer, I'll just show you an example

    First you have to create a blue print of your structure somewere in your code, using the keyword 'struct' followed by the name you wish to give this object

    Code:
    struct book
    Then you'll have to open a brace '{' and chose of which basic datatypes your object will consist (remember to put the semicolon ';' behind every datatype you chose)

    Code:
    struct book{
    int numberofpages;
    int isbn;
    char author;
    }
    Finish with a closing brace '}' and your blue print for a structure is complete

    Now you can declare a new book object in your code like this:

    Code:
    book HarryPotter
    Now, for the people who are still reading, and not dizzy and seeing purple cows dancing on the walls because of all the letters, will notice that we now have a book object. But that object doesn't hold any data, This is where aggregation comes in, or rather the use of an initializer list

    Code:
    book HarryPotter = { 100, 129529392, "J. Rowling" };
    The initializing values appear between a set of braces, seperated bt commas, in much the same way that you would initialize arrays. the sequence of initial values needs to match the sequence of members in the structure definition. this statement initializes each member of the object book, with the corresponding value between the braces.

    Now, for the people who are still reading -and not visiting a shrink because they told their moms that they saw purple cows dancing on the walls- and notice that while it's great to have a bunch of data inside one object, you can't do anything with it, I'll introduce the following:

    You can acces one data type (or from now on reffered to as a member of an object) of an object, at a time like this:

    Code:
    HarryPotter.author = "SCHiM"; //notice that we use the same name(author) to acces the value of author, as defined in our blue print
    You can just use these as you would normaly use data:

    int i = HarryPotter.numberofpages;

    for(int x; x != HarryPotter.isbn; x++)

    Would all be valid statements to make

    Unions

    A union is a data type that allows you to use the same block of memory to store values of different types at different times. The variables of an union are reffered to as members of the union. You can use a union in three ways:

    1. You can store different datatypes in the same memory block, at different times. The idea about this was to simply economize on the use of memory at a time when memory was scarce.
    2. You can save memory on a much larger scale using this 2nd technique. Suppose that you have a situation in which a large array of data is required but you don't know in advance of execution what the data type will be -it will be determined by the imput data. A union allows you to have several arrays of different basic types as members, and each array occupies the same block of memory (rather then each having it's own memory area). At compile time you've coverd all possibilities; and at execution time, the program will use whichever array is appropriate to the type of data entered.
    3. You may want to use the union to interpret the same data in two or more different ways. for example suppose you have a variable of type int, you may want to treat as four characters of type char.

    Be warned, unions are prone to errors!

    2. Classes:

    What's a class?

    Classes are the eccence of object-oriented programming (OOP)
    It's how you write programs in terms of objects that have something to do with the problem you try to solve (remember the game hack, years ago when we talked about stucts? No? me neither... it's probably because of the cows...err purple cows...)

    Another thing to note about OOP is that you try to solve the problem with a solution that is as close to the real situation as possible.

    Suppose you've a structure that defines banque accounts

    Code:
    struct bank accounts{
    int money;
    char* name;
    int pin;
    };
    You have a bank account:
    Code:
    bank accounts yourbankaccount = {100.000, "Your name", 1337 };
    only you know your secret ping, which is verry l33t btw...

    Now suppose thay an evil vilan comes allong, and does this
    yourbankaccount.pin = 1234;
    Now he would have all your money!!!!!
    But why would you?
    If you can just do this?
    yourbankaccount.money = (99^99^99^99^99^99^99^99)^99;

    Are we getting closer to solving the problem like we would do in real life?

    1. if your answer is yes, or if you are seeing purple cows again go back to kindergarten, or the shrink No siriously, seeing purple cows is not a good sign

    2. if your answer was no, then god bless your not so bright but brighter than the ones who said yes minds :P

    So here's when classes and data hiding comes in:
    Members of a class cannot be accessed directy if they aren't declared as: 'public' which in this case is a good thing.
    So if you have a class like this:

    Code:
    class bank{ //you use the keyword class to define an object as a class, just like you do with the struct
    int money;
    char* name;
    int pin;
    };
    Doing something like this:

    bank.pin = 1234;

    would no longer be possible, and produce an error from the compiler

    Now this is were the bright people come in again, they'll say: "wtf, we just discussed that the point of using structures and classes was that they could put a bunch of data types together!!!"

    Yes indeed, but that before I told you that you could use functions, that are defined public to access these members.

    How do use a class?

    First note 1 thing: all the members of a class have acces to each other

    So now I'm going to show you how to access the members of a class while not really accessing them!
    That looks like this:

    Code:
    class bank{
    public:
    int returnmoney();
    private:
    int money;
    char* name;
    int pin;
    }
    As you can see our blue print now holds a function protoype with the name returnmoney;

    Now this function doesn't yet do anything else than showing off his badd-ass name, but we can change that in any source file (the ones with the .cpp extension)

    Code:
    int bank::returnmoney(){
    return money;
    }
    As you can see, this makes the function return the money that your bank account currently holds, we can call this function like this:

    Code:
    bank yourbankaccount; //for now just imagine that there's data inside this class
    
    int moneyinback = yourbankaccount.returnmoney();
    Now the integer moneyinback, holds your total intrests.
    You can create any kind of member function

    As long as it's prototype is inside a header file (.h) and it's defenition is inside a source file (.cpp), this is because of memory usage, defining the function inside the class would create such a function every time an intance of that class is made, which is a a huge waste

    What are friend classes?

    Friend classes, are classes or functions that are not inside the class but can still access their members, it looks like this:

    Code:
    class someclass{
    friend int givesomeint();
    private:
    int someint;
    };
    Now the function someint could access the someint integer, without being part of the class itself

    That's it, more on classes are comming

    Big bad note: I havent even told half of everything there is to tell about structures, and even less then 10% about classes, if you guys show some intrest, I'll write some more, if not, then I won't


    I'm really tired right now, so that's the reason of the spelling mistakes
    Now I wanna go to bed

    -SCHiM

  2. The Following 4 Users Say Thank You to schim For This Useful Post:

    doofbla (09-05-2010),Hell_Demon (09-03-2010),hobosrock696 (09-04-2010),Jason (09-04-2010)

  3. #2
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    Just saw something that i don't agree with...

    "in fact, a class can functionally replace a C++ structure (hence why I called a structure a leftover since it's renderd obsolite by classes)"

    Never...

    Other then that.. good job
    Last edited by 'Bruno; 09-03-2010 at 06:48 AM.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  4. #3
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Well brinuz, you can replace structs by classes.

    Code:
    struct player
    {
        int health;
        int money;
    };
    Code:
    class player
    {
        int health;
        int money;
    
        void setMoney(int amount)
        {
            this.money = amount;
        }
    }
    Can both be mixed, but the classed have an advantage of letting you have functions in them(so you could loop through all players and do player.setMoney(0); for example(which is ofcourse the same as doing player.money = 0 but for larger things like aimbots or for example visibility checks classes are sexy ;P
    Ah we-a blaze the fyah, make it bun dem!

  5. #4
    mmbob's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    ja
    Posts
    653
    Reputation
    70
    Thanks
    1,157
    My Mood
    Bitchy
    Quote Originally Posted by Hell_Demon View Post
    Well brinuz, you can replace structs by classes.

    Code:
    struct player
    {
        int health;
        int money;
    };
    Code:
    class player
    {
        int health;
        int money;
    
        void setMoney(int amount)
        {
            this.money = amount;
        }
    }
    Can both be mixed, but the classed have an advantage of letting you have functions in them(so you could loop through all players and do player.setMoney(0); for example(which is ofcourse the same as doing player.money = 0 but for larger things like aimbots or for example visibility checks classes are sexy ;P
    You can have functions in structs too >_>.

  6. #5
    schim's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    My chair
    Posts
    367
    Reputation
    10
    Thanks
    114
    My Mood
    Twisted
    Quote Originally Posted by mmbob View Post
    You can have functions in structs too >_>.
    Yep that's true

  7. #6
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by mmbob View Post
    You can have functions in structs too >_>.
    can you? I thought only classes could have that o_O good to know
    Ah we-a blaze the fyah, make it bun dem!

  8. #7
    schim's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    My chair
    Posts
    367
    Reputation
    10
    Thanks
    114
    My Mood
    Twisted
    Quote Originally Posted by Hell_Demon View Post
    can you? I thought only classes could have that o_O good to know
    Yep you can, but it would be an overkill of course, why create a function inside a struct if you can just manipulate it without one

  9. #8
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow
    This is nice.... now if I only knew more c++ so I could actually apply this to hacking for now OFF TO CODE MORE USELESS PROGRAMSSS!!!!!! (and learn more c++)

    Also thanks

  10. #9
    doofbla's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    Biel*****/Germany
    Posts
    369
    Reputation
    10
    Thanks
    179
    My Mood
    Psychedelic
    Nice SchiM
    --> it's an awesome GO(O)D JOB!

    BUT correct me if i'm false:

    sometimes you define the functions IN the classes to use them as INLINE functions so you don't waste cpu time.

    I'm not so sure about this..

    GREAT TUT thx m8
    _____________________________________________

    READING TUTORIAL:

    1. READ MY POST
    2. THINK ABOUT MY POST
    3. PRESS THANKS
    4. MAYBE CORRECT MY POSTS :P




    Dijkstra:
    "Computer Science is no more about computers than astronomy is about
    telescopes."


    THANKS BUTTON RIGHT DOWN --->

  11. #10
    Void's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Inline.
    Posts
    3,198
    Reputation
    205
    Thanks
    1,445
    My Mood
    Mellow
    Quote Originally Posted by doofbla View Post
    Nice SchiM
    --> it's an awesome GO(O)D JOB!

    BUT correct me if i'm false:

    sometimes you define the functions IN the classes to use them as INLINE functions so you don't waste cpu time.

    I'm not so sure about this..

    GREAT TUT thx m8
    It will execute faster, but if you use the function too much it's going to take a lot of memory.

  12. The Following User Says Thank You to Void For This Useful Post:

    doofbla (09-06-2010)

  13. #11
    JayFabulous's Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    26
    Reputation
    10
    Thanks
    4
    My Mood
    Fine
    Is a virtual function, just a piece of code that can change in a derived method?
    Is a static member just one that can only be seen from inside the current file?

    These are the only things i'm currently having trouble with.

  14. #12
    radnomguywfq3's Avatar
    Join Date
    Jan 2007
    Gender
    male
    Location
    J:\E\T\A\M\A\Y.exe
    Posts
    8,858
    Reputation
    381
    Thanks
    1,823
    My Mood
    Sad
    Quote Originally Posted by JayFabulous View Post
    Is a virtual function, just a piece of code that can change in a derived method?
    Is a static member just one that can only be seen from inside the current file?

    These are the only things i'm currently having trouble with.
    If Class A has a virtual method mA, then when class C inherits class A, it can override mA, and make it do something else. It can also call back into Class A's mA routine. Virtual Methods are also used to create abstract classes and interfaces.

    A static method is one that can be called outside of an instance of the object. In other words, static methods are like normals methods only they cannot access non-static members of an object, they have access to private\protected\public static members of the class they are a member of, and can be called without initializing their corresponding object.



    There are two types of tragedies in life. One is not getting what you want, the other is getting it.

    If you wake up at a different time in a different place, could you wake up as a different person?


  15. The Following User Says Thank You to radnomguywfq3 For This Useful Post:

    Hell_Demon (09-05-2010)

  16. #13
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    You can replace a class, but it's an overkill to use a class for things like linked lists, just structs that contains Vars, that was what i meant to say.

    Sometimes it's an overkill to use a class when a struct is enough. (no need for functions) :\ And creating an array of classes... ouch.. :\

    Just my opinion.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  17. #14
    doofbla's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    Biel*****/Germany
    Posts
    369
    Reputation
    10
    Thanks
    179
    My Mood
    Psychedelic
    Quote Originally Posted by Void View Post
    It will execute faster, but if you use the function too much it's going to take a lot of memory.
    Thx -> also pressed

    I didn't know the point with the memory so... thx
    _____________________________________________

    READING TUTORIAL:

    1. READ MY POST
    2. THINK ABOUT MY POST
    3. PRESS THANKS
    4. MAYBE CORRECT MY POSTS :P




    Dijkstra:
    "Computer Science is no more about computers than astronomy is about
    telescopes."


    THANKS BUTTON RIGHT DOWN --->

  18. #15
    Kallisti's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,019
    Reputation
    52
    Thanks
    376
    My Mood
    In Love
    edit: nvm .

    未来が見えなくて怖いから
    未来が見えてしまって悲しいから
    目を閉じて優しい思い出に浸ってしまう




Similar Threads

  1. GET FREE MPGH VIP!!!LOOK HERE NOW AND FAST
    By aprill27 in forum Spammers Corner
    Replies: 10
    Last Post: 10-21-2017, 01:01 AM
  2. TAKE A LOOK AT THIS AND TELL ME WHAT IT IS
    By bigbadwolf in forum Combat Arms Europe Hacks
    Replies: 17
    Last Post: 04-19-2009, 01:09 PM
  3. [TuT]How to find no recoil and no spread
    By Twisted_scream in forum WarRock - International Hacks
    Replies: 10
    Last Post: 06-23-2008, 11:59 AM
  4. First Look: WR Information AND Screenshots
    By Dave84311 in forum WarRock - International Hacks
    Replies: 33
    Last Post: 07-09-2006, 01:41 AM