0

Can someone explain why my constructor is not setting Fraction c,d,e values to 0 when no parameter is provided? Results are: Fraction c=0,0 d=0,6e23 e=6e23,0.

I have found the workaround of setting Fraction::Fraction() {} to Fraction::Fraction() : m_num(0), m_deno(0) {} but I thought using Fraction::Fraction() {} would mean filling the values with 0s...

class Fraction {
public:
Fraction(double num, double deno);
Fraction(double num);
Fraction();

private:
double m_num;
double m_deno;
};

int main() {

Fraction a(4,5);      // a=4/5
Fraction b(2);        // b=2/1 
Fraction c, // c=0/0
         d, // d=0/6e23
         e; // e=6e23/0
return 0;
}

Fraction::Fraction() {}
Fraction::Fraction(double num) : m_num(num), m_deno(1) {}
Fraction::Fraction(double num, double deno) : m_num(num), m_deno(deno) {}
Valentin
  • 39
  • 5
  • 4
    Because there is no code that would do so? I don't see a single line here that would attempt to set anything to 0. *"I thought using Fraction::Fraction() {} would mean filling the values with 0s..."* - you thought wrong. Unless you explicitly specify how a member should be constructed it will be *default constructed* (which means that nothing is initialized in the case of `double`) – UnholySheep Dec 25 '21 at 23:20
  • 1
    `m_num` and `m_deno` don't start as `0` any more than they would if they were just function local variables. Another way of specifying starting values would be to use [default member initializers](https://en.cppreference.com/w/cpp/language/data_members#Member_initialization). – Nathan Pierson Dec 25 '21 at 23:22
  • 2
    You need to set those values to zero yourself. Otherwise it is just displaying the location of memory of c, d ,e – Adan Vivero Dec 25 '21 at 23:22
  • 1
    it is always good to set member variables to the values that you expect in the constructor, never take anything for granted. alt. assign in the declaration C++17 – AndersK Dec 25 '21 at 23:33
  • Also, tangential, are you sure you _want_ `m_deno` to be `0` in a default-constructed `Fraction`? Perhaps `0/1` for a default value would make more sense than `0/0`. – Nathan Pierson Dec 26 '21 at 00:03

2 Answers2

6

If you do not assign default values to C++ default types (for example: int, float, char) class variables inside the constructor, they are not going to be defaulted to zero. That will lead you to undefined behaviour. Check here to see in which cases the variables will be zero: Default initialization in C++

If you want them to be zero:

    private:
    double m_num = 0;
    double m_deno = 0;
bolov
  • 72,283
  • 15
  • 145
  • 224
1

I suggest you set those c, d, and e to be 0, if that is what you want it to be. Otherwise it is just going to be the value of a memory location of c, d, and e. I suggest you initialize them to 0, if that is what you want from them.

int main() {

Fraction a(4,5);      // a=4/5
Fraction b(2);        // b=2/1 
Fraction c(0);
Fraction d(0);
Fraction e(0);
return 0;
}
Adan Vivero
  • 422
  • 12
  • 36
  • Thanks for your answer but if only one parameter is provided my constructor will set the second to be 1 so it would not work to set both values to 0. – Valentin Dec 25 '21 at 23:46
  • @ValentinJub You can try using a delegating constructor `Fraction(double num) : Fraction(num, 1) {}` – cigien Dec 26 '21 at 00:02
  • @ValentinJub oh, I wasn't sure which constructor you were using to call c, d, and e. I just did the one with one parameter. Feel free to edit it on how it should be. – Adan Vivero Dec 27 '21 at 21:55