2017 c 1 C++ (1) C C++, C++, C 11, 12 13 (1) 14 (2) 11 1 n C++ //, [List 11] 1: #include <iostream> // C <stdinh> 2: 3: int main(void) { 4: std::cout << "hello" << std::endl; // 5: int n; //, 6: std::cout << "n = "; 7: std::cin >> n; // 8: int s = 0; 9: for (int i=1; i<=n; i++) s += i; 10: std::cout << "sum(1" << n << ") = " << s << std::endl; 11: return 0; 12: } 3: << std::cout, C stdout ( C stderr ( ), C++ std::cerr ) <<, std::endl std::cout << "hello\n"; std::cout << "hello" << std::endl;,,, std::endl, "\n", std::endl 7: >> std::cin, C stdin >>, 9: for int i for 1 1
C gcc g++ hellocpp, g++ hellocpp C, Cygwin aexe, Linux Mac OS X aout, /aexe (Cygwin ; exe ) /aout (Linux Mac OS X ), -o, myprog (Cygwin myprogexe), g++ -o myprog hellocpp, ( C printf ) 11 [List 11] ( ),,,, (,,,, int double,,, std:: std::,, using namespace std;, ( ), C++ 12 new delete C malloc(), free(), new delete 1 2
[List 12] 1: #include <iostream> 2: 3: int main(void) { 4: int *a = 0; // 5: a = new int; // int 1, 6: *a = 100; 7: std::cout << *a << std::endl; 8: delete a; // 9: return 0; 10: } 4:, C NULL, C++ 0 5: new,, (bad alloc ),,, catch, throw,, new [ ] delete [], n, n [List 13] 1: #include <iostream> 2: 3: int main(void) { 4: int n; 5: std::cin >> n; 6: int* a = new int[n]; // n, 7: // 7: for (int i = 0; i<n; i++) a[i] = i; 8: delete [] a; // delete [] (!) 9: return 0; 10: } new, delete,,,,, (memory leak, memory leakage) 1 3
13 131 C, (call-by-value) C++, [List 14] 1: #include <iostream> 2: 3: void inc(int x) { // x, 4: x ++; // 1, 5: std::cout << "x = " << x << std::endl; // 6: } 7: 8: int main(void) { 9: int a = 5; 10: inc(a); // a inc, 11: std::cout << "a = " << a << std::endl; // a? 12: return 0; 13: }, x = 6 a = 5, a a, C [List 15] 1: #include <iostream> 2: 3: void inc(int* x) { 4: (*x)++; 5: std::cout << "*x = " << *x << std::endl; 6: } 7: 8: int main(void) { 9: int a = 5; 10: inc(&a); 11: std::cout << "a = " << a << std::endl; 12: return 0; 13: }, *x = 6 a = 6 1 4
C++, (call-by-reference), &, ( ) [List 15] C++ [List 16] 1: #include <iostream> 2: 3: void inc(int& x) { // 4: x++; // x * 5: std::cout << "x = " << x << std::endl; 6: } 7: 8: int main(void) { 9: int a = 5; 10: inc(a); // & 11: std::cout << "a = " << a << std::endl; 12: return 0; 13: }, x = 6 a = 6 C++,, inc(&x), x=inc(x), 12 2 x y, x y x, y divrem(int&, int&), 132, C, 133 C, C++,, add 3, 3 1 5
[List 17] 1: #include <iostream> 2: 3: int add(int a, int b) {return a+b;} // (1) 4: int add(int a, int b, int c) {return a+b+c;} // (2) 5: double add(double a, double b) {return a+b;} // (3) 6: 7: int main(void) { 8: int x=3, y=4, z=5; 9: double p=314, q=244; 10: std::cout << add(x,y) << std::endl; // (1) 11: std::cout << add(x,y,z) << std::endl; // (2) 12: std::cout << add(p,q) << std::endl; // (3) 13: return 0; 14: } 10 12, add,, C++,, add(int,int), add(int,int,int), add(double,double), C, add 2 add ( ) (overloading), 134 max(int,int) (2, ) [List 18] 1: #include <iostream> 2: 3: int max(int a, int b) {return a>b? a : b;} 4: 5: int main(void) { 6: int x, y, z; : 7: z = max(x+1,y) + 3; : 8: return 0; 9: }, main(void) max(int,int), max(int,int) main(void) 1 6
(, ) [List 19] 1: int main(void) { 2: int x, y, z; : 4: z = ((x+1>y)? x+1 : y) + 3; : 5: return 0; 6: },, 1,,,,,,, inline [List 110] 3 : inline int max(int a, int b) {return a>b? a : b;} inline,, #define, inline,, #define max(a,b) ((a)>(b)?(a):(b)),, a = 0; max(++a,0); // a 2? max(++a,10); // a 1? 1, 14 (1) (class), C++, 1 max ( ++a > x? ++a : x ), ++a x ++a 2, 1 1 7
141, C struct, [List 111] 1: #include <iostream> 2: #include <asserth> // assert 3: 4: class stack { 5: private: 6: static const int max = 100; 7: int data[max]; 8: int sp; 9: public: 10: void push(int); // 11: void pop(); // 12: bool empty() const; // true 13: int top() const; // 14: int size() const; // 15: stack(); // 16: stack(); // 17: }; (17 ) (;)! (, ) 6 8: max, data[max], sp stack (member) 10 14: (member) (method) push(int d) d pop() empty() top() size() 1, 0 empty(), top(), size() const,, (const, ) push(int) pop(), const ( ) 1 8
15 16: stack() stack(), stack() stack (constructor; ) stack(); stack (destructor; ) 5: private, (6 8 ) 9: public, (10 16 ), stack,, public, stack ( ), stack (encupslation of data), 2: <asserth>, assert(bool), 7 [List 112] 1: void stack::push(int d) { 2: assert(sp<max); 3: data[sp++] = d; 4: } 5: 6: void stack::pop() { 7: assert(0<sp); 8: --sp; 9: } 10: 11: bool stack::empty() const { 12: return sp==0; 13: } 14: 15: int stack::top() const { 16: return data[sp-1]; 17: } 18: 19: int stack::size() const { 20: return sp; 21: } 22: 23: stack::stack() {sp = 0;} 24: 25: stack:: stack() {} stack::, stack 2: assert(sp<max);,, sp<max assert ( );, 0, 1 9
7: 11, 15, 19: const, const 23:,, 0 25:, (1), [List 113] 1: int main(void) { 2: stack s; // stack 3: spush(5); // s = (5) 4: spush(8); // s = (5 8) 5: spush(9); // s = (5 8 9) 6: std::cout << stop() << std::endl; // 9 7: spop(); // s = (5 8) 8: std::cout << stop() << std::endl; // 8 9: spop(); // s = (5) 10: spush(3); // s = (5 3) 11: std::cout << ssize() << std::endl; // s 2 12: while(!sempty()) { 13: std::cout << stop() << std::endl; 14: spop(); 15: } 16: return 0; 17: } 2: int i; s stack, s push(5), spush(5) (, p, p->push(5) ) s main(void) 13 ([List 111], [List 112], [List 113] ),, (2) int, stack 1 10
[List 114] 1: int main(void) { 2: stack* s = new stack; // 3: s->push(5); // ( ) 4: s->push(8); 5: s->push(9); 6: std::cout << s->top() << std::endl; 7: s->pop(); 8: std::cout << s->top() << std::endl; 9: s->pop(); 10: s->push(3); 11: std::cout << s->size() << std::endl; 12: while(!s->empty()) { 13: std::cout << s->top() << std::endl; 14: s->pop(); 15: } 16: delete s; // 17: return 0; 18: } 2: stack s, new ( ) 16: s ( ) stack 142 stack, inline,,,, [List 115] 1: class stack { 2: private: 3: static const int max = 100; 4: int data[max]; 5: int sp; 6: public: 7: void push(int d) {assert(sp<max); data[sp++] = d;} 8: void pop() {assert(0<sp); --sp;} 9: bool empty() const {return sp==0;} 10: int top() const {return data[sp-1];} 11: int size() const {return sp;} 12: stack() {sp = 0;} 13: stack() {} 14: }; 1 11
,, 1 143, (, ) stack, s(n);, n data n s, data [List 115] 12, (sz) [List 116] 121: stack(int sz=100) { 122: sp = 0; 123: max = sz; 124: data = new int[max]; 125: } 121: int sz=100,, 100 new, delete [List 115] 13 [List 117] 13 : stack() {delete [] data;} [List 113] 2, 45 [List 118] 2 : stack s(45);,, stack s; stack s(100); 1 12
14, max, 121, max, static const int ( int) ( ), 100, C++, data int data[max] data 144 2 stack equal(const stack&, const stack&) const, equal(const stack&, const stack&) 2,, true, false ( ) true false 2 bool typedef int bool; const bool false = 0; const bool true = 1;, (stack) (stack&) ( ), stack ( read-only ), const (, ), [List 119] 1: bool equal(const stack& s1, const stack& s2) { 2: bool eq = true; // eq=true, eq=false 3: if (s1sp!=s2sp) eq = false; // 4: else { 5: for (int j=0; j<s1sp && eq; j++) { 6: if (s1data[j]!=s2data[j]) eq = false; 7: // 8: } 9: } 10: return eq; 11: } 10 0, 12 1 1 13
[List 120] 1: int main(void) { 2: stack s1, s2; 3: s1push(5); 4: s1push(8); 5: s1push(9); 6: s2push(5); 7: s2push(8); 8: s2push(9); 9: s2push(10); 10: std::cout << equal(s1,s2) << std::endl; 11: s2pop(); 12: std::cout << equal(s1,s2) << std::endl; 13: return 0; 14: }, ( ) sstack2cpp: In function bool equal(const class stack &, const class stack &) : sstack2cpp:30: member sp is a private member of class stack sstack2cpp:30: member sp is a private member of class stack sstack2cpp:32: member sp is a private member of class stack sstack2cpp:33: member data is a private member of class stack sstack2cpp:33: member data is a private member of class stack, equal(const stack&, const stack&) stack, private,, equal(const stack&, const stack&), 2, public,,, equal(const stack&, const stack&),,,,, 12 friend [List 121] 1: class stack { 2: private: 3: static const int max = 100; 4: int data[max]; 5: int sp; 1 14
6: public: 7: void push(int); 8: void pop(); 9: bool empty() const; 10: int top() const; 11: int size() const; 12: friend bool equal(const stack&, const stack&); 13: stack(); 14: stack(); 15: }; 15 stack bool sum equal(const stack&, const stack&),, 2 true, false,, ( ) 15 (2) 2 Complex, real, imag,,, [List 122] 1: class Complex { 2: private: 3: double real; // 4: double imag; // 5: public: 6: Complex() {real = 00; imag = 00;} 7: Complex(double r, double i) {real = r; imag = i;} 8: Complex() {} 9: double re() const {return real;} // 10: double im() const {return imag;} // 11: void set re(double r) {real = r;} // 12: void set im(double i) {imag = i;} // 13: void print(std::ostream& os) const { // 14: os << real << "+" << imag << "i"; 15: } 16: };, a, b, c ( ), 1 15
[List 123] 1: int main(void) { 2: Complex a; // 1 3: aset re(111); 4: aset im(222); 5: Complex b(333,444); // 2 6: Complex c; // 3 7: c = Complex(999, 8888); 8: aprint(std::cout); std::cout << std::endl; 9: bprint(std::cout); std::cout << std::endl; 10: cprint(std::cout); std::cout << std::endl; 11: c = a; // 12: cprint(std::cout); std::cout << std::endl; 13: return 0; 14: } 2, Complex() Complex ( 0+0i), 3 4 5, 2 Complex(double, double), 6, 2, 7 Complex 16 [List 122], [List 123],, double r, r 0 Complex(double), main(void) Complex C, add(complex, Complex) [List 124] 1: Complex add(complex a, Complex b) { 2: double r = are() + bre(); 3: double i = aim() + bim(); 4: return Complex(r,i); 5: }, 1 16
[List 125] 1: int main(void) { 2: Complex x, y, z, a; 3: x = Complex(100, 200); 4: y = Complex(222, 314); 5: z = Complex(423, 999); 6: a = add(add(x,y),z); 7: xprint(std::cout); std::cout << std::endl; 8: yprint(std::cout); std::cout << std::endl; 9: zprint(std::cout); std::cout << std::endl; 10: aprint(std::cout); std::cout << std::endl; 11: return 0; 12: } C++ /, + -,,, a = add(add(x,y),z); a = x + y + z;, * /, + -,, +, -, *, / [List 126] 1: Complex operator+(complex a, Complex b) { 2: double r = are() + bre(); 3: double i = aim() + bim(); 4: return Complex(r,i); 5: } [List 124] 1 add operator+ operator+ + operator+ 2 operator+, int double operator+ (, ) inline, 17 double Complex, Complex 1 17
[List 127] 1: int main(void) { 2: Complex a(100, 200); 3: Complex b(300, 400); 4: Complex c(235, 532); 5: Complex x = 20 * c; 6: xprint(std::cout); std::cout << std::endl; 7: Complex y = a * b; 8: yprint(std::cout); std::cout << std::endl; 9: return 0; 10: } 47+1064i -5+10i Complex, << <<, +, operator<< [List 128] 1: std::ostream& operator<<(std::ostream& os, Complex& c) { 2: cprint(os); 3: return os; 4: } void std::ostream&, 3 os, (std::cout << "a = " << a << std::endl ) std::ostream, (, ) Complex,, int double 1 18
[List 129] 1: int main(void) { 2: Complex a[3]; // a[0], a[1], a[2] 3: a[0] = Complex(100,200); 4: a[1] = a[0] + Complex(000,100); 5: Complex *c = &(a[2]); // c a[2] 6: *c = a[0]; // *c a[2] 7: for (int i=0; i<3; i++) std::cout << a[i] << std::endl; 8: Complex *b = new Complex[3]; // 9: for (int i=0; i<3; i++) b[i] = a[i] + Complex(100,100); 10: for (int i=0; i<3; i++) std::cout << b[i] << std::endl; 11: delete [] b; // b 12: return 0; 13: } 18 [List 129] ( ) 1 Bjarne Stroustrup, ( ) /, C++ [ 3 ], /, ISBN4-7561-1895-X, 7,000 STL, 1 2 Herbert Schildt,,, C++,, ISBN4-88135-761-8, 3,200 C++ 3 Gregory Satir, Doug Brown,, C++ /, ISBN4-900900-04-4, 3,900 C C++ 4 Scott Meyers,, Effective C++ [ 2 ], /, ISBN4-7561- 1808-9, 3,800 C, C++ 5 Scott Meyers,,,, More Effective C++, /, ISBN4-7561-1853-4, 3,800 Effective C++ ( ) 6 Andrew Koenig, Barbara E Moo,, C++, /, ISBN4-7952-9721-5, 3,800 C++, 3 1 19
1 20 Nagisa ISHIURA