Questions about class constructions

Hi everybody. I have a few questions that came up to my mind while looking at the cplusplus.com tutorials.

First, I have read that by default, all members of a class that are not specified as protected or public will be assumed by the compiler as private. I have looked at google C++ coding styles for an answer, but sadly I could not find one. So, can we assume that not explicitly stating that a block of members are private could be considered as a good/bad programming practice?

Second, while studying I also read that there is a default definition for the assignment operator (=) with the class itself as a parameter. This is supposed to copy the whole content of all data members of the objected passed as argument to the one at the left side. So for example, if I have a class point and a point object called my_point, with X,Y coordenates (4,5) and I declare another one called second_point and do the following assignation

second_point=my_point;

the second_point will also have the coordenates (4,5). So what is the differences between using the assignment operator and using a copy constructor such as:

1
2
3
4
5
point::point(const point& my_point)
{
x=my_point.x;
y=my_point.y;
}


to do identical copies of an object?


In a program that I am writting, I want to check if the given input for function SetX(double x_value) is in fact a valid input (any number). I have been looking all over the web for some sort of "isDouble" and "isInteger" or "isNumber" C++ function in order to narrow the possibilities of errors to the minimum, that being that the user has given a string as an input. Nontheless, I have not found an answer yet. I have looked at http://www.learncpp.com/cpp-tutorial/135-stream-states-and-input-validation/ to check the validity of inputs. However, these functions receive only integers as parameters (so, as far as I understand, the portability of the program could be compromised when using doubles as inputs). I have thought on another verification process, looking if the given input has characters different from '.', but that has not worked either. Can anyone give me a suggestion as to how to do the verification on this specific point?

thanks
can we assume that not explicitly stating that a block of members are private could be considered as a good/bad programming practice?

I always put the public members first so I always have to use private explicitly. Even if you don't have to I think it makes it more readable to be explicit but that's just me.

what is the differences between using the assignment operator and using a copy constructor

The copy constructor is used to create a new object that is a copy of another object. The assignment operator is used when you have an existing object you want to change.

Can anyone give me a suggestion as to how to do the verification on this specific point?

A double is always a double. If you want to validate your input you should check the stream that you read from.
1
2
3
4
5
double value;
if (std::cin >> value)
{
	// the value was read successfully!
}
Thanks peter, this was really helpful. Regarding the second question, if I have a default constructor which sets the object to some initial parameters and use the assigment operator to modify it and replicate the attributes of an already created object, would this be equivalent to using a copy constructor?
Topic archived. No new replies allowed.