본문 바로가기

Programming/[C++]

(32)
[C++] pointer (포인터) 4) [] -> * 표현식 - 포인터는 주소 값을 저장하는 변수이다. - 포인터를 배열로 표현할 수 있고, 배열을 포인터로 표현할 수 있다. - 표현만 바뀌는 것이다. (배열과 포인터는 완전 다르다. 하지만 둘 모두 주소 값을 저장하고 있기에 호환 가능) ex) int a[4] = {4,3,2,1} 라는 배열의 선언과 초기화를 실행하면 (1차원 배열) 구분기준 참고사항 비고 4 3 2 1 a[0] 자체의 값 요소의 값 a[0] a[1] a[2] a[3] *a *(a+1) *(a+2) *(a+3) 괄호를 꼭 붙여야 함 &a[0] &a[1] &a[2] &a[3] &는 주소 표현 요소의 주소(&) a a+1 a+2 a+3 주소 값 + X (4byte) 100 104 108 112 주소 출력 값 ex) int ..
[C++] 콜론 초기화 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include using namespace std; class A { int a; public : A(int a) { this->a = a; } void dispA() { cout
[C++] 다양한 포인터(pointer) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include using namespace std; void main() { int a[2][3] = { 6, 5, 4, 3, 2, 1 }; int i, j; int(*p)[3]; p = a; //초기화 for ( i = 0; i
[C++] Template(템플릿) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include #include using namespace std; template void change(T &a, T &b) { int temp; temp = a; a = b; b = temp; } void main() { int a = 1, b = 2; char c = 'A', d = 'B'; float e = 3.7f, f = 4.3f; change(a, b); cout
[C++] Template 함수 (템플릿 함수) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include using namespace std; template int Compare(T t1, T t2) { return t1 - t2; } int Compare (const char *str1, const char *str2) { return strcmp(str1, str2); } void main() { if (Compare(10, 5) > 0) { cout
[C++] Template Class (템플릿 클래스) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include #include #pragma once using namespace std; template class MyTemplate { T data; public : MyTemplate(T_data); int Compare(T in); operator T(); }; template MyTemplate ::MyTemplate(T_data) { data = _data; } template int MyTemplate::Compare(T in) { return data - in; } template MyTemplate::operator T() { return data; }..
[C++] 성적 처리 프로그램 동적바인딩 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 #include using namespace std; void input(char *name, int *score, int num); void oper(int *score, float *avg, int num); void output(char *name, int *score, float *avg, int num); in..
[C++] Static (스태틱) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 #include using namespace std; /* static method 1. class를 통틀어서 오직 하나다 2. this pointer가 존재하지 않는다 3. instance field를 사용할 수 없다 4. 클래스명::메소드() 이렇게 사용도 할 수 있고, 객체 메소드() *p 객체를 만든 후에 가능 */ class A { int a; public ..