[Objective C ++ Reading Notes] 0012 _ do not forget each part of an object when copying it

Source: Internet
Author: User

The first program:

1 # include <iostream> 2 using namespace STD; 3 4 class demo {5 public: 6 demo (INT value); // default const 7 demo (const demo & RHs ); // copy the constructor 8 demo & operator = (const demo & RHs); // reload the value assignment operator 9 void printinfo (); // print the member value function 10 private: 11 int m_value; 12}; 13 14 Demo: Demo (INT value) {15 This-> m_value = value; 16} 17 18 Demo: Demo (const demo & RHs): m_value (RHS. m_value) {19 20} 21 22 demo & Demo: Operator = (const demo & RHs) {23 This-> m_value = RHS. m_value; 24 return * This; 25} 26 27 void Demo: printinfo () {28 cout <"object address:" <This <Endl; 29 cout <"m_value =" <this-> m_value <Endl; 30 cout <"----------------------------" <Endl; 31} 32 33 int main () 34 {35 demo D1 (10); // call the constructor 36 d1.printinfo (); 37 38 demo D2 (D1); // call the copy constructor 39 d2.printinfo (); 40 41 demo D3 (0); 42 D3 = D1; // call the overload assignment operator 43 d3.printinfo (); 44 45 system ("pause"); 46 Return 0; 47}

Output result:

Object address: 001af9c4
M_value = 10
----------------------------
Object address: 001af9b8
M_value = 10
----------------------------
Object address: 001af9ac
M_value = 10
----------------------------
Press any key to continue...

Procedure 2:

1 # include <iostream> 2 using namespace STD; 3 4 class demo {5 public: 6 demo (INT value, char ch ); // default const 7 demo (const demo & RHs); // copy constructor 8 demo & operator = (const demo & RHs ); // reload the value assignment operator 9 void printinfo (); // print the member value function 10 private: 11 int m_value; 12 char m_ch; // note that I append a member 13} here }; 14 15 Demo: Demo (INT value, char ch) {16 this-> m_value = value; 17 this-> m_ch = CH; 18} 19 20 Demo :: demo (const demo & RHs): m_value (RHS. m_value) {21 22} 23 24 demo & Demo: Operator = (const demo & RHs) {25 this-> m_value = RHS. m_value; 26 return * This; 27} 28 29 void Demo: printinfo () {30 cout <"object address:" <This <Endl; 31 cout <"m_value =" <this-> m_value <Endl; 32 cout <"m_name =" <this-> m_ch <Endl; 33 cout <"--------------------------" <Endl; 34} 35 36 int main () 37 {38 demo D1 (10, 'z '); // call the constructor 39 d1.printinfo (); 40 41 demo D2 (D1); // call the copy constructor 42 d2.printinfo (); 43 44 demo D3 (0, 'A'); 45 D3 = D1; // call the overload assignment operator 46 d3.printinfo (); 47 48 system ("pause"); 49 return 0; 50}

Output result:

Object address: 0017fcf4m_value = 10m_name = z jsonobject address: 0017fce4m_value = 10m_name = jsonobject address: 0017fcd4m_value = 10m_name = A ---------------------------- press any key to continue...

Section 3 program:

1 # include <iostream> 2 using namespace STD; 3 4 class base {5 public: 6 base (): B _value (0) {} 7 base (INT value ): B _value (value) {} 8 protected: 9 int B _value; 10}; 11 12 class Demo: Base {13 public: 14 demo (INT value ); // default constructor 15 demo (const demo & RHs); // copy constructor 16 demo & operator = (const demo & RHs ); // reload the value assignment operator 17 void printinfo (); // print the member value function 18 private: 19 int m_value; 20}; 21 22 Demo: Demo (INT value) {23 This-> m_value = value; 24 This-> B _value = value; 25} 26 27 Demo: Demo (const demo & RHs): m_value (RHS. m_value) {28 29} 30 31 demo & Demo: Operator = (const demo & RHs) {32 This-> m_value = RHS. m_value; 33 return * This; 34} 35 36 void Demo: printinfo () {37 cout <"object address:" <This <Endl; 38 cout <"m_value =" <this-> m_value <Endl; 39 cout <"B _value =" <this-> B _value <Endl; 40 cout <"--------------------------" <Endl; 41} 42 43 int main () 44 {45 demo D1 (10); // call the constructor 46 d1.printinfo (); 47 48 demo D2 (D1); // call the copy constructor 49 d2.printinfo (); 50 51 demo D3 (0); 52 D3 = D1; // call the overload assignment operator 53 d3.printinfo (); 54 55 system ("pause"); 56 return 0; 57}

Output result:

Object address: 002cfb6cm_value = 10b_value = 10 bytes object address: 002cfb5cm_value = 10b_value = 0 bytes object address: 002cfb4cm_value = 10b_value = 0 bytes press any key to continue...

Procedure 4:

1 # include <iostream> 2 using namespace STD; 3 4 class base {5 public: 6 base (): B _value (0) {} 7 base (INT value ): B _value (value) {} 8 base (const base & RHs) {9 This-> B _value = RHS. B _value; 10} 11 base & operator = (const base & RHs) {12 this-> B _value = RHS. B _value; 13 return * This; 14} 15 int getvalue () {16 return this-> B _value; 17} 18 19 private: 20 int B _value; 21}; 22 23 class Demo: base {24 public: 25 demo (INT value); // default constructor 26 demo (const demo & RHs ); // copy the constructor 27 demo & operator = (const demo & RHs); // reload the value assignment operator 28 void printinfo (); // print the member value function 29 private: 30 int m_value; 31}; 32 33 demo: Demo (INT value) {34 This-> base: Base (value); 35 This-> m_value = value; 36 // This-> B _value = value; no37} 38 39 Demo: Demo (const demo & RHs): m_value (RHS. m_value) {40 // base: Base (RHs); no41 this-> base: Base (RHs); 42} 43 44 demo & Demo :: operator = (const demo & RHs) {45 this-> m_value = RHS. m_value; 46 // base: Operator = (RHs); yes47 this-> base: Operator = (RHs); 48 return * This; 49} 50 51 void Demo :: printinfo () {52 cout <"object address:" <This <Endl; 53 cout <"m_value =" <this-> m_value <Endl; 54 cout <"B _value =" <this-> getvalue () <Endl; 55 cout <"----------------------------" <Endl; 56} 57 58 int main () 59 {60 demo D1 (10); // call the constructor 61 d1.printinfo (); 62 63 demo D2 (D1); // call the copy constructor 64 d2.printinfo (); 65 66 demo D3 (0); 67 D3 = D1; // call the overload assignment operator 68 d3.printinfo (); 69 70 System ("pause"); 71 return 0; 72}

Output result:

Object address: 0041fadcm_value = 10b_value = 10 ---------------------------- object address: 0041faccm_value = 10b_value = 10 bytes object address: 0041fabcm_value = 10b_value = 10 bytes press any key to continue...

Section 5 procedure:

1 # include <iostream> 2 using namespace STD; 3 4 class base {5 public: 6 base (): B _value (0) {} 7 base (INT value ): B _value (value) {} 8 base (const base & RHs) {9 Init (RHs); 10} 11 base & operator = (const base & RHs) {12 Init (RHs); 13 return * This; 14} 15 int getvalue () {16 return this-> B _value; 17} 18 19 private: 20 int B _value; 21 void Init (const base & RHs) {22 This-> B _value = RHS. B _value; 23} 24}; 25 26 class Demo: Base {27 public: 28 demo (INT value); // default const 29 demo (const demo & RHs ); // copy the constructor 30 demo & operator = (const demo & RHs); // reload the value assignment operator 31 void printinfo (); // print the member value function 32 private: 33 int m_value; 34 void Init (const demo & RHs) {35 This-> m_value = RHS. m_value; 36 This-> base: Base (RHs); 37} 38}; 39 40 Demo: Demo (INT value) {41 This-> base :: base (value); 42 This-> m_value = value; 43} 44 45 Demo: Demo (const demo & RHs) {46 Init (RHs ); 47} 48 49 demo & Demo: Operator = (const demo & RHs) {50 Init (RHs); 51 return * This; 52} 53 54 void Demo: printinfo () {55 cout <"object address:" <This <Endl; 56 cout <"m_value =" <this-> m_value <Endl; 57 cout <"B _value =" <this-> getvalue () <Endl; 58 cout <"----------------------------" <Endl; 59} 60 61 int main () 62 {63 demo D1 (10); // call the constructor 64 d1.printinfo (); 65 66 demo D2 (D1); // call the copy constructor 67 d2.printinfo (); 68 69 demo D3 (0); 70 D3 = D1; // call the overload assignment operator 71 d3.printinfo (); 72 73 system ("pause"); 74 return 0; 75}

Output result:

Object address: 003dfb6cm_value = 10b_value = 10 ---------------------------- object address: 003dfb5cm_value = 10b_value = 10 bytes object address: 003dfb4cm_value = 10b_value = 10 bytes press any key to continue...

 

 

 

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.