C++運(yùn)算符重載 成員函數(shù)與友元函數(shù)詳解
更新時(shí)間:2013年07月31日 09:47:48 作者:
以下是對(duì)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)文章
C語言位段(位域)機(jī)制結(jié)構(gòu)體的特殊實(shí)現(xiàn)及解析
這篇文章主要為大家介紹了C語言位段位域機(jī)制結(jié)構(gòu)體的特殊實(shí)現(xiàn)講解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-02-02
Visual?studio2022?利用glfw+glad配置OpenGL環(huán)境的詳細(xì)過程
這篇文章主要介紹了Visual?studio2022?利用glfw+glad配置OpenGL環(huán)境,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
C++利用類實(shí)現(xiàn)矩陣的數(shù)乘,乘法以及點(diǎn)乘
這篇文章主要為大家詳細(xì)介紹了C++如何利用類實(shí)現(xiàn)矩陣的數(shù)乘,乘法以及點(diǎn)乘,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++有一定幫助,需要的可以參考一下2022-11-11
C語言實(shí)現(xiàn)文件內(nèi)容按行隨機(jī)排列的算法示例
這篇文章主要介紹了C語言實(shí)現(xiàn)文件內(nèi)容按行隨機(jī)排列的算法,涉及C語言字符串、數(shù)組遍歷與隨機(jī)數(shù)相關(guān)算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
C/C++ 中堆和棧及靜態(tài)數(shù)據(jù)區(qū)詳解
這篇文章主要介紹了C/C++ 中堆和棧及靜態(tài)數(shù)據(jù)區(qū)詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

