DaZ
02-06-2003, 11:41 AM
just starting out basically in C++
ive got a class declared like the following;
-----------------------------------------------------------------
class rectangle
{
public:
rectangle();
~rectangle();
void SetLength(int length){itslength = length;}
int GetLength() const { return itslength;}
void SetWidth(int width){itswidth = width;}
int GetWidth() const {return itswidth;}
private:
int itslength;
int itswidth;
};
-----------------------------------------------------------------
the part of the code i dont understand is when declaring the constructor function underneath that section of code, the book i have states that it should be like the following
-----------------------------------------------------------------
// rectangle constructor declaration
rectangle::rectangle():
itswidth(5),
itslength(10)
{}
-----------------------------------------------------------------
now, this compiles and works correctly. and sets the member variables to 5 and 10 for itswidth and itslength.
however the only way ive learned to do this before is
// rectangle constructor declaration
rectangle::rectangle()
{
itswidth =5;
itslength=10;
}
this code also works correctly and compiles fine.
is the code ive put in above, that came with the book some kind of shortcut?? or does C++ allow for slight typos.
ive got a class declared like the following;
-----------------------------------------------------------------
class rectangle
{
public:
rectangle();
~rectangle();
void SetLength(int length){itslength = length;}
int GetLength() const { return itslength;}
void SetWidth(int width){itswidth = width;}
int GetWidth() const {return itswidth;}
private:
int itslength;
int itswidth;
};
-----------------------------------------------------------------
the part of the code i dont understand is when declaring the constructor function underneath that section of code, the book i have states that it should be like the following
-----------------------------------------------------------------
// rectangle constructor declaration
rectangle::rectangle():
itswidth(5),
itslength(10)
{}
-----------------------------------------------------------------
now, this compiles and works correctly. and sets the member variables to 5 and 10 for itswidth and itslength.
however the only way ive learned to do this before is
// rectangle constructor declaration
rectangle::rectangle()
{
itswidth =5;
itslength=10;
}
this code also works correctly and compiles fine.
is the code ive put in above, that came with the book some kind of shortcut?? or does C++ allow for slight typos.