C + + operator overloading simple practice writing an integer wrapper class

Source: Internet
Author: User

Operator.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <string>using namespace Std;class integer{public:// Explicit if you add explicit, you cannot implicitly construct//implicitly construct does not cause the copy constructor call integer (int num): m_num (num) {}//Overwrite the copy constructor must value the member of the Copy object    A deep copy is also required, such as reallocating memory Integer (const integer&integer) {m_num=integer.m_num; cout<< "CPY construction FUNCTION called" &LT;&LT;ENDL;} Suffix Form//If the suffix form so-shaped participant is 0void operator++ (int i) {this->m_num=m_num+1;} Prefix form void operator++ () {this->m_num++;} Front--void operator--() {this->m_num--;} suffix form void operator--(int i) {this->m_num=m_num-1;} Overload + If you accept not Integer&integerref then the copy constructor will still trigger the integer operator+ (integer &integerref) {return integer (m_num+ Integerref.m_num);} Integer operator+ (int i) {return m_num+i;} Integer operator-(int i) {return m_num-i;} Overload =void operator= (int i) {m_num=i;} Overload + =-=void operator+= (int i) {m_num+=i;} void operator-= (int i) {m_num-=i;} return value int Toval () {return m_num;} function return assignment causes the copy construct to call the integer GeTobj () {return *this;}        Convert to a string, Const. String ToString () {char buf[10]= ""; _itoa_s (m_num,buf,10); return string (BUF);} Copy myself//only triggers a copy construct or creates an object inside the function body and takes the object as a return value to trigger the copy constructor call//If return Integer (M_num) only when a new object is returned or when a new object is created by another object initialization; is the//functor that does not trigger the copy construct integer operator () () {return *this;} Overload standard input standard output standard error output for iOS streaming class library So we can only declare << >> insert extract operator as friend function friend istream& operator >> (istream& is,integer&intobj); Friend ostream& operator << (ostream&os,integer&intobj);//Extract data from object to variable < < >> operator overload void operator>> (int &i) {i=m_num;} Output to object void operator<< (int i) {m_num=i;} Private:int m_num;protected:};//Reload IO istream& operator >> (istream&is,integer&intobj) {is>> Intobj.m_num; return is;} ostream& operator << (ostream&os,integer&intobj) {os<<intobj.m_num; return OS;} int _tmain (int argc, _tchar* argv[]) {//implicitinteger Integerobj1=1;integer Integerobj2=2;//integerobj1=integerObj2; The copy constructor is not triggered by Integer obj=integerobj1 to trigger the copy construction call//equivalent to integerobj++0integerobj1++;//equivalent to ++x++integerobj1; cout<< "integerobj1=" <<integerobj1.toval () <<endl;integer intobjcmb=integerobj1+integerobj2; cout<< "intobjcmb=" <<intobjcmb.toval () <<endl;integer w=integerobj1;cout<< "w=" <<w. ToString () <<endl;w+=2;cout<< "w+2=" <<w.tostring () <<endl;cout<< "Copy w=" <<w () . ToString () <<endl;//overloads the standard input and output .....w=125;cout<< "w=" <<w<<endl;int i;w>>i;cout< < "i=w=" <<w<<endl;w<<1000;cout<< "w=" &LT;&LT;W&LT;&LT;ENDL; w>>i;cout<< "i= W= "&LT;&LT;W&LT;&LT;ENDL; return 0;}

C + + operator overloading simple practice writing an integer wrapper class

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.