1 / 34

Introduction to Programming

Introduction to Programming . Lecture 34. In Today’s Lecture. Arrays of objects Interaction of Arrays with Free Store new and delete operators Overloading of Arrays Operator. Arrays of Object. Date mydate [ 10 ] ;.

aguirrer
Download Presentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming Lecture 34

  2. In Today’s Lecture • Arrays of objects • Interaction of Arrays with Free Store • new and delete operators • Overloading of Arrays Operator

  3. Arrays of Object

  4. Date mydate [ 10 ] ;

  5. int intArray [ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ;

  6. Date mydate [ 10 ] = { Date ( 21 , 01 , 1979 ) , Date ( 21 , 02 , 1979 ) , Date ( 21 , 03 , 1979 ) , Date ( 21 , 04 , 1979 ) , Date ( 21 , 05 , 1979 ) , Date ( 02 , 06 , 1979 ) , Date ( 02 , 07 , 1979 ) , Date ( 02 , 08 , 1979 ) , Date ( 02 , 09 , 1979 ) , Date ( 02 , 10 , 1979 ) };

  7. Example Fill the array for ( int i = 0 ; i < arraySize ; i ++ ) { cin >> c [ i ] ; }

  8. Example Print in Reverse order for ( int i = arraySize ; i >= 0 ; i -- ) { cout << c [ i ] ; } Incorrect code

  9. Example Print in Reverse order for ( int i = ( arraySize – 1 ) ; i >= 0 ; i -- ) { cout << c [ i ] ; }

  10. Example void Date :: display ( ) { char * monthName [ ] = { "zero", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December" } ; cout << monthName [ month ] << ' ' << day << ", " << year << endl ; }

  11. Date mydate [ 10 ] ={ Date ( 21 , 01 , 1979 ) , Date ( “01-Jan-2002” ) , Date ( 21 , 03 , 1979 ) , Date ( “01-Feb-2002” ), Date ( 21 , 05 , 1979 ) , Date ( 02 , 06 , 1979 ) , Date ( 02 , 07 , 1979 ) , Date ( 02 , 08 , 1979 ) , Date ( 02 , 09 , 1979 ) , Date ( 02 , 10 , 1979 ) };

  12. String myString [ 5 ] = { "First line of message\n" , "Second line of message\n", String ( "Third line of message\n" ) , String ( '-' , 25 ) , String ( ) } ;

  13. String * text ; text = new String [ 5 ] ;

  14. String myString [ 5 ] = { "First line of message\n", "Second line of message\n", String ( "Third line of message\n" ) , String( '-', 25 ), String ( ) };

  15. Default Constructor

  16. String * text ; text = new String [ 5 ] ;delete text ; // Incorrect syntax for // deleting array

  17. delete [ ] text ;

  18. Example int * iPtr ; iPtr = new int [ 10 ] ; delete iPtr ;// bad usage

  19. delete [ ] iPtr ;delete [ ] text ;

  20. Overloading new Operator void * operator new ( size_t size ) { void * rtn = calloc ( 1 , size ) ; return rtn ; }

  21. Overloading delete Operator void operator delete ( void * ptr ) { free ( ptr ) ; }

  22. void * Date :: operator new ( size_t size ) ; Overloading new Operator for Date class

  23. Example int iArray [ 10 ] ; int i ; for ( i = 0 ; i < 100 ; i ++ ) { iArray [ i ] = i ; }

  24. Example int iArray [ 10 ] ; int i ; for ( i = 0; i < upperLimit ; i ++) { iArray [ i ] = i ; }

  25. [ ]

  26. int iArray [ 5 ] ;

  27. int & operator [ ] ( int index ) ;

  28. #define MAXNUM 10int iArray [ MAXNUM ] ;

  29. Overloaded Array Operator [ ] int & IntArray :: operator [ ] ( int index ) { int dummy = 0 ; if ( (index > ( MAXNUM – 1 ) ) { cout << "Error: invalid index call.\n“ ; return dummy ; } else return array [ index ] ; }

  30. Example class IntArray { private : int length ; int * iPtr ; public : IntArray ( int i ) ; ... } ;

  31. Example main ( ) { IntArray i ( 10 ) ; int j ; for ( j = 0 ; j < 10 ; j ++ ) i [ j ] = j ; }

  32. Example int & IntArray ::operator [ ] ( int index ) { int dummy = 0 ; if ( ( index < 0 ) || ( index >= length ) ) { cout << "Error : index out of range.\n" ; return dummy ; } else return array [ index ] ; }

  33. Example IntArray :: IntArray ( int i ) { int * iPtr ; iPtr = new int ( i ) ; length = i ; }

  34. Today’s lecture • Arrays of objects • Dynamically allocating arrays using new operator • Usages and overloading new and delete Operator • Array subscripting [ ] Operator

More Related