EECS
Lec20CopyCtorsDtorsRevisit_6per.pdf

# Lec20CopyCtorsDtorsRevisit_6per.pdf - EECS 402 Problem...

• 5

This preview shows pages 1–2. Sign up to view the full content.

1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 20 Revisiting Use Of: Copy Constructors Assignment Operators Destructors EECS 402 EECS 402 Andrew M Morgan 2 Problem Description Recall: Copy ctors are used when a copy of an object needs to be made (most often via a pass-by-value parameter) Assignment operators are used to assign one object's attributes to another object's Both of these do a "member-by-member copy" Algorithm: For each data member of the right-hand-side object, copy its value into the corresponding member of the left-hand-side object If the member is an integer, the 4 bytes containing the int are copied If the member is a double, the 8 bytes containing the double are copied If the member is a pointer, the 4 bytes containing the pointer are copied NOTE: Only the 4 bytes of the pointer are copied, even if the pointer is pointing to an array of 100 values! EECS 402 EECS 402 Andrew M Morgan 3 A Dynamic Array Class class DynAryClass { private: int num; char *vals; //Will pt to //array of chars public: void setVals(char a, char b, char c) { num = 3; vals = new char[num]; vals[0] = a; vals[1] = b; vals[2] = c; } void setVals(char a, char b, char c, char d) { num = 4; vals = new char[num]; vals[0] = a; vals[1] = b; vals[2] = c; vals[3] = d; } void changeVal(int index, char c) { if (index >= 0 && index < num) { vals[index] = c; } else { cout << "Out of range" << endl; } } void printInfo() const { int i; for (i = 0; i < num; i++) { cout << vals[i]; } cout << endl; } }; EECS 402 EECS 402 Andrew M Morgan 4 Use Of The DynAryClass, p.1 1000 da1.num 1018 1001 1019 1002 1020 1003 1021 1004 da1.vals 1022 1005 1023 1006 1024 1007 1025 1008 da2.num 1026 1009 1027 1010 1028 1011 1029 1012 da2.vals 1030 1013 1031 1014 1032 1015 1033 1016 1034 1017 1035 int main() { DynAryClass da1; DynAryClass da2; da1.setVals('d', 'r', 'e', 'w'); cout << "da1: "; da1.printInfo(); da2 = da1; cout << "da2 after assigning to da1: "; da2.printInfo(); cout << "Change da1 drew => draw" << endl; da1.changeVal(2, 'a'); cout << "da1: "; da1.printInfo(); cout << "da2: "; da2.printInfo(); return (0); } Notes: Red arrow indicates which instructions have been executed Purple filled cells indicate that memory has been allocated by the program for its use EECS 402 EECS 402 Andrew M Morgan 5 Use Of The DynAryClass, p.2 1000 da1.num 1018 1001 1019 1002 1020 1003 1021 1004 da1.vals 1022 1005 1023 1006 1024 1007 1025 1008 da2.num 1026 1009 1027 1010 1028 1011 1029 1012 da2.vals 1030 1013 1031 1014 1032 1015 1033 1016 1034 1017 1035 int main() { DynAryClass da1; DynAryClass da2; da1.setVals('d', 'r', 'e', 'w'); cout << "da1: "; da1.printInfo(); da2 = da1; cout << "da2 after assigning to da1: "; da2.printInfo(); cout << "Change da1 drew => draw" << endl; da1.changeVal(2, 'a'); cout << "da1: "; da1.printInfo(); cout << "da2: "; da2.printInfo(); return (0); } 1032 4 Notes: Red arrow indicates which instructions have been executed Purple filled cells indicate that memory has been allocated by the program for its use d r e w EECS 402 EECS 402 Andrew M Morgan 6 Use Of The DynAryClass, p.3 1000 da1.num 1018 1001 1019 1002 1020 1003 1021 1004 da1.vals 1022 1005 1023 1006 1024 1007 1025 1008 da2.num 1026 1009 1027 1010 1028 1011 1029 1012 da2.vals 1030 1013 1031 1014 1032 1015 1033 1016 1034 1017 1035 int main() { DynAryClass da1; DynAryClass da2; da1.setVals('d', 'r', 'e', 'w'); cout << "da1: "; da1.printInfo(); da2 = da1; cout << "da2 after assigning to da1: "; da2.printInfo(); cout << "Change da1 drew => draw" << endl; da1.changeVal(2, 'a'); cout << "da1: "; da1.printInfo(); cout << "da2: "; da2.printInfo(); return (0); } 1032 4 Notes: Red arrow indicates which instructions have been executed

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern