August 8th, 2004
A Short Introduction to Classes
Abstract
Why we need classes. Their scope. What are classes: definition and
discussion of classes. Functionality. More discussion. Examples.
Conclusions.
Introduction
Since the invention of the computers the poor programmers have been
taxed in their time by demanding customers and giant programs. In the
late 80’s and early 90’s was still alive the legend of the
one-man-project. Anyway the programming world has been filled with
hundreds of languages. Among these there were the B, BCPL and CPL
(Combined Programming Language), but none of these nor any other
language, fulfilled the common programmer needings. So one day in the
Bell Lab’s woke up a guy called Ritchie and announced to the world he
had a solution: the C language. That was pretty good: powerful,
flexible, it offered high level abstraction, mid-level programming,
and also low-level programming so to allow system programming.
All these languages had a structural skill: they were procedural
languages. They could not genetically manage Events and Messages.
With the advent of the Graphical User Interfaces (GUI) this problem
became more stringent. So a beautiful day of mid-spring came out a
guy, name’s Bjarne Stroustrup, who forced the programmers to drink
coffee by buckets. Through the implementation of a new language, the
C++, he introduced the idea of OOP and classes.
I Had Enough Classes at High School
One of the problems was that a data structure was publicly accessible
by other components of the program. That was risky, a programmer could
easily loose control on the data coherence.
There was the needing to wrap the data in a container, a container
moreover enforced by rules. Overall it would be nice if there was a
way to re-use already written components. It would be nicer to add to
a component - maybe written by third parties - new behaviors. So the
purposes of the OOP are three: a)
Encapsulation; b)
Inheritance; c)
Polymorphism. More on this later.
Now we have classes. A class can be defined as a data structure
souped with functions, all together. In OOP we call
attributes
the data, while the functions are called
methods. These two
things -
attributes and
methods - form a class. Among the
methods we distinguish between
accessor and
modifier.
With the purpose that not every code stub can be allowed to
manipulate data, usually we assign to the
attributes the flag
private. And we allow only some of the
methods to
manipulate those data, so that be saved the data integrity. For
example let’s say we have a class called Car, with
attributes
brandName, model, color :
class CCar
{
private:
string brandName;
string model;
string color;
public:
CCar( brandName = “Ford”, model = “Taurus”, color = “Pink”);
CCar();
~CCar();
// accessor function
string brandName();
string color();
string model();
// modifier function
void setBrandName( string);
void setModel( string);
void setColor( string);
};
The first thing to notice is I called it CCar: it is good rule to
prefix the name of the class with a capital C. This is optional but
adds clarity. While what is mandatory is the semicolon after
the closing curling bracket, at least in C++ . As you can see the
class Car has 3
attributes that are
private, plus some
methods to manipulate those
attributes,
methods
that are
public. So that among the
methods only those
declared
public can access the
private data. An other
thing is the methods are divided in two groups:
accessor and
modifier. The first group usually is used to return the value
of an
attribute, while the second one is used to change a value.
This was about
encapsulation.
Object Oriented Programming
Now let’s become familiar with an other concept. A class is the
description of an object and how it relates with the external world.
Just a description, a template in which to stuff the data and its
behaviors. At run-time we need something that is a Car, probably
several, not a schema. This is the idea of
object. An
object
is an instance of a
class. For this purpose, after the
keyword
public and before the
accessor methods, you can
notice three strange
methods that have the same name of the
class. Inasmuch we have seen we need to instantiate the
object of a
class, we need a mean to do an instance. The
first
method is called
constructor. It has something in
between its parenthesis. More on this later. Sometime programmers
either forget to instantiate an
object, or simply declare it
statically. So we need something called a default
constructor,
that’s the second method, the one with nothing in parenthesis.
Inasmuch the C++ language strongly demonstrates the proverb “There
is no such thing like a free meal”, it has no such thing called garbage
collector. We need something to destroy the
object after it’s
been used. That’s called
destructor, the method with a preceding
tilde in its name.
It is responsibility of the programmer to destroy
the object after its use.
So we have seen how to describe an
object, how to talk with
it, how it answers. All this stuff is like in the real world, to send
a message and to get an answer. In answer to an
event we sent a
message and the
object returns an other
message.
Indeed the
accessor methods return a string. This is how we
communicate with
objects. And for this reason it’s called
programming oriented to the
objects. A dozen years of
experimentation showed this is very useful in the industrial software
production.
Before going further, let’s shortly say that it could happen the
most part of cars we deal which is of the same brand name, model and
color. So we can save time simply invoking the constructor with no
actuals, and provide some default values for the attributes. This is
the meaning of a pink Ford Taurus, admitted that Ford made pink Taurus’s.
Polymorphism & Inheritance
So now we have this
object called Car, of which we can
read/write the
attributes. But they are only three: what if
after some use we need a new
attribute: the mileage of the car?
There is a procedure to derive from the
class Car another
class that include the further
attribute:
int mileage,
and to add some
methods to deal with it. So the
class
Car becomes a
base class, and the new one is a
derived class
. This procedure is called
Inheritance.
And this procedure can also be repeated. If we had started from a
generic
class called
CTransportationMean, from which we
derived transportation means with or without wheels, we would have
had the following diagram:
Now let’s suppose that the designer of the base
class
CTransportationMean foresaw an
attribute called
float pricePerKilometer, and its related
methods called
float pricePerKilometer() and
void setPricePerKilometer( float)
. Now when we derive the class
CBoat, it happens that the
marine mile has a different length from the terrestrial kilometer, so
the calculus of the price per unit of distance of a boat is different.
We can arrange the things to change the method to calculate the
cost, for both the derived class CWheels and CNoWheels. The ability
to answer to the same message with different behavior is called
polymorphism, from the ancient Greek ‘poly’: many and ‘morph’: forms,
so it means many forms, the capability to change, flexibility.
Conclusions
For sure this kind of analysis, when made before beginning to write a
program, is a different approach from a procedural language to a OOP
language. It helps a lot in reducing the probability to insert errors
in the code. The first stage, the analysis, is long but it saves time
later.
An other way to do less mistakes is to do the analyses with
methods and
procedures to model the
attributes and
the
behaviors of the
objects involved in our application,
and divide and schedule our steps to reach the goal. We need a
previous stage language. This has been invented, name’s is Uniform
Modeling Language, or UML. It’s highly used in the industry.
Of course C++ is not the only language to deal with objects.
There are many others like Eiffel, Ada, SmallTalk and the last lil
Java. After 25 years of programming I can affirm two things: C++ is
high malleable, sometime may have a dark syntax, but is very fast. Two,
Java looks to remain something good to program washing machines and
cell phones. And I would risk a small prophecy: if sun.com doesn’t
open Java to the open source and release the code, it will keep to
have such a short range of use.
C++ has lot more, but to talk of how it favored the development of
the GUI’s, or to talk of Templates and Standard Template Library, STL
- is out of the scope of this paper.
2004.08.09
Vincenzo Maggio