endl new line include iostream using namespace std












































- Slides: 44
생성자 예 생성자 정의 생성자 구현 endl은 new line 생성자 호출 #include <iostream> using namespace std; class my. Class { int a; public: void my. Class(); void show(); } my. Class: : my. Class() { a =10; } void my. Class: : show() { cout << “a=“ << a << endl; } void main() { my. Class obj; // 객체 생성됨 obj. show(); }
소멸자 예 소멸자 정의 소멸자 구현 #include <iostream> using namespace std; class my. Class { int a; public: void my. Class(); void ~my. Class(); void show(); } my. Class: : my. Class() { a =10; } my. Class: : ~my. Class() { cout <<“dest. . ”; } void my. Class: : show() { cout << “a=“ << a << endl; } void main() { my. Class obj; // 객체 생성됨 obj. show(); 소멸자 호출 }
#include <iostream> using namespace std; 매개변수를 갖는 생성자 정의 - 두 개의 생성 자 정의 - 함수 중복됨 매개 변수 있는 생 성자 구현 class my. Class { int a; public: my. Class(); my. Class(int x); void show(); } my. Class: : my. Class() { a =10; } my. Class: : my. Class(int x) { a =x; } void my. Class: : show() { cout << “a=“ << a << endl; } void main() { my. Class obj; my. Class obj 2(55); 매개변수 없는 생 동일한 표현 obj. show(); 성자 호출 obj 2. show(); my. Class obj 2 = my. Class(55); 매개변수 있는 생 } 성자 호출
void main() { B b; D d; 상속 예 private, protected 도 가능, 상속방법이 달라짐(7장) #include <iostream> using namespace std; class B { int n; public: void set. N(int i); int get. N(); } class D : public B { int m; public: void set. M(int i); int get. M(); } void B: : set. N(int i) { n = i; } int B: : get. N() { return n; } void D: : set. M(int i) { m = i; } int D: : get. M() { return m; } cout << b. get. N() << d. get. M(); } n set. N() get. N() m set. M() get. M() b d main() 실행 중 생성된 객체 인스턴스
객체 포인터 객체의 멤버 접근 � 객체. 멤버 � 객체포인터->멤버 객체 포인터 정의 객체 포인터 얻기 포인터로 멤버 호 출 #include <iostream> using namespace std; class my. Class { int a; public: my. Class(); void show(); } my. Class: : my. Class() { a =10; } void my. Class: : show() { cout << “a=“ << a << endl; } void main() { my. Class obj; my. Class* p; p = &obj; obj. show(); p->show(); }
구조체와 클래스 관계 예 class B { public: int n; void set. N(int i); int get. N(); private: int m; } struct B { int n; void set. N(int i); int get. N(); private: int m; } void B: : set. N(int i) { n = i; } int B: : get. N() { return n; } 동일 void B: : set. N(int i) { n = i; } int B: : get. N() { return n; } #include <iostream> using namespace std; void main() { struct B b; b. m = 10; b. set. N(5); cout << b. get. N(); }
#include <iostream> using namespace std; 자동 인라인 함수 예 class my. Class { 자동 인라인 함수 int a; public: my. Class(); void show() { cout << “a=“ << a << endl; }; } my. Class: : my. Class() { a =10; } void main() { my. Class obj; obj. show(); }
namespace 만들고 사용하기 using은 namespace공간의 이름을 사용하도록 컴파일러가 주는 지시 파일 A namespace 생성 namespace Project. A { int age; + void run(); 변수, 함수, 클래스, 구조체 등 포함 가능 int add(int a) { return a + 1; }; class Engine { int a; }; struct Data { int name; int age; }; } int age; 전역공간 age namespace Project. A { namespace에 추가 void run() { 코드 제공. . . (동일 파일 혹은 } 다른 파일로) } 파일 B namespace Project. A { void stop() {. . . . } } using namespace Project. A; void main() { cin >> age; Engine e = new Engine(); } using Project. A: : age; void main() { cin >> age; Project. A: : Engine e = new Project. A: : Engine(); } #include <파일A> #include <파일B> void main() { cin >> age; // 전역공간 age cin>>Project. A: : age; //namespace Project. A의 age }
namespace std와 ANSI 표준의 include 최근 버전의 C++ 표준 컴파일러 구버전의 C++ 표준 컴파일러 #include <iostream> #include <iostream. h> using namespace std void main() { std: : cout << "a"; } } } iostream 헤더 파일 내에 namespace srd가 정의되어 있음 #include <iostream. h> void main() { std: : cout << "a"; } 오류 발생 1examplesm. cpp(13) : error C 2653: 'std' : is not a class or namespace name
iostream 파일과 yvals. h 파일 iostream 내부적으로 include _STD_BEGIN // OBJECTS static ios_base: : Init _Ios_init; extern _CRTIMP istream cin; extern _CRTIMP ostream cout; extern _CRTIMP ostream cerr, clog; // CLASS _Winit class _CRTIMP _Winit { public: _Winit(); ~_Winit(); private: static int _Init_cnt; }; // WIDE OBJECTS static _Winit _Wios_init; extern _CRTIMP wistream wcin; extern _CRTIMP wostream wcout, wcerr, wclog; _STD_END . . . #if defined(__cplus) #define _STD_BEGIN { #define _STD_END. . . yvals. h std: : namespace std };
Pre Quiz - my. Rect에 필요한 멤 버 작성 이름 #include <iostream> using namespace std; 학번 class my. Rect { int w; int h; public: }; void main() { my. Rect obj 1; my. Rect obj 2(5, 4); obj 1. square(); obj 2. square(); } // 실행결과는 다음과 같다. 1 20 20소멸 1소멸 class my. Rect { int w; int h; public: my. Rect() { w = h = 1; } my. Rect(int a, int b) { w = a; h = b; } ~my. Rect() { cout << w*h << "소멸"<< endl; } void square() { cout << w*h<<endl; } } };
객체치환에 있어 유의할 사항 #include <iostream> using namespace std; class my. String { char *p; int len; public: my. String(char* s) { p = new char [strlen(s)+1]; strcpy(p, s); len = strlen(p); } ~my. String() { delete [] p; } void show() { cout << p; } }; void main() { my. String a("kitae"), b("hansung"); a = b; } a p len = 5 b p len = 7 a p len = 7 b p 복 사 len = 7 "kitae" "hansung " main() 함수가 종료하는 순간 객체 b가 먼저 사라 다음 a가 사라질 때 소멸자에서 "hansung 실행시간 오류 발생 "kitae" " 객체 치환의 해결 방법 : 연사자(=) 중복, 복
프렌드 함수 프렌드 함수란? � 클래스이 멤버 함수가 아님 � � 클래스에 어떤 함수를 프렌드로 선언가능 프렌드 함수는 선언된 클래스의 모든 멤버에 접근 가능 is. Even은 객체 a의 멤버 n에 접근할 수 다른 클래스의 멤버이거나 단순 함수 private 멤버에 접근할 필요가 있는 함수를 friend로 선언 class my. Class { int n; public: my. Class(int k) { n = k; } }; bool is. Even(my. Class a) { if(a. n % 2 == 0) return true; else return false; } 없다. void ejal main() { my. Class m(5); cout << is. Even(m); } class my. Class { int n; public: my. Class(int k) { n = k; } friend bool is. Even(my. Class a); }; bool is. Even(my. Class a) { if(a. n % 2 == 0) return true; else return false; } void main() { my. Class m(5); cout << is. Even(m); } is. Even은 객체 a의 멤버 n에 접근할 수 있