C + + overloaded operator (i) how to overload an operator __c++

Source: Internet
Author: User
Tags what operator

Just learn C + + students are always heard about what operator overload, as if very advanced appearance, then Hey Hei.

Now that we have a time class, we're going to overload the subtraction operator so that the computation between the time classes is the same as the numeric subtraction operation.

First, let's start with the simplest:

Time class:

then the function is implemented: (we only post the implementation of the constructor and sum functions here, because the rest is not available)


Finally, we define the Cmytime class in the main function to add:

after the operation is complete we look at the output:


so the play comes, we change the sum of the time class to operator+,

in the main function

coding. Sum (fixing) changed to coding + fixing;




OK, this is the simplest operator overload, and I'll introduce other knowledge of operator overloading in the next article posting.

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.