A.doc

Similar documents
c_cpp

FY.DOC

untitled

nooog

新版 明解C++入門編

Microsoft Word - ch04三校.doc

新・明解C言語入門編『索引』

C++ 程式設計

CC213

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

untitled

3.1 num = 3 ch = 'C' 2

Chapter12 Derived Classes

Microsoft Word 軟體設計第二部份範例試題_C++_ _1_.doc

C语言的应用.PDF

C/C++语言 - C/C++数据

untitled

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

CC213

Java

四川省普通高等学校

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

untitled

untitled

C/C++ 语言 - 循环

Microsoft Word - CPE考生使用手冊 docx

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

Microsoft Word cppFinalSolution.doc

概述

ebook39-5

epub 33-8

C 1

C/C++语言 - 分支结构

CH01.indd

Microsoft Word - 01.DOC

CC213

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

C PICC C++ C++ C C #include<pic.h> C static volatile unsigned char 0x01; static volatile unsigned char 0x02; static volatile unsigned cha

untitled

全国计算机技术与软件专业技术资格(水平)考试

Microsoft PowerPoint - string_kruse [兼容模式]

Strings

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

2.1 n ~á CPU q ˆ ~ µƒã î ˆ C++ à nù eô 1. (edit) 2. Í (preprocess) 3. (compile) 4. (link) 5. (load) 6. ˆ (execute) µl ô Ãs nù (editor) Í (preprocessor

第3章.doc

untitled

Microsoft Word - Book9

提问袁小兵:

Chapter 9: Objects and Classes

Oracle Solaris Studio makefile C C++ Fortran IDE Solaris Linux C/C++/Fortran IDE "Project Properties" IDE makefile 1.

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

2/14 Buffer I12, /* x=2, buffer = I 1 2 */ Buffer I243, /* x=34, buffer = I 2 43 */ x=56, buffer = I243 Buffer I243I265 code_int(int x, char *buffer)

Microsoft Word - 11.doc

_汪_文前新ok[3.1].doc

Ps22Pdf

epub83-1

第 15 章 程 式 編 写 語 言 15.1 程 式 編 写 語 言 的 角 色 程 式 編 寫 語 言 是 程 式 編 寫 員 與 電 腦 溝 通 的 界 面 語 法 是 一 組 規 則 讓 程 式 編 寫 員 將 字 詞 集 合 起 來 電 腦 是 處 理 位 元 和 字 節 的 機 器, 與

untitled

Spyder Anaconda Spyder Python Spyder Python Spyder Spyder Spyder 開始 \ 所有程式 \ Anaconda3 (64-bit) \ Spyder Spyder IPython Python IPython Sp

Microsoft Word - p11.doc

EJB-Programming-3.PDF

ebook14-4

2015年计算机二级(C语言)模拟试题及答案(四)

6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

untitled

CDWA Mapping. 22 Dublin Core Mapping

第5章修改稿

untitled

Microsoft Word - 09.數學 docx

2/80 2

Microsoft Word - CIN-DLL.doc

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

ebook8-30

INTRODUCTION TO COM.DOC

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

学习MSP430单片机推荐参考书

e bug 0 x=0 y=5/x 0 Return 4 2

Microsoft Word - C-pgm-ws2010.doc

科学计算的语言-FORTRAN95

6020

ebook45-5

EJB-Programming-4-cn.doc

Microsoft Word - PHP7Ch01.docx

PowerPoint Presentation

chp6.ppt

相 应 功 能 (5) 再 将 Boy 类 作 为 Girl 类 的 友 元 类, 在 Boy 类 的 某 成 员 函 数 VisitGirl(Girl & ) 中 访 问 Girl 类 的 私 有 成 员, 观 察 编 译 器 给 出 的 信 息 ( 6 ) 删 除 两 个 类 中 的 函 数 V

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

untitled

Gerotor Motors Series Dimensions A,B C T L L G1/2 M G1/ A 4 C H4 E


錄...1 說...2 說 說...5 六 率 POST PAY PREPAY DEPOSIT 更

Microsoft PowerPoint - Lecture7II.ppt

Microsoft PowerPoint - ds-1.ppt [兼容模式]

Fuzzy GP

51 C 51 isp 10 C PCB C C C C KEIL

EK-STM32F

Transcription:

A CopyRight: 2003, Mahler Works C << >> 1.1 C (5 min) 1.1.1C 1.1.21998 ANSIAmerican National Standards InsituteISO International Standards Organzation 1.1.3C/C++C C 1.1.4 C JAVA PLATFORM C 1.2 (5 min) 1.2.1 1.2.2 1.2.3 1.3 (5 min) 1.3.1Input Device 1.3.2Output Device 1.3.3Memory Unit 1.3.4Arithmetic Unit 1.3.5Central processing Unit (CPU) 1.3.6Secondary storage Unit 1.4 (5 min) 1.4.1Machine Language +1300042774 +1400593419 +1200274027 1.4.2Assembly Language ADD AX,2 MOV dx,ax 1.4.3High-level Language 1 44

Ex1.4 DOS DEBUG a 100 add ax,2 ENTER u100 Q 1.5 C (5 min) 1.5.1C.cpp ;.cxx ;.c 1.5.2C C Editor DISK Preprocessor DISK Compiler DISK Linker DISK Loader Primary Memory CPU Primary Memory 1.6 Microsoft Visual C++ (5 min) MS VC++ 1.7 - (15~30 min) //Prg. 1.7.1: prg1.7.1.cpp // int main() cout << I love u all baby!! ; return 0; // 0 1.7.1// 1.7.2 int main() int MAIN 1.7.3cout 1.7.4 \n \t 2 44

\r \a \\ \ 1.8 - (15~30 min) //Prg. 1.8.1: prg1.8.1.cpp // int main() cout << 2 + 3 ; return 0; // 0 1.8.1 1.8.2 1.8.3cin 1.8.4 1.9 (Memory Concept)(5~30 min) 1.9.1 1.10 (Arithmetic)(5~30min) 1.10.1 C C + F+7 F + 7 - p-c P c * Bm B * m / X/y X / y % R mod s R % s ( ) *, /, % 3 44

+, - 1.10.2 y = ax + b y = a * x + b 1.11 (Equality and Relational Operator)(5~30min) 1.11.1 C == X==Y X Y!= X!=Y X Y > > X > Y X Y < < X < Y X Y >= X >= Y X Y <= X <= Y X Y 1.11.2if //Prg. 1.11.2: prg1.11.2.cpp // int main() int x,y; cout << Input X: ; cin >> x; cout << Input Y ; cin >> y; if (x>y) cout << First is BIG ; if(x<y) cout << Second is BIG ; return 0; // 0 4 44

5 44

2.1 if/else (20 min) 2.1.1 if/else if else //Prg 2.1.1 : prg2_1_1 //if / else int grad; cout << ; cin >> grad ; if (grad >=60) cout << ; else cout << ; 2.2 (20 min) if 90 A else if 80 B else if 70 C else if 60 D else 2.3 while (20 min) 2.3.1while while int product = 2; 6 44

while ( product <= 1000 ) product = product + 1 ; 2.3.2 WHILE 1. -1 2. 99 2 2 * 1 = 2 2 * 2 = 4.. 2 * 9 = 18 3. 7 44

3.1 (20~40 min) 3.1.1 += C += 7 C = C + 7 -= D -= 4 D = D 4 *= E *= 5 E = E * 5 /= F /= 3 F = F / 3 %= G %=9 G = G % 9 3.1.2 / ++ A++ A = A + 1 ++ ++A A = A + 1 -- B-- B = B 1 -- --B B = B 1 //Prg. 3.1.2: prg3_1_2.cpp //++ int main() int c ; c = 5 ; cout << c << endl ; cout << c++ << endl ; cout << c << endl << endl ; c = 5 ; cout << c<< endl; cout << ++c << endl ; cout << c << endl ; return 0; // 0 3.2 (10~20 min) 3.2.1 1. (int counter;) 2. (counter = 1;) 3. (++counter;) 4. while (counter <= 10) 8 44

//Prg. 3.2.1: prg3_2_1.cpp // int main() int counter ; counter = 1; while (counter <= 10) cout << counter << endl ; ++counter; return 0; // 0 9 44

4.1for (20~60 min) 4.1.1for for (expression1; expression2; expression3) statement expression1 while (expression2) statement expression3 4.1.2for //Prg. 4.1.1: prg4_1_1.cpp // FOR int main() for (int counter = 1 ; counter <=10 ; counter++ ) cout << counter << endl ; return 0; // 0 4.2 for (20~60 min) 4.2.0FOR for( int number = 2; // number <= 100 ; // sum += numbner, number += 2) // 4.2.1 for(int i = 1; i <= 100 ; i++) 4.2.2 for(int i = 100; i >= 1 ; i--) 4.2.37 77 FOR 4.2.420 2 2 4.2.5 2,5,8,11,14,17,20 4.2.6 99,88,77,66,55,44,33,22,11,0 4.3switch (20~60 min) 4.3.1switch switch ( ) case 1 break; case 2 10 44

break; 4.3.2switch if/else 90 A80 B.. 4.4do/while (20~60 min) 4.4.1 while while( condition) 4.4.2do / while do statement while(condition); do statement while (condition); //Prg. 4.4.2: prg4_2_2.cpp //do/while int main() int counter = 1; do cout << counter << ; counter ++ ; while (counter <= 10) ; cout << endl ; return 0 ; 4.5 4.5.1 AND if 20 if age >= 20 && sex = 1 ; 4.5.2 OR 11 44

if age >= 20 or age <= 12 12 44

5.1 5min 5.2 (5~10 min) 5.2.1 math.h 5.2.2 ceil( x ) ceil( 9.2 ) is 10.0 cos( x ) Cosine cos( 0.0 ) is 1.0 exp( x ) e exp( 1.0 ) = 2.71282 pow( x, y ) X pow( 2, 3 ) is 8 5.3 (10~30min) 5.3.1 //Prg. 5.3.1: prg5_3_1.cpp // int square( int ) ; // int main() for ( i = 1 ; i <= 10 ; i ++) cout << square ( i ) << \t ; cout << endl ; return 0; // 0 int square( int y) return y * y ; 5.3.2 int square( int ) ; int square () int 5.3.3 return-value-type function-name( parameter-list) declarations and statements 13 44

5.3.4return return expression; 5.4 a,b,c C++ 14 44

6.1 6.1.1 C++ long double double float unsigned long int long int unsigned int int unsigned short int short int unsigned char char 6.2 (Header File) 6.2.1 6.2.2 6.2.3 6.3 (Random Number Generation) 6.3.1 i = rand() ; rand() stdlib.h 6.3.2 rand() Rand 0 32767 6.3.3 //Prg. 6.3.3: prg6_3_3.cpp // #include <stdlib.h> int main() for(int i = 1; i <= 20 ; i ++) cout << (1 + rand() % 6) << \t ; if ( i % 5 == 0 ) cout << endl ; // 15 44

return 0; // 0 6.3.4 6000 6.3.5 srand() srand( x ), x unsigned int //Prg. 6.3.5: prg6_3_5.cpp // #include <stdlib.h> int main() unsigned seed ; cout << ; cin >> seed ; srand ( seed ) ; for(int i = 1; i <= 20 ; i ++) cout << (1 + rand() % 6) << \t ; if ( i % 5 == 0 ) cout << endl ; // return 0; // 0 6.4 C++ 6000 16 44

7.1 overloading 7.1.1 C C C C 7.1.2 int add_values(int a, int b) return (a + b) ; int add_values(int a, int b,int c) return ( a + b + c) ; cout << 200 + 100 = << add_values(200,100) << endl; cout << 200 + 100 + 2 = << add_values(200,100, 2) << endl; 7.2 Recursion 7.2.1 (Recursion) 7.2.2 n * ( n-1 ) * (n-2). * 1 n! 7.2.3 FOR n n! 7.2.4(Recusion) N unsigned long n( unsigned long) 17 44

for (int i=1 ; i <=10 ; i++) cout << \t << i <<! = << n(i) << endl ; unsigned long n( unsigned long i) if (i<=1) return 1 ; else return i * n(i 1) 7.3 (call by value, or, call by refrerence) 7.3.1 (call by value) int add_value(int a, int b) return (a + b) ; int x, y ; x = 2, y = 3; cout << add_value(x,y) << endl ; cout << x << endl; cout << y << endl; Add_valuec MAIN x,y VALUE add_value AB CALL BY VALUE MAIN XY 7.3.2 (call by reference) void add_value(int &a, int &b) a = (a + b) ; int x, y ; x = 2, y = 3; add_value(x,y) ; cout << x << endl; cout << y << endl; X 7.4 7.4.1 C DEFAULT 18 44

void show(int a=1, int b=2, int c=3) cout << a << a << b << b << c << c << endl; show(); show(1001); show(1001,2002); show(1001;2002;3003); 19 44

8.1 8.1.1 C[0] 2 C[1] 3 C[2] 0 C[3] -123 C[4] 3 C[5] 1310 C[6] 41 C[7] 23 C[8] 54 C[9] 467 C[10] -2342 8.1.2 cout << c[0] << c[1] << c[10] ; x = x[3] / 10; 8.2 8.2.1 int b[10] ; int x[9], y[9] ; 8.2.2 int a, n[10] ; for (a = 0; a< 10 ; a++) n[a] = a ; cout << Elements Value ; for (a=0 ; a<10 ; a++) cout << a << \t << n[a] << endl; FOR int n[10] = 32, 26, 54, 12, 23, 44, 56, 78, 77, 88; 20 44

for (int j=0 ; j<10 ; j++) cout << no. << j << is << n[j] << endl ; 8.3constant varible 8.3.1 const int x = 7 ; cout << const variables x is : << x ; 8.3.2 const int x ; x=7; cout << const variables x is : << x ; 8.4 8.4.1 const int arraysize = 12 ; int a [ arraysize ] = 1, 3, 5, 7, 4, 12, 24, 44, 54, 53, 98, 12; int total = 0 ; for (int k=0; k< arraysize; k++) total += a [ k] ; cout << The sum is: << total ; 8.4.2 const int responsesize = 40, frequencysize = 11; int responses[ responsesize ] 21 44

= 1, 2, 6, 4, 8, 5, 9, 7, 8,10, 1, 6, 3, 8, 6,10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8,10; int frequency[ frequencysize ] = 0 ; for (int answer = 0; answer < responsesize ; answer++) ++frequency[ responses[ answer ] ]; cout << "Rating" << "\t" << "Frequency" << endl; for (int rate=1 ; rate <frequencysize ; rate++) cout << rate << "\t" << frequency[rate] << endl; 22 44

10.1 const int arraysize = 10 ; int a [ arraysize ] = 2,6, 4, 8, 10, 12, 89, 68, 45, 37 ; int i, hold ; cout << " \n"; for (i=0 ; i<arraysize; i++) cout << " " << a [ i ] ; for (int pass = 0 ; pass < arraysize - 1; pass++) for (i=0 ; i<arraysize ; i++) if ( a[i] > a[i+1] ) hold = a[i] ; a[i] = a[i+1]; a[i+1] = hold ; cout << "\n \n"; for (i=0 ; i<arraysize; i++) cout << " " << a [ i ] ; 23 44

10.2 C MEAN MEDIAN MODE C 24 44

25 44

10.3 (linear Search) CUSTOM Linear Search C 2 2 100 2 int linearsearch( const int array[], int key, int sizeofarray ); const int arraysize = 100; int a[ arraysize ], searchkey, element; for(int x=0; x<arraysize; x++) a[x] = 2 * x; cout << " (Search Key" ; cin >> searchkey ; element = linearsearch( a, searchkey, arraysize ) ; if( element!= -1 ) cout << " :" << element << endl ; else cout << " " << endl ; int linearsearch( const int array[], int key, int sizeofarray ) for(int n=0; n<sizeofarray; n++) if( array[n] == key ) 26 44

return n ; return -1 ; 10.4 Colum0 Colum1 Colum2 Column3 Row0 A[0][0] A[0][1] A[0][2] A[0][3] Row1 A[1][0] A[1][1] A[1][2] A[1][3] Row2 A[2][0] A[2][1] A[2][2] A[2][3] void printmarray(int a[][3]); int array1[2][3] = 1,2,3,4,5,6, array2[2][3] = 1,2,3,4,5,6, array3[2][3] = 1,2,4; cout << " array1 " << endl ; printmarray( array1 ); cout << " array2 " << endl ; printmarray( array2 ); cout << " array3 " << endl ; printmarray( array3 ); void printmarray(int a[][3]) for(int i=0;i<2;i++) for(int j=0;j<3;j++) cout << a[i][j] << ' ' ; cout << endl; 27 44

11.0 #include <string.h> char x[] = "Happy Birthday to You" ; char y[25], z[15]; cout << "The string in array x is:" << x << "\nthe string in array y is:" << strcpy(y,x) << '\n' ; strncpy(z,x,14); // NULL z[14] = NULL; cout << "The String in array z is: " << z << endl ; 11.1 C int *countptr, count; int Float *xptr, *ypter; 28 44

11.2 Int y =5; Int *yptr; yptr = &y ; y yptr int a; //a int *aptr; //aptr a=7; aptr = &a; //a aptr cout << "a " << &a ; cout << "\naptr " << aptr; cout << "\n\na " << a ; cout << "\n*aptr " << aptr; cout<< "\n\n " << "\n&*aptr = " << &*aptr << "\n*&aptr = " << *&aptr << endl ; 29 44

11.3 (Call-By-Reference) void bubblesortr(int *array, const int size); const int arraysize = 10; int a[ arraysize ] = 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 ; int i; cout << "Data Item in origrinal order\n" ; for(i=0;i<arraysize;i++) cout << " " << a[i] ; bubblesortr(a, arraysize); cout << "\ndata items in ascending order\n" ; for(i=0;i<arraysize;i++) cout << " " << a[i] ; cout << endl ; void bubblesortr(int *array, const int size) void swap(int *,int *); for(int pass=0;pass<size-1;pass++) for(int j=0;j<size-1;j++) if(array[j] > array[j+1]) swap(&array[j],&array[j+1]); void swap(int *element1ptr, int *element2ptr) int hold = *element1ptr; *element1ptr = *element2ptr; *element2ptr = hold; 30 44

11.4 suit[0] = H e a r t s \0 suit[1] = D i a m o n d s \0 suit[2] = C l u b s \0 suit[3] = S p a d e s \0 Heart 0 Diamonds 1 Clubs 2 Spades 3 Ace Two Three Four Five Six Seven Eight Night ten Jack Queen King 0 1 2 3 4 5 6 7 8 9 10 11 12 Deck[2][12] CLUB KING Club King deck DECK suit face suit[row]face[column] 2 9 suit face deck deck ^_^ 31 44

32 44

12.1 struct ; ; ; struct Time int hour; int minute; int second; ; void printstandard( const Time &t ); void printmilitary( const Time &t ); Time dinnertime; // TIME dinnertime dinnertime.hour = 18; // dinnertime.minute = 30; dinnertime.second = 0; cout << "The dinner will br held at "; printmilitary( dinnertime ); cout << "mlitary time, \nwhich is "; printstandard( dinnertime ); cout << "standard time.\n"; // dinnertime dinnertime.hour = 29; dinnertime.minute = 73; cout << "\ntime with invalid values: " ; printmilitary( dinnertime ); cout << endl ; void printmilitary( const Time &t ) cout << ( t.hour < 10? "0" : "") << t.hour << ":" << ( t.minute < 10? "0" : "") << t.minute ; 33 44

void printstandard( const Time &t ) cout << ((t.hour == 0 t.hour == 12 )? 12 : t.hour % 12 ) << ":" << (t.minute < 10? "0" : "" ) << t.minute << ":" << (t.second < 10? "0" : "" ) << t.second << (t.hour < 12? "AM" : "PM" ) ; 12.2 CLASS class struct CLASS STRUCT CLASS STRUCT class TimeClass public: TimeClass(); void settime(int,int,int); void printmilitary(); void printstandard(); private: int hour; int minute; int second; ; CLASS PUBLIC C PRIVATE CLASS CONTRUCTOR 12.2.1CLASS class STUDENT // STUDENT public: int no; int age; ; STUDENT curtis; cout << " "; cin >> curtis.no ; 34 44

cout << " " ; cin >> curtis.age ; cout << curtis.no << endl << curtis.age ; 12.2.2globale function & member function C CLASS STRUCT MEMBER FUNCTION class STUDENT // STUDENT public: void print(); int no; int age; ; // PRINT void STUDENT::print() cout << "[" << no << "," << age << "]" << endl ; STUDENT curtis; cout << " "; cin >> curtis.no ; cout << " " ; cin >> curtis.age ; curtis.print(); // 35 44

12.2.3public private PUBLIC PUBLIC PRIVATE PUBLIC MEMBER FUNCTION class STUDENT // STUDENT public: void print(); void input(int,int); private: int no; int age; ; // PRINT void STUDENT::print() cout << "[" << no << "," << age << "]" << endl ; // // no age PRIVATE // MEMBER void STUDENT::input(int i_no, int i_age) no = i_no; age = i_age; STUDENT curtis; int age,no ; cout << " "; cin >> no ; cout << " " ; cin >> age ; curtis.input(no, age); curtis.print(); // input // print 36 44

12.2.4constructor constructor constructor 0 contructor class STUDENT // STUDENT public: STUDENT(); // constructor void print(); void input(int,int); private: int no; int age; ; // // //private STUDENT::STUDENT() no = 0; age = 0; // PRINT void STUDENT::print() cout << "[" << no << "," << age << "]" << endl ; // // no age PRIVATE // MEMBER void STUDENT::input(int i_no, int i_age) no = i_no; age = i_age; STUDENT curtis; int age,no ; 37 44

cout << " "; curtis.print(); cout << " "; cin >> no ; cout << " " ; cin >> age ; curtis.input(no, age); curtis.print(); // input // print 12.3 CLASS CLASS class TimeClass public: TimeClass(); void settime(int, int, int); void printmilitary(); void printstandard(); private: int hour; int minute; int second; ; TimeClass::TimeClass() hour=minute=second=0; void TimeClass::setTime(int h,int m,int s) hour = (( h>=0 && h<24)? h : 0); minute = ( m>=0 && m< 60 )? m : 0; second = ( s>=0 && s< 60 )? s : 0; void TimeClass::printMilitary() cout << ( hour < 10? "0" : "") << hour << ":" << ( minute < 10? "0" : "") << minute ; void TimeClass::printStandard() cout << ((hour == 0 hour == 12 )? 12 : hour % 12 ) << ":" << (minute < 10? "0" : "" ) << minute 38 44

<< ":" << (second < 10? "0" : "" ) << second << (hour < 12? "AM" : "PM" ) ; TimeClass t; // t cout << "The initial militay time is: "; t.printmilitary(); cout << "\nthe initial standard time is: "; t.printstandard(); t.settime(13,27,6); cout << "\n\nthe militay time after set is: "; t.printmilitary(); cout << "\nthe standard time after set is: "; t.printstandard(); t.settime(99,99,99); cout << "\n\nthe militay time after invalid set is: "; t.printmilitary(); cout << "\nthe standard time after invalid set is: "; t.printstandard(); cout << endl ; 12.4CLASS CLASS GOOD private no price quan 1. constructor 2. input no,quan,price 3. print 4. cal 1 0 39 44

13.1CONSTCONSTANT MEMBER FUNCTION OBJECT C const Time noon(12,0,0); TIME NOON C CONST #include "time5.h" // Time::Time(int hr,int min,int sec)settime(hr,min,sec); void Time::setTime(int h,int m,int s) sethour(h); setminute(m); setsecond(s); void Time::setHour(int h) hour = ( (h>=0 && h<=24)? h:0 ); void Time::setMinute(int m) minute = ( (m>=0 && m<60)? m:0); void Time::setSecond(int s) second = ( (s>=0 && s<60)? s:0); int Time::getHour() const return hour; int Time::getMinute() const return minute; int Time::getSecond() const return second; void Time::printMilitary() const cout << ( hour < 10? "0" : "") << hour << ":" << ( minute < 10? "0" : "") << minute ; void Time::printStandatd() 40 44

cout << ( (hour == 12)? 12: hour % 12) << ":" << ( minute < 10? "0" : "") << minute << ":" << ( second < 10? "0" : "") << second << ":" << ( hour < 12? "AM" : "PM" ); // Time wakeup(6,45,0); const Time noon(12, 0, 0); //non-constant object //constant object //Member Function Object wakeup.sethour(18); //non-const non-const noon.sethour(12); //non-const const wakeup.gethour(); //const non-const noon.getminute(); //const const noon.printmilitary(); //const const noon.printstandatd(); //non-const const Compiling... paper13_1.cpp D:\C++\taiwan\test_for_paper13\paper13_1.cpp(52) : error C2662: 'sethour' : cannot convert 'this' pointer from 'const class Time' to 'class Time &' Conversion loses qualifiers D:\C++\taiwan\test_for_paper13\paper13_1.cpp(58) : error C2662: 'printstandatd' : cannot convert 'this' pointer from 'const class Time' to 'class Time &' Conversion loses qualifiers Error executing cl.exe. test_for_paper13.exe - 2 error(s), 0 warning(s) noon.sethour CONST sethour NON-CONST noon.printstandard() CONST CONST MEMBER FUNCTION 41 44

13.2 (operator overloading) #include <string.h> class string public: string(char *); //Constructor void str_append(char *); void chr_minus(char); void show_string(void); private: char data[256]; ; string::string(char *str) strcpy(data,str); void string::str_append(char *str) strcat(data,str); void string::chr_minus(char letter) char temp[256]; int i,j; for(i=0,j=0;data[i];i++) if(data[i]!= letter) temp[j++] = data[i]; temp[j] = NULL; strcpy(data,temp); void string::show_string() cout << data << endl ; 42 44

string title("rescued By C++"); string lesson("understanding Operator Overloading"); title.show_string(); title.str_append(" rescued me!"); title.show_string(); lesson.show_string(); lesson.chr_minus('n'); lesson.show_string(); #include <string.h> class string public: string(char *); //Constructor void operator +(char *); void operator -(char); void show_string(void); private: char data[256]; ; string::string(char *str) strcpy(data,str); void string::operator +(char *str) strcat(data,str); void string::operator -(char letter) char temp[256]; int i,j; for(i=0,j=0;data[i];i++) if(data[i]!= letter) temp[j++] = data[i]; temp[j] = NULL; strcpy(data,temp); void string::show_string() cout << data << endl ; 43 44

string title("rescued By C++"); string lesson("understanding Operator Overloading"); title.show_string(); title + " rescued me!"; title.show_string(); lesson.show_string(); lesson - 'n'; lesson.show_string(); 13.3 struct XY void setvalue(int,int); ; int x; int y; void XY::setValue(int a,int b) x=a; y=b; ostream &operator <<(ostream &ooo,const XY &t) ooo << "[" << t.x << "," << t.y << "]" << endl ; return ooo ; XY test1; test1.setvalue(1,1); cout << test1; 44 44