Programming Techniques Classes II - PowerPoint PPT Presentation

Loading...

PPT – Programming Techniques Classes II PowerPoint presentation | free to view - id: 689bb4-ODQwN



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Programming Techniques Classes II

Description:

Programming Techniques Classes II Important Class Features Spring 2009 – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 91
Provided by: Magda8
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Programming Techniques Classes II


1
Programming Techniques Classes II
  • Important Class Features
  • Spring 2009

2
References
  • C How to Program,
  • Deitel, Prentice Hall, 2003 by Pearson
    Education, Fourth Edition
  • Object Oriented Programming in C,
  • Robert Lafore, Waite Group Press, 1995, Second
    Edition

3
Classes II
  • Agenda
  • const objects and const member functions
  • friend functions and friend classes
  • Composition Objects as Members of Classes
  • Dynamic Memory Management with Operators new and
    delete
  • this pointer
  • static data members and static functions
  • The Container class
  • The iterator class

4
1. const objects and const member functions
  • Keyword const
  • Specify object not modifiable
  • Compiler error if attempt to modify const object
  • Example
  • const Time noon( 12, 0, 0 )
  • Declares const object noon of class Time
  • Initializes to 12

5
1. const objects and const member functions
  • const member functions
  • Member functions for const objects must also be
    const
  • Cannot modify object
  • Specify const in both prototype and definition
  • in Prototype
  • After parameter list
  • in Definition
  • Before beginning left brace

? Constructors and destructors Cannot be const
as they Must be able to modify objects
6
1. const objects and const member functions
  • Example 1

7
1. const objects and const member functions
  • Example 1

8
1. const objects and const member functions
const functions do not modify objects.
  • Example 1

9
1. const objects and const member functions
  • Example 1

10
1. const objects and const member functions
  • Example 1

Attempting to invoke non-const member function on
const object results in compiler error.
Attempting to invoke non-const member function on
const object results in compiler error even if
function does not modify object.
11
1. const objects and const member functions
  • Const member functions
  • Can be used for
  • All class objects
  • Must be used for
  • Const class objects

12
1. const objects and const member functions
  • Member initializer syntax
  • Can be used for
  • All data members
  • Must be used for
  • const data members
  • Data members that are references

13
1. const objects and const member functions
  • Example 2

14
1. const objects and const member functions
  • Example 2

15
1. const objects and const member functions
  • Example 2

16
Classes II
  • Agenda
  • const objects and const member functions
  • friend functions and friend classes
  • Composition Objects as Members of Classes
  • Dynamic Memory Management with Operators new and
    delete
  • this pointer
  • static data members and static functions
  • The Container class
  • The iterator class

17
2. friend functions and friend classes
  • friend functions
  • are defined outside classs scope
  • they have the right to access non-public members

18
2. friend functions and friend classes
  • Declaring friends
  • A function F ( ) by preceding function
    prototype with keyword friend inside class
  • A class classTwo as friend of another class
    class One by placing declaration of form
  • Example
  • friend F ( )
  • friend class ClassTwo
  • inside ClassOne definition

19
2. friend functions and friend classes
  • Properties of friendship
  • Friendship granted, not taken i.e.
  • Class B friend of class A
  • Class A must explicitly declare class B friend
  • Not symmetric
  • Class B friend of class A
  • Class A not necessarily friend of class B

20
2. friend functions and friend classes
  • Properties of friendship (cont.)
  • Not transitive
  • Class A friend of class B
  • Class B friend of class C
  • Class A not necessarily friend of Class C
  • friend

21
2. friend functions and friend classes
  • Example 1

22
2. friend functions and friend classes
  • Example 1

NOT a member function!
23
2. friend functions and friend classes
  • Example 1

24
2. friend functions and friend classes
  • Example 2 A global function as friend
  • class Distance // English Distance class
  • friend Distance Add_Dist (Distance , Distance)
  • private
  • int feet float inches
  • public
  • Distance ( ) // constructor (no args)
  • Distance (int ft, float in) ...................
    .......

25
2. friend functions and friend classes
  • Example 2 function implementation
  • Distance Add_Dist(Distance d2, Distance d3)
  • float inches d2.inches d3.inches int
    feet 0
  • if (inches gt 12.0)
  • inches - 12.0 feet
  • feet d2.feet d3.feet // add the feet
  • return Distance (feet, inches)

Non-member function!

Creating a member and returning its value!
26
2. friend functions and friend classes
  • Example 2
  • main ( )
  • Distance d1 (7, 5), d2 (3, 4)
  • Distance d3
  • d3 Add_Dist ( d1, d2)

How was it in the previous example of Add_dist?
Compare!
27
2. friend functions and friend classes
  • Example 3 class as a friend of another class
  • class X
  • int a, b
  • friend class F
  • public
  • X(int i1, int j 2 ) a(i), b(j)

28
2. friend functions and friend classes
  • Example 3 class as a friend of another class
  • class F
  • public
  • void print( X x )
  • cout ltlt "a is " ltlt x . a ltlt endl
  • cout ltlt "b is " ltlt x . b ltlt endl

29
2. friend functions and friend classes
  • Example 3 class as a friend of another class
  • int main( )
  • X xobj
  • F fobj
  • fobj . Print ( xobj )

Another alternative was to make only FPrint ( )
a friend to class X
30
Classes II
  • Agenda
  • const objects and const member functions
  • friend functions and friend classes
  • Composition Objects as Members of Classes
  • Dynamic Memory Management with Operators new and
    delete
  • this pointer
  • static data members and static functions
  • The Container class
  • The iterator class

31
3. Composition Objects as Members of Classes
  • Composition an object inside another one gt
    very strong automatic relationship
  • Aggregation an object inside another one, also.
    If implementation is done using pointers, then
    the outer object (may or may not be) is
    responsible for const and destruc. of inner
    objects.
  • Association an object with pointer to another
    object(s). Outer object is not resp. for const or
    destruc of the other object.

32
3. Composition Objects as Members of Classes
  • Composition versus Aggregation
  • In both we talk about objects of one class
    declared inside another class.
  • Aggregation differs from ordinary composition in
    that it pointers may be used, while in
    composition they are automatic.
  • In both, when the owning object is destroyed, so
    are the contained objects.

33
3. Composition Objects as Members of Classes
  • Composition versus Aggregation
  • Example
  • A university owns various departments (e.g.,
    chemistry)
  • Composition If departments are implemented as
    static array
  • Aggregation If departments are implemented as
    pointers

34
3. Composition Objects as Members of Classes
  • What about Association ?
  • An object with pointer to another object(s).
    Outer object is NOT resp. for const or destruc of
    the other object.
  • Example
  • Association Each department has a number of
    professors.
  • When university is deleted, professors are not
    deleted ( Refer to website notes)

35
Example
36
3. Composition Objects as Members of Classes
  • Construction of member objects
  • Member objects are constructed in order of
    declaration
  • Member objects are constructed before the
    enclosing outer class objects (host objects)

37
3. Composition Objects as Members of Classes
  • Example

38
3. Composition Objects as Members of Classes
  • Example

39
3. Composition Objects as Members of Classes
40
3. Composition Objects as Members of Classes
  • Example

41
3. Composition Objects as Members of Classes
  • Example

42
3. Composition Objects as Members of Classes
  • Example

43
3. Composition Objects as Members of Classes
  • Example

44
3. Composition Objects as Members of Classes
  • Example

45
3. Composition Objects as Members of Classes
46
3. Composition Objects as Members of Classes
  • Aggregation As opposed to composition
  • class Sales_info
  • float amount
  • Date ddmmyy

47
3. Composition Objects as Members of Classes
  • Aggregation As opposed to composition
  • class Sales_Person
  • Sales_info sales
  • public
  • Sales_Person (int num_sales 10)
  • Sales_Person ( )

48
3. Composition Objects as Members of Classes
  • Aggregation As opposed to composition
  • Sales_Person SalesPerson ( int c 10)
  • sales new sales_info c
  • Sales_Person Sales_Person ( )
  • delete Sales

49
3. Composition Objects as Members of Classes
  • Association As opposed to composition and
    aggregation
  • class Prof // forward declaration
  • class Course
  • char course_id 8 ..
  • Prof prof
  • public
  • Course ( char id, .., Prof p)
  • Course ( )

50
3. Composition Objects as Members of Classes
  • Association As opposed to composition and
    aggregation
  • Course Course ( char id, .., Prof p)
  • .
  • prof p
  • Course ( )
  • // No deletion of prof pointer!!
  • // He is still allowed to exist! ?!

51
Classes II
  • Agenda
  • const objects and const member functions
  • friend functions and friend classes
  • Composition Objects as Members of Classes
  • Dynamic Memory Management with Operators new and
    delete
  • this pointer
  • static data members and static functions
  • The Container class
  • The iterator class

52
4. Dynamic memory management
  • Same operators new and delete
  • new operator
  • e.g.
  • Time timePtr
  • timePtr new Time
  • Creates object of proper size for type Time
  • Calls default constructor for object
  • Error if no space in memory for object
  • Returns pointer of specified type

53
4. Dynamic memory management
  • Providing initializers
  • double ptr new double( 3.14159 )
  • Time timePtr new Time( 12, 0, 0 )
  • Allocating arrays
  • int gradesArray new int 10
  • Time timesArray new Time5

54
4. Dynamic memory management
  • delete
  • Destroy dynamically allocated object and free
    space
  • e.g.
  • delete timePtr
  • Operator delete
  • Calls destructor for object
  • Deallocates memory associated with object
  • Memory can be reused to allocate other objects

55
4. Dynamic memory management
  • Deallocating arrays
  • e.g.
  • delete gradesArray
  • Deallocates array to which gradesArray points
  • If pointer to array of objects
  • First calls destructor for each object in array
  • Then deallocates memory

56
Classes II
  • Agenda
  • const objects and const member functions
  • friend functions and friend classes
  • Composition Objects as Members of Classes
  • Dynamic Memory Management with Operators new and
    delete
  • this pointer
  • static data members and static functions
  • The Container class
  • The iterator class

57
5. this Pointer
  • Allows object to access own address
  • Not part of object itself
  • Implicit argument to non-static member function
    call
  • Implicitly reference member data and functions
  • Type of this pointer depends on Type of object

58
5. this Pointer
  • Type of this pointer depends on whether member
    function is const or not
  • In non-const member function of Employee this has
    type Employee const
  • i.e. Constant pointer to non-constant Employee
    object
  • In const member function of Employee this has
    type const Employee const
  • i.e. Constant pointer to constant Employee
    object

59
5. this Pointer
  • Example

60
5. this Pointer
  • Example

61
5. this Pointer
Will be explained more in operator moverloading!
  • Cascaded member function calls are possible using
    this!
  • Multiple functions invoked in same statement
    (Casacded member function calls)
  • Function returns reference pointer to same object
    return this
  • Other functions operate on that pointer
  • Functions that do not return references must be
    called last

62
5. this Pointer
  • Example

What is the return value of setHour, setMinute
and setSecond?
63
Classes II
  • Agenda
  • const objects and const member functions
  • friend functions and friend classes
  • Composition Objects as Members of Classes
  • Dynamic Memory Management with Operators new and
    delete
  • this pointer
  • static data members and static functions
  • The Container class
  • The iterator class

64
6. static data members and static functions
  • Static class variables are accessible through any
    object of class
  • public static variables can also be accessed
    using binary scope resolution operator()
  • Employee count
  • private static variables can only be accessed via
    public static member function (even when no class
    member objects exist)

65
6. static data members and static functions
  • To call public static member function combine
    class name with binary scope resolution operator
    () and function name
  • Employee getCount( )

66
6. static data members and static functions
  • static member functions and static data members
    exist independent of objects, hence
  • Static member functions cannot access non-static
    data or functions
  • No this pointer for static functions

67
6. static data members and static functions
  • Example

Compare with previous declaration for Name
strings!
68
6. static data members and static functions
  • in .cpp file

69
6. static data members and static functions
  • constructor in .cpp file

70
6. static data members and static functions
  • .cpp file

Operator delete deallocates memory.
Use static data member to store total count of
employees.
71
6. static data members and static functions
  • In main

new operator dynamically allocates space.
72
6. static data members and static functions
  • main (cont.)

73
6. static data members and static functions
  • Output

74
Classes II
  • Agenda
  • const objects and const member functions
  • friend functions and friend classes
  • Composition Objects as Members of Classes
  • Dynamic Memory Management with Operators new and
    delete
  • this pointer
  • static data members and static functions
  • The Container class and the iterator class
  • Proxy classes and forward declaration

75
7. The Container class and the iterator class
  • Composition that is used to store several
    instances of the composited data type is referred
    to as containment.
  • Examples are
  • arrays
  • linked lists
  • binary trees .

76
7. The Container class and the iterator class
  • These are classes that hide implemen-tation
    details from clients
  • Example stack data structure Client only wants
    LIFO data structure, he does not care how stack
    implemented
  • Data elements added (pushed) onto top
  • Data elements removed (popped) from top
  • Last-in, first-out (LIFO) data structure

77
7. The Container class and the iterator class
  • Example 1 An array abstract data type could
    include
  • Subscript range checking
  • Arbitrary range of subscripts instead of having
    to start with 0
  • Array assignment
  • Array comparison
  • Arrays that know their sizes
  • Arrays that expand dynamically to accommodate
    more elements

78
7. The Container class and the iterator class
  • Example 2 Queue Abstract Data Type
  • First in, first out (FIFO)
  • Enqueue Put items in queue one at a time
  • Dequeue Remove items from queue one at a time

79
7. The Container class and the iterator class
  • Queue ADT
  • Implementation hidden from clients
  • Clients may not manipulate data structure
    directly
  • Only queue member functions can access internal
    data

80
7. The Container class and the iterator class

C provides mechanisms for creating and
implementing abstract data type
81
7. The Container class and the iterator class
  • Container classes (collection classes) are
    designed to hold collections of objects
  • Common services
  • Insertion, deletion, searching, sorting, or
    testing an item
  • Examples
  • Arrays, stacks, queues, trees and linked lists

82
7. The Container class and the iterator class
  • Iterator objects (iterators)
  • Returns next item of collection or performs some
    action on next item
  • Can have several iterators per container e.g.
  • Book with multiple bookmarks
  • Each iterator maintains own position
  • Discussed further in Chapter 20

83
Classes II
  • Agenda
  • const objects and const member functions
  • friend functions and friend classes
  • Composition Objects as Members of Classes
  • Dynamic Memory Management with Operators new and
    delete
  • this pointer
  • static data members and static functions
  • The Container class and the iterator class
  • Proxy classes and forward declaration

84
8. Proxy classes and forward declaration
  • Proxy class
  • Hide implementation details of another class
  • Knows only public interface of class being hidden
  • Enables clients to use classs services without
    giving access to classs implementation

85
8. Proxy classes and forward declaration
  • Example class implementation

86
8. Proxy classes and forward declaration
  • class implementation (cont.)

87
8. Proxy classes and forward declaration
  • The Implementation class

88
8. Proxy classes and forward declaration
  • The interface implementation

89
8. Proxy classes and forward declaration
  • The interface implementation(cont.)

90
8. Proxy classes and forward declaration
  • main ( )
About PowerShow.com