Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.

Similar presentations


Presentation on theme: "Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient."— Presentation transcript:

1 Overloading Operator MySting Example

2 Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient but may make program difficult to understand

3 Operator Overloading Function can be overloaded Operators can also be overloaded as ordinary functions, – overload built-in C++ operators: give different meaning to operators such as +, +=, <<, etc. – taking one or more arguments of class or reference – Keyword operator To use an operator on a class object it must be overloaded except the assignment operator (=) or the address operator (&) – Assignment operator by default performs member_wise assignment – Address operator (&) by default returns the address of an object

4 Overloading an operator Write function definition as normal Function name is keyword operator followed by the symbol for the operator being overloaded operator+ used to overload the addition operator ( + ) add function + operator

5 Prototype class foo{ public : foo operator--(); // overload unary minus foo operator-(int); //binary minus foo-int foo operator-(foo); // foo - foo }; foo operator-(int, foo) ; // int - foo foo operator-(int, foo*) ; // illegal need foo or foo&

6 Member vs non-member functions Operator functions can be member or non- member functions – When overloading ( ), [ ], -> or any of the assignment operators, must use a member function Operator functions as member functions – implicitly acting on this object – Leftmost operand must be an object (or reference to an object) of the class – If left operand of a different type, operator function must be a non-member function

7 Non-member Function Operator functions as non-member functions – Must be friend s if needs to access private or protected members – Enable the operator to be commutative

8 Overloading restrictions Can not create new operators The precedence, associativity and arity do not change can not overload operators for built-in types – Cannot change how two integers are added

9 MyString Design MyString class: – Represent a sequence of characters – the length of the string is important – data member: s : pointer len – methods: constructor: int n or char * length, print, destructor

10 Declare the Class my_string class MyString{ public: MyString(int n); MyString(const char * str); ~ MyString(); int length() const ; //const member function void print() const; private: char * s; int len; };

11 Constructors MyString :: MyString(int n) { s = new char[n+1]; s[0] = ‘\0'; len =n; } MyString :: MyString(const char * str) { len = strlen(str); s = new char[len+1]; strcpy(s,str); }

12 Overload Operator + (MyString) member function Prototype: MyString operator +(const MyString &b) const; Definition: MyString MyString ::operator+ ( const MyString &b) const {cout << "member +" << endl; char * temp = new char[this->len + b.len +1]; strcpy(temp, this->s); strcat(temp, b.s); MyString str(temp); return str; }

13 Returning Local Object from a function Returning an object invokes the copy constructor while returning a reference doesn't. If a method or function returns a local object, it should return an object, not a reference. It is an error to return a pointer to a local object. Once the function completes, the local object are freed. The pointer would be a dangling pointer that refers to a nonexistent object.

14 Overload Operator + non-member function friend MyString operator+(const MyString & a, const MyString &b); MyString operator+(const MyString &a, const MyString &b) {cout<< "nonmember +" << endl; char * temp = new char[a.len + b.len + 1]; strcpy(temp, a.s); strcat(temp, b.s); MyString ss(temp); return ss; }

15 Overloading the Assignment Operator copy constructors copy constructors Called when a new object is created and set equal to an existing instance Called when a new object is created and set equal to an existing instance What’s the difference between the following lines of code? What’s the difference between the following lines of code? MyString str1,str2; … // What’s the difference between the following two // lines of code? MyString aString = str1; str2 = str1; The first assignment involves a copy constructor since a new object is being created. The first assignment involves a copy constructor since a new object is being created. The second is straight assignment to an existing variable, so no copy constructor is involved. The second is straight assignment to an existing variable, so no copy constructor is involved.

16 Overloading the Assignment Operator (cont) Remember, C++ will define a default and naïve copy constructor for you if you don’t provide one. Remember, C++ will define a default and naïve copy constructor for you if you don’t provide one. It will just copy member variables (potential for dangling pointers) It will just copy member variables (potential for dangling pointers) In the case of MyString, we’d need to override the default copy constructor to make sure the storage was copied properly. In the case of MyString, we’d need to override the default copy constructor to make sure the storage was copied properly. MyString::MyString(MyString &aCopy) { // Copy storage into new instance if necessary... } Again, this will take care of the case where someone tries to assign to a MyString variable when it is declared : Again, this will take care of the case where someone tries to assign to a MyString variable when it is declared : MyString aStr = anotherStr; MyString aStr = anotherStr;

17 Overloading the Assignment Operator (cont) However, when we need to handle the case where an existing variable is assigned a new value via the assignment operator, we overload the assignment operator: However, when we need to handle the case where an existing variable is assigned a new value via the assignment operator, we overload the assignment operator: The = operator is another special case binary operator... The = operator is another special case binary operator... MyString &MyString::operator=(const MyString &sourceStr) { //set the value return *this; // Huh? return *this; // Huh?} Remember that when overloading the = operator you are going to be assigning to an existing instance. If that instance has dynamically allocated data it should be freed. Remember that when overloading the = operator you are going to be assigning to an existing instance. If that instance has dynamically allocated data it should be freed. We return a reference so that str1 = str2 = str3 works... We return a reference so that str1 = str2 = str3 works...

18 Overloading the Assignment Operator (cont) we should always make sure that our source and destinations aren’t the same.. we should always make sure that our source and destinations aren’t the same.. We do this by adding the following code: We do this by adding the following code: MyString & MyString::operator=(const MyString &sourceStr) { // Make sure we actually have two pointers if (this != &sourceStr) if (this != &sourceStr) //set the value //set the value return *this; return *this;} “ this ”, “ this ”, is a pointer to the current instance of the class we are in. is a pointer to the current instance of the class we are in.

19 Overload = (MyString) MyString & MyString ::operator =(const MyString & str) { if (this != &str) //important, avoid self-assignment { len = str.len; delete [] s; s = new char [len]; strcpy(s, str.s); } return *this; }

20 Overload += (MyString) // += operator MyString &MyString ::operator+=(const MyString &str) { this->len += str.len + len ; char *tempPtr = new char[len +1]; strcpy(tempPtr, s); strcat(tempPtr, str.s); delete []s; s= tempPtr; return *this; }

21 Overload Subscripting char & my_string::operator [](int i) { return this->s[i]; } my_string ms1(“hello”); ms1[0] = '!'; // subscript operator [], returning reference

22 Friend Functions The keyword friend is function specifier It gives nonmember function access to the hidden members of the class escaping the data hiding restriction Make sure you have a good reason for escaping the restriction

23 Declare friend functions Declare in the class to which the function is a friend Good: put in the public part of the class Member functions of one class can be friend of another class, use the class scope quantifier All member functions of one class are friend functions of another, just write friend class classname

24 Overloading I/O Operator << The operator << has two arguments – ostream & – the ADT It must produce an ostream & Use a reference since you don’t want to copy the stream object

25 Overload << (my_string) friend ostream & operator<<(ostream &, const my_string &); ostream& operator<<(ostream &out, const my_string &str) { return (out << str.s << endl); }

26 >> istream& operator >> (istream &out, my_string &str)


Download ppt "Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient."

Similar presentations


Ads by Google