object oriented programming (oop) - cs304 power point slides lecture 20
TRANSCRIPT
![Page 1: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/1.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 1/28
Object-Oriented Programming
(OOP)Lecture No. 20
![Page 2: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/2.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 2/28
Other inary 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 3: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/3.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 3/28
Other inary Operatorsint main(){
String str1(“Test”);
String str2;
str2.SetString(“Ping”);
return 0;
}
![Page 4: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/4.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 4/28
Other inary 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 5: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/5.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 5/28
Other inary Operators
►Or, we can add a function whichchanges a character at nth location
class String{...
public:
void SetChar(char c, int pos);
...
};
![Page 6: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/6.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 6/28
Other inary Operators
void SetChar(char c, int pos){
if(bufferPtr != NULL){if(pos>0 && pos<=size)
bufferPtr[pos] = c;
}
}
![Page 7: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/7.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 7/28
Other inary Operators
►Now we can efficiently change a
single character:String str1(“Ping”);
str1.SetChar(„o‟, 2);
// str1 is now changed to “ Pong ”
![Page 8: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/8.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 8/28
![Page 9: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/9.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 9/28
Subscript Operatorint main(){
String str2;
str2.SetString(“Ping”);
str[2] = „o‟;
cout << str[2]; return 0;
}
![Page 10: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/10.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 10/28
Subscript Operator
class String{
...
public:
char & operator[](int);
...};
![Page 11: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/11.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 11/28
Subscript Operator
char & String::operator[](
int pos){
assert(pos>0 && pos<=size);
return stringPtr[pos-1];
}
![Page 12: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/12.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 12/28
Subscript Operator
int main() {
String s1(“Ping”);
cout <<str.GetString()<< endl;
s1[2] = „o‟;
cout << str.GetString();return 0;
}
![Page 13: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/13.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 13/28
Subscript Operator
►Output:
Ping
Pong
![Page 14: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/14.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 14/28
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 15: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/15.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 15/28
Function Operator
class String{
...
public:
char & operator()(int);
...};
![Page 16: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/16.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 16/28
Function Operator
char & String::operator()
(int pos){
assert(pos>0 && pos<=size);
return bufferPtr[pos-1];
}
![Page 17: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/17.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 17/28
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 18: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/18.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 18/28
Function Operator
►Output:
i
Pong
![Page 19: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/19.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 19/28
Function Operator
class String{
...
public:
String operator()(int, int);
...};
![Page 20: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/20.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 20/28
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;
}
![Page 21: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/21.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 21/28
Function Operator
int main(){
String s(“Hello World”); // “<<“ is overloaded
cout << s(1, 5);
return 0;
}
![Page 22: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/22.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 22/28
Function Operator
Output:
Hello
![Page 23: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/23.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 23/28
Unary Operators ►Unary operators:
& * + - ++ -- ! ~
►Examples:
--x
-(x++)
!(*ptr ++)
![Page 24: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/24.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 24/28
Unary Operators ►Unary operators are usuallyprefix, except for ++ and --
►++ and -- both act as prefixand postfix
►Example:
h++;
g-- + ++h - --i;
![Page 25: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/25.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 25/28
Unary Operators ►General syntax for unaryoperators:
Member Functions:TYPE & operator OP ();
Non-member Functions:
Friend TYPE & operator OP(TYPE & t);
![Page 26: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/26.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 26/28
Unary Operators
►Overloading unary „-‟:
class Complex{
...Complex operator - ();
// friend Complex operator
// -(Complex &);
}
![Page 27: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/27.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 27/28
Unary Operators
►Member function definition:
Complex Complex::operator -(){
Complex temp;temp.real = -real;
temp.img = -img;
return temp;
}
![Page 28: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20](https://reader030.vdocuments.net/reader030/viewer/2022021401/577cd59c1a28ab9e789b3f7b/html5/thumbnails/28.jpg)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-20 28/28
Unary Operators
Complex c1(1.0 , 2.0), c2;
c2 = -c1;
// c2.real = -1.0
// c2.img = -2.0
►Unary „+‟ is overloaded in the sameway