Big Three in C ++

Source: Internet
Author: User

Define an ordered array class to illustrate this problem:

# Include <iostream> using namespace STD; # define maxsize 20 Class A {public: A (void); // const &); // copy constructor A & operator = (const A &); // overload the value assignment operator ~ A () {// destructor cout <"destructor" <Endl;} void display (void); int size (void); bool isempty (void ); bool isfull (void); void insert (double); void Delete (double); Private: Double arr [maxsize]; int length ;}; A: A (void) // constructor {cout <"constructor" <Endl; length = 0; memset (ARR, 0, maxsize * sizeof (double);} :: A (const A & OBJ) // copy constructor {cout <"copy constructor" <Endl; For (INT I = 0; I <obj. length; I ++) {arr [I] = obj. arr [I];} length = o BJ. length;} A & A: Operator = (const A & OBJ) // overload assignment operator {cout <"overload assignment operator" <Endl; if (this = & OBJ) return * this; for (INT I = 0; I <obj. length; I ++) {arr [I] = obj. arr [I];} length = obj. length; return * This;} void a: Display (void) {If (isempty () {cout <"blank" <Endl; return ;} for (INT I = 0; I <length; I ++) cout <arr [I] <Endl; cout <Endl;} int A: size (void) {return length;} bool a: isempty (void) {return (Le Ngth <= 0? True: false);} bool a: isfull (void) {return (length> = maxsize? True: false);} void a: insert (Double X) // you can insert duplicate elements {If (isfull () {cout <"full! "<Endl; return;} For (INT I = 0; I <length; I ++) {If (x <arr [I]) {// locate the position to be inserted. Double * P = arr + length before inserting it into arr [I; // P points to the last position of the last element, int COUNT = length-I; // sets the ARR [I] and its following elements (total length-I) overall backward translation while (count --) {* P = * (p-1); // forward from the back to p --;} arr [I] = X; // insert X. In this case, P = arr + ilength ++; return ;}} arr [length] = x; // If X is larger than all elements in the original array, inserted in the last length ++;} void a: delete (Double X) {for (INT I = 0; I <length; I ++) {If (x = arr [I]) {for (Int J = I; j <length-1; j ++) arr [J] = arr [J + 1]; Length --; return ;}} if (isempty () cout <"null" <Endl; elsecout <"cannot find" <x <Endl ;}a fun (void) {cout <"Enter the fun () function body" <Endl; a obj; OBJ. insert (20); obj. insert (30); cout <"before return" <Endl; return OBJ; // copy constructor of the temporary object OBJ of OBJ // constructor cout of OBJ <"return" <Endl; // not executed} int main (void) {double _ arr [] = {2, 5, 10, 6, 8, 4, 1, 3, 9, 7}; int _ Len = sizeof (_ ARR)/sizeof (* _ ARR ); cout <"my" <Endl; A myobj; For (INT I = 0; I <_ Len; I ++) {myobj. insert (_ arr [I]);} myobj. display (); cout <"your" <Endl; A yourobj (myobj); yourobj. display (); cout <"his" <Endl; A hisobj; hisobj = yourobj; hisobj. display (); cout <"her" <Endl; A herobj = hisobj; hisobj. display (); cout <"call fun () before" <Endl; hisobj = fun (); // OBJ 'destructor cout <"call fun () after "<Endl; hisobj. display (); Return 0 ;}

Running result:

My constructor 12345678910your copy constructor 12345678910his constructor overload assignment operator 12345678910her copy constructor 12345678910 call fun () forward into fun () copy the constructor destructor before the function body constructor return heavy-duty assignment operator the constructor calls the post-fun () 2030 destructor the constructor

P.s. Please pay close attention to the situation when the class contains pointer object members !!!

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.