본문 바로가기

전체 글

(129)
[C++] 포함 오브젝트(has ~a) 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 #include #include using namespace std; class A{ string name; public : A() { cout age = age; } int getAge()const { cout
[C++] Friend 함수 (프렌드 함수) 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 #include using namespace std; class A { int a; int b; public: A(int a = 100000) { this->a = a; } void setA(int a) { this->a = a; } int getA() { return this->a = a; } int operator+ (int data) { return this->a + data; } A& operator= (const A &aa) { cout
[C++] Friend 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 29 30 31 32 33 34 35 36 37 38 #include using namespace std; //개념 : C++의 프렌드는 한쪽 방향으로만 진행된다. 퍼블릭으로 인식해서 마음대로 쓴다. 상대방은 내껄 쓸 수 없다. /* 1. friend class 2. friend method 3. friend function X 3번이 가장 많이 쓰이고 그 다음 1번 2번으로 많이 쓰인다. friend는 많이쓰면 좋지 않다 연산자함수 = (외부함수를 멤버함수로 만들 수 있다) 객체와 객체를 산술 연산이 가능하게 해준다 */ class A { int money; public..
[C++] 파일 입출력 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include #include using namespace std; void main() { ofstream fout; ifstream fin; fout.open("a.txt"); fout
[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 #include using namespace std; /* 소멸자 함수 1. 객체 소멸시 자동 호출 되어지는 함수 2. ~클래스명() {} //~는 필드 3. 매개변수를 선언할 수 없다. 4. 오버로딩이 불가능하다. (오직 하나) 5. const member function으로 만들 수 없다. 6. 객체의 잔여 메모리를 깨끗하게 정리하는 역할 호출 시점 1. 정적 메모리는 함수가 끝났을 때 2. 동적 메모리는 delete를 했을 때 */ class Apple { public : Apple() { cout
[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 #include using namespace std; class A { int *p; public: A() { p = new int; *p = 0; } ~A() { delete p; } A(const A &aa) { p = new int; *p = *aa.p; } void setA(int data) { *p = data; } int getA()const { return *p; } A& operator= (const A &aa) { if (this == &aa) { return *this; } delete p; p = new ..
[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 #include using namespace std; //1. 사용자 정의 함수 선언 void disp(int a); void input_ref(int &a); void input_add(int *p); void main() { int a = 10; disp(a); //call by value //a = input(a); //input_add(&a); input_ref(a); disp(a); } void input_ref(int &a) { cout a; } void input_add(int *..
[C++] cout object(씨아웃 오브젝트) 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 #include using namespace std; class A { int a; public: A(int a = 1000) { this->a = a; } void setA(int a) { this->a = a; } const int getA() { return a; } friend ostream& operator(istream &cin, A &aa); //값을 읽기만 할 때 const로 지정 //friend int ope..