http://ecomputernotes.com
class String{ private: char * bufferPtr; int size; public: String(); String(char * ptr); void SetString(char * ptr); const char * GetString(); ... };
http://ecomputernotes.com
Call: str2.SetString(Pong); This will delete the current buffer and allocate a new one Too much overhead if string is too big
single character:
String str1(Ping); str1.SetChar(o, 2); // str1 is now changed to Pong
http://ecomputernotes.com
Subscript Operator
An elegant solution: Overloading the subscript [] operator
http://ecomputernotes.com
Subscript Operator
int main(){ String str2; str2.SetString(Ping); str[2] = o; cout << str[2]; return 0; }
http://ecomputernotes.com
Subscript Operator
class String{ ... public: char & operator[](int); ... };
http://ecomputernotes.com
Subscript Operator
char & String::operator[]( int pos){ assert(pos>0 && pos<=size); return stringPtr[pos-1]; }
http://ecomputernotes.com
Subscript Operator
int main() { String s1(Ping); cout <<str.GetString()<< endl; s1[2] = o; cout << str.GetString(); return 0; }
http://ecomputernotes.com
Subscript Operator
Output:
Ping Pong
http://ecomputernotes.com
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
http://ecomputernotes.com
Function Operator
class String{ ... public: char & operator()(int); ... };
http://ecomputernotes.com
Function Operator
char & String::operator() (int pos){ assert(pos>0 && pos<=size); return bufferPtr[pos-1]; }
http://ecomputernotes.com
Subscript Operator
int main(){
String s1(Ping); char g = s1(2); s1(2) = o; cout << g << \n; cout << str.GetString(); return 0; // g = i
http://ecomputernotes.com
Function Operator
Output:
i Pong
http://ecomputernotes.com
Function Operator
class String{ ... public: String operator()(int, int); ... };
http://ecomputernotes.com
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; }
http://ecomputernotes.com
Function Operator
int main(){ String s(Hello World); // << is overloaded cout << s(1, 5); return 0; }
http://ecomputernotes.com
Function Operator
Output: Hello
http://ecomputernotes.com
Unary Operators
Unary
operators:
& * + - ++ -- ! ~
Examples:
Unary Operators
Unary
postfix
h++;
Example:
Unary Operators
General syntax for unary
operators:
Member Functions:
TYPE & operator OP ();
Non-member Functions:
Friend TYPE & operator OP (TYPE & t);
http://ecomputernotes.com
Unary Operators
Overloading unary -:
-(Complex &);
Unary Operators
Member function definition:
Complex Complex::operator -(){ Complex temp; temp.real = -real; temp.img = -img; return temp; }
http://ecomputernotes.com
Unary Operators
Complex c1(1.0 , 2.0), c2; c2 = -c1;
// c2.real = -1.0 // c2.img = -2.0
way
http://ecomputernotes.com