1 2 3 4 5 6 7 8 9

Constructors


To reduce the need for more identifiers, the constructor
functions have the same name as the class. There is no
confusion in the computer's mind.


Consider our earlier complex class...


class Complex {


private:
double real;
double imaginary;
public:
Complex();
// our constructor prototype
}


First note that now the data items realand imaginarywill
be hidden from the outside world (are private), but that the
access control mechanism (just the constructor for the
moment will be usable by everybody--is public).


The "body" of the constructor can be specified inside the
class, but if it is lengthy then it is usually done outside as
follows:


Complex :: Complex (double re, double im) {
real = re;
imaginary = im;
}

8 March, 1998

Page {PAGE }

TAM/DS