Tuesday, August 3, 2010

C++: Meet some terms used in classes

Well, I hope you understood the last message on classes - what they are and
things we can use to create rich set of custom data types. Now, let's take a
look at some terms used when talking about classes and objects.

First, the word "class" itself. In tech language, it means, " programmer
defined data type, a container containing data and functions for a specific
object." So, for instance, a computer can be a class, since a programmer
needs to create that custom data type for working with computers e.g. CPU,
RAM amount, etc. Or, a class might be everyday objects, including tables,
chairs, doors, rooms and so forth. Or it could be something specific to a
particular task, such as computer time, dates, appointments and even word
processor documents.

In order for us to work with classes, we need two things: member variables
(the properties of a given object) and functions to access and set member
variables (called member functions). Member variables could be anything,
including numbers, decimal numbers, strings or even a class that a
programmer has created. Member functions include ones to create, or
instanciate a class to existence(constructors), mutators to set member
variables and accessors to get a member variable. The mutators and accessors
are sometimes called "setters" and "getters", respectively.

In terms of syntax, a class has usual appearance of a function except that
there are no parentheses for storing arguments, and that you need to put a
semicolon after right brace(};). For instance, if I want to create a class
called "car," I have to write it as:
class car
{
// Whatever we need to put in...
};
The whole discussion of why we put in semicolon would be discussed much
later when we go into data structures.

Few more short definitions:
* Constructor: A constructor is a special member function to instanciate, or
make an object exist. There are usually two types of constructors with
variations of each of them: a "default constructor" for storing default data
for that class, and "parameterized constructor" to put necessary arguments
that a user (programmer) types when creating a variable of that class type
(after all, classes are custom data types).
* Mutator: A mutator sets something to a member variable of that class.
Usually they are "procedures" (void functions) with a prefix "set".
* Accessor: An accessor is a function to retrieve values stored in a member
variable. They have a return type and usually they are prefixed by the word
"get".
* Access keyword: An access keyword defines how much information, or data is
exposed to other programs. There are three of them: private, useful when a
programmer wants to limit access to a member variable or a function to a
given object only, public where any program and data (anyone) can access the
object and protected, which means that a child class (an inherited class
from a base class) wishes to use existing privilages of members of a parent.
The third keyword requires special attention - thus, I'll come back to it
when we build another layer of custom classes on top of a base class (called
inheritance).
* Destructor: A destructor is used to destroy an instance of an object. This
requires a keystroke in front of a constructor, which I'll show you much
later when the time arises.

And other terms, which you'll meet as time goes by. Starting from a few
posts later, I'll demonstrate an example of creating a simple class (called
car). Along that way, I'll give you some tips on creating your own classes
and have you go with building your own simple objects, declaring it under
main() and actually using it.
// JL P.S. Gotta run for CS lecture...

No comments:

Post a Comment