国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++運(yùn)算符重載 成員函數(shù)與友元函數(shù)詳解

2020-01-26 15:57:18
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

#include<iostream>
using namespace std;
class A
{
    int x,y;
    public:
    A(int xx,int yy):x(xx),y(yy){}
    A(){x=0;y=0;}
    A operator+(const A&b) //不加const限定,也可以
    { return A(x+b.x,y+b.y); }
    A operator-()
    { return A(-x,-y); }
    void show()
    {cout<<"x="<<x<<" y="<<y<<endl;}
};
void test_A()
{
    A a1(2008,512),a2(2013,420),a3;
    a3=a1+a2; //調(diào)用操作符重載函數(shù): a1.oprator +(a2)
    a3.show();
    a1=-a1; //調(diào)用操作符重載函數(shù): a1.operator -()
    a1.show();
}
/***********************
執(zhí)行結(jié)果
x=4021 y=93
x=-2008 y=-512
**********************/
class B
{
    int x,y;
    public:
        B(int xx,int yy):x(xx),y(yy){}
        B(){x=0;y=0;}
        friend B operator+(const B&a,const B&b);
        friend B operator-(const B&a);
        void show()
        {cout<<"x="<<x<<" y="<<y<<endl;};
};
B operator+(const B&a,const B&b)
{return B(a.x+b.x,a.y+b.y);}
B operator-(const B&a)
{return B(-a.x,-a.y);}
/***************************
class B
{
    int x,y;
    public:
        B(int xx,int yy):x(xx),y(yy){}
        B(){x=0;y=0;}
        friend B operator+(const B&a,const B&b)
                {return B(a.x+b.x,a.y+b.y);}
        friend B operator-(const B&a)
            {return B(-a.x,-a.y);}
        void show()
        {cout<<"x="<<x<<" y="<<y<<endl;};
}
********************************/
int main()
{
    B B1(1991,1105),B2(2013,62),B3;
    B3=B1+B2; //調(diào)用操作符重載函數(shù): a1.oprator +(a2)
    B3.show();
    B1=-B1; //調(diào)用操作符重載函數(shù): a1.operator +()
    B1.show();
}
/****************************
運(yùn)行結(jié)果:
x=4004 y=1167
x=-1991 y=-1105
Process returned 0 (0x0)   execution time : 0.021 s
Press any key to continue.

*****************************/


復(fù)制代碼 代碼如下:

#include<iostream>
using namespace std;
class A
{
    int x,y;
    public:
    A(int xx,int yy):x(xx),y(yy){}
    A(){x=0;y=0;}
    A operator+(const A&b) //不加const限定,也可以
    { return A(x+b.x,y+b.y); }
    A operator-()
    { return A(-x,-y); }
    void show()
    {cout<<"x="<<x<<" y="<<y<<endl;}
};
void test_A()
{
    A a1(2008,512),a2(2013,420),a3;
    a3=a1+a2; //調(diào)用操作符重載函數(shù): a1.oprator +(a2)
    a3.show();
    a1=-a1; //調(diào)用操作符重載函數(shù): a1.operator -()
    a1.show();
}
/***********************
執(zhí)行結(jié)果
x=4021 y=93
x=-2008 y=-512
**********************/
class B
{
    int x,y;
    public:
        B(int xx,int yy):x(xx),y(yy){}
        B(){x=0;y=0;}
        friend B operator+(const B&a,const B&b);
        friend B operator-(const B&a);
        void show()
        {cout<<"x="<<x<<" y="<<y<<endl;};
};
B operator+(const B&a,const B&b)
{return B(a.x+b.x,a.y+b.y);}
B operator-(const B&a)
{return B(-a.x,-a.y);}
/***************************
class B
{
    int x,y;
    public:
        B(int xx,int yy):x(xx),y(yy){}
        B(){x=0;y=0;}
        friend B operator+(const B&a,const B&b)
                {return B(a.x+b.x,a.y+b.y);}
        friend B operator-(const B&a)
            {return B(-a.x,-a.y);}
        void show()
        {cout<<"x="<<x<<" y="<<y<<endl;};
}
********************************/
int main()
{
    B B1(1991,1105),B2(2013,62),B3;
    B3=B1+B2; //調(diào)用操作符重載函數(shù): a1.oprator +(a2)
    B3.show();
    B1=-B1; //調(diào)用操作符重載函數(shù): a1.operator +()
    B1.show();
}
/****************************
運(yùn)行結(jié)果:
x=4004 y=1167
x=-1991 y=-1105
Process returned 0 (0x0)   execution time : 0.021 s
Press any key to continue.
*****************************/

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 固原市| 永德县| 乌兰浩特市| 汨罗市| 五原县| 林州市| 陇南市| 西和县| 东辽县| 红原县| 凌云县| 晋州市| 洛川县| 嘉祥县| 南江县| 新巴尔虎右旗| 张家界市| 察雅县| 甘洛县| 文水县| 子长县| 四会市| 大庆市| 改则县| 房山区| 兖州市| 塔城市| 应城市| 辽中县| 铜川市| 英超| 子长县| 上饶市| 毕节市| 延安市| 松阳县| 子长县| 大港区| 沈丘县| 张北县| 开化县|