简体   繁体   中英

Why is my class performing arithmetic when I haven't coded any?

I'm working on a homework project that is supposed to introduce us to classes. I've just started, but for some reason my member variables are displaying correctly until the end. It should just be passing those variables between functions then outputting them. It will take a numerator and denominator set by a parameterized constructor, set an object with them, then output what the numerator and denominator holds. It is taking the numerator and setting it to zero, even when it previously displayed the correct number in every function the class ran through. The denominator is even more weird because it turns into some huge number somehow. I just want to understand what is happening since I am new to this and the reason why isn't super apparent to me.

#include <iostream>

using std::cout;
using std::endl;
using std::ostream;

class RationalNum
{
public:
    RationalNum();
    RationalNum(int numer, int denom);
    int getNumerator() const { return numer; }
    int getDenominator() const { return denom; }
    void setNumerator(int);
    void setDenominator(int);
    void set(int& numer, int& denom);
    void output(ostream& outs);

private:
    int denom;
    int numer;
};

RationalNum::RationalNum() : numer(1), denom(1)
{}

RationalNum::RationalNum(int numer, int denom) //takes two integers for numer and denom, and reduces
{
    cout << "The numer and denom  in the param const are " << numer << denom << endl;
    set(numer, denom);
}

void RationalNum::set(int& numer, int& denom) //use other functions to take two params and set as numer and denom

{
    cout << "The numer and denom in set are " << numer << denom << endl;
    setNumerator(numer);
    setDenominator(denom);
}

void RationalNum::setNumerator(int numer) //takes an int, sets it as numer, and reduces it
{
    cout << "in setNumer, the numer is: " << numer << endl;
    //reduce(newNumer); //Not using yet
}

void RationalNum::setDenominator(int denom) //takes an int, sets it as denom, and reduces it
{
    cout << "in setDenom, the denom is: " << denom << endl;
    //reduce(newDenom); //Not using yet
}

void RationalNum::output(ostream& outs) //takes an ostream param
{
    cout << numer << "/" << denom << endl;
}


int main()
{
    RationalNum rn1, rn2(25, 10), rn3(24, -100);

    cout << "rn1 contains: ";
    rn1.output(cout);
    cout << endl;

    cout << "rn2 contains: ";
    rn2.output(cout);
    cout << endl;

    cout << "rn3 contains: ";
    rn3.output(cout);
}

This is what the actual results are. It is correct until the last two lines, and that is where I'm lost. rn2 should display 25/10 and rn3 should display 24/-100

The numer and denom  in the param const are 2510
The numer and denom in set are 2510
in setNumer, the numer is: 25
in setDenom, the denom is: 10

The numer and denom  in the param const are 24-100
The numer and denom in set are 24-100
in setNumer, the numer is: 24
in setDenom, the denom is: -100
rn1 contains: 1/1    
rn2 contains: 0/4196160    
rn3 contains: 0/4197376

The methods setNumerator and setDenominator are not doing anything. For this reason, the class member denom and numer and never initialized. Their value will thus be undefined, ie, potentially garbage.

To fix it, use:

void RationalNum::setNumerator(int n)
{
  numer = n; // I changed the parameter name to avoid confusion with the member
  cout << "in setNumer, the numer is: " << numer << endl;
  //reduce(newNumer); //Not using yet
}

Use a similar approach for the denominator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM