Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 31
Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
-
Upload
sameer-hane -
Category
Documents
-
view
222 -
download
0
Transcript of Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 20
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
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();
...
};
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;
}
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
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);
...
};
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;
}
}
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 ”
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
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;
}
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);
...};
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];
}
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;
}
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
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
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);
...};
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];
}
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;
}
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
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);
...};
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;
}
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;
}
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
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 ++)
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;
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);
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 &);
}
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;
}
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