c++--class operator overloading

Source: Internet
Author: User
Tags class operator

//operator Overloading//overriding rules need to satisfy the rules of the operator itself    classCmypoint {intx, y;  Public: Cmypoint (); Cmypoint (intXinty); ~Cmypoint (); Cmypointoperator+ (CmypointConst& PT)Const;//The value of this cannot be modifiedFriend Cmypointoperator-(CmypointConst&AMP;P1, CmypointConst&p2); Cmypoint&operator++();//front, because is self, so to add &Cmypointoperator++(int);//+ + post, int is the identifier is not used & is because, first assignment in the operation, assignment is to produce a temporary variable firstfriend Ostream&operator<< (ostream& OS, cmypointConst& PT);//friend because in the input output, the first parameter is not this, but the input output streamFriend istream&operator>> (istream& is, Cmypoint &PT);        }; Cmypoint cmypoint::operator+ (CmypointConst& PT)Const{cmypoint temppt; Temppt.x= This->x +Pt.x; Temppt.y= This->y +Pt.y; returntemppt; } myvectoroperator-(Myvector I, Myvector j) {returnMyvector (i.x-j.x, i.y-j.y); } cmypoint& Cmypoint::operator++()    {        (* This). x + +;  This->y++; return* This; } cmypoint Cmypoint::operator++(int) {Cmypoint temppt= * This;  This->x++;  This->y++; returntemppt; } ostream&operator<< (ostream& OS, cmypointConst&PT) {OS<< Pt.x <<'\ t'<< Pt.y <<Endl; returnOS; } IStream&operator>> (istream& is, Cmypoint &PT) {         is>>Pt.x;  is>>Pt.y; return  is; } cmypointoperator-(CmypointConst& P1, CmypointConst&p2)        {Cmypoint temppt; Temppt.x= p1.x-p2.x; Temppt.y= P1.y-p2.y; returntemppt; }

c++--class operator overloading

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.