// Calendars.m Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2013-07-12 Objective-C version of this file was created. // 2013-07-12 Last modification. // This programs demonstrates // - the use of the ISODate class to print calendars // - deriving a subclass from a class // No NSString strings are in use in this program. // The program was made with C-style strings because // they can be printed easily with the printf() function. #include #import #import "class_isodate_oc.h" @interface EnglishCalendar : NSObject { int this_month ; int this_year ; char** names_of_months ; // C-style array of strings char* week_description ; // C-style string } - (id) init_year: (int) given_year and_month: (int) given_month ; - (void) print ; @end @implementation EnglishCalendar - (id) init_year: (int) given_year and_month: (int) given_month { self = [ super init ] ; if ( self ) { static char* english_names_of_months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } ; static char english_week_description[] = " Week Mon Tue Wed Thu Fri Sat Sun" ; this_month = given_month ; this_year = given_year ; names_of_months = english_names_of_months ; week_description = english_week_description ; } return self ; } - (void) print { ISODate* a_day_in_this_month = [ [ ISODate alloc ] init_year: this_year and_month: this_month and_day: 1 ] ; int day_of_week_index = 0 ; int day_of_week_of_first_day = [ a_day_in_this_month index_for_day_of_week ] ; printf( "\n\n %s %d\n\n%s\n\n", names_of_months[ this_month - 1 ], this_year, week_description ) ; printf( "%4d ", [ a_day_in_this_month get_week_number ] ) ; // The first week of a month is often an incomplete week, // i.e., the first part of week belongs to the previous // month. In place of the days that belong to the previous // month we print just spaces. while ( day_of_week_index != day_of_week_of_first_day ) { printf( " " ) ; day_of_week_index ++ ; } while ( this_month == [ a_day_in_this_month month ] ) { if ( day_of_week_index >= 7 ) { printf( "\n%4d ", [ a_day_in_this_month get_week_number ] ) ; day_of_week_index = 0 ; } printf( "%5d", [ a_day_in_this_month day ] ) ; [ a_day_in_this_month increment ] ; day_of_week_index ++ ; } printf( "\n" ) ; } @end // end of EnglishCalendar implementation // Next, we'll specify a class named SpanishCalendar // that inherits the class above. The new class differs from // its superclass only so that it has a new kind of constructor. @interface SpanishCalendar : EnglishCalendar - (id) init_year: (int) given_year and_month: (int) given_month ; @end @implementation SpanishCalendar - (id) init_year: (int) given_year and_month: (int) given_month { self = [ super init_year: given_year and_month: given_month ] ; if ( self ) { static char* spanish_names_of_months[] = { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Deciembre" } ; static char spanish_week_description[] = "Semana Lun Mar Mie Jue Vie Sab Dom" ; names_of_months = spanish_names_of_months ; week_description = spanish_week_description ; } return self ; } @end int main( void ) { NSAutoreleasePool* autorelease_pool = [ [ NSAutoreleasePool alloc ] init ] ; EnglishCalendar* an_english_calendar = [ [ EnglishCalendar alloc ] init_year: 2013 and_month: 10 ] ; [ an_english_calendar print ] ; SpanishCalendar* a_spanish_calendar = [ [ SpanishCalendar alloc ] init_year: 2014 and_month: 12 ] ; [ a_spanish_calendar print ] ; [ autorelease_pool drain ] ; }