Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Unary Operators - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Unary Operators, Prefix, Postfix, Behavior of predefined types, Member function definition, Friend function definition, Type Conversion, Types of conversions, Drawback of conversions are main points of this lecture.

Typology: Slides

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(41)

215 documents

1 / 49

Toggle sidebar

Related documents


Partial preview of the text

Download Unary Operators - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 21

  • Unary operators are usually prefix,

except for ++ and --

  • ++ and -- both act as prefix and

postfix

  • Example:
    • h++;
    • g-- + ++h - --i;
  • Behavior of ++ and -- for pre-

defined types:

  • Post-increment ++:
    • Post-increment operator ++ increments the current value and then returns the previous value
  • Post-decrement -- :
    • Works exactly like post ++
  • Example:

int x = 1, y = 2; cout << y++ << endl; cout << y;

  • Output:

2

3

  • Example:

int y = 2; y++++; // Error

y++ = x; // Error

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

  • Behavior of ++ and -- for pre-

defined types:

  • Pre-increment ++:
    • Pre-increment operator ++ increments the current value and then returns it’s reference
  • Pre-decrement --:
    • Works exactly like Pre-increment ++
  • Example:

int y = 2; cout << ++y << endl; cout << y << endl;

•Output:

3
3
  • 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

  • Example (Pre-increment):

class Complex{

double real, img;

public: ...

Complex & operator ++ ();

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

} Docsity.com

  • Member function definition:

Complex & Complex::operator++(){

real = real + 1;

return * this;

}

  • Friend function definition:

Complex & operator ++ (Complex & h){

h.real += 1;

return h;

}

Complex h1, h2, h3;

++h1;

  • Function operator++() returns a

reference so that the object can be

used as an lvalue

++h1 = h2 + ++h3;

  • How does a compiler know

whether it is a pre-increment

or a post-increment?

  • A post-fix unary operator is

implemented using:

Member function with 1 dummy int argument

OR

Non-member function with two arguments

  • In post increment, current value of

the object is stored in a temporary

variable

  • Current object is incremented
  • Value of the temporary variable is

returned

  • Post-increment operator:

class Complex{

...

Complex operator ++ (int);

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

}

  • Member function definition:

Complex Complex::operator ++ (int){

*complex t = this;

real += 1;

return t;

}

  • Friend function definition:

Complex operator ++ (const

Complex & h, int){

complex t = h;

h.real += 1;

return t;

}

  • The dummy parameter in the

operator function tells compiler that it

is post-increment

  • Example:

Complex h1, h2, h3;

h1++;

h3++ = h2 + h3++; // Error…

  • The pre and post decrement

operator -- is implemented in

exactly the same way

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:

int g = (int)0.0210;

double h = double(35);

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

C style type casting

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 from any other type to

current type:

  • Requires a constructor with a single parameter
  • Conversion from current type to any

other type:

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