Summary of C ++ operatpr heavy load

Source: Internet
Author: User

I am not talking nonsense much. The code is clear. I have been studying C ++ for three years. At that time, I have not paid much attention to many details. Now I have read Objective C ++ and found that I have missed many classic actions. -_-...

Class cpoint {<br/> int X, Y; <br/> Public: <br/> cpoint (int x, int y) <br/> {<br/> // cout <"constructer be call" <Endl; <br/> This-> X = X; <br/> This-> Y = y; <br/>}< br/> cpoint () {<br/> x = 1; <br/> Y = 1; <br/>}< br/> void operator ++ () // return value shocould be cpoint if u want to use (++ point ). display () <br/>{< br/> X ++; <br/> Y ++; <br/>}< br/> friend cpoint operator ++ (cpoint & point, INT) <br/>{< br/> cpoint te MP = point; <br/> point. X ++; <br/> point. Y ++; <br/> return temp; <br/>}< br/> cpoint & operator -- () <br/>{< br/> X --; <br/> Y --; <br/> return * This; <br/>}< br/> cpoint * operator -- (INT) <br/>{< br/> cpoint * temp = new cpoint (x, y); // stack memory, it will not be free until you delete or program exit <br/> // call New get memory first, and then call the constructor <br/> X --; <br/> Y --; <br/> return temp; <br/>}< br /> Cpoint operator + (cpoint point) // (point + point2 ). display () to do that, you must return cpoint <br/>{< br/> cpoint temp (x, y); <br/> temp. X + = point. x; <br/> temp. Y + = point. y; <br/> return temp; <br/>}< br/> cpoint & operator + = (cpoint point) <br/> {<br/> X + = point. x; <br/> Y + = point. y; <br/> return * This; <br/>}< br/> cpoint operator = (cpoint & Point) // can not be extend. no friend, no victurl <br/>{< br/> X + = Point. x; <br/> Y + = point. y; <br/> return * This; // return for method (point = point + point2 ). display (); <br/>}< br/> operator double () // with out return value, no friend <br/>{< br/> return (double) x/y; <br/>}</P> <p> void * operator new (size_t size) // must return void *, when you use new cpoint (X, y), it will be call. <br/>{< br/> cout <"Size:" <size <Endl; <br/> // return malloc (size ); // they need memory <br/> re Turn: Operator new (size); <br/>}< br/> void * operator new [] (size_t size) <br/> {<br/> cout <"new [] size:" <size <Endl; <br/> return: Operator new (size ); <br/>}< br/> void * operator new (size_t size, int num1, char * Str) <br/>{< br/> cout <"Num: "<num1 <" str: "<STR <Endl; <br/> return malloc (size ); <br/>}< br/> void operator Delete (void * P, size_t size) <br/> {<br/> cout <"Call Delete" <size <Endl; <br/> // fr EE (p );. <br/>: Operator Delete (p); <br/>}< br/> void operator Delete (void * P, int num1, char * Str) //???? <Br/>{< br/> cout <"Call Delete 2" <Endl; <br/> free (P ); <br/>}< br/> void operator Delete [] (void * P, size_t size) <br/> {<br/> cout <"Call Delete []" <size <Endl; <br/>: Delete [] P; // It will delete all the array <br/>}< br/> void operator [] (INT index) <br/>{< br/> cout <index <Endl; <br/>}< br/> ~ Cpoint () <br/>{< br/> // cout <"Call destructer" <Endl; <br/>}< br/> void display () <br/> {<br/> cout <"X:" <x <"Y:" <Y <Endl; <br/>}</P> <p >}; </P> <p> class cpointchilden: Public cpoint {<br/> int Z; <br/> public: <br/> cpointchilden (int x, int y, int Z): cpoint (x, y) <br/>{< br/> This-> Z = z; <br/>}< br/>}; </P> <p> int main (INT argc, char * argv []) <br/>{< br/> cpoint point (1, 1); <br/> point. display (); <br/> (point ++ ). display (); <br/> point. display (); <br/> (point --)-> display (); // Memory leakage <br/> point. display (); <br/> (-- point ). display (); </P> <p> cpoint point2 (1, 2); <br/> point = point + point2; // (point + point2 ). display (); <br/> point. display (); </P> <p> cpointchilden point3 (7,8, 9); <br/> point3.display (); <br/> point3 ++; </P> <p> point3.display (); <br/> float num = point3; // It will use double () Firstly, and use (float ); <br/> cout <num <Endl; </P> <p> cpoint * point4 = new cpoint (3, 4); <br/> Delete point4; // Delete [] point4; Thay are different </P> <p> cpoint * point5 = new (0, "Hello world. ") cpoint (5, 6); <br/> Delete point5; </P> <p> cpoint * pointarray = new cpoint [3]; <br/> Delete [] pointarray; // ###### </P> <p> cout <Endl; <br/> cpoint pointarray2 [2]; // without call New or new []; <br/> pointarray2 [1] [2]; // 2 call operator [], you can also to do point2.operator [] (2 ); <br/> pointarray2 [1]. operator ++ (); // But operator ++, can not work <br/> pointarray2 [1]. display (); <br/> return 0; <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.