Example of an operator overload program in C ++

Source: Internet
Author: User
Heavy Load +, front --, ++; Rear ++; heavy load <operator <br/> # include <iostream> <br/> using namespace STD; <br/> class distance <br/>{< br/> PRIVATE: <br/> int feet; <br/> float inches; <br/> public: <br/> distance (): feet (0), inches (0.0) {}< br/> distance (INT ft, float in): feet (FT ), inches (in) {}< br/> void getdist () <br/>{< br/> cout <"Enter feet :"; <br/> CIN> feet; <br/> cout <"Enter inches:"; <br/> CIN> inches; <br/>}< br/> void showgist () Const <br/>{< br/> cout <feet <"--" <inches <Endl; <br/>}< br/> distance operator-(distance D2) // heavy load-operator <br/>{< br/> int F = feet-d2.feet; <br/> float I = inches-d2.inches; <br/> return distance (F, I); <br/>}< br/> distance operator + (distance D2) // overload + operator <br/>{< br/> int F = feet + d2.feet; <br/> float I = inches + d2.inches; <br/> return distance (F, I); <br/>}< br/> void operator --() // overload -- prefix operator number <br/>{< br/> -- Feet; <br/> -- inches; <br/>}< br/> distance operator ++ () // overload + + prefix operator number <br/> {<br/> + + feet; <br/> + + inches; <br/> return distance (feet, inches); <br/>}< br/> distance operator ++ (INT) // The overload ++ post operator number Int Is only a post sign <br/>{< br/> feet ++; <br/> inches ++; <br/> return distance (feet, inches); <br/>}< br/> bool operator <(distance D2) // overload <operator <br/>{< br/> float bf1 = feet + inches/12; <br/> float bf2 = d2.feet + d2.inches/ 12; <br/> return (bf1 <bf2 )? True: false; <br/>}< br/> void operator + = (distance D2) // overload + = Operator <br/>{< br/> feet + = d2.feet; <br/> inches + = d2.inches; <br/> If (INCHES> 12.0) <br/>{< br/> inches-= 12.0; <br/> feet ++; <br/>}</P> <p >}; <br/> int main () <br/>{< br/> distance dist1, dist2, dist3, dist4, dist5, dist6, dist7; <br/> cout <"set dist1 value:" <Endl; <br/> dist1.getdist (); <br/> cout <"set dist2 value:" <Endl; <br/> dist2.getdist (); <br/> dist3 = dist1-dist2; //-Use of operator numbers <br/> dist4 = dist1 + dist2; // + use of operator numbers <br/> -- dist1; <br/> dist5 = ++ dist1; <br/> dist6 = dist2 ++; <br/> dist7 + = dist1; <br/> dist1 + = dist2; <br/> cout <"display result:" <Endl; <br/> cout <"dist1 ="; <br/> dist1.showgist (); <br/> cout <"dist2 ="; <br/> dist2.showgist (); <br/> cout <"dist3 ="; <br/> dist3.showgist (); <br/> cout <"dist4 ="; <br/> dist4.showgist (); <br/> cout <"dist5 ="; <br/> dist5.showgist (); <br/> cout <"dist6 ="; <br/> dist6.showgist (); <br/> If (dist2 <dist1) <br/> cout <"dist2 less than dist1" <Endl; <br/> cout <"dist7 ="; <br/> dist7.showgist (); <br/> return 0; <br/>}< br/>

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.