PDA

View Full Version : Hacking the code


masterof_none
27-05-2003, 12:10 AM
Hello everybody,
yesterday, I talked about the why we need to create our own type,
In CS, they call it user defined type.
What is that? keep reading...
this is the summary of today;s discussion:

1. revise yesterday's stuff
2. intro to class
I urge you guys to go through this:! Very important stuff!

1.Revise Yesterday's stuff:
User defined type simply means you decide what do we want to store and how we store stuff. I'll go through again:

e.g, to store :
integer, we use int
e.g int blah;

non-integer number, we use float or double
e.g float blah, double stuff

character strings, we use, char
char stuff[10] //string with no more than 10 characters

Those int,float, char has been defined in ANSI-C. It is not defined by us, rather, they come with C .
so far so good, but, in real life, life is not as simple as character, integer and float. In real life, we have many things, such as humans, cars, girlfriends, or dinasours.

from yesterday;s example, let say we want to store a type dinasour:

dinasour Barney ;

But in C, they don;t provide type dinasour for you. they just have int, float,
char to name a few (if you're npt familiar with this, I think you can grab any C/C++ book for reference).

so how do we resolve this problem? we have to create our own dinasour.
now, first, decide, what kind of dinasour do you like:

name: Barney;
age :5


now, in C, they have what they call, struct for user-defined type.
e.g

typedef struct {
char name[10] ;
int age ;
} Dinasour ;

There we go, now, we have a dinasour, we can name it whatever we want.
so, since our favourite dinasour named Barney, we go:

Dinasour *barney ; //a pointer to our dinasour.
strcpy(barney->name,"Barney"); //note, you can;t do barney->name = //barney

barney->age = 5;


OK. then we;re done,That;s what they call user defined type.
(if you're not familiar with pointer, don;t worry, email me hasran@<hidden>, and I'll tell you more!).

Now, moving to part 2:
Intro to class.
What is class? class is just like struct above, but the difference is that, you can define functions to manipulate the data (actually more, but let;s talk abt function first).

yesterday, I talked abt function, (refer to yesterday's post).

so, to change our Dinasour to a "class", simply redefine class like this:

class Dinasour {
private:
char name[10];
int age ;

public:
int get_age();
};

Do you realize what;s the difference? instead of accessing directly to age,like before, (barney->age). we have to access the age like this:

int barney_age = barney->get_age();

This looks like unnecessary, why we must complicate our life?
You see, human like to complicate themselves. The struct/class difference is just only one of it ;-)

Actually, there are security issues. (private data (e.g name, age, can be accessed only through public stuff).

The reason is simple. Imagine you have $100. You put it outside your garage(give access to public). What would happen?
Unless you set up some charity organization (public function), there;s no way people can get your money. (unless they steal your money, and that;s how people hack your computer and get all the data)

OK, I guess , that;s all for today, see ya tomorrow.

littlebigone
04-06-2003, 03:48 AM
From the way you describe struct, i think it's save to say that struct would only allow you to define the state of something. For example, you can define a struct Dinosaur that has variables such as "name", "age", "height" and so on and so forth. With struct you can only modify the state of the dinosaur.

But with class, it seems that you've added another dimension to the object you're trying to describe. You've given it the ability to do things by allowing functions. For example, you can write a function "sing" that lets the Dinosaur sing!!! :) You can even make it "eat", "walk", "sleep", and even "dance".

It seems that with class, we're pretty much able to almost simulate a real living dinosaur's behaviour. Which is the idea behind OOP i think. OOP allows you to create objects which simulate real life objects and allow these objects to interact in a programming environment.

I hope what i;ve just written makes sense and most importantly is correct. So admin's feel free to edit and add whatever needs to be edited.

masterof_none
04-06-2003, 06:50 AM
yea, thanks a lot!,
I think that's another way (and better) way to describe what OOP is.
Since most of us usually give up programming when being introduced to the 'class' concept, I think it's important to us to kind of relate the class, with struct, int, char, etc.
This is what I meant:

to store integer, use:
int blah ;

to store character, use :

char blahblah;

to store a combination of int and character, use:

typedef struct {
int blah;
char blahblah;
} Dinasour;


to make the dinasour sing 10 times, use class,

class Dinasour {
int sing;
char blah ;

void set_how_many_times_can_the_dinasour_sing(int x);
int get_how_many_times_can_the_dinasour_sing();
};


there we go, the dinasour, now can sing to how many times you give into the parameter set_how_many_times_dinasour_sing(int x);

so, we can use, in the main program,

int main() {
Dinasour barney; //barney is a dinasour that can sing .
barney.set_how_many_times_the_dinsour_can_sing(10);

//now, we want to know how many times,
cout<<"Barney can sing" + barney.get_how_many_times_can_the_dinasour_can_sing() +"times!";

}

we will get the output :
Barney can sing 10 times! ;

such is a Barney life. ;-)