|
Lesson 12:
Introduction to Classes
C++ is a bunch of small additions to C, and one major
addition. This one addition is the
object oriented approach. As its name suggests, this
deals with objects. Of course, these are
not real-life objects. Instead, this objects are the
essential definitions of real world
objects, or people. Structures are one step away from
these objects, they do not possess one
element of them: functions. The definition of these
objects are called classes. The easiest
way to think about a class is to imagine a structure
that has functions.
What is this mysterious structure? Well, it is not only
a collection of variables under
one heading, but it is a collection of functions under
that same heading. If the structure is
a house, then the functions will be the doors. They
usually will be the only way to modify the
variables in this structure, and they are usually the
only to to access the variables in this
structure.
From now on, we shall call these structures with
functions classes(I guess Marx would not
like C++). The syntax for these classes is simple.
First, you put the keyword 'class' then
the name of the class. Our example will use the name
computer. Then you put the different
variables you want the class to hold. In our example,
there will be only one, processor speed.
However, before putting down the different variable, it
is necessary to put the degree of
restriction on the variable. There are three levels of
restriction. The first is public, the
second protected, and the third private. For now, all
you need to know is that the public
specifier allows any part of the program, including what
is not part of the class, access the
variables specified as public. The private specifier
allows only the functions of the class
that owns (not a technical term) the variable to access
that variable.
#include <iostream.h>
class computer //Standard way of defining the class
{
private: //This means that all the variables under this,
until a new type of restriction is
//placed, will only be accessible to functions that are
part of this class.
//NOTE: That is a colon, NOT a semicolon...
int processorspeed;
public: //This means that all of the functions below
this(and variables, if there were any)
//are accessible to the rest of the program.
//NOTE: That is a colon, NOT a semicolon...
void setspeed(int p); //These two functions will be
defined outside the class
int readspeed();
}; //Don't forget the trailing semi-colon
void computer::setspeed(int p) //To define a function
outside put the name of the function
//after the return type and then two colons, and then
the name
//of the function.
{
processorspeed = p;
}
int computer::readspeed() //The two colons simply tell
the compiler that the function is part
//of the class
{
return processorspeed;
}
void main()
{
computer compute; //To create an 'instance' of the
function, simply treat it like you would
//a structure. (An instance is simply when you create an
actual object
//from the class, as opposed to having the definition of
the class)
compute.setspeed(100); //To call functions in the class,
you put the name of the instance,
//and then the function name.
cout<<compute.readspeed(); //See above note.
}
As you can see, this is a rather simple concept.
However, it is very powerful. It makes it
easy to prevent variables that are contained(or owned)
by the class being overwritten
accidentally. It also allows a totally different way of
viewing programming. However, I want
to end this tutorial as an introduction. I am going to
be writing more tutorials on classes,
which will go into more details on classes.
PLEASE send comments on this tutorial. It was written
much after all the others had been
written, and I am want to ensure that I am upholding the
standard of the previous tutorials. I
want to be certain this tutorial is clear.
Note: My homepage is http://www.cprogramming.com. My
email is webmaster@cprogramming.com. Please
email me with comments and or suggestions. If you want
to use this on your own site please
email me and add a link to http://www.cprogramming.com.
|