Isn't this initialization too, even though constructor had no arguments at all. |
Yes and no. Constructor initializes the object.
However,
1 2 3 4 5 6
|
Point::Point()
// this is the point where x and y are initialized
{
this->x=0; // this is assignment to already inititalized x
this->y=0; // this is assignment to already inititalized y
}
|
There is specific
initializer list syntax that allows calling the constructors of members of a class:
1 2 3 4
|
Point::Point()
: x(0), y(0) // this is the point where x and y are initialized
{
}
|
Yes, you can write:
1 2 3 4 5 6 7
|
class Point {
public:
Point();
Point( double x );
Point( double x, double y );
// ...
};
|
However, then you have to write
three constructors. You yourself wrote that you "actually have to create
a single constructor". The default arguments make that possible. I have given you a link that describes default arguments.
Casting is conversion. You have a value of type T, but need a value of type U. If we can create a temporary U object from the T value, then the problem is solved.
Explicit casting is ... explicit. We clearly write instruction that performs the cast.
For example:
1 2
|
int x = 7;
float y = static_cast<float>( x ) / 2;
|
Implicit cast occurs when we are not explicit, but the compiler finds a function that it can use to perform the cast.
For example:
1 2 3 4 5
|
void foo( Point p );
int main() {
foo( 3.14 ); // 3.14 is a double
}
|
This code is valid, if compiler can create a Point from a double. It can, if Point has a constructor that can be called with one double argument, and that constructor does not have the
explicit
qualification.