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