C++运算符重载、友元函数重载

本文介绍了如何使用C++中的运算符重载来实现分数的基本运算,包括加、减、乘、除,并确保输出结果是最简分数形式。文章通过两种方式实现了这一目标:一是通过成员函数重载运算符,二是通过友元函数重载运算符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验要求:
1、设计一个类,用自己的成员函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。要求:
(1)输出结果是最简分数(可以是带分数);
(2)分母为1,只输出分子。

#include<iostream>
using namespace std;
class score{
public:
    score(int X=0,int Y=0){
        x=X;
        y=Y;
    }
    void base(){
        int t;
        t=x%y;
        while(t!=1){
            x=y;
            y=t;
            t=x%y;
        }
    }
    void print(){
        int z;
        if((x<y)&&(y!=1)){
            cout<<x<<"/"<<y<<endl;
        }
        if((x>y)&&(y!=1)){
            z=x/y;
            if(x%y!=0)
            cout<<z<<"("<<x%y<<"/"<<y<<")"<<endl;
            else
            cout<<z<<endl;
        }
    }
    score operator+(score c);
    score operator-(score c);
    score operator*(score c);
    score operator/(score c);
private:
    int x,y;
};
score score::operator+(score c)
{
    score temp;
    if(y!=c.y){
      temp.y=y*c.y;
      temp.x=x*c.y+c.x*y;
    }
   return temp;
}
score score::operator-(score c){
    score temp;
 if(y!=c.y) {
    temp.y=y*c.y;
    temp.x=x*c.y-c.x*y;
    }
 return temp;
}
score score::operator*(score c){
    score temp;
    if(y!=c.y) {
     temp.y=y*c.y;
    temp.x=x*c.x;
    }
     return temp;
}
score score::operator/(score c){
    score temp;
    if(y!=c.y) {
     temp.y=y*c.x;
    temp.x=x*c.y;
    }
     return temp;
}
int main(){
    score A1(3,2),A2(5,7),A3,A4,A5,A6;
     A1.print();
     A2.print();
     A3=A1+A2;
     A3.print();
     A4=A1-A2;
     A4.print();
     A5=A1*A2;
     A5.print();
     A6=A1/A2;
     A6.print();
     return 0;
}

这里写图片描述

分析:对几个运算重载使之能够进行分数的运算,在运算的时候还要注意输出最简分数,所以开始需要解决的问题是如何实现运算符的重载,通过operator实现对几个不同的运算符的重载,然后通过辗转相除法求该分式有无最大公约数,对分数进行化简,需要注意的是当分母为一的时候注意去掉分母,还有就是在输出的时候要注意当分子大于分母的时候要记得化为带分式。
2、用友元函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。

#include<iostream>
using namespace std;
class score{
public:
    score(int X=0,int Y=0){
        x=X;
        y=Y;
    }
    void base(){
        int t;
        t=x%y;
        while(t!=1){
            x=y;
            y=t;
            t=x%y;
        }
    }
    void print(){
        int z;
        if((x<y)&&(y!=1)){
            cout<<x<<"/"<<y<<endl;
        }
        if((x>y)&&(y!=1)){
            z=x/y;
            if(x%y!=0)
            cout<<z<<"("<<x%y<<"/"<<y<<")"<<endl;
            else
            cout<<z<<endl;
        }
    }
    friend score operator+(score &a,score &b);
    friend score operator-(score &a,score &b);
    friend score operator*(score &a,score &b);
    friend score operator/(score &a,score &b);
private:
    int x,y;
};
score operator+(score &a,score &b)
{
    score temp1;
    if(a.y!=b.y){
      temp1.y=a.y*b.y;
      temp1.x=a.x*b.y+b.x*a.y;
    }
   return temp1;
}
score operator-(score &a,score &b){
    score temp1;
 if(a.y!=b.y) {
    temp1.y=a.y*b.y;
    temp1.x=a.x*b.y-b.x*a.y;
    }
 return temp1;
}
score  operator*(score &a,score &b){
    score temp1;
    if(a.y!=b.y) {
     temp1.y=a.y*b.y;
    temp1.x=a.x*b.x;}
     return temp1;
}
score operator/(score &a,score &b){
    score temp1;
    if(a.y!=b.y) {
     temp1.y=a.y*b.x;
    temp1.x=a.x*b.y;
    }
     return temp1;
}
int main(){
    score A1(1,2),A2(6,5),A3,A4,A5,A6;
     A1.print(); A2.print();
     A3=A1+A2;
     A3.print();
     A4=A1-A2;
     A4.print();
     A5=A1*A2;
     A5.print();
     A6=A1/A2;
     A6.print();
     return 0;}

这里写图片描述

分析:我们在上一个实验的基础上通过友元函数实现上述要求,通过友元关系可以实现不同类或对象的成员函数之间,类的成员函数与一般函数之间进行数据共享机制。我们先在类中声明这几个重载函数为该类的友元,这里要注意此时重载函数的参数有两个而不是一个,友元函数需要两个参数分别调用在主函数往里面传递的值,友元函数最大的好处就是能够调用类中的私有数据成员,还能保证类的封装性。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值