Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Programming (OOP) Lecture No. 20
2
Other Binary operators
We have seen the following string class till now: class String{ private: char * bufferPtr; int size; public: String(); String(char * ptr); void SetString(char * ptr); const char * GetString(); ... };
3
Other Binary Operators
int main(){ String str1(“Test”); String str2; str2.SetString(“Ping”); return 0; }
4
Other Binary Operators
What if we want to change the string from “Ping” to “Pong”?? {ONLY 1 character to be changed…} Possible solution: Call: str2.SetString(“Pong”); This will delete the current buffer and allocate a new one Too much overhead if string is too big
5
Other Binary Operators
Or, we can add a function which changes a character at nth location class String{ ... public: void SetChar(char c, int pos); };
6
Other Binary Operators
void SetChar(char c, int pos){ if(bufferPtr != NULL){ if(pos>0 && pos<=size) bufferPtr[pos] = c; }
7
Other Binary Operators
Now we can efficiently change a single character: String str1(“Ping”); str1.SetChar(‘o’, 2); // str1 is now changed to “Pong”
8
An elegant solution: Overloading the subscript “[]” operator
9
Subscript Operator int main(){ String str2; str2.SetString(“Ping”);
str[2] = ‘o’; cout << str[2]; return 0; }
10
class String{ ... public: char & operator[](int); };
Subscript Operator class String{ ... public: char & operator[](int); };
11
Subscript Operator char & String::operator[]( int pos){
assert(pos>0 && pos<=size); return stringPtr[pos-1]; }
12
Subscript Operator int main() { String s1(“Ping”);
cout <<str.GetString()<< endl; s1[2] = ‘o’; cout << str.GetString(); return 0; }
13
Subscript Operator Output: Ping Pong
14
Overloading () Must be a member function
Any number of parameters can be specified Any return type can be specified Operator() can perform any generic operation
15
class String{ ... public: char & operator()(int); };
Function Operator class String{ ... public: char & operator()(int); };
16
Function Operator char & String::operator() (int pos){
assert(pos>0 && pos<=size); return bufferPtr[pos-1]; }
17
Subscript Operator int main(){ } String s1(“Ping”);
char g = s1(2); // g = ‘i’ s1(2) = ‘o’; cout << g << “\n”; cout << str.GetString(); return 0; }
18
Function Operator Output: i Pong
19
class String{ ... public: String operator()(int, int); };
Function Operator class String{ ... public: String operator()(int, int); };
20
Function Operator String String::operator()(int index, int subLength){
assert(index>0 && index+subLength-1<=size); char * ptr = new char[subLength+1]; for (int i=0; i < subLength; ++i) ptr[i] = bufferPtr[i+index-1]; ptr[subLength] = ‘\0’; String str(ptr); delete [] ptr; return str; }
21
Function Operator int main(){ String s(“Hello World”);
// “<<“ is overloaded cout << s(1, 5); return 0; }
22
Function Operator Output: Hello
23
Unary operators: & * + - ++ -- ! ~ Examples: --x -(x++) !(*ptr ++)
24
Unary Operators Unary operators are usually prefix, except for ++ and -- ++ and -- both act as prefix and postfix Example: h++; g h - --i;
25
Unary Operators General syntax for unary operators: Member Functions:
TYPE & operator OP (); Non-member Functions: Friend TYPE & operator OP (TYPE & t);
26
Unary Operators Overloading unary ‘-’: class Complex{ ...
Complex operator - (); // friend Complex operator // -(Complex &); }
27
Unary Operators Member function definition:
Complex Complex::operator -(){ Complex temp; temp.real = -real; temp.img = -img; return temp; }
28
Unary Operators Unary ‘+’ is overloaded in the same way
Complex c1(1.0 , 2.0), c2; c2 = -c1; // c2.real = -1.0 // c2.img = -2.0 Unary ‘+’ is overloaded in the same way
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.