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
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:
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