Title: Class Composition
1Class Composition
- Meaning and syntax
- Initialization.
- Constant and reference members.
- Static class members.
- Container classes.
- Friend classes.
2Meaning and syntax
class Head int numofeyes public Head
( ) Head( Head h) numofeyes
h.numofeyes Head(int num) numofeyes
num int getneyes( ) return numofeyes
class Body int numoflegs float length
public Body ( ) Body( Body b)
numoflegs b.numoflegs length
b.length Body(int n, float l)
numoflegs n length l int
getnlegs( ) return numoflegs int
getlength( ) return length
class Insect private float
weight Head itsHead Body
itsBodypublic Insect (Head h, Body b,
float w ) itsHead h itsBody b
weight w Insect taller(Insect I)
if (I.itsBody.getlength() gt
itsBody.getlength()) return I
else return this
Insect is a composite class, some of its data
members are objects of other classes (member or
component classes).
Use of methods of member classes
3Initialization
To construct an object of the composite class,
objects of the component classes have to be
constructed first, i.e. whenever a constructor of
the composite class is called, a constructor of
each component class is called. But which
constructor ?? Two cases
Assignment
Initialization lists
class Insect private float
weight Head itsHead Body
itsBodypublic Insect (Head h, Body b,
float w ) itsHead(h), itsBody(b)
weight w Insect taller(Insect I)
if (I.itsBody.getlength() gt itsBody.getlength()
) return I else
return this
class Insect private float
weight Head itsHead Body
itsBodypublic Insect (Head h, Body b,
float w ) itsHead h itsBody b weight
w Insect taller(Insect I) if
(I.itsBody.getlength() gt itsBody.getlength())
return I else
return this
The default constructor of the component classes
is called, then the assignment operator is used.
Programmer can specify which constructor of the
component classes is used, e.g. copy constructor,
general,
4Initialization Lists
class Insect private float
weight Head itsHead Body
itsBodypublic Insect (Head h, int i,
float f, float w ) itsHead(h), itsBody(i,
f) weight w Insect
taller(Insect I) if
(I.itsBody.getlength( ) gt itsBody.getlength( ))
return I else
return this
These two initialization statements choose two
different constructors (different parameter
list), of course these constructors have to be
defined in classes Head and Body. Here the copy
constructor of the class Head is used and the
general constructor of the class Body is used.
The initialization list selects which
constructors are going to be used to construct
the member objects (according to the parameters
used to call the constructor). In some cases
initializaton has to be used instead of
assignment.
5Constant data members
A constant data member of a certain class is a
data member whose value wont be changed during
the lifetime of its object. With constant data
members we have to use initialization lists. Why ?
class Insect private const float
weight Head itsHead Body
itsBodypublic Insect (Head h, int i,
float f, float w ) itsHead(h),
itsBody(i, f), weight(w) Insect
taller(Insect I) if
(I.itsBody.getlength( ) gt itsBody.getlength( ))
return I else
return this
Notice the empty brackets, now all members are
initailized and the constructor has nothing else
to do
6Reference data members
With Reference data members we have to use
initialization lists. Why ?
class Insect private float
weight Head itsHead Body
itsBodypublic Insect (Head h, Body b,
float w ) itsHead(h), itsBody(b)
weight w Insect
taller(Insect I) if
(I.itsBody.getlength( ) gt itsBody.getlength( ))
return I else
return this
Here we have to use initialization for itsHead
and itsBody objects, but for weight we have the
choice between initialization and assignment
Here itsHead and itsBody will be references to h
and b (from the parameter list) e.g. whatever
change we do to itsHead will be mapped to h
7Static object members
In composite classes, component objects can be
declared as static (same as we did with simple
variables)
class Body int numoflegs float length
static Body referencebody public Body
( ) Body( Body b) numoflegs
b.numoflegs length b.length
Body(int n, float l) numoflegs n
length l int getnlegs( ) return
numoflegs int getlength( ) return
length
Here, an object of class body will contain an
object of its same class (i.e. referencebody).
This can only be done with static and reference
members. Why ?
Body Bodyreferencebody(6, 17.7) Main ( ) .
Initialization of the static object
8Friend classes
class Head friend class Insect int
numofeyes public Head ( ) Head(
Head h) numofeyes h.numofeyes
Head(int num) numofeyes num
class Body friend class Insect int
numoflegs float length public Body (
) Body( Body b) numoflegs
b.numoflegs length b.length
Body(int n, float l) numoflegs n
length l
Compare this code to the previous one!
class Insect private float
weight Head itsHead Body
itsBodypublic Insect (Head h, Body b,
float w ) itsHead h itsBody b
weight w Insect taller(Insect I)
if (I.itsBody.length gt itsBody.length)
return I else
return this
Now methods in Insect class can directly access
private data members of Head and Body, we dont
have to define methods for this purpose in Head
and Body, this can simplify code