Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Months of the year December January November October February
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Introduction of Programming Lecture 28. Today’s Lecture How memory allocation is done in How memory allocation is done in C++ C++ How is it different.
Chubaka Producciones Presenta :.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
January 2012 Monday Tuesday Wednesday Thursday Friday Sat/ Sun / /8 14/15 21/22 28/
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
Chicas, este calendario si es pa' nosotras !!!!!.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/30/2012 and 10/31/2012 -Code PrintCalendar.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.

You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
February 11, 2005 More Pointers Dynamic Memory Allocation.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
Dynamic memory allocation and Pointers Lecture 4.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
Months of the year. jANUARY New year’s day fEBRUARY Valentine’s day.
Calendar for 2011 Months of the Year with Holidays.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
TEMPORAL VISUALIZATION OF DATA FROM THE FRENCH SENTINEL NETWORK.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
CMSC 202 Lesson 8 Classes II. Warmup Declare a class (.h part) named “Cup” It has a data member that says how full it is One method called “Drink” that.
Introduction to Programming
Dictation practice 2nd Form Ms. Micaela-Ms. Verónica.
TIMELINES PHOTOS This is an example text
TIMELINES PHOTOS This is an example text
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
13-block rotation schedule
Introduction to Programming
Timelines – 12 Months (years)
1   1.テキストの入れ替え テキストを自由に入れ替えることができます。 フチなし全面印刷がおすすめです。 印刷のポイント.
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
Classes Short Review of Topics already covered Constructor
2017/18 Payment Calendar Due Date Cut-off Day 1st of every month
McDonald’s Kalender 2009.
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
Problem Gambling Clicks to Opgr.org
2300 (11PM) September 21 Blue line is meridian..
McDonald’s calendar 2007.
1 - January - Sun Mon The Wed Thu Fri Sat
Proud As A Peacock! We are very proud of__________________
Teacher name August phone: Enter text here.
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
Circle Chart Template Process Name.
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
February 2007 Note: Source:.
JUNE 2010 CALENDAR PROJECT PLANNING 1 Month MONDAY TUESDAY WEDNESDAY
MONTHS OF THE YEAR January February April March June May July August
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
McDonald’s calendar 2007.
Production Month Sun Hours K Monthly Kwh Tou Peak Value After Kwh
Habitat Changes and Fish Migration
2015 January February March April May June July August September
Habitat Changes and Fish Migration
E W ©
Presentation transcript:

Introduction to Programming Lecture 34

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

Arrays of Object

Date mydate [ 10 ] ;

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

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 ) };

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

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

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

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

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 ) };

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

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

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

Default Constructor

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

delete [ ] text ;

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

delete [ ] iPtr ; delete [ ] text ;

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

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

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

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

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

[ ]

int iArray [ 5 ] ;

int & operator [ ] ( int index ) ;

#define MAXNUM 10 int iArray [ MAXNUM ] ;

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

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

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

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

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

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