0% found this document useful (0 votes)
172 views49 pages

Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

The document discusses unary operators like increment (++) and decrement (--) in C++. It explains that ++ and -- can be used as both prefix and postfix operators. It also discusses the differences between using them as prefix vs postfix operators for built-in types like int. For user-defined types like classes, it explains how to overload the ++ and -- operators as member functions or friend functions. The document also discusses type conversions in C++, both implicit and explicit conversions, and how to use constructors and operator overloading to define conversions between user-defined types and built-in types.

Uploaded by

Sameer Hane
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
172 views49 pages

Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 21

The document discusses unary operators like increment (++) and decrement (--) in C++. It explains that ++ and -- can be used as both prefix and postfix operators. It also discusses the differences between using them as prefix vs postfix operators for built-in types like int. For user-defined types like classes, it explains how to overload the ++ and -- operators as member functions or friend functions. The document also discusses type conversions in C++, both implicit and explicit conversions, and how to use constructors and operator overloading to define conversions between user-defined types and built-in types.

Uploaded by

Sameer Hane
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 49

Object-Oriented Programming (OOP) Lecture No.

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

of ++ and -- for predefined types:


Post-increment ++:
Post-increment

operator ++ increments the current value and then returns the previous value

Post-decrement --:
Works

exactly like post ++

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

Post-increment ++ returns by value, hence an error while assignment

Unary Operators
Behavior

of ++ and -- for predefined types:


Pre-increment ++:
Pre-increment

operator ++ increments the current value and then returns its reference

Pre-decrement --:
Works

exactly like Pre-increment ++

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

Pre-increment ++ returns by reference, hence NOT an error

Unary Operators
Example

(Pre-increment):

class Complex{
double real, img; public:
...

Complex & operator ++ (); // friend Complex & operator // ++(Complex &); }

Unary Operators
Member

function definition:

Complex & Complex::operator++(){


real = real + 1; return * this;

Unary Operators
Friend

function definition:

Complex & operator ++ (Complex & h){ h.real += 1; return h; }

Unary Operators
Complex h1, h2, h3; ++h1;
Function

operator++() returns a reference so that the object can be used as an lvalue


++h1 = h2 + ++h3;

Unary Operators
How

does a compiler know whether it is a pre-increment or a post-increment ?

Unary Operators
A

post-fix unary operator is implemented using:


Member function with 1 dummy int argument
OR Non-member function with two arguments

Unary Operators
In

post increment, current value of the object is stored in a temporary variable


Current Value

object is incremented

of the temporary variable is returned

Unary Operators
Post-increment

operator:

class Complex{

...
Complex operator ++ (int); // friend Complex operator // ++(const Complex &, int); }

Unary Operators
Member

function definition:

Complex Complex::operator ++ (int){ complex t = *this; real += 1; return t; }

Unary Operators
Friend

function definition:
Complex & h, int){

Complex operator ++ (const

complex t = h;
h.real += 1; return t; }

Unary Operators
The

dummy parameter in the operator function tells compiler that it is post-increment


Example:

Complex h1, h2, h3;

h1++;
h3++ = h2 + h3++; // Error

Unary Operators
The

operator -- is implemented in exactly the same way

pre and post decrement

Type Conversion
The

compiler automatically performs a type coercion of compatible types


e.g:

int f = 0.021;
double g = 34;

// type float is automatically converted // into int. Compiler only issues a // warning

Type Conversion
The

user can also explicitly convert between types:


C style type casting

int g = (int)0.0210; double h = double(35);

// type float is explicitly converted // (casted) into int. Not even a warning // is issued now

Type Conversion
For

user defined classes, there are two types of conversions


From any other type to current type
From current type to any other type

Type Conversion
Conversion

current type:

from any other type to

Requires a constructor with a single parameter


Conversion

other type:

from current type to any

Requires an overloaded operator

Type Conversion
Conversion

from other type to current type (int to String):


class String{
...

public: String(int a); char * GetStringPtr()const; };

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); }

char * String::GetStringPtr() const{ return bufferPtr; }

Type Conversion
int main(){ String s = 345; cout << s.GetStringPtr() << endl; return 0; }

Type Conversion
Output:

String(int) called 345

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:

String(int) called ASCII code 65 for A !!! 2


String size is also 2 instead of 1

Type Conversion
There is a mechanism in C++ to restrict automatic conversions Keyword explicit
Casting

must be explicitly performed by the user

Type Conversion
Keyword

explicit only works with constructors Example:


class String{ public: explicit String(int); };

Type Conversion
int main(){ String s; // Error s = A; return 0; }

Type Conversion
int main(){ String s1, s2;
// valid, explicit casting

s1 = String(101); // OR s2 = (String)204; return 0;

Type Conversion
There

is another method for type conversion:


Operator overloading

(Converting from current type to any other type)

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:

class String{ public: operator int(); operator char *(); };

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:

class String{ public: String(char *); operator int(); };

Type Conversion
int main(){ String s(Fakhir"); // << is NOT overloaded cout << s; return 0; }

Type Conversion
Output:
Junk Returned

Type Conversion
Modifying

String class:

class String{ public: String(char *); int AsInt(); };

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; }

You might also like