Operator usage in C ++

Source: Internet
Author: User

Operator in C ++ has two usage methods: Operator Overloading operator overload,
One is implicit conversion of operator casting operations.

# Include <iostream> using namespace STD; Class num {protected: int M_a; int m_ B; public: num (int A, int B): M_a (A), m_ B (B) {} num operator + (const num & N) {This-> M_a + = n. m_a; this-> m_ B + = n. m_ B; return * This;} operator bool () {If (this-> M_a = 0 & this-> m_ B = 0) return false; return true ;} /* the input and output operators can only be defined as friend functions */friend ostream & operator <(ostream & OS, num & N) ;}; ostream & operator <(ostream & OS, num & N ){ OS <n. m_a <"," <n. m_ B <Endl; return OS;} int _ tmain (INT argc, _ tchar * argv []) {num A (1, 2); cout <; num B (3, 4); cout <B; A = a + B; cout <A <Endl; if (a) {cout <"! = 0 "<Endl;} return 0 ;}

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.