1. 程式人生 > >C++運算子過載、友元函式過載

C++運算子過載、友元函式過載

實驗要求:
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;}

這裡寫圖片描述

分析:我們在上一個實驗的基礎上通過友元函式實現上述要求,通過友元關係可以實現不同類或物件的成員函式之間,類的成員函式與一般函式之間進行資料共享機制。我們先在類中宣告這幾個過載函式為該類的友元,這裡要注意此時過載函式的引數有兩個而不是一個,友元函式需要兩個引數分別呼叫在主函式往裡面傳遞的值,友元函式最大的好處就是能夠呼叫類中的私有資料成員,還能保證類的封裝性。