Docstoc

UNIX-LEC-review

Document Sample
UNIX-LEC-review Powered By Docstoc
					                                                                  1




                                                    IS 0020
                      Program Design and Software Tools
                           Introduction to C++ Programming


                                                  Review slides
                                                  Mar 15, 2005




 2003 Prentice Hall, Inc. All rights reserved.
                                                                                 2

      Review topics
      1.      Lvalue/rvalue
      2.      Reference variable + Calling functions by reference
      3.      Passing arrays to functions
      4.      Function pointers
      5.      Arrays of pointers to functions (e.g. menus)
      6.      Using member selection operators ('.' '->')
      7.      Returning a reference to a private data member
      8.      *this pointer
              1. implicit & explicit use of *this pointer
              2. *this pointer & cascaded function calls
      9. When are destructors used, why not yet in the classes we're creating?
      10. When is it appropriate to use 'new' and 'delete' from the <new>
          library?
      11. Classes?
      12. Polymorphism?
      13. Multiple Inheritance?


 2003 Prentice Hall, Inc. All rights reserved.
                                                                        3
      Confusing Equality (==) and Assignment (=)
      Operators
      • Lvalues
              – Expressions that can appear on left side of equation
              – Can be changed
                     x = 4;
      • Rvalues
              – Only appear on right side of equation
              – Constants, such as numbers (i.e. cannot write 4 = x;)
      • Lvalues can be used as rvalues, but not vice versa




 2003 Prentice Hall, Inc. All rights reserved.
                                                              4

      References and Reference Parameters

      • Call by value
              – Copy of data passed to function
              – Changes to copy do not change original
              – Prevent unwanted side effects
      • Call by reference
              – Function can directly access data
              – Changes affect original
      • Reference parameter
              – Alias for argument in function call
                     • Passes parameter by reference
              – Use & after data type in prototype
                     • void myFunction( int &data )
                     • Read “data is a reference to an int”
              – Function call format the same
                     • However, original can now be changed

 2003 Prentice Hall, Inc. All rights reserved.
                                                                               5

      References and Reference Parameters

      • Pointers
              – Another way to pass-by-refernce
      • References as aliases to other variables
              – Refer to same variable
              – Can be used within a function
                     int count = 1;     // declare integer variable count
                     int &cRef = count; // create cRef as an alias for count
                     ++cRef; // increment count (using its alias)

      • References must be initialized when declared
              – Otherwise, compiler error
              – Dangling reference
                     • Reference to undefined variable


 2003 Prentice Hall, Inc. All rights reserved.
                                                                     6

      Passing Arrays to Functions

      • Specify name without brackets
              – To pass array myArray to myFunction
                                    int myArray[ 24 ];
                                    myFunction( myArray, 24 );
              – Array size usually passed, but not required
                     • Useful to iterate over all elements
      • Arrays passed-by-reference
              – Functions can modify original array data
              – Value of name of array is address of first element
                     • Function knows where the array is stored
                     • Can change original memory locations



 2003 Prentice Hall, Inc. All rights reserved.
                                                                         7

      Passing Arrays to Functions

      • Functions taking arrays
              – Function prototype
                     • void modifyArray( int b[], int arraySize );
                     • void modifyArray( int [], int );
                         – Names optional in prototype
                     • Both take an integer array and a single integer
              – No need for array size between brackets
                     • Ignored by compiler
              – If declare array parameter as const
                     • Cannot be modified (compiler error)
                     • void doNotModify( const int [] );




 2003 Prentice Hall, Inc. All rights reserved.
                                                                              8

      Using const with Pointers
      • const qualifier
              – Value of variable should not be modified
              – const used when function does not need to change a variable
              – Principle of least privilege
      • const pointers
              – Always point to same memory location
              – Default for array name
              – Must be initialized when declared
      • Four ways to pass pointer to function
              – Nonconstant pointer to nonconstant data
                     • Highest amount of access
              – Nonconstant pointer to constant data
              – Constant pointer to nonconstant data
              – Constant pointer to constant data
                     • Least amount of access

 2003 Prentice Hall, Inc. All rights reserved.
                                                                                                               9
1    // Fig. 5.13: fig05_13.cpp
2    // Attempting to modify a constant pointer to
                                                                                           Outline
3    // non-constant data.
4
                                                                                  fig05_13.cpp
5    int main()
6    {
                                                                                  (1 of 1)
7       int x, y;
8                                                                                 fig05_13.cpp
9                                               ptr is constant
        // ptr is a constant pointer to an integer that can pointer       to      output (1 of 1)
10                                Can modify x (pointed to to
        // be modified through ptr, but ptr always pointsby the
                                                integer.
11      // same memory location.
                                  ptr) since x not constant.
                                  Cannot modify ptr to point
12      int * const ptr = &x;
13                                  to new address since ptr is
14      *ptr = 7;    // allowed:    constant.
                                 *ptr is not const
15      ptr = &y;    // error: ptr is const; cannot assign new address
                                                   Line 15 generates compiler
16
17      return 0;    // indicates successful       error by
                                               termination attempting to assign
18                                                 new address to constant
19   } // end main                                 pointer.

d:\cpphtp4_examples\ch05\Fig05_13.cpp(15) : error C2166:
   l-value specifies const object




                                                                                   2003 Prentice Hall, Inc.
                                                                                  All rights reserved.
                                                                                                        10
1    // Fig. 5.14: fig05_14.cpp
2    // Attempting to modify a constant pointer to constant data.
                                                                                    Outline
3    #include <iostream>
4
                                                                           fig05_14.cpp
5    using std::cout;
6    using std::endl;
                                                                           (1 of 1)
7
8    int main()
9    {
10      int x = 5, y;
11
12                                          ptr is integer.
        // ptr is a constant pointer to a constant constant pointer   to
13                                          integer constant.
        // ptr always points to the same location; the integer
14      // at that location cannot be modified.
15      const int *const ptr = &x;
16                                 Cannot modify x (pointed to
17      cout   << *ptr << endl;    by ptr) since *ptrto point
                                   Cannot modify ptr declared
18                                 to new address since ptr is
                                   constant.
19      *ptr                        is const;
               = 7; // error: *ptr constant. cannot assign new  value
20      ptr = &y;    // error: ptr is const; cannot assign new address
21
22      return 0;    // indicates successful termination
23
24   } // end main




                                                                            2003 Prentice Hall, Inc.
                                                                           All rights reserved.
                                                                           11

      Relationship Between Pointers and Arrays

      • Arrays and pointers closely related
              – Array name like constant pointer
              – Pointers can do array subscripting operations
      • Accessing array elements with pointers
              – Element b[ n ] can be accessed by *( bPtr + n )
                     • Called pointer/offset notation
              – Addresses
                     • &b[ 3 ] same as bPtr + 3
              – Array name can be treated as pointer
                     • b[ 3 ] same as *( b + 3 )
              – Pointers can be subscripted (pointer/subscript notation)
                     • bPtr[ 3 ] same as b[ 3 ]


 2003 Prentice Hall, Inc. All rights reserved.
                                                                                      12

      Function Pointers

      • Calling functions using pointers
              – Assume parameter:
                     • bool ( *compare ) ( int, int )
              – Execute function with either
                     • ( *compare ) ( int1, int2 )
                             – Dereference pointer to function to execute
                   OR
                     • compare( int1, int2 )
                             – Could be confusing
                                • User may think compare name of actual function in
                                  program




 2003 Prentice Hall, Inc. All rights reserved.
                                                                                                               13
1    // Fig. 5.25: fig05_25.cpp
2    // Multipurpose sorting program using function pointers.
                                                                                           Outline
3    #include <iostream>
4
                                                                                  fig05_25.cpp
5    using std::cout;
6    using std::cin;
                                                                                  (1 of 5)
7    using std::endl;
8
9    #include <iomanip>
10
11   using std::setw;                                          Parameter is pointer to
12                                                             function that receives two
13   // prototypes                                             integer parameters and returns
14   void bubble( int [], const int, bool (*)( int, int ) );   bool result.
15   void swap( int * const, int * const );
16   bool ascending( int, int );
17   bool descending( int, int );
18
19   int main()
20   {
21      const int arraySize = 10;
22      int order;
23      int counter;
24      int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
25




                                                                                   2003 Prentice Hall, Inc.
                                                                                  All rights reserved.
                                                                                                14
26   cout << "Enter 1 to sort in ascending order,\n"
27        << "Enter 2 to sort in descending order: ";
                                                                            Outline
28   cin >> order;
29   cout << "\nData items in original order\n";
                                                                   fig05_25.cpp
30
31   // output original array
                                                                   (2 of 5)
32   for ( counter = 0; counter < arraySize; counter++ )
33      cout << setw( 4 ) << a[ counter ];
34
35   // sort array in ascending order; pass function ascending
36   // as an argument to specify ascending sorting order
37   if ( order == 1 ) {
38      bubble( a, arraySize, ascending );
39      cout << "\nData items in ascending order\n";
40   }
41
42   // sort array in descending order; pass function descending
43   // as an agrument to specify descending sorting order
44   else {
45      bubble( a, arraySize, descending );
46      cout << "\nData items in descending order\n";
47   }
48




                                                                    2003 Prentice Hall, Inc.
                                                                   All rights reserved.
                                                                                                               15
49      // output sorted array
50      for ( counter = 0; counter < arraySize; counter++ )
                                                                                           Outline
51         cout << setw( 4 ) << a[ counter ];
52
                                                                                  fig05_25.cpp
53      cout << endl;
                                                                                  (3 of 5)
54
55      return 0;    // indicates successful termination
56
57   } // end main
58
59
                                                  compare is pointer to
     // multipurpose bubble sort; parameter compare is a pointer to
60                                               function that receives two
     // the comparison function that determines sorting order
61   void bubble( int work[], const int size,    integer parameters and returns
62                bool (*compare)( int, int ) ) bool result.
63   {
64      // loop to control passes                Parentheses necessary to
65      for ( int pass = 1; pass < size; pass++ )
                                                  indicate pointer to function
66
67         // loop to control number of comparisons per pass
68
                                          Call passed function
           for ( int count = 0; count < size - 1; count++ )
69
                                           compare; dereference
70            // if adjacent elements are  pointer to execute function.
                                           out of order, swap them
71            if ( (*compare)( work[ count ], work[ count + 1 ] ) )
72               swap( &work[ count ], &work[ count + 1 ] );




                                                                                   2003 Prentice Hall, Inc.
                                                                                  All rights reserved.
                                                                                                  16
73
74   } // end function bubble
                                                                              Outline
75
76   // swap values at memory locations to which
                                                                     fig05_25.cpp
77   // element1Ptr and element2Ptr point
78   void swap( int * const element1Ptr, int * const element2Ptr )
                                                                     (4 of 5)
79   {
80      int hold = *element1Ptr;
81      *element1Ptr = *element2Ptr;
82      *element2Ptr = hold;
83
84   } // end function swap
85
86   // determine whether elements are out of order
87   // for an ascending order sort
88   bool ascending( int a, int b )
89   {
90      return b < a;   // swap if b is less than a
91
92   } // end function ascending
93




                                                                      2003 Prentice Hall, Inc.
                                                                     All rights reserved.
                                                                                       17
94    // determine whether elements are out of order
95    // for a descending order sort
                                                                   Outline
96    bool descending( int a, int b )
97    {
                                                          fig05_25.cpp
98       return b > a;   // swap if b is greater than a
99
                                                          (5 of 5)
100   } // end function descending
                                                          fig05_25.cpp
Enter 1 to sort in ascending order,                       output (1 of 1)
Enter 2 to sort in descending order: 1

Data items in original order
   2   6   4   8 10 12 89 68         45   37
Data items in ascending order
   2   4   6   8 10 12 37 45         68   89



Enter 1 to sort in ascending order,
Enter 2 to sort in descending order: 2

Data items in original order
   2   6   4   8 10 12 89 68         45   37
Data items in descending order
  89 68 45 37 12 10        8   6      4   2




                                                           2003 Prentice Hall, Inc.
                                                          All rights reserved.
                                                                                     18

      Function Pointers

      • Arrays of pointers to functions
              – Menu-driven systems
              – Pointers to each function stored in array of pointers to
                functions
                     • All functions must have same return type and same parameter
                       types
              – Menu choice  subscript into array of function pointers




 2003 Prentice Hall, Inc. All rights reserved.
                                                                                                            19
1    // Fig. 5.26: fig05_26.cpp
2    // Demonstrating an array of pointers to functions.
                                                                                        Outline
3    #include <iostream>
4
                                                                               fig05_26.cpp
5    using std::cout;
6    using std::cin;
                                                                               (1 of 3)
7    using std::endl;
8
9    // function prototypes
10   void function1( int );
11   void function2( int );
12   void function3( int );
13
14   int main()
                                                Array initialized with names
15   {
16
                                                of three functions; function
        // initialize array of 3 pointers to functions that each
17      // take an int argument and return void names are pointers.
18      void (*f[ 3 ])( int ) = { function1, function2, function3 };
19
20      int choice;
21
22      cout << "Enter a number between 0 and 2, 3 to end: ";
23      cin >> choice;
24




                                                                                2003 Prentice Hall, Inc.
                                                                               All rights reserved.
                                                                                                      20
25      // process user's choice
26      while ( choice >= 0 && choice < 3 ) {
                                                                                  Outline
27
28          // invoke function at location choice in array f
                                                                         fig05_26.cpp
29          // and pass choice as an argument
30          (*f[ choice ])( choice );
                                                                         (2 of 3)
31
32          cout << "Enter a number between 0 and 2, 3 to end: ";
33          cin >> choice;
34      }                                  Call chosen function by
35                                         dereferencing corresponding
36      cout << "Program execution         element in array.
                                     completed." << endl;
37
38      return 0;    // indicates successful termination
39
40   } // end main
41
42   void function1( int a )
43   {
44      cout << "You entered " << a
45           << " so function1 was called\n\n";
46
47   } // end function1
48




                                                                          2003 Prentice Hall, Inc.
                                                                         All rights reserved.
                                                                               21
49   void function2( int b )
50   {
                                                           Outline
51      cout << "You entered " << b
52           << " so function2 was called\n\n";
                                                  fig05_26.cpp
53
54   } // end function2
                                                  (3 of 3)
55
56   void function3( int c )                      fig05_26.cpp
57   {                                            output (1 of 1)
58      cout << "You entered " << c
59           << " so function3 was called\n\n";
60
61   } // end function3

Enter a number between 0 and 2, 3 to end: 0
You entered 0 so function1 was called

Enter a number between 0 and 2, 3 to end: 1
You entered 1 so function2 was called

Enter a number between 0 and 2, 3 to end: 2
You entered 2 so function3 was called

Enter a number between 0 and 2, 3 to end: 3
Program execution completed.




                                                   2003 Prentice Hall, Inc.
                                                  All rights reserved.
                                                                          22

      Accessing Structure Members

      • Member access operators
              – Dot operator (.) for structure and class members
              – Arrow operator (->) for structure and class members via
                pointer to object
              – Print member hour of timeObject:
                          cout << timeObject.hour;
                                          OR
                                    timePtr = &timeObject;
                                    cout << timePtr->hour;
              – timePtr->hour same as ( *timePtr ).hour
                     • Parentheses required
                         – * lower precedence than .



 2003 Prentice Hall, Inc. All rights reserved.
                                                                    23
      Subtle Trap: Returning a Reference to a
      private Data Member
      • Reference to object
              – &pRef = p;
              – Alias for name of object
              – Lvalue
                     • Can receive value in assignment statement
                         – Changes original object
      • Returning references
              – public member functions can return non-const
                references to private data members
                     • Client able to modify private data members




 2003 Prentice Hall, Inc. All rights reserved.
                                                                                                               24
1    // Fig. 6.21: time4.h
2    // Declaration of class Time.
                                                                                           Outline
3    // Member functions defined in time4.cpp
4
                                                                                  time4.h (1 of 1)
5    // prevent multiple inclusions of header file
6    #ifndef TIME4_H
7    #define TIME4_H
8
9    class Time {
10
11   public:
12      Time( int = 0, int = 0, int = 0 );
13      void setTime( int, int, int );
                                                 Function to demonstrate
14      int getHour();                           effects of returning reference
15                                               to private data member.
16      int &badSetHour( int );   // DANGEROUS reference return
17
18   private:
19      int hour;
20      int minute;
21      int second;
22
23   }; // end class Time
24
25   #endif




                                                                                   2003 Prentice Hall, Inc.
                                                                                  All rights reserved.
                                                                                                25
25   // return hour value
26   int Time::getHour()
                                                                            Outline
27   {
28      return hour;
                                                                   time4.cpp (2 of 2)
29
30   } // end function getHour
31
32   // POOR PROGRAMMING PRACTICE:
33   // Returning a reference to a private data member.
34   int &Time::badSetHour( int hh ) Return reference to private
35   {
                                      data member hour.
36      hour = ( hh >= 0 && hh < 24 ) ? hh : 0;
37
38      return hour;   // DANGEROUS reference return
39
40   } // end function badSetHour




                                                                    2003 Prentice Hall, Inc.
                                                                   All rights reserved.
                                                                                                             26
1    // Fig. 6.23: fig06_23.cpp
2    // Demonstrating a public member function that
                                                                                         Outline
3    // returns a reference to a private data member.
4    #include <iostream>
                                                                                fig06_23.cpp
5
6    using std::cout;
                                                                                (1 of 2)
7    using std::endl;
8
9    // include definition of class Time from time4.h
10   #include "time4.h"
11
12   int main()
13   {
14      Time t;                                               badSetHour returns
15                                                            reference to private data
16      // store in hourRef the reference returned by         member hour.
                                                        badSetHour
17      int &hourRef = t.badSetHour( 20 );
18
19                                     Reference allows setting
        cout << "Hour before modification: " << hourRef;          of
20                                       private data member
21      // use hourRef to set invalid   value in Time object t
                                         hour.
22      hourRef = 30;
23
24      cout << "\nHour after modification: " << t.getHour();
25




                                                                                 2003 Prentice Hall, Inc.
                                                                                All rights reserved.
                                                                                                                  27
26      // Dangerous: Function call that returns
27      // a reference can be used as an lvalue!
                                                                                              Outline
28      t.badSetHour( 12 ) = 74;
29
                                                                                     fig06_23.cpp
30      cout <<   "\n\n*********************************\n"
31           <<                             Can use function call as
                  "POOR PROGRAMMING PRACTICE!!!!!!!!\n"
                                                                                     (2 of 2)
32           <<   "badSetHour as an lvalue, lvalue to set invalid value.
                                            Hour: "
33           <<   t.getHour()                                                        fig06_23.cpp
34           <<   "\n*********************************" << endl;                     output (1 of 1)
35
36      return 0;
37
38   } // end main

Hour before modification: 20
Hour after modification: 30
                                                           Returning reference allowed
*********************************                          invalid setting of private
POOR PROGRAMMING PRACTICE!!!!!!!!                          data member hour.
badSetHour as an lvalue, Hour: 74
*********************************




                                                                                      2003 Prentice Hall, Inc.
                                                                                     All rights reserved.
                                                                                    28

      Default Memberwise Assignment

      • Assigning objects
              – Assignment operator (=)
                     • Can assign one object to another of same type
                     • Default: memberwise assignment
                         – Each right member assigned individually to left member
      • Passing, returning objects
              – Objects passed as function arguments
              – Objects returned from functions
              – Default: pass-by-value
                     • Copy of object passed, returned
                        – Copy constructor
                             • Copy original values into new object


 2003 Prentice Hall, Inc. All rights reserved.
                                                                29
      Dynamic Memory Management with Operators
      new and delete
      • Dynamic memory management
              – Control allocation and deallocation of memory
              – Operators new and delete
                     • Include standard header <new>
      • new
                     Time *timePtr;
                     timePtr = new Time;
              – Creates object of proper size for type Time
                     • Error if no space in memory for object
              – Calls default constructor for object
              – Returns pointer of specified type
              – Providing initializers
                     double *ptr = new double( 3.14159 );
                     Time *timePtr = new Time( 12, 0, 0 );
              – Allocating arrays
                     int *gradesArray = new int[ 10 ];


 2003 Prentice Hall, Inc. All rights reserved.
                                                                                  30
      Dynamic Memory Management with Operators
      new and delete
      • delete
              – Destroy dynamically allocated object and free space
              – Consider
                     delete timePtr;
              – Operator delete
                     • Calls destructor for object
                     • Deallocates memory associated with object
                         – Memory can be reused to allocate other objects
              – Deallocating arrays
                     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

 2003 Prentice Hall, Inc. All rights reserved.
                                                                         31

      static Class Members

      • static class variable
              – “Class-wide” data
                     • Property of class, not specific object of class
              – Efficient when single copy of data is enough
                     • Only the static variable has to be updated
              – May seem like global variables, but have class scope
                     • Only accessible to objects of same class
              – Initialized exactly once at file scope
              – Exist even if no objects of class exist
              – Can be public, private or protected




 2003 Prentice Hall, Inc. All rights reserved.
                                                                                         32

      static Class Members
      • Accessing static class variables
              – Accessible through any object of class
              – public static variables
                     • Can also be accessed using binary scope resolution operator(::)
                              Employee::count
              – private static variables
                     • When no class member objects exist: Can only be accessed via
                       public static member function
                            Employee::getCount()
      • static member functions
              – Cannot access non-static data or functions
              – No this pointer for static functions
                     • static data members and static member functions exist
                       independent of objects


 2003 Prentice Hall, Inc. All rights reserved.
                                                                                                            33
1    // Fig. 7.17: employee2.h
2    // Employee class definition.
                                                                                        Outline
3    #ifndef EMPLOYEE2_H
4    #define EMPLOYEE2_H
                                                                               employee2.h (1 of 2)
5
6    class Employee {
7
8    public:
9       Employee( const char *, const char   * ); // constructor
10      ~Employee();                         // destructor
11      const char *getFirstName() const;    // return first name
12      const char *getLastName() const;          static member
                                             // return last name   function
13                                               can only access static data
14      // static member function                members and member
15      static int getCount(); // return #   objects instantiated
                                                 functions.
16
17   private:
18      char *firstName;
19      char *lastName;
                                             static data member is
20                                           class-wide data.
21      // static data member
22      static int count; // number of objects instantiated
23
24   }; // end class Employee
25




                                                                                2003 Prentice Hall, Inc.
                                                                               All rights reserved.
                                                                                                             34
26   #endif
                                                                                         Outline

1    // Fig. 7.18: employee2.cpp                                                employee2.h (2 of 2)
2    // Member-function definitions for class Employee.
3    #include <iostream>                                                        employee2.cpp
4                                                                               (1 of 3)
5    using std::cout;
6    using std::endl;
7
8    #include <new>           // C++ standard new operator
9    #include <cstring>       // strcpy and strlen prototypes
10
11   #include "employee2.h"                      Initialize static
                              // Employee class definition         data
12                                                member exactly once at file
13   // define and initialize static data   memberscope.
14   int Employee::count = 0;
15
                                                  static member function
16   // define static member function that returns number of
17   // Employee objects instantiated
                                                accesses static data
18   int Employee::getCount()                   member count.
19   {
20      return count;
21
22   } // end static function getCount




                                                                                 2003 Prentice Hall, Inc.
                                                                                All rights reserved.
                                                                                                            35
23
24   // constructor dynamically allocates space for
                                                                                        Outline
25   // first and last name and uses strcpy to copy
26   // first and last names into the object
                                                                                employee2.cpp
27   Employee::Employee( const char *first, const char *last ) operator dynamically
                                                          new
28   {
                                                                                (2 of 3)
                                                          allocates space.
29      firstName = new char[ strlen( first ) + 1 ];
30      strcpy( firstName, first );
31
32                                Use static data
        lastName = new char[ strlen( last ) + 1 ]; member   to
33      strcpy( lastName, last ); store total count of
34                                employees.
35      ++count;   // increment static count of employees
36
37      cout << "Employee constructor for " << firstName
38           << ' ' << lastName << " called." << endl;
39
40   } // end Employee constructor
41
42   // destructor deallocates dynamically allocated memory
43   Employee::~Employee()
44   {
45      cout << "~Employee() called for " << firstName
46           << ' ' << lastName << endl;
47




                                                                                2003 Prentice Hall, Inc.
                                                                               All rights reserved.
                                                                                                    36
48      delete [] firstName;   // recapture memory
49      delete [] lastName;    // recapture memory
                                                                                Outline
50
51      --count;   // decrement static count of employees
                                                                       employee2.cpp
52
53   } // end destructor ~Employee static
                              Use            data delete deallocates
                                          Operator member to           (3 of 3)
54                             store totalmemory. of
                                          count
55   // return first name of employee
                              employees.
56   const char *Employee::getFirstName() const
57   {
58      // const before return type prevents client from modifying
59      // private data; client should copy returned string before
60      // destructor deletes storage to prevent undefined pointer
61      return firstName;
62
63   } // end function getFirstName
64
65   // return last name of employee
66   const char *Employee::getLastName() const
67   {
68      // const before return type prevents client from modifying
69      // private data; client should copy returned string before
70      // destructor deletes storage to prevent undefined pointer
71      return lastName;
72
73   } // end function getLastName



                                                                        2003 Prentice Hall, Inc.
                                                                       All rights reserved.
                                                                              37
      Operator Functions As Class Members Vs. As
      Friend Functions
      • Operator functions
              – Member functions
                     • Use this keyword to implicitly get argument
                     • Gets left operand for binary operators (like +)
                     • Leftmost object must be of same class as operator
              – Non member functions
                     • Need parameters for both operands
                     • Can have object of different class than operator
                     • Must be a friend to access private or protected data
      • Example Overloaded << operator
              – Left operand of type ostream &
                     • Such as cout object in cout << classObject
              – Similarly, overloaded >> needs istream &
              – Thus, both must be non-member functions
 2003 Prentice Hall, Inc. All rights reserved.
                                                                           38
      Operator Functions As Class Members Vs. As
      Friend Functions
      • Commutative operators
              – May want + to be commutative
                     • So both “a + b” and “b + a” work
              – Suppose we have two different classes
              – Overloaded operator can only be member function when its
                class is on left
                     • HugeIntClass + Long int
                     • Can be member function
              – When other way, need a non-member overload function
                     • Long int + HugeIntClass




 2003 Prentice Hall, Inc. All rights reserved.
                                                                   39
      Overloading Stream-Insertion and Stream-
      Extraction Operators
      • << and >>
              – Already overloaded to process each built-in type
              – Can also process a user-defined class
      • Example program
              – Class PhoneNumber
                     • Holds a telephone number
              – Print out formatted number automatically
                     • (123) 456-7890




 2003 Prentice Hall, Inc. All rights reserved.
                                                                                                             40
1    // Fig. 8.3: fig08_03.cpp
2    // Overloading the stream-insertion and
                                                                                         Outline
3    // stream-extraction operators.
4    #include <iostream>
                                                                                fig08_03.cpp
5
6    using   std::cout;
                                                                                (1 of 3)
7    using   std::cin;
8    using   std::endl;
9    using   std::ostream;
10   using   std::istream;
11
12   #include <iomanip>
13
                                      Notice function prototypes for
14   using std::setw;
                                      overloaded operators >> and <<
15
16   // PhoneNumber class definitionThey must be non-member friend
17   class PhoneNumber {
                                    functions, since the object of class
18      friend ostream &operator<<( ostream&, const PhoneNumber & );
                                    Phonenumber appears on the right       of
19      friend istream &operator>>( istream&, PhoneNumber & );
20
                                      the operator.
21   private:                          cin << object
22      char areaCode[ 4 ];   //   3-digit area code and null
                                       cout >> object
23      char exchange[ 4 ];   // 3-digit exchange and null
24      char line[ 5 ];       // 4-digit line and null
25
26   }; // end class PhoneNumber



                                                                                 2003 Prentice Hall, Inc.
                                                                                All rights reserved.
                                                                                                               41
27
28   // overloaded stream-insertion operator; cannot be
                                                                                           Outline
29   // a member function if we would like to invoke it with
30   // cout << somePhoneNumber;
                                                                              fig08_03.cpp
31   ostream &operator<<( ostream &output, const PhoneNumber &num )
32   {
                                                                              (2 of 3)
33      output << "(" << num.areaCode << ") "
                                                              The expression:
34             << num.exchange << "-" << num.line;
                                                              cout << phone;
35                                                              is interpreted as the function call:
36      return output;     // enables cout << a << b << c;      operator<<(cout, phone);
37
                                                                output is an alias for cout.
38   } // end function operator<<
39
40   // overloaded stream-extraction operator; cannot be This allows objects to be cascaded.
41   // a member function if we would like to invoke it with
                                                          cout << phone1 << phone2;
42   // cin >> somePhoneNumber;
                                                          first calls
43   istream &operator>>( istream &input, PhoneNumber &num )
                                                          operator<<(cout, phone1), and
44   {
45      input.ignore();                      // skip (
                                                          returns cout.
46      input >> setw( 4 ) >> num.areaCode; // input area code
47      input.ignore( 2 );                                Next, cout << phone2 executes.
                                             // skip ) and space
48      input >> setw( 4 ) >> num.exchange; Stream manipulator setw
                                             // input exchange
49      input.ignore();                      // skip dash (-)
                                            restricts number of characters
50      input >> setw( 5 ) >> num.line;     read. setw(4) allows 3
                                             // input line
51
                                           characters to be read, leaving
52      return input;      // enables cin >> a >> b >> c;
                                           room for the null character.


                                                                                   2003 Prentice Hall, Inc.
                                                                                  All rights reserved.
                                                                                                   42
53
54   } // end function operator>>
                                                                               Outline
55
56   int main()
                                                                      fig08_03.cpp
57   {
58      PhoneNumber phone; // create object phone
                                                                      (3 of 3)
59
60      cout << "Enter phone number in the form (123) 456-7890:\n";   fig08_03.cpp
61                                                                    output (1 of 1)
62      // cin >> phone invokes operator>> by implicitly issuing
63      // the non-member function call operator>>( cin, phone )
64      cin >> phone;
65
66      cout << "The phone number entered was: " ;
67
68      // cout << phone invokes operator<< by implicitly issuing
69      // the non-member function call operator<<( cout, phone )
70      cout << phone << endl;
71
72      return 0;
73
74   } // end main

Enter phone number in the form (123) 456-7890:
(800) 555-1212
The phone number entered was: (800) 555-1212



                                                                       2003 Prentice Hall, Inc.
                                                                      All rights reserved.

				
DOCUMENT INFO
Description: UNIX is an operating system which was first developed in the 1960s, and has been under constant development ever since. By operating system, we mean the suite of programs which make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops. UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows which provides an easy to use environment. However, knowledge of UNIX is required for operations which aren't covered by a graphical program, or for when there is no windows interface available, for example, in a telnet session.