![]() |
Can anyone shed some light please...
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. |
Quote:
I have two classes A and B with some contructors. And, I have another class C which has two objects one each of the above mentioned classes. You may pass the parameters to the constructors of the objects of classes A and B as above. Code:
class A { |
I dont know if it works for primitive data types too.
|
nice one thanks.
its just that the book ive got goes along quite happily, and then it throws something like that in and doesnt explain it.. thanks again for clearing it up for me. DaZ |
| All times are GMT +1. The time now is 06:06 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.