Rochester Institute of Technology
RIT Scholar Works
Theses
Thesis/Dissertation Collections
9-12-1988
ClassC/Elaine: a multiple inheritance object
oriented C language
Paul Kirkaas
Follow this and additional works at:
http://scholarworks.rit.edu/theses
This Thesis is brought to you for free and open access by the Thesis/Dissertation Collections at RIT Scholar Works. It has been accepted for inclusion
in Theses by an authorized administrator of RIT Scholar Works. For more information, please contact
Recommended Citation
Rochester Institute of Technology
School of Computer Science and Applied Technology
ClassC /Elaine:
A Multiple Inheritance Object Oriented C Language
by
Paul Kirkaas
A thesis submitted to the faculty of the School of Computer Science and
Technology
in
partial fulfillment of the requirements for the degree of Master of
Science
in
Computer Science.
Approved by:
James Heliotis
Dr James Heliotis
Andrew Kitchen
Dr Andrew Kitchen
Peter G. Anderson
Class C
/
Elaine:
A Multiple Inheritance Object Oriented C Language
by Paul Kirkaas
I, Paul Kirkaas, hereby grant permission to the Wallace Memorial Library of RIT
to reproduce my thesis in whole or in part provided the source is credited. Any
reproduction will not be commercial use or profit.
Paul Kirkaas
Paul Kirkaas
-
1
ABSTRACT
Object
oriented
programming is
a
way
of
abstracting
information
andoperations
to
make
programming
more
efficient
and
reliable.
C
is
a
non objectoriented
programming
language
that
has become
a
de facto
language
standardin
academic
and
industrial
applications
because
of
its
power
and
flexibility.
ClassC
is
an
attempt
to
add
object
orientation
on
the
existing
framework
that
C
provides.
Specifically,
ClassC
adds
the
new
data
type
object
,and
the
new
aggregate
declaration,
class.
ClassC
differs from
other
C-based
object
oriented
languages
such
as
C++
Objective-C in
three
points:
1)
ClassC
provides
true
multiple
inheritance,
which
is
very
commonly
discussed in
the
description
of object oriented
languages,
but
very rarely
actually
implemented.
2)
ClassC
provides
both
strict
type
checking
on
objects
as
well
as
typeless
dynamic
binding
a variable of
type
object
may
be
assigned
any
classinstantiation.
Dynamic
binding
is
typical
of
interpreted
languages like
lisp
and
Smalltalk,
but
rarein
compiled
languages like
C
and
Pascal,
etc.
3)
ClassC
offers
automatic
garbage
collection
of
memory
no
longer
referenced
by
any
object variables.
ClassC/Elaine
is intended
to
be
a
flexible
system
that
allowsprogrammers
to
explore
different
qualities
of
object
oriented
programming,
including
strong
andweak
type checking,
and single and multiple
inheritance. This
thesis
is
a
description
of
the
background,
design,
and
implementation
ofthe
ClassC language.
Keywords
Object Oriented
Programming, C++,
Objective-C, Smalltalk,
ClassC
Computing
Review
Subject Codes
Primary
Code:
D.3.2
Language Classifications
CONTENTS
1.
Introduction
1.1
ClassC/Elaine
1.2
Motivation
1.3
Background
.1.4
System Description
1.5
Language Description
1.6
Functional Overview
2.
Functional
Specification
ClassC/Elaine
Programming
Guide
10
2.1
Introduction
to
ClassC/Elaine
10
2.2
Overview
11
2.3
Defining
classes13
2.4
Defining
classfunctions
15
2.5
Declaring
andInitializing
Objects
16
2.6
Deleting
Objects
18
2.7
Using
Objects
19
2.8
Use
ofMember
Functions
21
2.10
Using
the
-GGarbage Collection Option
22
2.11
Using
ClassC/Elaine
23
2.12
Keywords
and special symbols24
2.13
Examples
ofClassC
programs24
3.
Implementation
25
3.1
Statement/Declaration
Mixing
25
3.2
Inline Comments
25
3.3
Class
Definition
26
3.4
Inheritance Mechanism
31
3.5
Object
Table
38
3.6
Objects
38
3.7
Object
39
3.8
Runtime
Library
Functions
41
3.9
The Problem
ofNon-Idempotentcy
49
4.
Implementation
52
4.1
Statement/Declaration
Mixing
52
4.2
Inline Comments
52
4.4
Inheritance
Mechanism
58
4.5
Object
Table
65
4.6
Objects
65
4.7
Object
66
4.8
Runtime
Library
Functions
68
4.9
The Problem
ofNon-Idempotentcy
76
5.
Appendicies
Sample ClassC Programs
79
5.1
ChessMovs.e
79
1.
Introduction
Object
orientedprogramming
is
away
ofabstracting
information
and operationsto
makeprogramming
more efficient and reliable.G is
anon object orientedprogramming
language
that
has
become
ade facto language
standardin
academic andindustrial
applicationsbecause
ofits
powerand
flexibility.
ClassC is
an attemptto
add object orientation onthe existing
framework
that
C
provides.
1.1
ClassC/Elaine
ClassC
is
aC-based
object orientedprogramming language.
The language
adds object orientedcapabilities
to the
C programming
language.
Specifically,
ClassC
addsthe
newdata
type
object ,and
the
new aggregatedeclaration,
class.Class
definitions
arelike C
structures exceptthey
may
contain
function
declarations,
andthey
may inherit
componentsfrom
multipleancestors;
objects areinstances
of aparticularclass.An
object variablemay
be
declared
by
a classname,
in
which caseit
must represent
only
objects ofthat
class ordescendants
ofthat class,
or an object variablemay
be
declared
by
the
keyword
object,
in
which caseit
canbe
usedto
representany type
of objectin
the
system with no
type
checking.Elaine
is
the programming
environmentfor ClassC.
Initially
it
consists ofthe
runtimelibrary
functions
necessary to
implement
ClassC;
enhancements willbe
addedovertime.
There
are otherC-based
object orientedlanguages,
such asC++
andObjective-C,
but ClassC
differs
from
these
otherlanguages
in
three
points.1)
ClassC
providestrue
multipleinheritance,
whichis
very commonly
discussed in
the
description
ofobject oriented
languages,
but
very
rarely actually
implemented.
2)
ClassC
providesboth
stricttype checking
on objects as well astypeless
dynamic
binding
-a
variable of
type
objectmay
be
assignedany
classinstantiation.
Dynamic
binding
is
typical
ofinterpreted
languages like
Lisp
andSmalltalk,
but
rarein
compiledlanguages like Cand Pascal.
1-ClassC/Elaine
Paul Kirkaas
3)
ClassC
offers automatic garbage collection ofmemory
nolonger
referencedby
any
objectvariables.
1.2
Motivation
Object
orientedprogramming
provides away
to
abstractdata
and control.It
allowsthe
programmer
to
create modelsthat
areinternally
complex andsophisticated,
yet which present asimple and uniform
interface
to
the
user.There
aretwo
broad
argumentsfor
Object
Oriented
programming.
The first
is
that
Object
Oriented programming
improves
productivity
andreliability
by
allowing the
reuse ofcode and
the
standardization ofcommondata
types
and operations.The
second argumentis
that
by
allowing
greaterabstraction,
object orientedlanguages
arecontinuing the trend
in
programming
languages
towards
a morehuman like
andless
machinelike
meansof
expressing
concepts.This implies
that
object orientedprogramming
canfacilitate
notonly
bigger
programs,
but
clevererones as well.This is
my primary
interest.
The C
language
wasdesigned
with a particularphilosophy
aswell;
and one which often conflictswith
many
ofthe
common argumentsfor
object oriented programming.In
general,
C
is designed for
efficiency
and enforcesfairly
casualtype
checking.C
allowsthe
programmer unusualintimacy
withthe
data
representation andthe
machine.There is
noinformation
hiding
withC.
These
characteristics givethe
programmerflexibility
andthe
ability
to
coercethe
language
in
unusual ways.
This is
valuable,
but
can alsobe
dangerous
for
unsophisticated users.Object
orientedlanguages
emphasize much moreinformation
hiding
and abstraction.The
useris
isolated from
the
machine anddata
representation asmuchas possible.Both
these
philosophieshave
merit.The
task
ofcombining
these
features into
a singlelanguage is
to
somedegree
a subjectiveone,
and one which always makes some compromisefor
every
decision
ClassC/Elaine
paul
Kirkaas
For
example,
in most object orientedlanguages,
the
data
components can notbe
accesseddirectly.
The
user canonly
changethe
data
componentthrough
a memberfunction
(
or"message").
This
preserves
abstraction,
but
is
less
efficient,
moretime
consuming
to write,
andirritating.
The
choicemade
for ClassC
wasto
allowthe
programmer
direct
accessto
data
components ofobjects.The
goal ofClassC/Elaine,
then,
is
to
provide ausableC
based
object orientedlanguage
that
offersan alternative
to
previousimplementations.
Most
ofthe
code executedin
any
programis
straightforward
anddoes
notbenefit
from
an object oriented approach(
e.g,
for-loops,
arithmeticoperations,
etc).This
code shouldbe
implemented
asefficiently
as possiblefor
rapid execution.This
is
the
motivationfor using C
asthe
base language.
Object
type
data
manipulations shouldbe
used
for
operations which areconceptually
intricate,
where use of object orientedtechniques
couldgive
both
the
programmer andthe
program moreclarity
and expressiveness.This is
the
motivationfor enhancing
C
withobject orientedfeatures;
moreover, this
is
the
reasonfor
giving
ClassC
ahigher
level
of abstractionthat
C++
orObjectiveC
through
multipleinheritance
and untypedobjects.In
orderto
be
genuinely useful,
ClassC
was made compatible withexisting C language facilities.
It is
compatible with all
existing
standard^include
files,
as well as alllibrary
packages.It
allowsseparate compilation and
linking
ofClassC
modules,
as well asthe
creation ofindividual ClassC
libraries.
l.S Background
The
foundations
ofObject Oriented
programming
werelaid
by
the
programming language
Simula,
which was
developed
in
Norway
in
the early
1960's
[DAH66].
It introduced
the
concept of userdefined
classes,
and wasdeveloped
as a simulationlanguage for
modeling
complex systems.The
power of
this
concept was notwidely
recognized untilAlan
Kay
andthe
researchgroup
atXerox
PARC developed
the
Smalltalk interactive
environment.It
evolvedfrom Dr. Kay's
concept of acomputercalled
the
Dynabook,
which wouldbe
very
userfriendly
andsuitable
as alearning
aidfor
-3-ClassC/Elaine
paul Kirkaaa
children
[GOL83J.
Smalltalk
remains
to this
day
the
prototypical example of an object orientedlanguage.
It
is arich and complex environmentthat
shows
the
power ofthe
object oriented concept.Yet
this
richness andcomplexity
has
a price.Smalltalk
is
big,
slow, expensive,
and available onvery
few
machines.Its
primary
drawback from
a systemsdevelopment
perspectiveis its
slowness.Smalltalk
runs on a virtual machine/
interpreter,
whichis inappropriate
for large
software systems.This
is unfortunate,
because
software systems are classic candidatesfor
the productivity
benefits
ofobject oriented programming.
Concepts like
queues, stacks,
hash tables,
etc. areideal
abstractiontypes.
But
anoperating
system writtenin
Smalltalk
running
on conventionalhardware
wouldbe
very
slow.Enter
C++ [STR86].
C++
wasdeveloped
atAT&T Bell
Laboratories,
home
ofUnix, C,
Kernighan
and
Ritchie.
It
wasdeveloped
by
Bjarne
Stroustrup
withthe
intent
ofgiving
as much objectoriented structure
to
C
aspossible,
whilesacrificing
none ofthe efficiency
of pureC
code.It
is
therefore
perhapsthe
best
systemdevelopment
language
availabletoday.
The C++
compilertranslates
C++
codeinto
standardC.
Thus,
C++
mustimplement its
objectoriented
features
in
aC format.
It
does
this
by
using
standardC
structures.An
objectin
C++
is
implemented
in
two
ways.The
function
components ofthe
class are given unique names andwritten
to
the
outputfile.
The
function
members are associated withthe
classratherthan
withthe
object.
This
meansthat
allinstances
of a classhave
unique privatedata
but
sharethe
samecomponent
functions.
The data
portion of aC++
objectis
implemented in
a standardC
structure,
with allthe
data
components of
the
object as elements ofthe
generated structure.Inheritance
is
implemented
by
copying the
parent object structure andconcatenating the
new elements ofthe
derived
class afterthe
elements ofthe
parent class.The
component sequence ofthe
parent andderived
classes aretherefore
identical
up to
the
end ofthe
parent class.Consequently,
the
address of aninstance
of aderived
class canbe
assignedto
an object pointer of a parent class andthe
standardC
structureClassC/Elaine
paul Kirkaas
This
makes objectdereference
easy
andefficient,
but
makes multipleinheritance difficult for
reasonsdiscussed later.
Objective-C is
alanguage developed
by
Dr.
Brad Cox
atProductivity
Products
International
andseems much
like
C++
with adifferent
syntactic structure.The
two
languages
offer similarfeatures
[COX86].
1.4
System
Description
The
ClassC/Elaine
compiler wasbe
developed
underUnix
using the
Unix lexical
analyzerLex
andthe
Unix
parser generatorYacc,
and usesthe
standardUnix/C libraries.
The
running
systemconsists of
three
files-1.
The
parserefront-The
ClassC/Elaine front
endparser,
writtenin C.
The
parsertranslates
ClassC/Elaine
.esource
files
into
".c"
C language files.
2.
The Unix
Shellscript
ce-The
shellscript ceis
the
compiler commandinvoked
by
the
user.It
parsesthe
commandline
options and source
files,
appliesthe
appropriate versionofthe
parser andruntimelibrary,
andcalls
the
C
compiler cc onthe
result.3.
The
ClassC/Elaine
runtimeroutines,
libce.a.
libce.a
contains runtimefunctions
suchas_new(),_deref(),etc.1.5
Language
Description
The
conceptfor
ClassC differs from
those
of otherC-based
object orientedlanguages.
ClassC
wasdeveloped
as alearning
tool
for data
abstraction andobject orientedprogramming
concepts.ClassC/Elaine
Paul Kirkaas
philosophy
is
reflectedin
the
decision
to
provide multipleinheritance
anddynamic
binding
ofobjects.
There
is
some runtime costto
providethese
features,
mainly function
call overhead.ClassC is
asuperset ofC;
that
is,
allC
operations are supportedby
ClassC,
andpre-existing
library
and object
files
as well as standardheader
files
are all compatible withClassC.
The
language
wasdeveloped
in
aUnix
operating
systemenvironment, using the
Unix language development
tools.
The
parser acceptsstandardC
code and passesit
to the
outputfile
unchanged.It
maintains symboltables
andscoping
information
whilebuilding
the
parsetree.
In
additionto
standardC,
the
parser accepts and processesthe
following:
1.
The
aggregatedefinition
class-PL
classdeclaration
is
muchlike
a standardC
structuredeclaration.
A
classis
composed ofMembers;
of whichthere
aretwo types:
a.
Variable
members-which are passive
data
components ofthe
classinstances.
b.
Function
members-which are
functions
that
act onthe
private member variables of classinstances.
In
addition,
aclassdefinition may
contain alist
of parent classesfrom
which components areinherited.
2.
The
class memberfunction
definition-Different
classesmay have
memberfunctions
withthe
samename; e.g.,
print.To distinguish
them,
class memberfunctions
aredefined using
the
class name and ascoping
operator.
3.
Objects-All
objects aredynamically
allocated at runtimeby
explicitcalls
to
a 'newQ'function.
A
variable of
type
objectis
really
anindex into
an objecttable.
An
objectmay
be
declared
in
ClassC/Elaine
paul Kirkaag
1.
With
a classname-Class
names are usedto
declare
object variables.Object
variablesdeclared
this way
canonly
represent objects ofthat
particularclasstype
orits descendants.
2.
With
the type
specifierobject-A
variabledeclared
withthe type
specifier object canbe
usedto
representany type
ofobject
instance
at all.4.
The
object memberdereference-The
memberdereferencing
operator usedin
ClassC
is
":>".
C++
usesthe
ordinary
C
structure
dereferencing
operatorsfor
objectdereferencing,
but
the
dereferencing
mechanismin
ClassC is
implemented
differently
and suggeststhe
use of aseparate symbol.5.
Variable
declarations-Variable
declarations
canbe
made anywherein
ablock.
This is intended
to
moreclosely
associate
the
variabledeclaration
withits
use.Every
variable must stillbe declared before its
first
use.1.6
Functional Overview
Classes:
A
classis
declared
asfollows:
class
Class_name
:SuperClass_l
:SuperClass_2
: ...{
type
declarator,
type
declarator,
type
declarator,
type
declarator,
};
where
Superclasses
areoptional and separatedby
the
inheritance
operator":";
and wheredeclarator
ClassC/Elaine
Paul Kirkaa3
Two
specialtypes
offunctions
that
may be
declared
asa class member areinitQ
anddeletef).
initf)
is
that
class'sinitializer
function.
The
initf)
function
is
defined
by
the
user andis
automatically
invoked
when an object ofthat
classis
created.Otherwise,
if
initf)
is
notdeclared,
uninitializedmemory
space willbe
allocated.deletef)
is
the
classdelete function.
It
shouldbe defined
by
the
userto
cleanup
any
specialmemory
resources which objects of
that
classmay
allocateto themselves
(through
mallocf),
for
instance).
Objects:
Objects
are createddynamically
from
the
heap;
there
are no static objects.Each
objectis
aninstance
of a specific class.A
member of an object(the
member canbe
either variable orfunction)
is
dereferenced
by
the
objectdereferencing
operator":>".
The
generalform
ofthe
objectdereference is:
"
objectExpression:> member
Name;";
where
"objectExpression"
is
usually
just
a variable name.In
general,
however,
an object expressioncan also
be
the
result of a pointerdereference,
a return valuefrom
afunction,
etc.The
type
ofthe
entity
returnedby
the
objectdereferencing
operatoris
determined
by
the
type
ofthe
objectexpression.
If
the
classto
whichthe
object expressionbelongs
canbe
determined
at compiletime,
the type
ofthe
returneddereferenced
elementis determined
by
its
type
asdeclared in
the
classdefinition.
If
however
the
object expression evaluatesto
aninstance
ofthe
generic classobject,
its
true
classcannot
be
determined
at compiletime.
In
this
casethe type
ofthe
result ofthe
dereference is
defaults
to
objectas well.The
programmer can castthe
resultto
be
ofany type
and overridethe
default.
Member Functions:
Member functions
aredefined
withthe
classname,
scoping
operator"::",
andthe
function
name-e.g
*Son::print()\
Arguments
aredeclared just
like in
anordinary
C function definition.
An
ClassC/Elaine
Paul Kirkaas
with no special
dereferencing
orcasting
required.A
referenceto
"self"is
passedautomatically
on acall
to
a memberfunction,
andmay
be
usedexplicitly
by
the
programmerin
an object's memberfunction definition
to
referencethe
objectitself.
Examples:
Declare
classSon
to
be
adescendant
of classesPapa
andMama.
class
Son
:Papa
:Mama
{
int
a;
char *b;
initf);
/*Optional
initialization function
*/
void print();
};
Define
memberfunction
initf)
of classSon:
Son::initfx,y)
int
x;
char *y;
{
b
=(char
*)
malloc(strlen(y) +
1);
strcpy
(b,y);
a=
x;
}
Declare
anobject& initialize
to
be
ofclassSon:
Son
xmpl =Son:>new(5, "Hello");
Send
the
message"print"
to
xmpl:xmpl:>print();
Reset
member element "b"of xmpl:
xmpl:>b =
ClassC/Elaine
paul Ki_kaa3
2.
Functional
Specification
-ClassC/Elaine
Programming
Guide
2.1
Introduction
to
ClassC/Elaine
ClassC is
an object orientedprogramming language based
onC,
andit is
assumedthe
userhas
aboth
a solidknowledge
ofthe
C language
and a conceptualunderstanding
of object orientedprogramming.
Like
C++
andObjectiveC,
ClassC
is
really
apreprocessor,
converting
ClassC
source codeinto
C,
then
into
assembly
language,
andfinally
into
machinecode.ClassC
is
asuperset ofthe
C
language,
and all
C
constructs except enum are accepted andsemantically
unchanged(
enum was notimplemented because
it
differs
from
other aggregatetypes.
It
would require a special symboltable,
and
is
notwidely
usedin C.
It
wouldbe
atedious
but
notdifficult
feature
to
add).ClassC/Elaine has
the
following
features:
ClassC's
objectoriented extensionsto
C
1.
Class Definition:
A
classis defined like
aC
structure,
exceptthat
it
may
inherit
componentsfrom
anarbitrary
list
of ancestorclasses,
andmay
include
functions
as components.2.
Objects:
An instance
ofa class asdefined
aboveis
an object.Objects
canbe
declared in
oneof
two
ways.A
classdefinition
is
like
atypedef
in C.
This
meansthat
a variable canbe
declared
by
using
a class name as atype
declaration.
Variables
sodeclared
canonly
be
assigned objects of
that
sametype
or ancestortypes.
In
addition,
a newdata
type
objectis
introduced,
whichcanbe
usedto
declare
variables which representany
classtype.
3.
Garbage Collection Option:
Invoking
the
"ce"compile command with
the
option "-G"results
in
the
inclusion
ofthe
ClassC
garbage collection mechanism.ClassC's
non-object orientedextensionsto
C
ClassC/Elaine
Paul Kirkaas
Following
C++,
any
text
between
"//"and
the
newlineis ignored.
2.
Statement/Declaration
Mixing:
Statements
and variabledeclarations
canbe intermixed
in
the
body
of ablock,
aslong
as eachvariable
is declared before its
use.The distinction between
classes, objects,
and object variablesin
ClassC
shouldbe
understood.A
class
type
definition
is
usedto
define
the
validdata
components and operations(methods,
messages)
for
objects ofthat
class.An
objectis
aninstance
of a particularclass,
createddynamically
from
the
heap
at runtimeby
explicit callsto
"className
:> new()".An
object variableis
implemented
as aninteger
index
into
an objecttable.
Since
the type
(class)
of each objectis kept in its
objecttable
entry,
it
need notbe
a characteristic ofthe
object variableitself.
This
allowsdynamic
binding
ofobject variables
to
objects.In
otherC based
object orientedlanguages,
the type
ofthe
object variable mustbe declared
to
be
ofa specific
class,
and canbe
used representonly
objects ofthat
particularclass,
or classesderived
from
that
class.In
ClassC,
type checking
is
performedonly
onthose
objectsdeclared
to
be
of aparticular class.
The
declaration
object canbe
used as a catch-allto
hold
any
system objecttype
withno
compatibility
checking.This is
usefulfor
collectionclassessuch as sets which shouldbe
ableto
contain allobjecttypes.
2.2
Overview
This
sectionintroduces
the
flavor
ofprogramming
in
ClassC.
More detailed
explanations canbe
found in
the
following
sections.Objects
in ClassC
are muchlike
structureswhich canhave function
components.The
":>"
operator
is
usedto
dereference
a component of an object.If
the
componentis
adata/variable
type
component,
it
canbe
used as anl-value
-
that
is,
it
can appear onthe
left
side of an assignmentexpression.
There is
no conceptof"private"
components
in
ClassC.
ClassC/Elaine
paul
Kirkaas
A function
component
in
ClassC
is
like
a messagein
Smalltalk.
Messaging
couldhave
been
implemented
with aSmalltalk like
syntax, but
one goal ofClassC
wasto
be
asC-like
as possiblewhile
providing
object oriented extensions.Hence,
messages arguments are passedlike
regularfunction
argumentsin C.
Thus
the
expression:"thisObj
:>message(argl,arg2);"
applies
the
memberfunction
"messagef)"
to the
object"thisObj"
with
the
arguments"argl" and
"arg2".
In
ClassC,
classes aredefined
by
the
inheritance
of componentsfrom
parent classes and anadditional
declaration
sectionfor
components uniqueto the
classbeing
defined.
A
classnamemay be
usedto
declare
an objectvariable,
whichthen
represents a pointerto
an objectof
that
classtype.
However,
the
objectitself is
not created until an explicitinitialization
callis
made.
For
example, the
declaration
"MyClass
declares
"objVariable"to
be
a pointerto
an object oftype
"MyClass";
but
it is initialized
to the
NULL
pointer(0).
To
create a new object oftype
"MyClass",
sendthe
message"newf)"
to
"MyClass"
"MyClass:>new()
".
It is
possibleto
declare
an object variable andinitialize
the
objectin
one step:"MyClass
objVariable =MyClass:>new();"
Class
names are also used whendefining
memberfunctions.
Since
several classesmay
have
memberClassC/Elaine
Paul Kirkaas
identify
it.
2.3
Defining
classesA
classin ClassC
is defined
muchlike
a structurein
C.
A
classdefinition is indicated
by
the
keyword
"class",
followed
by
the
name ofthe
classto
be
defined,
an optionalinheritance list
ofpreviously
defined
classes,
andthe
body
ofthe
classdefinition.
The
definition
body
is just
asequence of
declarations in
a standardC
format.
This declaration
setmay
include function
andobject
declarations.
The
form
of aclassdefinition is
thus:
class
Class_name
:SuperClass_l
:SuperClass_2
: ...{
type
declarator,
type
declarator,
type
declarator,
type
declarator,
};
Three
things to
bear
in
mind about classes-1.
Class
Definitions-No
otherclasses,
structures ortypedefs
may
be
defined
within a classdefinition,
althoughpreviously
defined
typedefs
and structuresmay
be
used within a
definition,
and other classeswhich are
defined
elsewhere withinthe
file
(earlier
orlater)
may
be
usedto
declare
membercomponents.
2.
Class
Names-The
"class*keyword
is
usedonly
ondefinition.
Thereafter
the
defined
class nameis
usedlike
a new
type
name.This
meansthat
unlikestructures,
you cannothave
a variable withthe
13-ClassC/Elaine
Paul
Kirkaas
same name as aclass.
3.
No Unnamed
Classes-Unlike
structin
C,
all classes mustbe
given aname;
furthermore,
it is impossible
to
declare
anobject
instance
of aclassin
the
same statement asthe
classdefinition.
Class Inheritance
Classes
may inherit
components(data
andmethods) from
anarbitrary
number ofantecedents.This
implies
a potential conflict whenthe
same component nameis
usedin
morethan
one ancestor.This
conflict
is
resolved asfollows:
1.
If
the
conflictinvolves
two
componentsofthe
same namebut
ofdifferent
declared
type
in
two
or more
different
ancestors, this
is declared
to
be
a semanticerror,
compilationis
aborted andan appropriate error message
is
reported.2.
If
the
conflictinvolves
components ofthe
same name andthe
samedeclared
type
in
eachancestor, then the
componentfrom
the
parent earliestin
the
list is defaulted.
This
default inheritance
ofthe
componentis irrelevant
for
data
components,
but
criticalfor
function
components.That
is,
if
the
samefunction
name andtype
is declared in
severalancestors, the
first
definition
encountered whentraversing
the
list
of ancestorsis
the
function
definition
usedfor
the
newclass.3.
If
the
conflictinvolves
a componentexplicitly
declared
in
the
body
ofthe
current classdefinition
and an ancestor ofthat
newclass, the
newdefinition
is
used,
which supersedes allancestor
definitions.
This is
the
case evenif
the
type
ofthe
new componentdeclaration
conflicts with
the type
declaration
of acomponent ofthe
samenamein
an ancestorclass.Note
that
if
the
user wantsto
supersede afunction definition
from
a parentclass, that
function
mustbe
declared
in
the
body
ofthe
new class as well asbeing
redefined asClassC/Elaine
Paul Ri_kaa3
2.4
Defining
classfunctions
1)
Declaration
Different
classesmay
have
memberfunctions
withthe
same name.Similarly,
aderived
classmay
redefine a
function
which wasdefined
previously
in
an ancestor class.It
is
therefore necessary to
have
a meansto
distinguish
whichofthe
several possible memberfunctions is
being
defined.
This is
done
withthe
class name andthe
function
namejoined
withthe
scoping
operator"::".
So
to
define
the
memberfunction
"printQ"
of class
"Bishop"
which
takes
a char * argument"str"
and returns
an
integer
int
Bishop::print(
str)
char *
str;
{
(Body
ofdefinition)
}
2) Body
The
body
of a memberfunction definition
is like
the
body
of anordinary
function
body
in
ClassC,
except
in
the treatment
ofthe
components ofthe
objectto
whichthe
message/functionis
being
sent.
Specifically,
the
body
ofa memberfunction definition
can referto
other elements ofthe receiving
object without
explicitly
dereferencing
them.
The
appropriatedereferences
areimplicit
in
the
function
itself.
For
example:class
Piece
{
char*name;
object
position;
int
checkPositionf);
int
showf);
void
movef);
void report
();
/*
Send
messageto
coordinator*/
};
15-ClassC/Elaine
Paul Kirkaas
is
adeclaration for
the
class piece.The
memberfunction
"movef)"of class
Piece
mightbe defined
as
follows:
void
Piece::move(
newPosition, board
)
objectnewPosition;
object
board;
{
if
(checkPosition (newPosition
) )
{
board:>atput(newPosition,
self);
position =newPosition;
}
else report
("Illegal
Move");
}
Note
the
use ofthe
identifier
self
in
the
definition
above.Self is
an object variable whichis
automatically
generatedby
the
parser as an argumentto the
memberfunction.
It
representsthe
target
objectto
whichthe
function/message is
being
sent.The
variable name"position"
used
in
the
definition
of"Piece:
aboveis known
to
be
a component of classPiece,
andis
therefore
assumed
to
referto the target
object.The
statement"position
=could
have
been
equivalently
rewritten"self
:>position =In
general, the
"self"
variable
is
used whenthe
memberfunction
manipulatesthe
entire objectrather
than the
components ofthat
object.In
this case,
for
example,
it is
usedto
registerthe
instance
ofthe
Piece
classto
its
new position withthe
object"board".
2.5
Declaring
andInitializing
Objects
Objects
canbe declared in
one oftwo
ways:ClassC/Elaine
Paul
Kirkaa3
2) By
the
name of aclassdefined
elsewherein
the
file.
When
an object variableis
declared
by
a classname,
all operationssubsequently
performed onthat
variable are checked
for
conformance at compiletime.
That
is,
operations are checkedto
ensurethat
they
are compatible withthe
object'sdeclared
class or ancestorclasses,
andthe
returned resultof
the
messageis
castto the
appropriatetype.
Variables
that
arejust
declared
to
be
oftype
object are not subjectto
any
class conformancechecking
until runtime.Thus,
any
object operationmay
be
appliedto
variablesdeclared
to
be
oftype
object.This
givesthe
user moreflexibUity
in
the
manipulation ofthe object,
but
atthe
risk ofruntimeerrors
if
the
userhas
usedthe
objectincorrectly.
It
is
suggestedthat
use ofthe type
name objectbe
restrictedto
such applications as collectionclasses,
whereit is desirable
to
allow objects of alltypes
to
be
stored.These
objecttype
names aresyntactically
equivalentto
standardC
scalartype
nameslike
"int",
"char", "float",
etc.This
meansthat
they
canbe
used notonly to
declare
variables oftype object,
but
arraysof,
pointersto,
andfunctions
returning
object as well.So
some possible objectdeclarations:
object
x;
MyClass
y[4];
object
(*i)[3j|7);
Bishop funcf);
A
new object variableis
essentially
anempty
slotinto
which an objectmay
be
placed.Objects
themselves
are createddynamically
from
the
heap
at runtimeby
a special call or message called"newO".
The
message"newQ"
is
sentto
a class nameto
create a newinstance
ofthat
class.This
new
instance
may then
be
assignedto
anobject variable(be
putin
the
slot).Note: Classes
in ClassC
are notthemselves
objects,
but
aresyntacticly treated
as such withthe
17-ClassC/Elaine
Paul Kirkaag
"new()"
message.
So,
for
example,
to
make a newobject of classBishop
you write"Bishop:>new()"
where
the
symbol":>"
is
the
messagepassing
ordereferencing
operator ofClassC.
An
object variablemay
be initialized
onits declaration line like
any
otherC
variable.So,
to
assignthe
newinstance
ofBishop
to the
object variable mypiece we could write either:Bishop
mypiece;
mypiece =Bishop:>new();
or
Bishop
mypiece =Bishop
:>new();
It is
also possibleto
create a special"initf)"
function
for
aclass whichis
automatically
called when anew object of
that
clas3is
created.The
"initf)"function is declared
anddefined
like
any
other classmember
function
exceptthat
it
must notbe
declared
to
be
ofany type
or returnany
value.An
"initf)"
function
may
be defined
to take arguments,
in
which casethe
callto
"
C/a.37Vame:>new(argl,arg2,...)
"is
made withthe
appropriate arguments.New
objects createdfrom
classesthat
have
no"initf)"
function
associated withthem
are createdwith uninitialized memory.
2.6
Deleting
Objects
Garbage
collectionis
performedautomatically
whenthe
ClassC
"-G"compiler option
is
used.The
user can also
explicitly
delete
unwanted objects andreleasetheir memory
spaceto the
heap.
An
objectis deleted
by
sending
it
the
message"deletef)
"
ClassC/Elaine
Paul
Kirkaas
The function
"deletef)"does
nothave
to
be
declared
as a component of an objectto
be
usedto
delete
an object.It
canbe
usedto
delete
any
object,
andjust
releasesthe
memory specifically
allocated
to
that
objectby
the
callto
"newQ".
It is
possible,
however,
that
some objectsmay
have
acquired additionalmemory
resources.For
instance,
an objectmay
contain a pointerto
adynamically
allocatedstring.If
the
objectis deleted
without
freeing
the string, the
string
willremain, uncollected,
floating
in
memory.It is
thus
possibleto
explicitly
define
a memberfunction
"deletef)"
as part of a class
definition.
This
member"deleteQ"
function
is
defined
like
any
class memberfunction,
andthe
useris
responsible
for
cleaning up
any
additionalmemory
allocatedto the
objecthere.
When
the
message"deletef)"
is
sentto
an object(possibly
witharguments)
the
userdefined
member"deletef)"
function is
calledto
cleanup
user allocatedmemory, then the
system"_deleteQ"
function is
calledto
delete
the
objectitself.
2.7
Using
Objects
There
arethree
different
waysin
which object variablescanbe
used.1.
They
canbe
used withthe
dereferencing
(component
selection)
operator ":>"
-e.g.,
"mypiece:>position
= or"mypiece:>set
(newPosition);"
2.
They
canbe
usedin
assignmentstatements withother objects orfunctions
returning objects;
e.g., "objl
= obj2;".3.
They
canbe
passedas argumentsto
functions;
e.g."newobj
=transform(oldobj);".
Dereferencing
Objects
Member Data:
The
operator ":>"is
usedto
dereference
or select components of an object.If
this
componentis
adata
component, the
expression19-ClassC/Elaine
Paul Kirkaas
"
object_name :> component
name"
is
an1-value;
that
is,
the
expression canbe
used on either sideofthe
"= "(assignment)
operator.Components
of a class canbe
ofany
legal C
type,
or aclassC
object.If
the
object variablehas
been
declared
with a classname,
componenttype
interpretation is done
automatically.This
meansthat
if
object"objl"
has been declared
to
be
of classSampClass
andcomponent"mycomp"
is
anarray
offloats,
the
assignment"objl:>mycomp
[6]
= 2.718;"has
the
expected result.But
asdiscussed
above,
object variablesdeclared
to
be
oftype
objectaredynamically
bound
andtheir type
cannotbe determined
at compiletime.
Thus,
there
is
noway to
determine
the
type
of component"mycomp"
at compile
time.
Example:
Consider
two
classes,
ClassA
andClassB.
Each
classhas
a component named"size".
The
declaration
of"size"
for
classClassB "int
for ClassA it is "float
size;".If
the
object variable"objVar"
is declared
to
be
oftype object,
it is
notbound
to
any
classtype
at compiletime
andthe
compiler
has
noway to
know
the type
ofthe
expression"objVar:>size".
This
is
resolvedin
the
following
way:1.
The
default
returntype
ofthe
":>"operator
is
type
"object".
Thus,
if
an objectis
expected,
nospecial
treatment
is
required.2
If
an abstractdeclaration
appearsbetween
the
component selector andthe component, the
type
ofthe
expressionis
castto the type
ofthe
abstractdeclaration.
The
abstractdeclaration
is
basically
aC
cast exceptthat the
conversionis
exact.That
is,
if
the
componentis
anarray
of
6
floats,
the
abstractdeclaration
for
that
component
MUST be
(
float
[6]
).
In
C,
the
appropriatecast would
be
(
float
*);
the
cast(
float
[6] )
is illegal
in
standardC.
Similarly,
if
ClassC/Elaine
Paul
Kirkaas
structure.
So if
the
expected returntype
of"objVar:>size;"
is
float,
it
mustbe
expressed as"objVar:>(float)size".
This interface is
awkward, but is
provided as an additionalfeature for
the
user.When
an objectvariable
is declared
as aninstance
of a particular class ratherthan
withthe
keyword
object, these
conversions are performed
automatically
and need notbe
ofconcernto the
user.2.8
Use
of Member
Functions
Member
functions
are accessedthrough the
same ":>"selection operator
by
which memberdata
areselected.
A
memberfunction
can access other memberfunctions
withinits
body;
in
particular,
member
functions
of a childcan access and callfunctions inherited
by
that
childfrom its
ancestors.Similarly,
aninherited function
that
referencesfunction
componentsthat
have
been
redefinedin
the
derived
class will callthe
redefinedderived
classfunction
ratherthan the
function
for
whichit
wasoriginally
defined.
As
withdata
components,
function
component return values areautomatically
cast
to their
declared
types
for
object variablesdeclared
by
aclassname,
and canbe
manually
castas
described
abovefor
object variablesdeclared
by
the
keyword "object".
2.9
Object Assignment
Objects
canbe
assignedto
object variablesthrough the
standardC
assignment operator"=".
This
means a reference
between
the
object variable andthe
objectitself is
established.No
new objectsare created
by
this
mechanism.For
example,
if
"a"and "b"
are
both
objectvariables, then the
assignment
"a
= b;"does NOT duplicate
the
object pointedto
by
b.
Rather,
both
"a" and"b" now
reference
the
same object.Thus,
any
changes madeto
object "b"will
be
madeto
object "a"as well.
In
orderto duplicate
anobject, the
user must allocate newmemory
andinitialize it
appropriately,
usually
through
a memberfunction.
For
example, to
create a
duplicate
of object"objGen",
onemight write
"ACIass
dupObj
=where
"copyQ"
ClassC/Elaine
paul
Kirkaas
object.
2.10
Using
the
-GGarbage Collection Option
Garbage
collectionis
usefulfor large
programsthat
uselarge
amounts ofmemory,
or programs withrepeating
loops
that
reassign object variables withoutexplicitly
freeing
old values.Garbage
collection
is
afeature
which allowsthe
programmerto
be less
rigorous whenwriting
his/her
program,
but is
costly
in
terms
of computer resources andshouldbe
usedjudiciously.
Note
that
whenthe
garbage collection optionis
invoked,
objects arestatically scoped,
andhence
arenot stacked
in
recursivefunction
calls.Note
alsothat
modules compiled withthe
garbage collectionoption must not
be
combinedwith other modules compiled withoutit.
Garbage
collectiontakes
placeonly
after a certain number of new objectshave
been
allocated,
andthereafter
only
at specifiedintervals.
Both
the
initial
threshold
andthe
frequency
canbe
modifiedfrom
their
default
valuesby
the
user.The
externalinteger
"CEGthreshold"
is
the
numberof newobjectsthat
willbe
allocatedbefore
the
garbage collection mechanism
is
calledfor
the
first
time.
On
systems withlarge
memory,
it
oftenmakes sense
to
allowthis
numberto
be
quitelarge
before
trying
any
collection.The
value canbe
changed
in
any function
by declaring
the
variable"extern
int
CEGthreshold;"
and
setting
it
to the
desired
value.The
frequency
ofthe
collectionis
modifiable as well.Frequency
ofcollection,
CEGfrequency,
is
defined
ashow
many
callsto
new()
are madebetween
collection attempts.It is
wastefulto
collectthe
entire objecttable every time the
userwantsto
allocate anew object.
After
the threshold
sizeCEGthreshold
has
been
reached,
collectiontakes
place after each"CEGfrequency"
calls
to new(),
ClassC/Elaine
paul ^^
"extern int
CEGfrequency;"That
is,
the
integer
CEGfrequency
representshow
many
callsto
newQ
are madebetween
eachattempt at collection of
the
objecttable.
Again,
it is
only necessary
to
declare
the
variableCEGfrequency
if it
is desired
to
modify its
default
value.The
valuemay
be
changedby
assigning
anewinteger
to
it.
The default
valueofCEGthreshold
is
1000;
default for
CEGfrequency
is
100.
2.11
Using
ClassC/Elaine
The
classdefinitions
shouldin be
put atthe
beginning
ofthe
sourcefile,
and should occurbefore
any
function definitions.
If
alarge
multi-file projectis
being developed,
all classdefinitions
shouldbe
madein
header
files
which are^included in
eachfile
that
manipulates objects ofthose
classes.The
memberfunctions
of each class shouldbe defined
only once,
and canbe defined
in
any
file
andlinked
in
atload
time.
The
ce commandinvokes
the
ClassC
compiler and processes commandline
options.The
only
optionspecial
to
ceis
the
garbage collection option"-G".
Other
commandline
optionsimplement
standard ccoptions asdescribed below:
cecommand
line
options-O
Invoke
the
C
optimizeronthe
output.-o name
Call
the
executablefile
"name".
-g
Compile
for
debugging.
ClassC/Elaine
Paul Kirkaas
Compile for
profiling.
Create
with garbage collection.-c
Create
objectfile;
do
notlink
into
executable.-llibname
Search
the
library
"lib/tiname.a"for
unresolvedreferences whenlinking.
Use
the
"-c"option and
the
Unix
"ar"library
archive commandto
build ClassC libraries.
2.12
Keywords
and special symbolsII
class
object
self
new
delete
Class
dereference
operatorClass
scoping
operatorInheritance
operatorClassC inline
comment operatorClass definition keyword
Object
type
nameTarget
objectin
memberfunction
definition
ClassC
library
dereference function
ClassC
library
object creationfunction
ClassC
library
object creationfunction
ClassC
library
reference countdecrement
ClassC
library
reference countincrement
ClassC
library
delete function
2.13
Examples of ClassC
programsClassC/Elaine
Paul
Ki_kaaa
S.
Implementation
The
ClassC/Elaine
compiler
wasimplemented
in
aUnix System V
environment withthe
Yacc
andLex Unix language
development
tools.
The input files
arefirst
processedby
the
C
preprocessor cpp.This
combines allthe
^include
.hfiles,
substitutes allthe
#define's,
stripsthe
comments,
andinserts
appropriate sourcefile/line
numberinformation
for
error reporting.The
outputis
directed
to
a/tmp
file
whichis
then
parsedby
the
ClassC
compiler,
which creates a converted x outputfile.
Finally,
the
standardC
compileris
invoked
onthe
generated .cfile
to
producethe
executable codeor
intermediate
.ofiles
for
separate compilation.The first
task
was
to
build
aC
language
parserto
make sure
that
the
language
wouldbe
a proper superset ofC
(the
newC
type
enum was notincluded).
The
the
parser wasthen
modifiedincrementally
to
addthe
features
ofClassC.
The
ClassC/Elaine
system wasimplemented
asfollows:
S.l
Statement/Declaration
Mixing
In
ClassC,
statements anddeclarations
canbe
mixedtogether
within a singleblock,
aslong
asvariables are
declared
before
being
used.In
standardC
alldeclarations
are requiredatthe
start ofablock before
any
executable statements.In
orderto
convertthe
ClassC blocks into C
format,
the
code within each
block is
separatedinto
two
collections a statementlist
and adeclaration list.
This
is
a recursiveprocedure,
because blocks
canbe
nested andblocks
withinblocks
arethemselves
statements.
So
a global variable"block_number"
is
maintained andthe
block
codeis
partitionedinto
the appropriately
nestedcollection ofblock
statements anddeclarations.
At
the
end of ablock,
the
statements anddeclarations
arere-merged,
with allthe
declarations
floated
to the
top
ofthe
block.
The
orderofthe
declarations
is
maintainedsothat
referencesto
previousdeclarations
withinthe
sameblock
arevalid.3.2 Inline Comments
All
text
between
"//"and
the
newline characteris
aninline
comment andis
removedfrom
the
input
file
by
the
lexical
analyzer generatedby
lex.
2i-ClassC/Elaine
paul Ri_kaas
3.3
Class
Definition
A
classdefinition is
writtenin
the
form:
class
Classjname
:SuperClass_l
:SuperClass_2
:{
type
declarator,
type
declarator,
type
declarator,
type
declarator,
};
where
Superclasses
are optional ancestor classes anddeclarators
may
be
any
legal
ClassC
declaration;
particularly,
they
may be function
declarations
or objectdeclarations
(not
definitions).
The
parseris
atwo
pass parser.The
first
passjust builds
alist
of class namesfrom
the
classdefinitions in
the
file.
No kind
oferrorchecking
is done
atthis
stage.The
second passis
the true
parse,
but
allthe
classesdefined in
the
file
are nowknown
to the
compiler.Thus,
aclass whichis
defined
anywherein
the
file
canbe
used anywherein
the
file.
This
allowsforward
referencing
inside
class
definitions,
sothat
classA
canbe
defined
to
contain objects of classB
while classB
canlikewise
be defined
to
contain objects of classA.
When
the
second pass ofthe
parser encounters a classdefinition,
it
first
processes allthe
declarations
withinthe
body
ofthe
definition.
The
variabledeclarations
aretreated
just like
structure component
declarations
and are enteredinto
a symboltable
whichis
maintainedfor
each