Kate Gregory with material from Deitel and Deitel - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

Kate Gregory with material from Deitel and Deitel

Description:

Kate Gregory. with material from Deitel and Deitel. CO 204. Week 6 ... Kate Gregory. with material from Deitel and Deitel. CO 204. Encapsulation to the Rescue ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 50
Provided by: kategr
Category:

less

Transcript and Presenter's Notes

Title: Kate Gregory with material from Deitel and Deitel


1
Week 6
  • Lab 2 is marked
  • Hand in Lab 3
  • Questions from Last Week
  • Operator Overloading
  • Lab 4

2
Schedule
3
Stack vs. Heap
  • Allocate on the stack
  • if (x 0)
  • Rectangle r(3,4)
  • cout
  • Object exists only while execution is between the
    brace brackets

4
Stack vs. Heap
  • Allocate on the heap
  • if (x 0)
  • Rectangle pr new Rectangle(3,4)
  • cout area()
  • Object continues to exist until it is deleted
  • If you plan to save a pointer and use it
    elsewhere, it must point to a location on the heap

5
Gets and Sets
  • class BankAccount
  • private
  • int balance //in pennies
  • public
  • int getbalance() return balance
  • void setbalance(int bal)
  • balance bal
  • // other stuff

6
Encapsulation to the Rescue
  • class BankAccount
  • private
  • float dollarbalance //in dollars
  • public
  • int getbalance()
  • return (int) (dollarbalance100)
  • void setbalance(int bal)
  • dollarbalance bal/100.0
  • // other stuff

7
Overloading Revisited
  • When a two functions have the same name, they are
    overloaded
  • class foo
  • public
  • int something(int x)
  • int something()

8
Operator Overloading
  • All languages have operator overloading
  • int x 1 1
  • float f 1.0 1.0
  • Usually, the compiler is the only one who gets
    to play

9
Operator Overloading
  • How do you compare two objects?
  • Date d1, d2
  • // they get values somehow
  • if (d1.equals(d2))
  • // something

10
Operator Overloading
  • Wouldnt this be nicer?
  • Date d1, d2
  • // they get values somehow
  • if (d1 d2)
  • // something

11
Operator Overloading
  • How about this?
  • Date d1, d2
  • // they get values somehow
  • if (d1 ! d2)
  • d1 d2 3

12
How do you overload an operator?
  • You write a function
  • The name is operator followed by the symbol
  • operator
  • operator

13
Where does the function go?
  • For binary operators
  • class A, B
  • A a
  • B b
  • // give them values somehow
  • int x a b

14
Operator Overload as Member Function
  • // in A.h
  • class A
  • // whatever else it has
  • int operator(B b)
  • // in A.cpp
  • int Aoperator(B b)
  • // something

15
Operator Overload as Global Function
  • // not in any class
  • int operator(A a, B b)
  • Typically the declaration is in the header file
    for A and the implementation is in the
    implementation file for A

16
Operator Overload as Global Function
  • // in A.h
  • class A
  • // whatever else it has
  • int operator(A a, B b)
  • // in A.cpp
  • int operator(A a, B b)
  • // something

17
Your class isnt always on the left
  • class A
  • A a
  • int x A 2
  • int y 2 A
  • What functions does the compiler look for?

18
Your class on the left
  • int x A 2
  • Aoperator(int i)
  • operator(A a, int i)
  • The choice is yours

19
Your class on the right
  • int x 2 A
  • intoperator(A a)
  • Not possible!
  • operator(int i, A a)
  • Your only choice

20
Coding a Global Operator
  • Sometimes its easy
  • class A
  • private
  • int x
  • public
  • int operator(int arg)
  • //other stuff
  • int operator(int arg, A a)

21
Coding a Global Operator
  • Heres a neat trick
  • int Aoperator(int arg)
  • return x arg
  • int operator(int arg, A a)
  • return a arg

22
Coding a Global Operator
  • Sometimes the operator is not reversible like
    that
  • The global function will need access to private
    member variables of the class it works with
  • It needs to be an honourary member of the class
  • friend

23
Where does the function go?
  • For unary operators
  • class U
  • U u1, u2
  • // give them values somehow
  • u2 !u1

24
Operator Overload as Member Function
  • // in U.h
  • class U
  • // whatever else it has
  • U operator!()
  • // in U.cpp
  • U Uoperator()
  • // something

25
Operator Overload as Global Function
  • // in U.h
  • class U
  • // whatever else it has
  • U operator!(U u)
  • // in U.cpp
  • U operator!(U u)
  • // something

26
What can you overload?
  • Binary
  • - /
  • - /
  • -- (pre and post)

27
What can you overload?
  • Unary
  • -
  • !
  • Some other scary ones eg

28
Operator Consistency
  • Operator should do something that feels like
    adding
  • Matrix add, complex number add
  • Container (list, queue) add an element
  • String concatenate
  • Increase date
  • Dont mess with peoples heads

29
Operator Consistency
  • If you have defined similar operators, they
    should work the same way.
  • These three expressions should all have the same
    result
  • A a
  • a a 1
  • a 1
  • a

30
Tip
  • Use one operator to implement the others
  • bool Aoperator(const A arg)
  • // whatever
  • bool Aoperator!(const A arg)
  • return !(this arg)

31
More Rules
  • You cant change the order of operations
  • You cant invent new operators, including unary
    versions of binary-only operators such as /.
  • You cant overload operators that work on only
    fundamental types
  • int operator(int i, int j)

32
Overloading
  • cout
  • Employee e
  • cout
  • Compiler is looking for
  • ostream operator
  • Typically the code is just like a display()
    function
  • Return the ostream that was passed

  • 33
    Simple Array Class
    • Implement an Array class with
    • Range checking
    • Array assignment
    • Arrays that know their size
    • Outputting entire arrays with
    • Array comparisons with and !
    • Element access with

    34
    array.h
    • class Array
    • friend ostream operatorArray a)
    • public
    • Array( int size 10 )
    • Array( const Array a) //copy constructor
    • Array()
    • int getSize() const return size
    • const Array operator( const Array a)
    • bool operator( const Array a) const
    • bool operator!( const Array right ) const
    • return !( this right )
    • int operator( int )
    • const int operator( int ) const
    • private
    • int size
    • int ptr // pointer to actual content

    35
    Using the Array class
    • int main()
    • Array integers1(7), integers2
    • cout
    • cout
    • integers15 1000
    • cout
    • if ( integers1 integers2 )
    • cout
    • else
    • cout
    • return 0

    36
    Implementing Array - constructor
    • ArrayArray( int arraySize )
    • size ( arraySize 0 ?
    • arraySize 10 )
    • ptr new int size
    • for ( int i 0 i
    • ptr i 0

    37
    Implementing Array - destructor
    • ArrayArray()
    • delete ptr
    • Whenever your destructor does something
      destructive, you must code a copy constructor and
      an assignment operator

    38
    Destructive Destructors
    • Not all classes have a destructor that actually
      cleans up
    • Free memory
    • Close file
    • Release lock or database connection or ...
    • When the destructor cleans up you need to be sure
      it will never go off accidentally

    39
    Shallow and Deep Copies
    • An object holds a resource (pointer to memory,
      name of file, pointer to connection object)
    • If you copy the object bit-for-bit, now two
      objects hold the same pointer (or handle or name
      or whatever)
    • When one goes out of scope, the destructor cleans
      up
    • The other has a pointer to nowhere!

    40
    Implementing Array copy constructor
    • ArrayArray( const Array init ) size(
      init.size ), ptr(new intinit.size)
    • for ( int i 0 i
    • ptri init.ptri

    41
    Copy Constructor takes a reference
    • Imagine this code
    • Array a2(a1)
    • This uses the copy constructor to construct a2
    • Takes by value need to make a copy of a1 to pass
      to the function
    • Use the copy constructor.
    • But that takes by value, so need to make a copy
    • Use the copy constructor
    • . . .

    42
    Implementing Array assignment operator
    • const Array Arrayoperator( const Array right
      )
    • // always check for self-assignment
    • if ( right ! this )
    • if ( size ! right.size )
    • delete ptr
    • size right.size
    • ptr new int size
    • for ( int i 0 i
    • ptr i right.ptr i
    • return this // enables x y z

    43
    Copy Constructor vs Assignment operator
    • Its not about whether the operator is used
    • Its about whether something is being constructed
    • Array a1(10)
    • Array a2(a1)
    • Array a3 a2
    • a1 a3

    44
    Implementing Array equality test
    • bool Arrayoperator( const Array right )
      const
    • if ( size ! right.size )
    • return false // arrays of different
      sizes
    • for ( int i 0 i
    • if ( ptr i ! right.ptr i )
    • return false // arrays are not equal
    • return true // arrays are equal

    45
    Implementing Array element access
    • int Arrayoperator( int subscript )
    • if( subscript 0 subscript
    • return ptr subscript
    • else
    • // should throw exception or something
    • exit(1)
    • Why does it return a reference?
    • integers15 1000

    46
    Implementing Array
    • const int Arrayoperator( int subscript )
      const
    • if( subscript 0 subscript
    • return ptr subscript // const reference
    • else
    • // should throw exception or something
    • exit(1)
    • For use with const arrays (since its const)
    • Guarantees the object will stay const by not
      providing a reference to the inside, which could
      be changed.

    47
    Implementing Array
    • ostream operatorArray a )
    • int i
    • for ( i 0 i
    • output
    • return output //enables cout

    48
    Remember The Requirements
    • Implement an Array class with
    • Range checking
    • Array assignment
    • Arrays that know their size
    • Outputting entire arrays with
    • Array comparisons with and !
    • Element access with

    49
    For Next class
    • Enjoy Reading Week see you Feb 24
    • Read chapter 9
    • Do Lab 4
    • Study for Midterm
    • Feb 24, class time, this room
    • Will cover everything till today
    • You may be asked to write code in handwriting
    • One hour, closed book
    • Worth 25
    Write a Comment
    User Comments (0)
    About PowerShow.com