Lec18ThisPtrFriends_6per.pdf

Lec18ThisPtrFriends_6per.pdf - class Point2Class cfw...

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

View Full Document Right Arrow Icon
1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 18 Savitch Ch. 10.3, 8.2 The "this" Pointer Friend Functions Friend Classes EECS 402 EECS 402 Andrew M Morgan 2 Consider The Following Program class Point2Class { private: float x; float y; public: Point2Class(float inX, float inY):x(inX), y(inY) { ; } float getX() const { return (x); } //Bad style float getY() const { return (y); } //Bad style }; ostream &operator<<(ostream &os, const Point2Class &rhs) { os << "Point2Class attrs: x: " << rhs.getX() << " y: " << rhs.getY(); return (os); } int main() { Point2Class p2a(6.5, 9.1); Point2Class p2b(7, -7); cout << p2a << endl; cout << p2b << endl; return (0); } Point2Class attrs: x: 6.5 y: 9.1 Point2Class attrs: x: 7 y: -7 EECS 402 EECS 402 Andrew M Morgan 3 Program Discussion The previous program works fine and was designed well, providing an overloaded insertion operator (<<) for the class user What if an additional requirement is made to have a member function called "printInfo" that provides the same functionality as the overloaded insertion operator? You don't want to duplicate the functionality in the new function Having duplicate code is problematic if a change needs to be made. Often, the change is made in only one location, and the duplicate code no longer works the same way You don't want to have to move the functionality in the existing function to the new function This can be time consuming, especially for a function much larger than the example Ideally, your new member function would make use of the existing functionality in the insertion operator EECS 402 EECS 402 Andrew M Morgan 4 Addition Of printInfo Function class Point2Class { private: float x; float y; public: Point2Class(float inX, float inY):x(inX), y(inY) { ; } float getX() const { return (x); } float getY() const { return (y); } void printInfo() const; }; ostream &operator<<(ostream &os, const Point2Class &rhs) { os << "Point2Class attrs: x: " << rhs.getX() << " y: " << rhs.getY(); return (os); } void Point2Class::printInfo() const { cout << ???????? << endl; } The << operator expects a reference to an object of type Point2Class on the right-hand-side. What do you write in place of the question marks? EECS 402 EECS 402 Andrew M Morgan 5 Intro To "this" Pointer In order for a member function to be called, it must be called "on and object" using the dot or arrow operator i.e. "p2a.printInfo();" calls the printInfo function on the p2a object The printInfo() function doesn't seem to take any parameters However, there is an implicit parameter that is the first parameter to every member function This parameter doesn't appear in the prototype, header, or call, but it IS there It is a pointer to the object that the function was called on, and is called "this" class Point2Class { private: float x; float y; public: //... float getX() const { return (x); } }; class Point2Class { private: float x; float y; public: //... float getX( //Point2Class *this ) const { return (this->x); } }; These two examples are interchangeable, since the "this" parameter is implicit to the getX (and all other) member function.
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
Image of page 2
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