Hello I made this class that has a default constructor and a constructor with default argument
Something.h
class Something // Fraction class
{
public:
Something (void); // default ctor
Something (int xx, int yy = 1 );
int x,y;
}
Something.cpp
Something::Something(){}
Something::Something(int xx, int yy)
{
x = xx;
y = yy;
}
but when I make object with no parameter and print it, it will show that x = 0, y = 0;
where might be the problem 🙁
Thanks!
If you call with no parameters, you will be calling this constructor
instead of this
so your initialisation code wont get called as you have provided none explicitly and you will get a default value upon initialisation of your integer members – in this case it was zero, but it could be any integer value. For this reason it is good practice to initialise your member variables in the constructor implementation. For example implementing it like this
will result in
xandybeing set to 1 when you create an instance of your object with the zero argument constructor.