为什么我的 class 在我没有编码的情况下执行算术?
Why is my class performing arithmetic when I haven't coded any?
我正在做一个家庭作业项目,应该向我们介绍 classes。我刚刚开始,但由于某种原因我的成员变量显示正确直到结束。它应该只是在函数之间传递这些变量然后输出它们。它将接受由参数化构造函数设置的分子和分母,用它们设置一个对象,然后输出分子和分母所包含的内容。它采用分子并将其设置为零,即使它之前在 class 运行 期间的每个函数中都显示了正确的数字。分母更奇怪,因为它不知何故变成了一个巨大的数字。我只是想了解发生了什么,因为我是新手,以及为什么对我来说不是很明显的原因。
#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);
}
实际结果是这样的。直到最后两行都是正确的,这就是我迷路的地方。 rn2 应显示 25/10,rn3 应显示 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
方法 setNumerator
和 setDenominator
没有做任何事情。由于这个原因,class 成员 denom
和 numer
并没有被初始化。因此,它们的值将是未定义的,即可能是垃圾。
要修复它,请使用:
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
}
对分母使用类似的方法。
我正在做一个家庭作业项目,应该向我们介绍 classes。我刚刚开始,但由于某种原因我的成员变量显示正确直到结束。它应该只是在函数之间传递这些变量然后输出它们。它将接受由参数化构造函数设置的分子和分母,用它们设置一个对象,然后输出分子和分母所包含的内容。它采用分子并将其设置为零,即使它之前在 class 运行 期间的每个函数中都显示了正确的数字。分母更奇怪,因为它不知何故变成了一个巨大的数字。我只是想了解发生了什么,因为我是新手,以及为什么对我来说不是很明显的原因。
#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);
}
实际结果是这样的。直到最后两行都是正确的,这就是我迷路的地方。 rn2 应显示 25/10,rn3 应显示 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
方法 setNumerator
和 setDenominator
没有做任何事情。由于这个原因,class 成员 denom
和 numer
并没有被初始化。因此,它们的值将是未定义的,即可能是垃圾。
要修复它,请使用:
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
}
对分母使用类似的方法。