C++運(yùn)算符重載 成員函數(shù)與友元函數(shù)詳解
#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.
*****************************/
#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.
*****************************/
相關(guān)文章
Visual Studio 2019 如何新建 Win32項(xiàng)目的方法步驟
這篇文章主要介紹了Visual Studio 2019 如何新建 Win32項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03對(duì)for循環(huán)中表達(dá)式和循環(huán)體的執(zhí)行順序詳解
今天小編就為大家分享一篇對(duì)for循環(huán)中表達(dá)式和循環(huán)體的執(zhí)行順序詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06C/C++百行代碼實(shí)現(xiàn)熱門(mén)游戲消消樂(lè)功能的示例代碼
這篇文章主要介紹了C/C++百行代碼實(shí)現(xiàn)熱門(mén)游戲消消樂(lè)功能的示例代碼,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07C語(yǔ)言中 int main(int argc,char *argv[])的兩個(gè)參數(shù)詳解
這篇文章主要介紹了C語(yǔ)言中 int main(int argc,char *argv[])的兩個(gè)參數(shù)詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03C++?分割字符串?dāng)?shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C++?分割字符串?dāng)?shù)據(jù)的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09C++指針數(shù)組、數(shù)組指針、數(shù)組名及二維數(shù)組技巧匯總
這篇文章主要介紹了C++指針數(shù)組、數(shù)組指針、數(shù)組名及二維數(shù)組技巧匯總,對(duì)于深入理解C++數(shù)組與指針來(lái)說(shuō)非常重要,需要的朋友可以參考下2014-08-08C語(yǔ)言動(dòng)態(tài)順序表實(shí)例代碼
大家好,本篇文章主要講的是C語(yǔ)言動(dòng)態(tài)順序表實(shí)例代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽2021-12-12