+ All Categories
Home > Documents > 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better...

2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better...

Date post: 05-Sep-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
25
C++ 프로그래밍 입문 2장 나은 C로서의 C++ (1) 표준 입출력 네임스페이스 (고전 C++와 표준 C++) 함수 오버로딩 디폴트 매개변수 new와 delete bool 자료형 C++ is not C
Transcript
Page 1: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

C++ 프 그래밍 문

2 나 C C++ (1)

네 스페 스 (고전 C++ C++)

함수 버

폴트 매개변수

new delete

bool 료

C++ is not C

Page 2: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 1

1. 출

C++ : C 든 라 브러 포함

n printf, scanf 함수 사 가능

: int, double, 문 열 값 받고 출 하

#include <cstdio>using namespace std;

int main(void){

int iVar;double dVar;char str[20];

printf("int, double, 문 열 : ");scanf("%d %lf %s", &iVar, &dVar, str);

printf("int 값 : %d\n", iVar);printf("double 값: %f\n", dVar);printf("문 열 : %s\n", str);

return 0;}

주 전달

VC++ 6.0 경 삭제

cstdio : ??

à 2.2 네 스페 스참고

Page 3: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 2

#include <iostream> // cin, cout 출 포함

using namespace std; // 반드시 들어가야 함

int main(void)

{

int iVar = 55;

double dVar = 1.11;

char str[20] = "C++";

cout << iVar << endl; // 출 연산 << 사

cout << dVar << endl << str << endl;

return 0;

}

1. 출 : cin, cout 사

C++ 출 방법

n cin : 객체, >> 연산 함께 사

n cout : 출 객체, << 연산 함께 사

: cout

연 적 << 사 가능

상 타 에 신경쓰 !

endl à “\n”과 동 .

바 수행해 보라.

Page 4: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 3

#include <iostream>

using namespace std;

int main(void)

{

int iVar;

double dVar;

char str[20];

cout << "int, double, 문 열 값 : ";

cin >> iVar >> dVar; // 연산 >> 사

cin >> str;

cout << "int 값 : " << iVar << endl;

cout << "double 값: " << dVar << endl;

cout << "문 열 : " << str << endl;

return 0;

}

1. 출 : cin, cout 사

: cin, cout 사

연 적 >> 사 가능

상 타 에 신경쓰 !

문 열 : 공백문

단 à 11

Page 5: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 4

2. 네 스페 스

다 시나 문제점 ?

n 프 그래 A 프 그래 B가 만든 라 브러 사 하여 프

그램 하고 한다.

n 문제점 : (함수 , 변수 등) 충돌 가능

n 해결 방 : 네 스페 스( 공간)

sum 함수는

LibA sum 가,

LibB sum 가?

Page 6: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 5

namespace Samsung {int g_SVar;int Plus(int x, int y) {

return (x + y);}int Minus(int x, int y);

}

int Samsung::Minus(int x, int y){

return (x - y);}

int main(void){

Microsoft::g_MVar = Microsoft::Minus(5, 2);cout << Microsoft::g_MVar << endl;

return 0;}

2. 네 스페 스

네 스페 스 : 식별 들에 한 그룹

: Microsoft, Samsung 네 스페 스

#include <iostream>

using namespace std;

namespace Microsoft {

int g_MVar;

int Plus(int x, int y)

{

return (x + y);

}

int Minus(int x, int y)

{

return (x - y);

}

}

Microsoft 네 스페 스

Samsung 네 스페 스

다 Plus, Minus 존

함수 정 :: 사

네 스페 스 통해 별

Page 7: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 6

2. 네 스페 스

전역 네 스페 스

n 네 스페 스에 포함 는 역

n : 전역 네 스페 스에 Sum 함수가 는 경

Ø Sum(3, 4) 또는 ::Sum(3, 4)

특정 네 스페 스에 포함 는 식별 전역 네 스페 스(또

는 다 네 스페 스)에 는 것처럼 사 하는 방법

n using Microsoft::Minus;

n using Samsung::Plus;

특정 네 스페 스에 포함 는 든 식별 전역 네 스페

스에 는 것처럼 사 하는 방법

n using namespace Microsoft;

n using namespace Samsung;

단, 복 는 식별 경 여전히 해당 네 스페 스

여 함!

Page 8: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 7

2. 네 스페 스 : 고전 C++ C++

고전 C++ : 네 스페 스 개념

C++ : library 든 식별 à std 네 스페 스

#include <iostream.h> // 고전 C++ 헤 파

int main(void){

cout << "고전 C++" << endl;

return 0;}

VC++ 6.0 원함

VC++ 8.0 원하

#include <iostream>

using std::cout;using std::endl;

int main(void){

cout << " C++" << endl;// std::cout << " C++" << std::endl; // using 언 없 경

return 0;}

C++ 헤 파

VC++ 6.0, 8.0 원using namespace std;

든 식별 사 가능!

Page 9: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 8

2. 네 스페 스 : 고전 C++ C++

존 C 함수 사 ß 실제 C 라 브러 사 함

#include <stdio.h>

int main(void){

printf("C++ Programming");

return 0;}

다 과 같 체 가능

#include <cstdio>

using namespace std;

cstdio 파 다 과 같 stdio.h include하고

, stdio.h 내 든 식별

std 네 스페 스 포함시키고

원리

// cstdio 파#include <stdio.h>

namespace std {using ::printf;using ::scanf;using ::fread;using ::fwrite;using ::FILE:...... // 생략

}

using 란 특정 네 스페 스 식별

다 네 스페 스에 치시키는 것!

든 C 라 브러 들 같 방식

사 고

주 : VC++ 6.0 경 cstdio 파 에는 단순히

#include <stdio.h>만 술

라 , std에 포함 using … 사 가

전역에 는 printf

std 네 스페 스에

포함시킴

Page 10: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 9

#include <iostream>using namespace std;

namespace CompanyA { // 네 스페 스 CompanyA int g_VarA;void func1() { cout << "ComapanyA::func1" << endl; }void func2() { cout << "ComapanyA::func2" << endl; }

}

namespace CompanyB { // 네 스페 스 CompanyB using namespace CompanyA; // 네 스페 스 CompanyA를 CompanyB 포함int g_VarB;void func1() { cout << "ComapanyB::func1" << endl; }

namespace DeptC { // CompanyB 내에 네 스페 스 DeptC void func1() { cout << "CompanyB::DeptC::func1" << endl; }

}}

int main(void){

CompanyB::func1();CompanyB::func2(); // 실제 는 CompanyA func2 함수실행CompanyB::DeptC::func1();

return 0;}

2. 네 스페 스 : 첩 네 스페 스

: CompanyA 네 스페 스 내에 DeptC라는 네 스페 스

Page 11: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 10

3. 함수 버

: 다 프 그램 문제점 ?

#include <iostream>using namespace std;

void swap(int *x, int *y) // 실매개변수 2개 값{

int temp = *x;*x = *y;*y = temp;

}

int main(void){

int a = 3, b = 4;double c = 1.1, d = 2.2;

swap(&a, &b);swap(&c, &d);

cout << "a = " << a << ", b = " << b << endl;cout << "c = " << c << ", d = " << d << endl;

return 0;}

컴파 에러 : double * à int * 묵시적 변 가

swap((int *) &c, (int *) &d) 변경한다 ?

논 적 에러 : 수점 하 처 가

해결책 : int, double 별 swap 함수 !

Page 12: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 11

3. 함수 버

swap 함수 수정

void swapi(int *x, int *y) // int 값 2개{

int temp = *x;*x = *y;*y = temp;

}

void swapd(double *x, double *y) // double 값 2개{

double temp = *x;*x = *y;*y = temp;

}

문제점 : 출하는 곳에

swapi(&a, &b);

swapd(&c, &d);

변경

* 동 한 함수 사 가능

à함수함수 버버

void swap(int *x, int *y){

int temp = *x;*x = *y;*y = temp;

}

void swap(double *x, double *y){

double temp = *x;*x = *y;*y = temp;

}

Page 13: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 12

3. 함수 버

함수 버 조건

n 함수 또는 매개변수(개수 또는 타 )가 달라 함

n 반 무

Ø int func(int n); char func(int n); // 버 가능

함수 버 시 주

#include <iostream>using namespace std;

char square(char c) // char 값 제곱{

return (c * c);}

long square(long val) // long 값 제곱{

return (val * val);}

int main(void){

cout << square(3) << endl; // 3(int) char, long 다 변 가능

return 0;}

컴파 에러 발생

Page 14: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 13

4. 폴트 매개변수

: x y승 하는 power 함수 . 단, 번째 매개변수 y가 전

달 x 2승 반

#include <iostream>using namespace std;

int power(int x) // x 2승{

return (x * x);}

int power(int x, int y) // x y승{

int i;int result = 1;

for (i = 0; i < y; i++)result *= x;

return result;}

int main(void){

cout << power(3) << endl; // 3 2승cout << power(3, 3) << endl; // 3 3승

return 0;}

함수 버 통해 가능

폴트 매개변수 사 한다

하나 함수 끝!

int power(int x, int y = 2){

int i;int result = 1;

for (i = 0; i < y; i++)result *= x;

return result;}

Page 15: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 14

4. 폴트 매개변수

폴트 매개변수 복 한 사

n int power(int a, int b);

n int f(int x, int y = power(3, 4)); // 폴트 값 : 함수 출 가능

주 사항

n 폴트 매개변수 값 에 터 수

Ø , 함수 출 시 값 터 역순 생략 가능

Ø int f(int a, int b = 3, int c = 5); f(5); // O

Ø int g(int a, int b = 3, int c); g(1, , 3); // X

n 폴트 매개변수 값 함수 프 토타 또는 정 하나에만 술

à 반적 프 토타 에 술

Ø int f(int a = 3); int f(int a = 3) { return a; } // X

n 함수 버 과 함께 사 할 경 주

Ø int power(int x, int y = 2);

Ø int power(int x);

Ø power(3); // 다 출 가능, 함수?

Page 16: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 15

5. new delete

C

n malloc과 free 사 한 동적 할당 및 해제

n int *p1 = (int *) malloc(sizeof(int)); free(p1);

n int *p2 = (int *) malloc(sizeof(int) *3); free(p1);

C++

n malloc, free 사 가능

n new, delete 연산 사

Ømalloc, free 보다 편

Ø int *p1 = new int; delete p1;

Ø int *p2 = new int[3]; delete [] p2; // 배열 해제

n 할당과 동시에 초 가능

Ø int *p1 = new int(100); // 배열 경 초 가능

Page 17: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 16

5. new delete

: int 변수 하나 동적 할당 및 초

#include <iostream>

using namespace std;

int main(void)

{

int *p = new int(100); // int 변수 동적 할당 및 100 초기

cout << "포 터 변수 주 : " << &p << endl;

cout << "포 터 변수 값 : " << p << endl;

cout << "동적 변수 값 : " << *p << endl;

delete p;

return 0;

}

Page 18: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 17

#include <iostream>using namespace std;

int main(void){

int i;int *p = new int[5]; // int 변수 5개 동적 할당

for (i = 0; i < 5; i++)p[i] = i; // 포 터를 통해 배열처럼 사

for (i = 0; i < 5; i++)cout << p[i] << " ";

cout << endl;

delete [] p; // 배열 동적 할당 시 해제

return 0;}

5. new delete

: 배열 동적 할당

Page 19: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 18

5. new delete

: 2차원 배열 동적 할당

#include <iostream>using namespace std;

int main(void){

int **p;int i, j;p = new int *[4];

for (i = 0; i < 4; i++)p[i] = new int[5];

for (i = 0; i < 4; i++) {for (j = 0; j < 5; j++) {

p[i][j] = i + j;}

}

for (i = 0; i < 4; i++) {for (j = 0; j < 5; j++) {

cout << p[i][j] << "\t";}cout << endl;

}

for (i = 0; i < 4; i++)delete [] p[i];

delete [] p;

return 0;}

int 포 터에 한 포 터

p[0], …, p[3] int 포 터

각 포 터에 해

int 5개 배열 생

2차원 배열처럼사동적 해제

Page 20: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 19

6. bool 료

C 스타 bool 료

n typedef 통해 참, 거짓 시하는 타 , 내 적 int

n typedef int BOOL;

C++ : bool

n 값 : true, false

n 수식 연산에 사 가능

Ø true à 1, false à 0

n 다 타 값 bool 동 변 가능

Ø 0 à false, 그 à true

ü bool tf1 = true;

ü int a = 1 – tf1 // a는 0

ü bool tf2 = a; // tf2는 false가

Page 21: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 20

#include <iostream>using namespace std;

typedef int BOOL;

int main(void){

BOOL bVar1 = 3;bool bVar2 = 3;

cout << "bVar1 : " << bVar1 << endl;cout << "bVar2 : " << bVar2 << endl;

int a = bVar1 + 2; // BOOL int 취급int b = bVar2 + 2; // bool true, false 취급

cout << "a : " << a << endl;cout << "b : " << b << endl;

return 0;}

6. bool 료

: C, C++ 비

C 스타

Page 22: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 21

7. C++ is not C

C C++ 사 차 ( 가 )

1. void 포 터 다 타 포 터 사 동 변

2. 역 변수 치

3. 함수 프 토타 에 매개변수가 는 경 해

4. 조체 변수 방법

1. void 포 터ßà 다 타 포 터 동 변

#include <iostream>using namespace std;

int main(void){

int *p = malloc(sizeof(int) * 3); // (void *) 반 , C++ 동 변 X

free(p);

return 0;}

int *p = (int *) malloc(sizeof(int) * 3);

C에 는가능 : 동 변 O

Page 23: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 22

7. C++ is not C

2. 역 변수 치

n C : 다 문 나 전

n C++ : 간에 가능

#include <iostream>using namespace std;

int main(void){

int i = 5;int Sum;Sum = 0;

for (int i = 1; i <= 100; i++) { // i는 for 블 내에 만 사 가능Sum += i;

}

cout << "i : " << i << endl;cout << "합계: " << Sum << endl;

return 0;}

C++ : for문에 가능, 개 i는 다 변수

주 : VC++ 6.0에 는 for문에

한 변수 그 에 계 사

가능à컴파 에러 발생

à 개 i 같 변수

à C++ 개념에 맞

Page 24: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 23

7. C++ is not C

3. 함수 매개변수가 는 경 해

n C : 해당 함수 출 시 실매개변수 개수, 타 검사하

à 실매개변수에 계 출 가능 (실매개변수 무시)

n C++ : void 간주

Ø void f(); // void f(void); 동

#include <stdio.h>

void f() { // 매개변수를 정하 않 경printf("test\n");

}

int main(void){

f(3.5);f(3);f('a');f(1, 2);

return 0;}

C에 는수행 가능

C++에 는 수행 가능!

f 함수 출 시 실매개변수가 함

Page 25: 2장더나은C로서의C++ (1) - contents.kocw.netcontents.kocw.net/document/CPP02_CPP as a Better C-1.pdf · 2장더나은C로서의C++ (1) 1 1. 표준입출력 C++ : C의모든라이브러리를포함

2 나 C C++ (1) 24

7. C++ is not C

4. 조체 변수 방법

n C : struct Point P1; // struct 반드시 동반

n C++ : struct Point P1; Point P1; // struct 가능

※ C, C++ 끊 : 점 참고

#include <iostream>using namespace std;

struct Point {int x, y;

};

int main(void){

Point P1 = { 1, 2 }; // struct를 포함하 않고 변수 언

return 0;}

C에 는에러 발생


Recommended