![Page 1: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/1.jpg)
CSC241 Object-Oriented Programming (OOP)
Lecture No. 10
![Page 2: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/2.jpg)
Stream Insertion operator
►Often we need to display the data on the screen
►Example:
int i = 1, j = 2;
cout << "i = " << i << "\n";
cout << "j = " << j << "\n";
![Page 3: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/3.jpg)
Stream Insertion operator
Complex c1;
cout << c1;
cout << c1 << 2;
// Compiler error: binary '<<' : no operator // defined which takes a right-hand // operand of type ‘class Complex’
![Page 4: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/4.jpg)
Stream Insertion operator
class Complex{
…
public:
…
void operator << (const Complex & rhs);
};
![Page 5: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/5.jpg)
Stream Insertion operator
int main(){
Complex c1;
cout << c1; // Error
c1 << cout;
c1 << cout << 2; // Error
return 0;
};
![Page 6: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/6.jpg)
Stream Insertion operator
class Complex{
…
public:
…
void operator << (ostream &);
};
![Page 7: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/7.jpg)
Stream Insertion operator
void Complex::operator << (ostream & os){
os << '(' << real << ', ' << img << ')';
}
![Page 8: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/8.jpg)
Stream Insertion operator
class Complex{
...
friend ostream & operator << (ostream & os, const Complex & c);
};
Note: this objectis NOT const
Note: return typeis NOT const
![Page 9: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/9.jpg)
Stream Insertion operator
// we want the output as: (real, img)
ostream & operator << (ostream & os, const Complex & c){
os << '(' << c.real << ', ' << c.img << ')';
return os;
}
![Page 10: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/10.jpg)
Stream Insertion operator
Complex c1(1.01, 20.1), c2(0.01, 12.0);
cout << c1 << endl << c2;
![Page 11: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/11.jpg)
Stream Insertion operator
Output:
( 1.01 , 20.1 )
( 0.01 , 12.0 )
![Page 12: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/12.jpg)
Stream Insertion operator
cout << c1 << c2;
is equivalent to
operator<<(operator<<(cout, c1), c2);
![Page 13: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/13.jpg)
Stream Extraction Operator
►Overloading “>>” operator:
class Complex{
...
friend istream & operator >> (istream & i, Complex & c);
};Note: this objectis NOT const
![Page 14: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/14.jpg)
Stream Extraction Operator
istream & operator << (istream & in, Complex & c){
in >> c.real;
in >> c.img;
return in;
}
![Page 15: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/15.jpg)
Stream Extraction Operator
►Main Program:Complex c1(1.01, 20.1);
cin >> c1;// suppose we entered
// 1.0025 for c1.real and
// 0.0241 for c1.img
cout << c1;
![Page 16: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/16.jpg)
Stream Extraction Operator
Output:
( 1.0025 , 0.0241 )
![Page 17: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/17.jpg)
Other Binary operators
►Overloading comparison operators:class Complex{
public:
bool operator == (const Complex & c);
//friend bool operator == (const //Complex & c1, const Complex & c2);
bool operator != (const Complex & c);
//friend bool operator != (const //Complex & c1, const Complex & c2);
…
};
![Page 18: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/18.jpg)
Other Binary operators
bool Complex::operator ==(const Complex & c){
if ((real == c.real) && (img == c.img)){
return true;
}
else
return false;
}
![Page 19: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/19.jpg)
Other Binary operators
bool operator ==(const Complex& lhs, const Complex& rhs){
if ((lhs.real == rhs.real) && (lhs.img == rhs.img)){
return true;
}
else
return false;
}
![Page 20: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/20.jpg)
Other Binary operators
bool Complex::operator !=(const Complex & c){
if ((real != c.real) || (img != c.img)){
return true;
}
else
return false;
}
![Page 21: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/21.jpg)
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();
...
};
![Page 22: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/22.jpg)
Other Binary Operators
int main(){
String str1("Test");
String str2;
str2.SetString("Ping");
return 0;
}
![Page 23: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/23.jpg)
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
![Page 24: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/24.jpg)
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);
...
};
![Page 25: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/25.jpg)
Other Binary Operators
void SetChar(char c, int pos){
if (bufferPtr != NULL){
if (pos>0 && pos <= size)
bufferPtr[pos] = c;
}
}
![Page 26: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/26.jpg)
Other Binary Operators
►Now we can efficiently change a single character:
String str1("Ping");
str1.SetChar('o', 2);
// str1 is now changed to “Pong”
![Page 27: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/27.jpg)
Subscript Operator►An elegant solution:
►Overloading the subscript “[]” operator
![Page 28: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/28.jpg)
Subscript Operatorint main(){
String str2;
str2.SetString("Ping");
str[2] = 'o';
cout << str[2];
return 0;
}
![Page 29: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/29.jpg)
Subscript Operator
class String{
...
public:
char & operator[](int);
...
};
![Page 30: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/30.jpg)
Subscript Operator
char & String::operator[](int pos){
assert(pos>0 && pos <= size);
return stringPtr[pos - 1];
}
![Page 31: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/31.jpg)
Subscript Operator
int main() {
String s1("Ping");
cout << str.GetString() << endl;
s1[2] = 'o';
cout << str.GetString();
return 0;
}
![Page 32: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/32.jpg)
Subscript Operator
►Output:
Ping
Pong
![Page 33: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/33.jpg)
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
![Page 34: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/34.jpg)
Function Operator
class String{
...
public:
char & operator()(int);
...
};
![Page 35: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/35.jpg)
Function Operatorchar & String::operator()(int pos){
assert(pos>0 && pos <= size);
return bufferPtr[pos - 1];
}
![Page 36: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/36.jpg)
Subscript Operator
int main(){
String s1("Ping");
char g = s1(2); // g = ‘i’
s1(2) = 'o';
cout << g << "\n";
cout << str.GetString();
return 0;
}
![Page 37: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/37.jpg)
Function Operator
►Output:
i
Pong
![Page 38: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/38.jpg)
Function Operatorclass String{
...
public:
String operator()(int, int);
...
};
![Page 39: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/39.jpg)
Function OperatorString 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;
}
![Page 40: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/40.jpg)
Function Operator
int main(){
String s(“Hello World”);
// “<<“ is overloaded
cout << s(1, 5);
return 0;
}
![Page 41: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/41.jpg)
Function Operator
Output:
Hello
![Page 42: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/42.jpg)
Unary Operators
►Unary operators: & * + - ++ -- ! ~
►Examples:--x
-(x++)
!(*ptr ++)
![Page 43: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/43.jpg)
Unary Operators
►Unary operators are usually prefix, except for ++ and --►++ and -- both act as prefix and postfix►Example:
h++;
g-- + ++h - --i;
![Page 44: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/44.jpg)
Unary Operators
►General syntax for unary operators:
Member Functions:TYPE & operator OP ();
Non-member Functions:Friend TYPE & operator OP
(TYPE & t);
![Page 45: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/45.jpg)
Unary Operators
►Overloading unary ‘-’:
class Complex{
...
Complex operator - ();
// friend Complex operator -(Complex &);
};
![Page 46: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/46.jpg)
Unary Operators
►Member function definition:
Complex Complex::operator -(){
Complex temp;
temp.real = -real;
temp.img = -img;
return temp;
}
![Page 47: CSC241 Object-Oriented Programming (OOP) Lecture No. 10](https://reader035.vdocuments.net/reader035/viewer/2022062304/56649e695503460f94b66277/html5/thumbnails/47.jpg)
Unary Operators
Complex c1(1.0, 2.0), c2;
c2 = -c1;
// c2.real = -1.0
// c2.img = -2.0
Unary ‘+’ is overloaded in the same way