{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture4CS32Nachenburg

lecture4CS32Nachenburg - 1 M onday J anuar y 11 Resour ce M...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Monday, January 11 th Resource Management, Part 2 Assignment Operators Basic Linked Lists Insertion, deletion, traversals, etc. Advanced Linked Lists Tail Pointers Doubly-linked Lists Bonus : Enter the Project Possibility Contest and Win and get a 1/ 3 rd raise in your grade for CS32!
Image of page 1

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

View Full Document Right Arrow Icon
2 Time for your favorite game! Programming Language Inventor Or Serial Killer See if you can guess who uses a keyboard and who uses a chainsaw!
Image of page 2
3 The Assignment Operator We just learned how to initialize the value of a new class variable to the value of an existing variable . main() { Circ foo(1,2,3); Circ bar(4,5,6); bar = foo ; } Now lets learn how to set the value of an existing variable to the value of an another existing variable . In this example, both foo and bar have been constructed. Both have had their member variables initialized. Then we set bar equal to foo .
Image of page 3

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

View Full Document Right Arrow Icon
4 The Assignment Operator main() { Circ foo(1,2,3); Circ bar(4,5,6); bar = foo ; } In this case, the copy constructor is NOT used to copy values from foo to bar . Instead, a special member function called an assignment operator is used. If you don’t define your own assignment operator Then C++ provides a default version that just copies each of the members. foo m_x m_y m_rad 1 2 3 bar m_x m_y m_rad 4 5 6 1 2 3 Lets see how to define our own assignment operator .
Image of page 4
5 class Circ { public: Circ(float x, float y, float r) { m_x = x; m_y = y; m_rad = r; } float GetArea(void) { return(3.14159*m_rad*m_rad); } private: float m_x, m_y, m_rad; }; void Assign(const Circ &src) { m_x = src.m_x; m_y = src.m_y; m_rad = src.m_rad; } The Assignment Operator The syntax for an assignment operator is a bit confusing. So lets define a simpler version first… Here’s how we’d use our new function. main() { Circ foo(1,2,3); Circ bar(4,5,6); bar.Assign(foo) ; } / / same as bar = foo; class Circ { ... void Assign( const Circ &src ) { m_x = src.m_x; m_y = src.m_y; m_rad = src.m_rad; } ... private: m_x m_y m_rad } bar 4 Hmmm.. This looks familiar, doesn’t it? What does it remind you of? class Circ { ... void Assign( const Circ &src ) { m_x = src.m_x; m_y = src.m_y; m_rad = src.m_rad; } ... private: m_x m_y m_rad } foo 1 2 3 Note : our “ bar ” variable already had m_x set to 4 , and now we’re replacing its original value with foo’s m_x value of 1 . 1 6 5 2 3 When we’re done, bar is a perfect clone of foo !
Image of page 5

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

View Full Document Right Arrow Icon
6 class Circ { public: Circ(float x, float y, float r) { m_x = x; m_y = y; m_rad = r; } float GetArea(void) { return(3.14159*m_rad*m_rad); } private: float m_x, m_y, m_rad; }; The Assignment Operator void Assign (const Circ &src) { m_x = src.m_x; m_y = src.m_y; m_rad = src.m_rad; } operator= 1. The function name is operator= Now lets see what a real assignment operator looks like. 2. The function return type is a reference to the class . Circ & 3. The function returns *this when its done. return(*this); The const keyword guarantees that the source object (src) is not modified during the copy.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern