Class Notes (809,487)
Canada (493,752)
CMPT 225 (60)
John Edgar (28)
Lecture 2

CMPT 225 Week 2 Lecture 1

4 Pages
Unlock Document

Simon Fraser University
Computing Science
CMPT 225
John Edgar

Write a new copy constructor that will create a deep copy MyStack(const MyStack & st); // copy constructor Need to give it an object of the same type as the type I'm building const means that the parameter I'm passing can't change - copy constructor can't accidentally destroy thing I am making a copy of & passes by reference (not making a copy when we pass it to copy constructor, just giving it address) In .cpp: MyStack::Mystack(const MyStack & st) { n = st.n; top =; // don't want arr = st.arr; -> copies address of array in dynamic memory, no new array arr = new float[n]; // creates new array for (int i = 0; i < top; i++) { arr[i] = st.arr[i]; // copies over old contents } } Haven't yet implemented destructor Resposible for cleaning up any dynamic memory associated with an object Much like in push, want to delete array so we don't cause a memory leak. Never explicitly call a destructor, they are called for you. MyStack::~MyStack(void) { delete[] arr; } MyStack stack123() { MyStack result; result.push(1); result.push(2); result.push(3); return result; } Basically just example of writing a function that will return a stack. void assignTest() { MyStack st = stack123(); cout << st.pop() << endl; cout << st.pop() << endl; cout << st.pop() << endl; } Works! But, if we change it to: MyStack st; st = stack123(); It doesn't work and just spits out garbage values!Also VS thinks you're doing something bad. In first example, actually ends up calling copy constructor. Takes that as shortcut, as other way is more complicated. End up creating a default stack first. Assigns an existing stack (the one you returned) to the stack you just built. Can't call copy constructor. How about: MyStack st; MyStack st1; st1.push(1); st1.push(2); st1.push(3); st = st1; Gives error, but works! result is a stack that is local to that function (stack123) Because you don't need the function any more when the function ends, it calls the destructor to clean it up. So, you have deleted result's arr, and you have nothing to assign to the new one. Back to original problem! If we comment out delete[] arr, it works. Moral is not don't write destructors. Want to write, just fix so it still works. Need to make assignment work correctly. Going to overload operator. Can't do in Java. Language digression: int y = z = 1; int x = y + z; // or plus(y, z); + is operator int plus(int a, int b) { return a + b; } Cannot create operators for which there are not existing symbols. But, can create new versions of existing operators. So, we can overload the assignment operator. Can tell operating system what we want to do when we assign one stack to another one. Can be abused horribly, don't make incomprehensible code! In .h: // going to return a reference to a stack // just name operator whatever the operator is MyStack & operator= (const MyStack & st); In .cpp: MyStack & MyStack::operator= (const MyStack & st) { // copied from copy constructor // delete[] arr;
More Less

Related notes for CMPT 225

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.