Operator Overloading - PowerPoint PPT Presentation

About This Presentation
Title:

Operator Overloading

Description:

The assignment operator (=) may be used with every class without explicit overloading. ... Differences between copy constructor and assignment operator ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 10
Provided by: Penelope69
Learn more at: https://www.cse.unr.edu
Category:

less

Transcript and Presenter's Notes

Title: Operator Overloading


1
Operator Overloading
  • CS 308 Data Structures

2
What is operator overloading?
  • Changing the definition of an operator so it can
    be applied on the objects of a class is called
    operator overloading.
  • To overload an operator, we need to write a
    function for the operator we are overloading.

3
Special cases
  • The assignment operator () may be used with
    every class without explicit overloading.
  • The default behavior of the assignment operator
    is a memberwise assignment of the data members of
    the class.
  • The address operator () may also be used with
    objects of any class without explicit
    overloading.
  • It returns the address of the object in memory.

4
Explicit overloading of the assignment operator
  • The default overloading is not enough for classes
    with pointer members.
  •  
  • void operator(class_name)
  •  
  • class string
  • private
  • char s
  • int size
  • public
  • string(char ) // constructor
  • string() // destructor
  • void operator(string)
  • void print()
  • void copy(char )

5
  • void stringoperator(string old_str)
  • char tmp
  • size old_str.size
  • tmp new charsize1 // assign new memory
  • strcpy(tmp, old_str.s)
  • delete s // must release previously
    assigned memory
  • s tmp
  •  
  • void main()
  • string str1("George")
  • string str2("Mary")
  • string str3("John")
  •  
  • str1.print() // what is printed ?
  • str2.print()

str2 str1 str3.copy("Ha ha")  
str1.print() // what is printed now ?
str2.print()  
6
Differences between copy constructor and
assignment operator
  • The copy constructor creates a new object.
  • The assignment operator works on an already valid
    object.

7
Another example overloading the operator
  • class Array
  • private
  • int numElems
  • int arr
  • public
  • Array(int) // constructor
  • Array() // destructor
  • int operator(int)
  •  

ArrayArray(int n) numElems n arr new
intn   ArrayArray() delete arr
8
  • int Arrayoperator(int index)
  • if ((index lt 0) (index gt numElems))
  • cout ltlt "Out of bounds error !!" ltlt endl
  • exit(0) // error invalid index !!
  • else
  • return(arrindex)
  •  
  • void main()
  • int i
  • Array A(10)
  •  
  • for(i0 ilt10 i) // i10 error !!
  • Ai i

9
Comments on operator overloading
  • Attempting to create new operators via operator
    overloading is a syntax error.
  • Attempting to change the "arity" of an operator
    via operator overloading is a syntax error.
  • Overloading is allowed only if at least one
    operand is a class instance (e.g., you cannot
    overload an operator to take two integers as
    operands).
Write a Comment
User Comments (0)
About PowerShow.com