Lecture-18-OperatorOverloading

Gatech georgia tech string msg gatech threw an

This preview shows page 5 - 12 out of 20 pages.

gatech += "Georgia Tech"; String msg = gatech + " threw an interception." ; cout << msg; msg = byu + " kicked a field goal." ; cout << msg; for (int x=0; x < msg.Length(); ++x) { cout << msg[x] ; } }
Image of page 5

Subscribe to view the full document.

class String { private: // string data stored internally // as a heap-allocated C-string char * str; public: String() { Init(""); } String(const char * s) { Init(s); } String(const String & s) { Init(s.str); } ~String() { Free(); } Internal String representation private: void Init(const char * s) { str = new char[strlen(s) + 1]; strcpy(str, s); } void Free() { delete [] str; str = 0; } };
Image of page 6
class String { public: String & operator =(const char * s) { Free(); Init(s); return *this; } String & operator =(const String & s) { if (&s != this) { Free(); Init(s.str); } return *this; } }; operator =
Image of page 7

Subscribe to view the full document.

class String { public: char GetChar(int index) const { return str[index]; } void SetChar(int index, char c) { str[index] = c; } }; operator []
Image of page 8
class String { public: char GetChar(int index) const { return str[index]; } void SetChar(int index, char c) { str[index] = c; } char & operator [](int index) { return str[index]; } }; operator []
Image of page 9

Subscribe to view the full document.

void UpperCase(String & s) { for (int x=0; x < s.Length(); ++x) { s[x] = toupper(s[x]); } } operator [] Example L-value R-value
Image of page 10
class String { public: void Append(const char * s) { char * newStr = new char[strlen(str) + strlen(s) + 1]; strcpy(newStr, str); strcat(newStr, s); Free(); str = newStr; } void Append(const String & s) { Append(s.str); } }; operator +=
Image of page 11

Subscribe to view the full document.

Image of page 12
You've reached the end of this preview.
  • Winter '12
  • KenRodham
  • char getchar

{[ 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