Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21
Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21
21
Unary Operators
Unary ++
operators are usually prefix, except for ++ and -and -- both act as prefix and postfix
h++; g-- + ++h - --i;
Example:
Unary Operators
Behavior
operator ++ increments the current value and then returns the previous value
Post-decrement --:
Works
Unary Operators
Example:
int x = 1, y = 2; cout << y++ << endl; cout << y;
Output: 2 3
Unary Operators
Example:
int y = 2; y++++; y++ = x; // Error // Error
Unary Operators
Behavior
operator ++ increments the current value and then returns its reference
Pre-decrement --:
Works
Unary Operators
Example: int y = 2; cout << ++y << endl; cout << y << endl;
Output: 3 3
Unary Operators
Example: int x = 2, y = 2; ++++y; cout << y; ++y = x; cout << y; Output: 4 2
Unary Operators
Example
(Pre-increment):
class Complex{
double real, img; public:
...
Complex & operator ++ (); // friend Complex & operator // ++(Complex &); }
Unary Operators
Member
function definition:
Unary Operators
Friend
function definition:
Unary Operators
Complex h1, h2, h3; ++h1;
Function
Unary Operators
How
Unary Operators
A
Unary Operators
In
object is incremented
Unary Operators
Post-increment
operator:
class Complex{
...
Complex operator ++ (int); // friend Complex operator // ++(const Complex &, int); }
Unary Operators
Member
function definition:
Unary Operators
Friend
function definition:
Complex & h, int){
complex t = h;
h.real += 1; return t; }
Unary Operators
The
h1++;
h3++ = h2 + h3++; // Error
Unary Operators
The
Type Conversion
The
int f = 0.021;
double g = 34;
// type float is automatically converted // into int. Compiler only issues a // warning
Type Conversion
The
// type float is explicitly converted // (casted) into int. Not even a warning // is issued now
Type Conversion
For
Type Conversion
Conversion
current type:
other type:
Type Conversion
Conversion
Type Conversion
String::String(int a){ cout << "String(int) called..." << endl; char array[15]; itoa(a, array, 10); size = strlen(array); bufferPtr = new char [size + 1]; strcpy(bufferPtr, array); }
Type Conversion
int main(){ String s = 345; cout << s.GetStringPtr() << endl; return 0; }
Type Conversion
Output:
Type Conversion
Automatic
drawbacks Conversion takes place transparently even if the user didnt wanted the conversion
conversion has
Type Conversion
User can write the following code to initialize the string with a single character: int main(){ String s = A; cout << s.GetStringPtr()<< endl << s.GetSize() << endl; return 0; }
Type Conversion
Output:
Type Conversion
There is a mechanism in C++ to restrict automatic conversions Keyword explicit
Casting
Type Conversion
Keyword
Type Conversion
int main(){ String s; // Error s = A; return 0; }
Type Conversion
int main(){ String s1, s2;
// valid, explicit casting
Type Conversion
There
Type Conversion
General
Syntax:
TYPE1::Operator TYPE2();
Must
be a member function NO return type and arguments are specified Return type is implicitly taken to be TYPE2 by compiler
Type Conversion
Overloading
pre-defined types:
Type Conversion
String::operator int(){ if(size > 0) return atoi(bufferPtr); else return -1; }
Type Conversion
String::operator char *(){ return bufferPtr; }
Type Conversion
int main(){ String s("2324"); cout << (int)s << endl << (char *)s; return 0; }
Type Conversion
Output:
2324 2324
Type Conversion
User-defined
types can be overloaded in exactly the same way Only prototype is shown below:
class String{ operator Complex(); operator HugeInt(); operator IntVector(); };
Type Conversion
Modifying
String class:
Type Conversion
int main(){ String s(Fakhir"); // << is NOT overloaded cout << s; return 0; }
Type Conversion
Output:
Junk Returned
Type Conversion
Modifying
String class:
Type Conversion
int String::AsInt(){ if(size > 0) return atoi(bufferPtr); else return -1; }
Type Conversion
int main(){ String s(434"); // << is NOT overloaded cout << s; //error cout << s.AsInt(); return 0; }