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)

213 documents

1 / 49

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 21
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31

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;
  • Example:

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

  • Output:
  • Example:

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

y++ = x; // Error

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

  • Example:

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

•Output:

  • 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

  • Member function definition:

Complex & Complex::operator++(){

real = real + 1;

return * this;

}

  • Friend function definition:

Complex & operator ++ (Complex & h){

h.real += 1;

return h;

}

  • 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

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

}

  • 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