|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raz (3) | |
|
hello all plz help me with this homework. consider the definition of the following class: class CC { public : CC (); //line 1 CC (int); //line 2 CC (int, int); //line 3 CC (double, int); //line 4 . . . private: int u; double v; }; a. give the line number containing the constructor that is executed in each of the following declarations: i. CC one; ii. CC two(5, 6); iii. CC three(3.5, 8); b. write the definiiton of the constructor in line 1 so that the private member variables are initialized to 0. c. write the definition of the constructor in line 2 so that the private member variable u is initialized according to the value of the parameter, and the private member variable v in initialized to 0. plz help as much as u can.. | |
|
|
|
| Bazzy (6258) | |
| Show your efforts before asking | |
|
|
|
| raz (3) | |
|
a. i. line 1 ii. line 3 iii. line 4 b. CC() { u=0; v=0; } c. cc(int i) { u=i; v=0; is that true?? } | |
|
|
|
| Bazzy (6258) | |
|
a. Correct b. It isn't wrong but using an initializer list is better: CC() : u(0), v(0) {}c. The same as (b.) and 'cc' should be 'CC' | |
|
|
|
| mcleano (922) | ||
For performance or ease-of-reading? I haven't really come across a source where they actually teach this method so I was fairly confused the first time some of you more advanced programmers told me about this. | ||
|
|
||
| Bazzy (6258) | ||
{ x=y; } way initializes 'x' and then calls the = operator, the : x(y) way directly calls the 'x' constructor
| ||
|
|
||
| btripp (160) | |
| i usually dont use the initializer list because I usually have error checking I want to perform. Its pretty standard to use the lists if you are using inheritance though | |
|
|
|
| pjwarez (12) | ||
It's possible to use a try/catch block with an initializer list. | ||
|
|
||
| mcleano (922) | |
| Thanks Bazzy. | |
|
|
|
| raz (3) | |
| thx all | |
|
|
|