欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

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

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

復(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.
*****************************/

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有