c++ homework

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..
Show your efforts before asking
closed account (z05DSL3A)
http://www.cplusplus.com/doc/tutorial/classes/
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??
}
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'
b. It isn't wrong but using an initializer list is better: CC() : u(0), v(0) {}

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.
For performance or ease-of-reading?
Performance, the { x=y; } way initializes 'x' and then calls the = operator, the : x(y) way directly calls the 'x' constructor
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
i usually dont use the initializer list because I usually have error checking I want to perform


It's possible to use a try/catch block with an initializer list.
Thanks Bazzy.
thx all
Topic archived. No new replies allowed.