C++ 运算符流操作符重载和友元--笔记



/************************************************************************************************
 * 名  称: friend.cpp
 * 功  能:学习C++ Premier 的笔记之操作符重载,友元
 * 描  述:1、友元函数:允许函数访问类的所有成员。
          2、直接重载操作符,则其中一个操作数必然是本类
          3、使用友元重载操作符,则可以任意选择操作数,注意 1 处 的区别
          4、重载>> <<流操作
          5、单目运算符 最好重载为 成员函数
          6、只能使用成员函数重载:=,(),[],->,new,delete
          7、复合赋值运算符,建议重载为成员函数。(+= ,-=,&=,等)
          8、其他运算符,建议重载为友元。
 * 作  者:JarvisChu
 * 时  间:2011-7-23 创建
 *************************************************************************************************/
#include <iostream>

using namespace std;

class CRectangle{
public:
    //构造函数
    CRectangle(){}
    CRectangle(double ht,double wid){height = ht;  width = wid;}

    //计算面积
    double Area(){  return height*width;}
    double Width(){return width;}
    double Height(){return height;}

    //重载输入操作符,友元
    friend istream& operator >>(istream& in,CRectangle& cls);
    //重载输出操作符,友元
    friend ostream& operator <<(ostream& out,CRectangle& cls);

    //重载 +-*/ 运算法,返回面积 和差积除
    double operator + (const CRectangle rect);   //只实现这一个,下面三个类似
    double operator - (const CRectangle rect){return 0.0;}
    double operator * (const CRectangle rect){return 0.0;}
    double operator / (const CRectangle rect){return 0.0;}

    //重载 [] 运算符,[0] 返回 height, [1] 返回 width
    double operator [] (const int index);

    //重载 > < = 比较操作符,注意友元 与 非友元 的区别
    friend bool operator > (CRectangle rect_1, CRectangle rect_2);// 1处
    bool operator < (CRectangle rect);

    //析构函数
    ~CRectangle(){}
private:
    double height;
    double width;
};

istream& operator >> (istream& in, CRectangle& cls){
    cout<<"输入矩形的高和宽: ";
    in>>cls.height>>cls.width;
    return in;
}

ostream& operator << (ostream& out, CRectangle& cls){
    out<<"高:"<<cls.height<<"; 宽:"<<cls.width;
    return out;
}

double CRectangle::operator +(CRectangle rect){
    return Area()-rect.Area();
}

double CRectangle::operator [](int a){
    switch(a){
        case 0:
            return Height();
            break;
        case 1:
            return Width();
            break;
        case 2:
            return Area();
            break;
        default:
            return 0.0;
            break;
    }
}

bool operator >(CRectangle rect_1, CRectangle rect_2){
    return (rect_1.Area() > rect_2.Area())  ? true:false;
}

bool CRectangle::operator < (CRectangle rect){
    return  (Area() < rect.Area())  ? true:false;
}

int main()
{
    CRectangle rect_1;
    CRectangle rect_2(10,20);

    //输入输入流 重载
    cin>>rect_1;
    cout<<endl<<"rect_1: "<<rect_1<<endl;
    cout<<"rect_2: "<<rect_2<<endl<<endl;

    //四则运算 重载
    cout<<"+: "<<rect_1+rect_2<<endl;

    //重载[]
    cout<<"height[0]: "<<rect_1[0]<<endl<<"width [1]: "<<rect_1[1]<<endl<<" area [2]: "<<rect_1[2]<<endl<<endl;

    //比较运算符 重载
    if(rect_1 > rect_2) cout<<"rect_1 > rect_2 :  true"<<endl;
    if(rect_1 < rect_2) cout<<"rect_1 < rect_2 :  true"<<endl;

    return 0;
}


posted @ 2011-07-23 20:57  JarvisChu  阅读(302)  评论(0编辑  收藏  举报