endl new line include iostream using namespace std
- Slides: 91
생성자 예 생성자 정의 생성자 구현 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); 동일한 표현 my. Class obj 2 = my. Class(55); obj. show(); obj 2. show(); }
상속 예 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; } void main() { B b; D d; 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 만들고 사용하기 파일 A namespace 생성 변수, 함수, 클래스, 구조체 등 포함 가능 전역공간 age 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; namespace Project. A { void run() {. . . } } 파일 B + namespace Project. A { void stop() {. . . . } } using은 namespace공간의 이름을 간소하게 사용하도록 컴파일러가 주는 지시어 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++ 표준 컴파일러 #include <iostream. h> void main() { cout << "a"; } 최근 버전의 C++ 표준 컴파일러 #include <iostream> using namespace std void main() { cout << "a"; } #include <iostream> 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 "kitae" "hansung" "kitae" 복사 p len = 7 "hansung" main() 함수가 종료하는 순간 객체 b가 먼저 사라지고 다음 a가 사라질 때 소멸자에서 실행시간 오류 발생 객체 치환의 해결 방법 : 연사자(=) 중복, 복사 생성자
프렌드 함수 프렌드 함수란? 클래스이 멤버 함수가 아님 클래스에 어떤 함수를 프렌드로 선언가능 프렌드 함수는 선언된 클래스의 모든 멤버에 접근 가능 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에 접근할 수 있다.
객체 배열 a a=0 a a=1 a a=2 a a=3 a a=4 a a=5 a a=6 for(int i=0; i<10; i++) srr[i]. set. A(i); for(int i=0; i<10; i++) cout << srr[i]. get. A() << endl; a a=7 a a=8 이 프로그램의 실행 결과는? a a=9 객체도 배열로 사용 가능 하다. class samp { int a; public: void set. A(int x) {a = x; } int get. A() {return a; } }; 객체배열 선언 void main() { int arr[10]; samp srr[10]; 객체 배열 사용 }
객체 배열 초기화 class samp { int a; public: samp(int x) {a= x; } int get. A() {return a; } }; samp srr[4] = { samp(-1), samp(2), samp(3), samp(4) }; void main() { 동일한 표현 samp srr[4] = {-1, 2, 3, 4}; for(int i=0; i<4; i++) cout << srr[i]. get. A() << endl; samp srr[4][2] = {-1, 2, 3, 4, 5, 6, 7, 8}; for(int i=0; i<4; i++) { cout << srr[i][0]. get. A() << ‘ ‘; cout << srr[i][1]. get. A() << endl; } 이차원 배열 선언 samp srr[4] = { samp(-1), samp(2), samp(3), samp(4), samp(5), samp(6), samp(7), samp(8) }; 이차원 배열 사용 } a = -1 a=2 a=3 a=4 이 프로그램의 실행 결과는?
객체 포인터 class samp { int a; public: void set. A(int x) {a = x; } int get. A() {return a; } }; 객체 포인터 선언 void main() { samp srr[10]; samp *p; p = &srr[0]; for(int i=0; i<10; i++) { p->set. A(i); p++; } p = srr; for(int i=0; i<10; i++) { cout << p->get. A() << endl; p++; } 객체 포인터에 값 치환 객체 포인터 사용 } 동일한 표현 for(int i=0; i<10; i++) { p[i]. get. A(i); } 동일한 표현 p = &srr[0];
this 포인터(이 객체의 주소) this 는 포인터이다. this는 멤버 메소드가 호출될 때 전달되는 묵시적 인자(매개변수, 즉 로칼 변수)이다. 비정적 (non static) 멤버 메소드에만 전달된다. this는 무엇에 대한 주소인가? 호출되는 멤버 함수가 소속된 객체의 주소이다. class samp { int a; public: samp(int x) {this->a = x; } void set. A(int x) {this->a = x; } int get. A() {return this->a; } }; void main() { samp ob(3); ob. set. A(5); ob. get. A(); } 컴파일 class samp { int a; public: samp(samp *this, int x) {this->a = x; } void set. A(samp *this, int x) {this->a = x; } int get. A(samp *this) {return this->a; } }; void main() { samp ob(3); ob. set. A(&ob, 5); ob. get. A(&ob); }
this는 로칼 변수, 자기 객체의 주소 ob 1 class samp { int a; public: samp(samp *this, int x) {this->a = x; } void set. A(samp *this, int x) {this->a = x; } int get. A(samp *this) {return this->a; } }; void main() { samp ob 1(3); samp ob 2(3); samp ob 3(3); ob 1. set. A(&ob 1, 5); ob 2. set. A(&ob 2, 6); ob 3. set. A(&ob 3, 7); } a. . . void set. A(samp* this, . . ) {this->a = x; }. . . ob 2 a. . . void set. A(samp* this, . . ) {this->a = x; }. . . ob 3 a. . . void set. A(samp* this, . . ) {this->a = x; }. . .
call by reference call by value(포인터 이용) call by reference void neg(int p) { p = -p; } void neg(int *p) { *p = -*p; } void neg(int &p) { p = -p; } void main() { int n = 5; neg(n); void main() { int n = 5; neg(&n); void main() { int n = 5; neg(n); cout << n; } } } 예상 출력 결과는? p 5 -5 n 5 p neg스택 main스택 n -5 p neg스택 5 main스택 neg스택 n -5 5 main스택 p는 n의 다른 이름
Homework #3 공연 예약 시스템 힌트 void main() { Seat. Type *p; p = new Seat. Type(); p[0]. set. Type(‘S’, 10); // S 타입 p[1]. set. Type(‘A’, 15); // A 타입 p[2]. set. Type(‘B’, 20); // B 타입 // 콘솔에서 사용자와 대화하고 필요한 // Seat. Type에 대해 필요한 기능을 호출. . . . . p[0]. reserve(); . . class Seat. Type { char type; Seat* p. Seat; public: Seat. Type(); ~Seat. Type(); void set. Type(char type, int num); bool reserve() { // 콘솔로부터 사용자에게서 이름과 번호를 알아내고 p. Seat[5]. reserve(이름); } bool cancel(); bool show(); }; class Seat { char *name; public: Seat(); ~Seat(); void cancel(); void reserve(char *); bool match(char *); }; delete [] p; } Seat. Type main() 함 수 Seat
Pre Quiz - samp클래스에 틀린 부분은? 이름 학번 #include <iostream> using namespace std; class samp { int a; public: samp(int a) {this->a = a} void set. A(int x) {a = x; } int get. A() {return a; } }; void main() { samp srr[10]; samp *p; /* 이곳에 실행 결과와 동일한 결과가 나도록 코딩하시요. */ } // 실행결과는 다음과 같다. 0 1 2. . . 9 class samp { int a; public: samp(int a) {this->a = a} void set. A(int x) {a = x; } int get. A() {return a; } }; p = &srr[0]; for(int i=0; i<10; i++) { p->set. A(i); p++; } p = srr; for(int i=0; i<10; i++) { cout << p->get. A() << endl; p++; }
디폴트 인자 사용시 유의 사항 디폴트 인자들은 정상 인자보다 오른쪽에서만 사용 void g(int a); void s(int a, int b=0); void p(int a, int b, int c=0); void r(int a, int b=0, int c=0); void f(int a=0; int b=0); void w(int a; int b=0, int c);
함수중복의 모호성(1) 함수 중복은 정상, 함수 호출의 모호성 float f(float x); //1 f(float x); double f(double d); //2 double f(double d); void f(unsigned char c); //1 f(unsigned c); void f(char c); //2 f(char c); void main() { { main() float a= 5. 0; double b= 5. 0; f(a); // 1 f(5. 0); //2 f(5. 0); f(b); //2 f(10); // 모호 } } void main() { { main() f('c'); //2 f(86); // 모호 } }
함수중복의 모호성(2) 함수 중복 자체의 모호성 void f(int x); //1 x); void f(int &x); //2 void f(int [] p); void f(int *p); // 두 함수는 동일한 것 void main() { { main() int x[10]; f(x); } } void main() { { main() int aint =a 5; = 5; f(a); // 모호 } } void f(int a); //1 a); void f(int a, inta, b=0); //2 int b=0); void main() { { main() f(3, 5); //2 f(3); // 모호 } }
함수의 주소, 중복함수의 주소 변수 fp를 선언하는 선언문 space() 함수의 선언문 void main() { void (*fp)(int); void space(int); void (*fp 2)(int, char); void space(int, char); space(10. '*'); space(10); fp에 space 함수의 주소 지정 space 함수 호출 변수 fp 2를 선언하는 선언문 space() 함수의 선언문 fp = space; fp(5); fp 2 = space; fp 2(10, '*'); fp = space; fp(5); } } void space(int count) { for(int i=0; i<count; i++) cout <<' '; } fp 2에 space(int, char) 함수의 주소 지정 space 함수 호출 void space(int count) { for(int i=0; i<count; i++) cout <<' '; } void space(int count, char c) { for(int i=0; i<count; i++) cout <<c; } 이 두 함수를 디폴트 인자를 가진 함수 하나로 구성할 수 있을까?
연산자 중복에 대한 질문 하나, friend 필요성 b=a+1; b=1+a; class Coord { int x, y; friend Coord operator +(int n, Coord a); }; Coord operator + (int n, Coord a) { Coord temp; temp. x = n + a. x; temp. y = n + a. y; return temp; } b=a. +(1); 변환 변환 수정 b=1. (a); b=+(1, a); class Coord { int x, y; }; Coord operator + (int n, Coord a) { Coord temp; temp. x = n + a. x; x, y는 private 멤버 temp. y = n + a. y; return temp; }
Pre Quiz - main()이 가능하도록 연산자 작 성 이름 학번 #include <iostream> using namespace std; class Array { int num[10] public: Array() {} Array(int n) { for(int i=0; i<10; i++) num[i] = n; ; } void show() { for(int i=0; i<10; i++) cout << num[i]; } }; void main() { Array a(1), b(2); b = 2 + a; b. show(); } // 실행결과는 다음과 같다. 4 4 4. . . 4 수정된 Array 클래스 혹은 기타 첨부된 부분을 이곳에 작성하시 오.
기본 클래스에 대한 접근 제어 Derived 1은 Base의 멤버들 상속받은 때 모두 원래 있는 속성을 그대로 물려받는다. class Derived 1 : public Base { int y; public: void set. Y(int y) {this->y = y; } void show() {cout << x << m<<y; } }; void main() { Derived 1 d; d. m = 6; d. x = 5; d. y = 10; d. set. X(10); d. set. Y(20); } Derived 2은 Base의 멤버들 상속받은 때 모두 private 속성으로 재 설정한다. class Derived 2 : private Base { int y; public: void set. Y(int y) {this->y = y; } void show() {cout << x <<m<< y; } }; void main() { Derived 2 d; d. m = 6; d. x = 5; d. y = 10; d. set. X(10); d. set. Y(20); } class Base { int x; protected: int m; public: void set. X(int x) {this->x = x; } void show() {cout << x; } }; Derived 3은 Base의 멤버들 상속받은 때 모두 protected 속성으로 재 설정한다. class Derived 3 : protected Base { int y; public: void set. Y(int y) {this->y = y; } void show() {cout << x <<m<< y; } }; void main() { Derived 3 d; d. m=6; d. x = 5; d. y = 10; d. set. X(10); d. set. Y(20); }
상속 관계의 클래스의 생성자 인자 처리 Base(3) Derived 1(3, 5) void main() { Derived 2 d 2(3, 5); } class Base { int x; public: Base(int x) { this->x = x; } }; class Derived 1 : public Base { int y; public: Derived 1 (int x, int y) : Base(x+5) { this->y = y; } }; class Derived 2 : public Derived 1{ int m; public: Derived 2(int x, int y) : Derived 1(x, y) { m = x*y; } };
생성자 호출의 디폴트 class Base { public: Base() { cout << "const"; } ~Base() {cout >> "dest"; } }; 실행1 class Derived 1 : public Base { public: 실행2 Derived 1 () : Base() { cout << "const 1"; } ~ Derived 1 () {cout >> "dest 1"; } }; void main() { Derived 2 d 2; } class Derived 2 : public Derived 1{ public: Derived 2() : Derived 1() { cout << "const 2"; } ~ Derived 2 () {cout >> "dest 2"; } }; 실행3
다중 상속(Multiple Inheritance) 화살표는 다중 상속에 따른 생성자 호출 관계를 보여줌 class Base 1 class Base 2 3 5 class Derived 1 : public Base 1 class Derived 2 : public Base 2 2 4 다중 상속문 예 class Derived 3 : public Derived 1, Derived 2 Base 2 Derived 2 멤버 Base 1 1 멤버 멤버 Derived 1 멤버 void main() { Derived 3 d; } Derived 3 멤버 객체 d main()에서 객체 d 내에 public 멤버들을 모두 접근할 수 있음
다중 상속의 문제점 class Base { public: int x; }; 3 Base의 멤버가 이중으로 객체에 삽입되는 문제점. 동일한 x를 접근하는 프로그램이 서로 다른 x에 접근하는 결과를 낳게되어 잘못된 실행 오류가 발생된다. 5 class Derived 1 : public Base class Derived 2 : public Base 2 다중 상속문 4 class Derived 3 : public Derived 1, Derived 2 Base 1 void main() { Derived 3 d; d. x = 10 ; } 멤버(int x) Derived 2 멤버 Base ? 멤버(int x) Derived 1 멤버 Derived 3 멤버 객체 d
다중 상속의 해결 방법 1: 범위지정연산자 사용 class Base { public: int x; }; 3 프로그램 내에서 x에 접근할 때마다 클래스 이름으로 범위를 지정하는 방법 사용 d. Derived: : x = 10 5 class Derived 1 : public Base class Derived 2 : public Base 2 다중 상속문 4 class Derived 3 : public Derived 1, Derived 2 Base 1 void main() { Derived 3 d; d. Derived 1: : x = 10 ; } 멤버(int x) Derived 2 멤버 Base 멤버(int x) Derived 1 멤버 Derived 3 멤버 객체 d
다중 상속의 해결 방법 2: 가상 기본 클래스 class Base { public: int x; }; 3 • 5 번 과정을 실행할 때 이미 Base의 객체들이 만들어 졌는지 확인하고 이미 만들어진 Base 객체를 상속받도록 하는 가상 상속을 이용 • 실제 사용 예 : ios, istream, ostream, iostream 5 class Derived 1 : virtual public Base class Derived 2 : virtual public Base 2 다중 상속문 4 class Derived 3 : public Derived 1, Derived 2 1 void main() { Derived 3 d; d. x = 10 ; } Derived 2 멤버 Base 멤버(int x) Derived 1 멤버 Derived 3 멤버 객체 d
- #include iostream using namespace std
- #include iostream using namespace std
- #include iostream #include string using namespace std
- #include iostream #include string using namespace std
- Include iostream using namespace std
- Include iostream using namespace std int main
- Include iostream using namespace std
- Que significa #include iostream en c++
- 표준체중
- #include iostream using namespace std
- #include iostream using namespace std
- Cout<<"hello";
- Fungsi include iostream
- Using namespace std
- Using namespace std
- Using namespace std c++
- #include iostream #include cmath
- #include stdio.h #include stdlib.h #include string.h
- #include stdio.h #include conio.h #include stdlib.h
- #include stdio.h #include stdlib.h int main()
- Include cstdio
- Std new line
- Cstdlib
- #include iostream.h void main()
- #include iostream
- Iostream h
- Int
- Include cstdio
- Include iostream h
- Std::set include
- String h
- H
- Split speech
- 66454 subject code
- Python namespace
- System.collections.generic namespace
- C#10 namespace
- Hierarchy of domain name system
- Namespace in c++
- Namespace in c++
- Namespace in c++
- Jaxb remove namespace from root element
- Identify the type of namespace: "challenger.ate.tbda.edu.".
- Root cern download
- Using system.collections
- Dtfd switch
- Iostream c++
- Iostream stdio
- Main program
- C++ cout.width
- The designer expresses the ideas in terms related to the
- Using line to create value
- Line patterns design
- Number line rounding
- Subtraction of integers using number line
- How to find the gcf of a negative and positive number
- New york pennsylvania new jersey delaware
- New oil and new wineskin
- Marquee theater new hartford ny
- Strengths of articles of confederation
- New-old approach to creating new ventures
- Marketing management
- Njbta
- New classical and new keynesian macroeconomics
- Chapter 16 toward a new heaven and a new earth
- Neil thisse is a loyalist answers
- New classical and new keynesian macroeconomics
- Wilson progressive accomplishments
- New mexico time line
- Html new line tag
- To start a new line in a cell press the ____ keys
- Ulysses poem analysis line by line
- Fair daffodils theme
- Thou blind man's mark analysis
- 14 lines sonnet
- Taj mahal simile
- Sonnet 60 shakespeare
- Unconventional love poem
- Sonnet 57 shakespeare
- Sonnet 130 title