C++ 1 C C++ C++ C C C++ 1.1 C printf() scanf() C++ C 1-1 #include <stdio.h> int a; scanf( "%d", &a ); printf( "a = %d\n", a ); C++ 1-2 int a; std::cin >> a; std::cout << "a = " << a << std::endl; 1-1 1-2 C++( 1-2 ) stdio.h iostream iostream.h C++ include.h 1-2 scanf() std::cin >> std::cin (stream) >> ( std::cin ) int a 1-2 printf() std::cout << ( std::cout ) a std::endl ( end of line ) C++ (std::cin, std::cout) ( << >> ) printf() scanf() 1
1-1 1-2 double 2 std::cin, std::cout 1.2 C fopen() fclose() C++ std::fstream 1-3 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.txt", std::ios::out ); fout << a << std::endl; fout.close(); 1-4 #include <fstream> std::fstream fin; int a; fin.open( "data.txt", std::ios::in ); fin >> a ; fin.close(); std::cout << a << std::endl; 1-3 1-4 fstream open() open() std::ios::out << open() 2
std::ios::in >> 1-3 1-4 1.3 new delete C malloc() free() C++ new delete C int n 1-5 #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-6 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-6 3
new malloc() (sizeof(int)*n ) 1-5 int n n double new delete 1.4 std::vector new delete C++ std::vector 1-6 std::vector 1-7 #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 1-6 std::vector 1.5 std::string C char BOF( ) C++ std::string C++ std::string 1-8 #include <string> 4
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 == 1-9 #include <string> std::string a; std::string b; std::cin >> a; b = " "; if( a == b ) std::cout << " \n"; else std::cout << " \n"; 1-7 std::string a, b, c a+b c 1.6 ( ) C C++ ( ) 1-8 1-10 5
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; 1.7 C C++ 1-9 test1() test2() test3() 1-11 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 ); test3( a3 ); std::cout << a1 << " " << a2 << " " << a3 << std::endl; 6
2 ( ) C++ int a,b int ( ) a b ( ) 2.1 C++ C C (SCORE) 2-1 #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++ 2-2 class SCORE int math; int english; ; SCORE sc; 7
sc.math = 80; sc.english = 70; std::cout << "math = " << sc.math << " english = " << sc.english << std::endl; 2-1 2-2 struct class int math; SCORE math, english SCORE math,english C math english mean() 2-3 #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() 2-4 class SCORE 8
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; 2-3 2-4 mean() math english sc. 2-4 mean() SCORE maen() sc.maen(). () 2-1 2-4 math english int max() 2.2 ( ) ( ) SCORE tokai,oyama 2-5 class SCORE int math; int english; ; double mean() return ( math + english )/2.0; 9
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 2-2 max() SCORE tokai,oyama, 3 2.3 (construncor) SCORE 2-6 class SCORE int math; int english; SCORE( int m, int e ) math = m; english = e; ; double mean() return ( math + english )/2.0; SCORE tokai( 10, 20 ), oyama( 90, 80 ); 10
std::cout << "mean of tokai = " << tokai.mean() << std::endl << "mean of oyama = " << oyama.mean() << std::endl; main() SCORE( int m, int e ) (destruntor) 2-7 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() 2-3 11
2-8 #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; 2.4 ( ) ( :inheritance) (FRUIT) 2-9 #include <string> class FRUIT std::string name; FRUIT( std::string n ) name = n; 12
; void show_name() std::cout << name << std::endl; (APPLE) (ORANGE) 2-10 class APPLE : public FRUIT ; APPLE() : FRUIT( " " ) class ORANGE : public FRUIT ; ORANGE() : FRUIT( " " ) APPLE apple; ORANGE orange; apple.show_name(); orange.show_name(); 2-10 APPLE ORANG : public FRUIT FRUIT : FRUIT( ) apple.show name() orange.show name() 2-11 class APPLE : public FRUIT 13
int size; APPLE() : FRUIT( " " ) size = 10; ; void show_size() std::cout << size << " cm" << std::endl; APPLE apple; apple.show_name(); apple.show_size(); 2-12 class APPLE : public FRUIT int size; APPLE() : FRUIT( " " ) size = 10; ; void show_name() std::cout << " " << name << " " << std::endl; APPLE apple; 14
apple.show_name(); main() apple.show name() (FRUIT) show name() (APPLE) show name() 2-4 (class DRINK) (std::string name) (int yen) (void show name(),void show yen()) 2 2.5 2-10 APPLE 2-10 main() 2-13 APPLE apple; apple.name = " "; apple.show_name(); C ( ) C++ public private protected 3 public public private 2-14 #include <string> class FRUIT private: 15
std::string name; FRUIT( std::string n ) name = n; ; FRUIT fruit( " " ); fruit.name = " "; private name main() protected 2-15 #include <string> class FRUIT protected: std::string name; FRUIT( std::string n ) name = n; ; class APPLE : public FRUIT APPLE() : FRUIT( " " ) ; APPLE apple; 16
apple.name = " "; main() protected FRUIT name 2-16 #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 2-17 17
class FRUIT std::string name; FRUIT( std::string n ) name = n; ; name private 2-5 DRINK private main() private main() 2.6 ( ) 18
3 3.1 has a is a has a( ) is a( ) has a A B ( ) A has a B 3-1 class TIRE TIRE() ; class CAR TIRE front_right, front_left, rear_right, rear_left; ; CAR() 3-1 (CAR) 4 (TIRE) is a A B A ia a B 3-2 class CAR CAR() ; class TRUCK : public CAR TRUCK() ; 3-2 (TRUCK) (CAR) has a is a A B A B 3-3 19
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(); 3-3 (HUMAN) (CAR) (void drive car()) 3-1 3-3 has a (HUMAN) (CAR) void drive car() 3.2 PAD has a is a 1 + public - private # protected (HUMAN) 20
1: 3-4 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) 21
3.3 has a( ) has a 3-5 class PENCIL PENCIL() ; class HUMAN PENCIL pencil; ; HUMAN() 3: has a 3.4 is a( ) is a 3-6 class CAR CAR() ; class TRUCK : public CAR 22
; TRUCK() 4: is a 3.5 3-7 class PENCIL PENCIL() ; class HUMAN HUMAN() ; void use_pencil() PENCIL pencil; 23
5: 3-2 DRINK 24
4 (1) ABC DEF 2 ( ) off ( ) (2) ( ) ( ) TV 2 TV TV ABC DEF 2 (is a) TV ABC DEF (has a) (3) TV (TV ) CHANNEL ( ) 2 CHANNEL ABC (ABC ) DEF (DEF ) 2 TV ABC abc DEF def 2 private void change channel() ( ) public off CHANNEL std::string name ( ) private std::string get name() ( ) public ABC DEF TV -abc +change channel() -def CHANNEL -name +get name() ABC CHANNEL DEF CHANNEL (4) (5) C++ 25
6: TV 4-1 #include <string> //---------------------------------------- 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 26
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" ) std::cout << abc.get_name() << std::endl; if( input == "1" ) std::cout << def.get_name() << std::endl; if( input == "off" ) break; //---------------------------------------- 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 4-1 TV GHI 27
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 ) 28