如何使用 c++ 中的类编写有理分数
How to write Rational Fractions using classes in c++
我必须实现Rational
类才能得到有理分数。header.h
文件由我的老师提供,所以我必须跟进。我还必须在Rational::Rational(const Rational& cRational)
函数中编写复制构造函数,以便可以复制object
。我已经编写了代码,但在输出中添加分数是错误的。谁能帮我弄清楚这个?我的编码有什么问题,特别是在Rational::addition(const Rational &a)
或我该如何解决它?
输出:
Begin Rational Class Tests
Testing default constructor: 1/1
Testing std constructor: 1/2
Testing copy constructor: 1/2
Testing addition: 4/4 + 1/2 = 4/4 // it should be 1/2 + 1/2 = 4/4
主要功能:
int main()
{
cout << "Begin Rational Class Testsnn";
cout<<"Testing default constructor: ";
Rational n1;
n1.printRational();
cout << endl << endl;
cout<<"Testing std constructor: ";
Rational n2(1,2);
n2.printRational();
cout << endl << endl;
cout<<"Testing copy constructor: ";
Rational n3(n2);
n3.printRational();
cout << endl << endl;
cout<<"Testing addition: ";
n1 = n2.addition(n3);
n2.printRational();
cout <<" + ";
n3.printRational();
cout <<" = ";
n1.printRational();
cout << endl << endl;
}
头文件:
class Rational {
public:
Rational(); // default constructor
Rational(int, int); //std (initialisation) constructor
Rational(const Rational&); //copy constructor
Rational addition(const Rational &);
void printRational();
private:
int numerator;
int denominator;
};
我的程序:
//default constructor
Rational::Rational()
{
numerator = 1;
denominator = 1;
}
//initialize constructor
Rational::Rational(int n, int d)
{
numerator = n;
if (d==0)
{
cout << "ERROR: ATTEMPTING TO DIVIDE BY ZERO" << endl;
exit(0); // will terminate the program if division by 0 is attempted
}
else
denominator = d;
}
//copy constructor
Rational::Rational(const Rational& cRational)
{
numerator = cRational.numerator;
denominator = cRational.denominator;
}
//addition
Rational Rational::addition(const Rational &a)
{
numerator = numerator * a.denominator + a.numerator * denominator;
denominator = denominator * a.denominator;
return Rational(numerator,denominator);
}
void Rational::printRational()
{
cout << numerator << "/" << denominator ;
}
你的加法函数正在修改*this
的成员变量,这会导致奇怪的事情发生。
更合适的原型是
Rational addition(const Rational &) const;
因为这会让编译器告诉您您正在做一些奇怪的事情。
您可以使用局部变量而不是分配给成员,也可以完全不使用中间变量:
Rational Rational::addition(const Rational &a)
{
return Rational(numerator * a.denominator + a.numerator * denominator,
denominator * a.denominator);
}
只需在 addition(( 中为分子和分母的新值创建具有不同名称的新变量,有点像这样......
int n, d; //you could use better names
n = numerator * a.denominator + a.numerator * denominator;
d = denominator * a.denominator;
return Rational(n, d);
我检查了一下,这在这里有效。
通常,不要对同一作用域中的两个变量使用相同的名称。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- C++:TypeDef使用元组
- 使用std::multimap迭代器创建std::list
- 从不同线程使用int64的不同字节安全吗
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么在全局范围内使用"extern int a"似乎不行?
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用Google Mock来模拟gettimeofday()
- 如何使用默认参数等选择模板专业化
- 为什么使用 "this" 指针调用派生成员函数?
- 使用新行和不使用新行读取文件
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何确定我已使用非编码文件到达 EOF?
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 使用CMake创建QML插件
- 使用 LLVM 的 libc++ 时,__1 符号从何而来?
- 在std::cout之后使用std::cin时,换行符从何而来
- 为什么以及在何处在C++中使用引用和指针
- 基例如何影响使用递归函数的哪些行