You are on page 1of 3

Class Exercise 7: Operator Overloading

Object-Oriented Programming (83-131), 5771

1) Operator Overloading
MyString.h
class MyString { private: char* m_str; public: MyString(); MyString(const MyString& other); MyString(const char* str); ~MyString(); MyString& operator=(const char* str); MyString& operator=(const MyString& right); bool operator==(const char* str) const; bool operator==(const MyString& right) const; char& operator[](unsigned int pos); MyString operator+(const MyString& right) const; MyString operator+(const char* right) const; friend MyString operator+(const char* left, const MyString& right); int size() const; void print() const; };

MyString.cpp
MyString::MyString() : m_str(NULL) { m_str = new char[1]; m_str[0] = '\0'; } MyString::MyString(const MyString& other) : m_str(NULL) { operator=(other); } MyString::MyString(const char* str) : m_str(NULL) { operator=(str); } MyString::~MyString() { delete[] m_str; }

cont

Class Exercise 7: Operator Overloading

Object-Oriented Programming (83-131), 5771

MyString.cpp cont.
MyString& MyString::operator=(const char* str) { delete[] m_str; m_str = new char[strlen(str) + 1]; strcpy(m_str, str); return *this; } MyString& MyString::operator=(const MyString& right) { return (operator=(right.m_str)); }

bool MyString::operator==(const char* str) const { return (strcmp(m_str, str) == 0); } bool MyString::operator==(const MyString& right) const { return (operator==(right.m_str)); }

char& MyString::operator[](unsigned int pos) { if (pos > strlen(m_str)) { throw ("Out of range"); } return m_str[pos]; } MyString MyString::operator+(const MyString& right) const { char dest[1000]; strcpy(dest, m_str); strcat(dest, right.m_str); return (MyString(dest)); } MyString MyString::operator+(const char* right) const { char dest[1000]; strcpy(dest, m_str); strcat(dest, right); return (MyString(dest)); } int MyString::size() const { return strlen(m_str); } // This function is not a member of MyString MyString operator+(const char* left, const MyString& right) { char dest[1000]; strcpy(dest, left); strcat(dest, right.m_str); return (MyString(dest)); }

Class Exercise 7: Operator Overloading

Object-Oriented Programming (83-131), 5771

2) More operator overloading.


Fraction.h
class Fraction { public: Fraction(int nn=0, int dn=1); // 1 declaration = 3 constructors // ... operator double() { return (double(m_iNom) / m_iDenom); } // Operator Casting Fraction& operator++(); Fraction operator++(int); // prefix - ++a // postfix - a++

double-

friend istream& operator>>(istream &is, Fraction &f); friend ostream& operator<<(ostream &os, const Fraction &f); private: int m_iNom; int m_iDenom;

};

Fraction.cpp
Fraction::Fraction(int nn, int dd) : m_iNom (nn), m_iDenom (dd) { if (m_iDenom == 0) m_iDenom = 1; } Fraction& Fraction::operator++() { m_iNom += m_iDenom; return *this; } Fraction Fraction::operator++(int) { int nn = m_iNom; int dd = m_iDenom; m_iNom += m_iDenom; return Fraction(nn, dd); } // Global functions: istream& operator>>(istream &is, Fraction &frac) { char divSign; is >> frac.m_iNom >> divSign >> frac.m_iDenom; if (frac.m_iDenom == 0) frac.m_iDenom = 1; return is; } ostream& operator<<(ostream& os, const Fraction &frac) { return (os << frac.m_iNom << "/" << frac.m_iDenom); }