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