运算符==重载

已定义分数类,通过成员函数完成==的运算符重载。使得可以分数1==分数2的判断。

函数接口定义:
class Fraction      //分数
{
private: 
    int num = 0;    //分子
    int den = 1;    //分母
public:    
    //构造函数
    Fraction() {}
    Fraction(int num, int den);
    //打印分数 
    void PrintFraction() const;
    //约分
    Fraction Reduct();     
};
类的成员函数均已定义。

裁判测试程序样例:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
class Fraction      //分数
{
private: 
    int num = 0;    //分子
    int den = 1;    //分母
public:    
    //构造函数
    Fraction() {}
    Fraction(int num, int den);
    //打印分数 
    void PrintFraction() const;
    //约分
    Fraction Reduct();     
    /* 请在这里填写答案 */
};
//成员函数均已定义,代码略
int main()
{
    int n1,n2,n3,n4;
    char c;
    cin>>n1>>c>>n2>>n3>>c>>n4;
    Fraction f1(n1,n2),f2(n3,n4);
    if(f1==f2) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;     
    return 0;
}
输入样例1:
1/2 1/2
输出样例1:
YES
输入样例2:
1/2 1/3
输出样例2:
NO

 bool operator==(const Fraction& other)  //接受一个 const Fraction& 类型的参数 other,表示另一个分数。 
    {
        return num * other.den== other.num * den ;
    }

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值