struct
. In fact if you wanted to use a class
instead of a struct
but don't want to have to change any of your code you could do what I do to the date struct
(below).
struct date
{
int year, month, day;
};
And make it look something like:
class date
{
public:
int year, month, day;
};
Of course this is blasphemous coding and if any stick-up-the-ass C++ programmer catches you doing this then he'll have your head date
class the constructor function would be called date
as well. When you create an object based on a class the constructor function is called. For example if we have a class called date
(yes, I'm going to wring every last ounce of usefulness out of crummy ol' date
) and we create an object called dt
:date dt;
dt
, dt
's constructor function date()
is called (only if it exists of course). To see this in action try the following snippet of code:
/* -example 1--------------------------------------------------------------- */
#include <iostream.h>
class date
{
public:
date()
{
cout << "We have just created an object based on this (date) class!"
<< endl;
}
int year, month, day;
};
void main()
{
date dt;
}
/* ------------------------------------------------------------------------- */
We have just created an object based on this (date) class!
". So you see, when we created the object dt
that function was automatically called as well. Many people have asked me the use of this, so I will try to explain. The constructor function (date()
in this case) is used mainly for resetting the variable members of the class. In the above example I could have shown this by putting the following line in the function:year = 0; month = 0; day = 0;
year = 1998; month = 10; day = 6;
cout << "Input a year: ";
cin >> year;
cout << "Input a day: ";
cin >> day;
cout << "Input a month: ";
cin >> month;
date
example because by this point you should be fairly used to its aging charm.date
class) that we wanted to set its variables when we create the object. And we want these values to be different each time. Let's say we want to create two objects (based on the date
class): neil_dob
and joey_dob
. And we know when we're coding this that neil_dob
should be 8-19-1979
and joey_dob
should be 11-28-1976
. Since we already know what the values of these objects should be, making the user type them in would be tiring. One way we could do this is by changing each of the variables individually:
date neil_dob, joey_dob;
neil_dob.year = 1979;
neil_dob.month = 8;
neil_dob.day = 19;
joey_dob.year = 1976;
joey_dob.month = 11;
joey_dob.day = 28;
jae_dob
or nurit_dob
?!?! Yes, you're probably thinking that by that time you would no longer have fingers but gnarly taloned paws. Fear not, there is a simple answer to this (not the only one, but hey, I'm trying to have a point to this year
, month
, and day
. And in this constructor we could set the values of each of the variables to the parameter's values. So if we pass (1979, 8, 19)
, then it will place those values appropriately (year
would become 1979
, etc.). Now, I know what you're thinking? What could this wonderful invention cost? 10,000 dollars? No, just two easy steps my friend.
date (int y, int m, int d)
{
year = y;
month= m;
day = d;
}
date(1979,8,19);
date dt;
()
and put them after the name of the object, but before the semi-colon:
date dt(1979,8,19);
neil_dob
and joey_dob
):
/* -example 2--------------------------------------------------------------- */
#include <iostream.h>
class date
{
public:
date(int y, int m, int d)
{
year = y;
month= m;
day = d;
}
int year, month, day;
};
void main()
{
date neil_dob(1979,8,19);
date joey_dob(1976,11,28);
}
/* ------------------------------------------------------------------------- */
int x,y;
date neil_dob(1979,8,19), joey_dob(1976,11,28);
date neil_dob(1979,8,19);
. It breaks down like this:date |
symbolizes that we are going to create an object based on this class definition. Simply speaking we're going to make something. |
neil_dob |
is the name of the object that we're going to create. |
( |
also known as the infamous and often forgotten left parenthesis tells us that we're going to be passing some parameters. |
1979, |
tells us that the first parameter (y ) in the constructor function will be this value. |
8, |
tells us that the second parameter (m ) in the constructor function will be this value. |
19 |
tells us that the third and last parameter (d ) in the constructor function will be this value. |
) |
says that we're done passing parameters and there ain't gonna be no more of that. |
; |
of course states that this statement is finished. |
y
is 1979
, m
is 8
, and d
is 19
. The following statements ensue:
year = y;
month= m;
day = d;
y
has been set to 1979
, the value of variable member year
becomes 1979
as well (year = y;
or in this case it might look like year = 1979;
). You probably get what happens next and by the time we reach the end of the function year
has been set to 1979
, month
has been set to 8
, and day
has been set to 19
.void just_a_func()
{
int local_var; // <--------- we create local_var
// we do something here with local_var
} // <------------------------ local_var is destroyed at this point
If local_var
had been an object based on a class, its constructor would have been called where we created it, and its destructor where it was destroyed (at the end of the function). When the destructor is called you can be sure that your object is going bye bye, and you use the chance to fix all the misgivings your object may have wreaked during its life.~
). So for our class date
the destructor prototype would be:~date();
Pretty simple. The biggest difference (that I see) between constructors and destructors is that the latter cannot have any parameters. I mean seriously, what and how are you going to pass things to something that's on its way to doom. Its like throwing a rope over a cliff after someone has fallen. Its too late! /* -example 2--------------------------------------------------------------- */
#include <iostream.h>
int num_date_objects; // global variable to keep track of the number
// of 'date' objects
class date
{
public:
// constructor!
date(int y, int m, int d)
{
year = y;
month= m;
day = d;
num_date_objects++; // add one to the number of date objects, this
// number will be THIS object's id number
id = num_date_objects;
cout << "Calling constructor, creating date object #" << id << "!"
<< endl;
}
// destructor!
~date()
{
cout << "Calling destructor! *AWOOGA* *AWOOGA*! date object #"
<< id << " has perished!" << endl;
}
int year, month, day, id;
};
void main()
{
num_date_objects = 0;
date neil_dob(1979,8,19);
date joey_dob(1976,11,28);
}
/* ------------------------------------------------------------------------- */
After running the above you'll get the following output:Calling constructor, creating date object #1!
Calling constructor, creating date object #2!
Calling destructor! *AWOOGA* *AWOOGA*! date object #2 has perished!
Calling destructor! *AWOOGA* *AWOOGA*! date object #1 has perished!
Note the order in which the objects are destroyed. It is the exact opposite of the order in which they were created. You know there's a nice acronym that stands for something that relates to this, but by god I totally forgot what it was.public
). However there are parts that they jealously guard (protected
) that only people close to them can touch like legs, noses, or elbows. And lastly there are those parts that they cover with fig leaves that only they (or their "better half") have access to. We call these "private
s".date dt;
) then all of its public members can be accessed from the function you created it in. For example when we created dt
in main()
we had access to its public members like year
or day
. We could have accessed them by joining the name (dt
) and the variable member (year
, month
, or day
) with a period:
void main()
{
date dt;
dt.year = 1998;
dt.month= 10;
dt.day = 6;
}
public
in the date
class. Had we listed them under private
or protected
we would not be able to do this (compiler would puke).protected
class members can only be used inside the class they were declared in or derived/inheriting classes (if you don't know class inheritance yet then don't worry too much about the difference between protected
and private
... yet). So if we derived a class called astro_date
from the class date
, then all the protected members of date
would be accesible by astro_date
's functions.private
class members on the other hand, can only be accessed by the class they are listed in. Not in derived classes or anything, JUST the class they are listed in (drilly this into your head). private
means private
as in, don't show or allow access to anyone other than myself (or better half Page Content & Design ©1998 By Neil C. Obremski |
![]() home |
![]() prfce |
![]() loop |
![]() refs |
![]() ptrs |
![]() struct |
![]() class |
![]() array |
![]() rsrc |
![]() site |
![]() indx |
Wed May 12 21:45:26 1999 |