1 C++ 1.1 C C++ C++ C C C++ 1.1.1 C printf() scanf() C++ C hello world printf() 1-1 #include <stdio.h> printf( "hello world\n" ); C++ 1-2 std::cout << "hello world" << std::endl; C scanf() 1-3 #include <stdio.h> int a; scanf( "%d", &a ); printf( "a = %d\n", a ); C++ 1
1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; 1-3 1-4 C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >> std::cin (stream) >> ( std::cin ) int a 1-4 printf() std::cout << ( std::cout ) a std::endl ( end of line ) C++ (std::cin, std::cout) ( << >> ) printf() scanf() 1-1 (10 ) 1-2 (double ) 2 std::cin, std::cout (10 ) 1.1.2 C fopen() fclose() C++ std::fstream 1-5 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.txt", std::ios::out ); fout << a << std::endl; fout.close(); 2
1-6 #include <fstream> std::fstream fout; int a; fout.open( "data.txt", std::ios::in ); fout >> a ; fout.close(); std::cout << a << std::endl; 1-5 1-6 fstream open() open() std::ios::out << open() std::ios::in >> 1-3 (10 ) 1-4 (10 ) 1.1.3 C C++ ( ) 1-7 int a; std::cin >> a; std::cout << a << std::endl; 3
1-8 int a; std::cin >> a; std::cout << a << std::endl; ( ) C++ ( ) 1-5 (10 ) 1-9 int a = 3; int a; std::cin >> a; std::cout << a << std::endl; std::cout << a << std::endl; 4
1.1.4 new delete C malloc() free() C++ new delete C int n 1-10 #include <stdio.h> #include <string.h> int n, i; int *a; n = 4; a = ( int* ) malloc( sizeof( int ) * n ); for( i = 0; i < n ; ++i ) scanf( "%d", a+i ); for( i = 0; i < n ; ++i ) printf( "[%d]=%d\n", i, a[i] ); free( a ); C++ new, delete 1-11 int n = 4; int* a = new int[n]; for( int i = 0; i < n; ++i ) std::cin >> a[i]; for( int i = 0; i < n; ++i ) std::cout << "[" << i << "]=" << a[i] << std::endl; delete a; a = new int[n] int n delete a 1-11 new malloc() (sizeof(int)*n ) ( ) C++ JavaScript Ruby 1-6 int n n double new delete (10 ) 5
1.1.5 std::vector new delete C++ std::vector 1-11 std::vector 1-12 #include <vector> int n = 4; std::vector< int > a; a.resize( n ); for( int i = 0; i < n; ++i ) std::cin >> a[i]; for( int i = 0; i < n; ++i ) std::cout << "[" << i << "]=" << a[i] << std::endl; vector std::vector< int > int std::vector a.resize( n ) std::vector delete ( ) ( ) std::vector (iterator) 1-7 std::vector ( 10 ) 1.1.6 std::string C char (BOF) C++ std::string C sprintf() 1-13 #include <stdio.h> #include <string.h> char a[256]; char b[256]; char c[256]; scanf( "%s", a ); 6
scanf( "%s", b ); sprintf( c, "%s%s", a, b ); printf( "%s\n", c ); std::string + 1-14 #include <string> std::string a; std::string b; std::string c; std::cin >> a; std::cin >> b; c = a + b; std::cout << c << std::endl; string 2 C strcmp() 1-15 #include <stdio.h> #include <string.h> char a[256]; char b[256]; scanf( "%s", a ); strcpy( b, " " ); if( strcmp( a, b ) == 0 ) printf( " \n" ); else printf( " \n" ); 7
std::string == 1-16 #include <string> std::string a; std::string b; std::cin >> a; b = " "; if( a == b ) std::cout << " \n"; else std::cout << " \n"; 1-8 int n n std::string std::vector (10 ) 1.1.7 ( ) C C++ ( ) 1-9 (10 ) 1-17 double test( int a ) return a + 1; double test( double a ) return a - 1; int a = 1; double b = 1; std::cout << test( a ) << std::endl; std::cout << test( b ) << std::endl; 8
1.1.8 C C++ 1-18 void test( int& z ) z += 1; int a = 1; test( a ); std::cout << a << std::endl; a test() 2 * ( ) ( ) C++ 1-10 test1() test2() test3() (10 ) 1-19 void test1( int a ) a += 1; void test2( int* a ) *a += 1; void test3( int& a ) a += 1; int a1 = 1, a2 = 1, a3 = 1; test1( a1 ); test2( &a2 ); 9
test3( a3 ); std::cout << a1 << " " << a2 << " " << a3 << std::endl; 10
1.2 ( ) C++ int a,b int ( ) a b ( ) 1.2.1 C++ C C (SCORE) 1-20 #include <stdio.h> struct SCORE int math; int english; ; struct SCORE sc; sc.math = 80; sc.english = 70; printf( "math = %d english = %d\n", sc.math, sc.english ); C++ 1-21 class SCORE int math; int english; ; SCORE sc; 11
sc.math = 80; sc.english = 70; std::cout << "math = " << sc.math << " english = " << sc.english << std::endl; 1-20 1-21 struct class int math; SCORE math, english SCORE math,english C math english mean() 1-22 #include <stdio.h> struct SCORE int math; int english; ; double mean( struct SCORE sc ) return ( sc.math + sc.english )/2.0; struct SCORE sc; sc.math = 80; sc.english = 70; printf( "math = %d english = %d\n", sc.math, sc.english ); printf( "mean = %lf\n", mean( sc ) ); C++ mean() 1-23 class SCORE 12
int math; int english; double mean() return ( math + english )/2.0; ; SCORE sc; sc.math = 80; sc.english = 70; std::cout << "math = " << sc.math << " english = " << sc.english << std::endl << "mean = " << sc.mean() << std::endl; 1-22 1-23 mean() math english sc. 1-23 mean() SCORE maen() sc.maen(). () 1-11 1-23 math english int max() 1.2.2 ( ) ( ) SCORE tokai,oyama 1-24 class SCORE int math; int english; ; double mean() return ( math + english )/2.0; 13
SCORE tokai,oyama; tokai.math = 10; tokai.english = 20; oyama.math = 90; oyama.english = 80; std::cout << "mean of tokai = " << tokai.mean() << std::endl << "mean of oyama = " << oyama.mean() << std::endl; tokai.mean() oyama.mean() tokai.mean() tokai math,english oyama.mean() oyama math,english 1-12 max() SCORE tokai,oyama, 3 1.2.3 (construncor) SCORE 1-25 class SCORE int math; int english; SCORE( int m, int e ) math = m; english = e; ; double mean() return ( math + english )/2.0; 14
SCORE tokai( 10, 20 ), oyama( 90, 80 ); std::cout << "mean of tokai = " << tokai.mean() << std::endl << "mean of oyama = " << oyama.mean() << std::endl; main() SCORE( int m, int e ) (destruntor) 1-26 class SCORE int math; int english; SCORE( int m, int e ) math = m; english = e; ~SCORE() std::cout << " \n"; ; double mean() return ( math + english )/2.0; SCORE tokai( 10, 20 ), oyama( 90, 80 ); std::cout << "mean of tokai = " << tokai.mean() << std::endl << "mean of oyama = " << oyama.mean() << std::endl; main() tokai,oyama SCORE() 1-13 ( 10 ) 15
1-27 #include <string> class FRUIT std::string name; FRUIT( std::string n ) name = n; std::cout << name << " \n"; ; ~FRUIT() std::cout << name << " \n"; FRUIT *fr = new FRUIT( "apple" ); delete fr; fr = new FRUIT( "orange" ); delete fr; 1.2.4 ( ) ( :inheritance) (FRUIT) 1-28 #include <string> class FRUIT std::string name; FRUIT( std::string n ) name = n; 16
; void show_name() std::cout << name << std::endl; (APPLE) (ORANGE) 1-29 class APPLE : public FRUIT ; APPLE() : FRUIT( " " ) class ORANGE : public FRUIT ; ORANGE() : FRUIT( " " ) APPLE apple; ORANGE orange; apple.show_name(); orange.show_name(); 1-29 APPLE ORANG : public FRUIT FRUIT : FRUIT( ) apple.show name() orange.show name() 1-30 class APPLE : public FRUIT 17
int size; APPLE() : FRUIT( " " ) size = 10; ; void show_size() std::cout << size << " cm" << std::endl; APPLE apple; apple.show_name(); apple.show_size(); 1-31 class APPLE : public FRUIT int size; APPLE() : FRUIT( " " ) size = 10; ; void show_name() std::cout << " " << name << " " << std::endl; APPLE apple; 18
apple.show_name(); main() apple.show name() (FRUIT) show name() (APPLE) show name() 1-14 (DRINK) (name) (yen) (show name(),show yen()) 2 (10 ) 1.2.5 1-29 APPLE 1-29 main() 1-32 APPLE apple; apple.name = " "; apple.show_name(); C ( ) C++ public private protected 3 public public private 1-33 #include <string> class FRUIT private: 19
std::string name; FRUIT( std::string n ) name = n; ; FRUIT fruit( " " ); fruit.name = " "; private name main() protected 1-34 #include <string> class FRUIT protected: std::string name; FRUIT( std::string n ) name = n; ; class APPLE : public FRUIT APPLE() : FRUIT( " " ) ; APPLE apple; 20
apple.name = " "; main() protected FRUIT name 1-35 #include <string> class FRUIT protected: std::string name; FRUIT( std::string n ) name = n; ; class APPLE : public FRUIT APPLE() : FRUIT( " " ) ; void change_name() name = " "; APPLE apple; apple.change_name(); FRUIT APPLE name private private 1-36 21
class FRUIT std::string name; FRUIT( std::string n ) name = n; ; name private 1-15 DRINK private main() private main() 1.2.6 ( ) 22
1.3 1.3.1 has a is a has a( ) is a( ) has a A B ( ) A has a B 1-37 class TIRE TIRE() ; class CAR TIRE front_right, front_left, rear_right, rear_left; ; CAR() 1-37 (CAR) 4 (TIRE) is a A B A ia a B 1-38 class CAR CAR() ; class TRUCK : public CAR TRUCK() ; 1-38 (TRUCK) (CAR) has a is a A B A B 1-39 23
class CAR CAR() std::cout << " \n"; ~CAR() std::cout << " \n"; void drive() std::cout << " \n"; ; class HUMAN HUMAN() ; void drive_car() CAR car; car.drive(); HUMAN human; human.drive_car(); 1-39 (HUMAN) (CAR) (void drive car()) 1-16 1-39 has a (HUMAN) (CAR) void drive car() 24
1.3.2 PAD has a is a 1: 1 + public - private # protected (HUMAN) 1-40 class HUMAN std::string name; protected: void change_name( std::string new_name ) name = new_name; HUMAN() ; void show_name() std::cout << name << std::endl; 2: (HUMAN) 25
1.3.3 has a( ) has a 1-41 class PENCIL PENCIL() ; class HUMAN PENCIL pencil; ; HUMAN() 3: has a 1.3.4 is a( ) is a 1-42 class CAR CAR() ; class TRUCK : public CAR 26
; TRUCK() 4: is a 1.3.5 1-43 class PENCIL PENCIL() ; class HUMAN HUMAN() ; void use_pencil() PENCIL pencil; 27
5: 1.4 (1) ABC DEF 2 ( ) off ( ) (2) ( ) ( ) TV 3 TV TV ABC DEF 2 (is a) TV ABC DEF (has a) (3) TV (TV ) SCREEN ( ) CHANNEL ( ) 3 CHANNEL ABC (ABC ) DEF (DEF ) 2 TV SCREEN screen ABC abc DEF def 3 private void change channel() ( ) public off SCREEN void show text( std::string text ) ( ) public CHANNEL std::string name ( ) private std::string get name() ( ) public ABC DEF 28
TV -screen +change channel() -abc -def SCREEN +show text() CHANNEL -name +get name() ABC CHANNEL DEF CHANNEL (4) 6: TV (5) C++ 1-44 #include <string> class SCREEN SCREEN() void show_text( std::string text ) std::cout << text << std::endl; ; 29
//---------------------------------------- class CHANNEL std::string name; CHANNEL( std::string n ) name = n; std::string get_name() return name; ; //---------------------------------------- class ABC : public CHANNEL ABC() : CHANNEL( "ABC " ) ; //---------------------------------------- class DEF : public CHANNEL DEF() : CHANNEL( "DEF " ) ; //---------------------------------------- class TV SCREEN screen; ABC abc; DEF def; TV() std::cout << " on\n"; ~TV() std::cout << " off\n"; void change_channel() for(;;) std::cout << " [ 0 or 1 or off ] : "; std::string input; std::cin >> input; ; if( input == "0" ) screen.show_text( abc.get_name() ); if( input == "1" ) screen.show_text( def.get_name() ); if( input == "off" ) break; 30
//---------------------------------------- TV tv; tv.change_channel(); (6) on [ 0 or 1 or off ] : 0 ABC [ 0 or 1 or off ] : 1 DEF [ 0 or 1 or off ] : off off 31
1.5 100 (1) (4) OK C++ (1) (4) PC (1) (10 ) (2) (1) is a has a (20 ) (3) (2) (20 ) (4) (3) (20 ) (5) (4) C++ (20 ) (6) (10 ) 32