Computer Notes - Unary Operators - PowerPoint PPT Presentation

About This Presentation
Title:

Computer Notes - Unary Operators

Description:

- Computer Notes - Unary Operators in Object oriented Programming what is Unary Operators Explain about it in detail .explain it with example – PowerPoint PPT presentation

Number of Views:354

less

Transcript and Presenter's Notes

Title: Computer Notes - Unary Operators


1
Unary Operators
http//ecomputernotes.com
2
Unary Operators
  • Unary operators are usually prefix, except for
    and --
  • and -- both act as prefix and postfix
  • Example
  • h
  • g-- h - --i

http//ecomputernotes.com
3
Unary Operators
  • 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

http//ecomputernotes.com
4
Unary Operators
  • Example
  • int x 1, y 2
  • cout ltlt y ltlt endl
  • cout ltlt y
  • Output
  • 2
  • 3

http//ecomputernotes.com
5
Unary Operators
  • Example
  • int y 2
  • y // Error
  • y x // Error

Post-increment returns by value, hence an
error while assignment
http//ecomputernotes.com
6
Unary Operators
  • Behavior of and -- for pre-defined types
  • Pre-increment
  • Pre-increment operator increments the current
    value and then returns its reference
  • Pre-decrement --
  • Works exactly like Pre-increment

http//ecomputernotes.com
7
Unary Operators
  • Example
  • int y 2
  • cout ltlt y ltlt endl
  • cout ltlt y ltlt endl
  • Output
  • 3
  • 3

http//ecomputernotes.com
8
Unary Operators
  • Example
  • int x 2, y 2
  • y
  • cout ltlt y
  • y x
  • cout ltlt y
  • Output
  • 4
  • 2

Pre-increment returns by reference, hence NOT
an error
http//ecomputernotes.com
9
Unary Operators
  • Example (Pre-increment)
  • class Complex
  • double real, img
  • public
  • ...
  • Complex operator ()
  • // friend Complex operator // (Complex )

http//ecomputernotes.com
10
Unary Operators
  • Member function definition
  • Complex Complexoperator()
  • real real 1
  • return this

http//ecomputernotes.com
11
Unary Operators
  • Friend function definition
  • Complex operator (Complex h)
  • h.real 1
  • return h

http//ecomputernotes.com
12
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

http//ecomputernotes.com
13
Unary Operators
  • How does a compiler know whether it is a
    pre-increment or a post-increment ?

http//ecomputernotes.com
14
Unary Operators
  • A post-fix unary operator is implemented using
  • Member function with 1 dummy int argument
  • OR
  • Non-member function with two arguments

http//ecomputernotes.com
15
Unary Operators
  • 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

http//ecomputernotes.com
16
Unary Operators
  • Post-increment operator
  • class Complex
  • ...
  • Complex operator (int)
  • // friend Complex operator // (const
    Complex , int)

http//ecomputernotes.com
17
Unary Operators
  • Member function definition
  • Complex Complexoperator (int)
  • complex t this
  • real 1
  • return t

http//ecomputernotes.com
18
Unary Operators
  • Friend function definition
  • Complex operator (const
  • Complex h, int)
  • complex t h
  • h.real 1
  • return t

http//ecomputernotes.com
19
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

http//ecomputernotes.com
20
Unary Operators
  • The pre and post decrement operator -- is
    implemented in exactly the same way

http//ecomputernotes.com
21
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

22
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
23
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

http//ecomputernotes.com
24
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

25
Type Conversion
  • Conversion from other type to current type (int
    to String)
  • class String
  • ...
  • public
  • String(int a)
  • char GetStringPtr()const

26
Type Conversion
  • StringString(int a)
  • cout ltlt "String(int) called..." ltlt endl
  • char array15
  • itoa(a, array, 10)
  • size strlen(array)
  • bufferPtr new char size 1
  • strcpy(bufferPtr, array)
  • char StringGetStringPtr() const
  • return bufferPtr

http//ecomputernotes.com
27
Type Conversion
  • int main()
  • String s 345
  • cout ltlt s.GetStringPtr() ltlt endl
  • return 0

http//ecomputernotes.com
28
Type Conversion
  • Output
  • String(int) called
  • 345

http//ecomputernotes.com
29
Type Conversion
  • Automatic conversion has drawbacks
  • Conversion takes place transparently even if the
    user didnt wanted the conversion

http//ecomputernotes.com
30
Type Conversion
  • User can write the following code to initialize
    the string with a single character
  • int main()
  • String s A
  • cout ltlt s.GetStringPtr()ltlt endl
  • ltlt s.GetSize() ltlt endl
  • return 0

http//ecomputernotes.com
31
Type Conversion
  • Output
  • String(int) called
  • 65
  • 2

ASCII code for A !!!
String size is also 2 instead of 1
32
Type Conversion
  • There is a mechanism in C to restrict automatic
    conversions
  • Keyword explicit
  • Casting must be explicitly performed by the user

http//ecomputernotes.com
33
Type Conversion
  • Keyword explicit only works with constructors
  • Example
  • class String
  • public
  • explicit String(int)

http//ecomputernotes.com
34
Type Conversion
  • int main()
  • String s
  • // Error
  • s A
  • return 0

http//ecomputernotes.com
35
Type Conversion
  • int main()
  • String s1, s2
  • // valid, explicit casting
  • s1 String(101)
  • // OR
  • s2 (String)204
  • return 0

http//ecomputernotes.com
36
Type Conversion
  • There is another method for type conversion
  • Operator overloading
  • (Converting from current type to any other type)

http//ecomputernotes.com
37
Type Conversion
  • General Syntax
  • TYPE1Operator TYPE2()
  • Must be a member function
  • NO return type and arguments are specified
  • Return type is implicitly taken to be TYPE2 by
    compiler

38
Type Conversion
  • Overloading pre-defined types
  • class String
  • public
  • operator int()
  • operator char ()

http//ecomputernotes.com
39
Type Conversion
  • Stringoperator int()
  • if(size gt 0)
  • return atoi(bufferPtr)
  • else
  • return -1

http//ecomputernotes.com
40
Type Conversion
  • Stringoperator char ()
  • return bufferPtr

http//ecomputernotes.com
41
Type Conversion
  • int main()
  • String s("2324")
  • cout ltlt (int)s ltlt endl
  • ltlt (char )s
  • return 0

http//ecomputernotes.com
42
Type Conversion
  • Output
  • 2324
  • 2324

http//ecomputernotes.com
43
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()

http//ecomputernotes.com
44
Type Conversion
  • Modifying String class
  • class String
  • public
  • String(char )
  • operator int()

http//ecomputernotes.com
45
Type Conversion
  • int main()
  • String s(Fakhir")
  • // ltlt is NOT overloaded
  • cout ltlt s
  • return 0

http//ecomputernotes.com
46
Type Conversion
  • Output
  • Junk Returned

http//ecomputernotes.com
47
Type Conversion
  • Modifying String class
  • class String
  • public
  • String(char )
  • int AsInt()

http//ecomputernotes.com
48
Type Conversion
  • int StringAsInt()
  • if(size gt 0)
  • return atoi(bufferPtr)
  • else
  • return -1

http//ecomputernotes.com
49
Type Conversion
  • int main()
  • String s(434")
  • // ltlt is NOT overloaded
  • cout ltlt s //error
  • cout ltlt s.AsInt()
  • return 0

http//ecomputernotes.com
Write a Comment
User Comments (0)
About PowerShow.com