C++運算符重載——輸入/輸出運算符


為了與IO標准庫一致,重載輸入輸出運算符函數的第一個行參應該是流的引用,第二個行參是對象的引用。

如果重載為類的成員函數,第一個行參應該是對象的引用,第二個行參是流的引用。

使用方式是 ClassObj << cout 這樣與標准IO庫就不一致了,所以輸入輸出運算符不能重載為類的成員函數,可以重載為類的友元函數和普通函數。

通常重載輸出運算符的第二個行參是const的,因為輸出一個類不許要更改它;

但是重載輸入運算符的第二個行參必須是非const的,否則無法賦值。

重載的基本方法如下:

//重載輸出運算符
ostream& operator<<(ostream& out, const ClassType& obj)
{
    out << /*想要輸出的內容1*/ << /*想要輸出的內容2*/ <<...;
    return out;
}

//重載輸入運算符
istream& operator<<(istream& in, ClassType& obj)
{
    in >> /*想要輸入的內容1*/ >> /*想要輸入的內容2*/ >>...;
//檢查錯誤 和 文件結束的可能性
return in; } 

例子:類Persion使用友元函數的方式重載了輸入輸出運算符,類PersionA使用了普通函數重載了輸入輸出運算符。

#include <cstring>
#include <iostream>
using namespace std;

class Persion
{
public:
    //constructor
    Persion(const char *pname, unsigned int ag, double he,double we):age(ag),height(he),weight(we){strcpy(name,pname);}

    //operator overload : <<
    friend ostream& operator<<(ostream& out, const Persion& ref)
    {
        out<<ref.name<<"\t"<<ref.age<<"\t"<<ref.height<<"\t"<< ref.weight;
        return out;
    }
    //operator overload : >>
    friend istream& operator>>(istream& in, Persion& ref)
    {
        char name[40] = {0};
        unsigned int ag = 0;
        double he = 0;
        double we = 0;

        in>>name>>ag>>he>>we;

        //check that if the inputs succeeded
        if (in)
        {//Input Succeeded
            strcpy(ref.name, name);
            ref.age = ag;
            ref.height = he;
            ref.weight = we;
        }
        else
        {//Input Failed
        }

        return in;
    }
private:
    char name[40];
    unsigned int age;
    double height;
    double weight;
};

class PersionA
{
public:
    //constructor
    PersionA(const char *pname, unsigned int ag, double he,double we):age(ag),height(he),weight(we){strcpy(name,pname);}
    //GetData
    char* GetName(void){return name;}
    unsigned int& GetAge(void){return age;}
    double& GetHeight(void){return height;}
    double& GetWeight(void){return weight;}


private:
    char name[40];
    unsigned int age;
    double height;
    double weight;
};

//operator overload : <<
ostream& operator<<(ostream& out, PersionA& ref)
{
    out<<ref.GetName()<<"\t"<<ref.GetAge()<<"\t"<<ref.GetHeight()<<"\t"<<ref.GetWeight();
    return out;
}
//operator overload : >>
istream& operator>>(istream& in, PersionA& ref)
{
    char name[40] = {0};
    unsigned int ag = 0;
    double he = 0;
    double we = 0;

    in>>name>>ag>>he>>we;

    //check that if the inputs succeeded
    if (in)
    {//Input Succeeded
        strcpy(ref.GetName(), name);
        ref.GetAge() = ag;
        ref.GetHeight() = he;
        ref.GetWeight() = we;
    }
    else
    {//Input Failed
    }

    return in;
}
int main(void)
{
    Persion per("Jack", 20, 175, 65);
    cout << per << endl;
    cin>>per;
    cout << per << endl;

    PersionA perA("Jack", 20, 175, 65);
    cout << perA << endl;
    cin>>perA;
    cout << perA << endl;
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM