C + + operator overloaded member functions and friend functions

Source: Internet
Author: User
Tags continue
The following is an introduction to the C + + operator overloaded member functions and friend functions, the need for friends can come over the reference copy code code as follows:


#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)//without the const limit, you can also


{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; Call operator overloaded function: A1.oprator + (A2)


a3.show ();


a1=-a1; Call operator overloaded function: A1.operator-()


a1.show ();


}


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


Execution Results


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; Call operator overloaded function: A1.oprator + (A2)


b3.show ();


b1=-b1; Call operator overloaded function: A1.operator + ()


b1.show ();


}


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


Run Result:


x=4004 y=1167


x=-1991 y=-1105


Process returned 0 (0x0) execution time:0.021 s


Press any key to continue.

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

Copy Code code as follows:




#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)//without the const limit, you can also


{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; Call operator overloaded function: A1.oprator + (A2)


a3.show ();


a1=-a1; Call operator overloaded function: A1.operator-()


a1.show ();


}


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


Execution Results


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; Call operator overloaded function: A1.oprator + (A2)


b3.show ();


b1=-b1; Call operator overloaded function: A1.operator + ()


b1.show ();


}


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


Run Result:


x=4004 y=1167


x=-1991 y=-1105


Process returned 0 (0x0) execution time:0.021 s


Press any key to continue.


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.