멋진 Plug in

Read more »

Prototype 1(Arduino, Computer)

Arduino

Senosr.ino
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
void setup() {
Serial.begin(2000000);
}

void loop() {
int x_val = analogRead(A0);
int y_val = analogRead(A1);
int z_val = analogRead(A2);

int AE = analogRead(A7);

if(AE >= 500){
AE = 1;
}
else{
AE = 0;
}

Serial.print(x_val);
Serial.print(",");
Serial.print(y_val);
Serial.print(",");
Serial.print(z_val);
Serial.print(",");
Serial.print(AE);
Serial.print("\n");

delay(20);
}
Read more »

제어문

조건문

if-elseif-else

1
2
3
4
5
6
7
8
9
10
if 조건1
실행 문장
elseif 조건2
실행 문장
elseif 조건3
실행 문장
...
else
실행 문장
end

switch

1
2
3
4
5
6
7
8
9
switch(변수)
case(값1)
실행 문장
case(값2)
실행 문장
...
otherwise
실행 문장
end
Read more »

시작

Start - Part Design - Enable hybrid design 체크 박스 해제

  • 확대
    • 가운데 누른 상태 - 왼쪽 클릭 - 위아래 이동

곡선의 정확도

Tools - Option - Display - Performance - 3D Accuracy - 0.01

축 설정

Infrastructure - Part Infrastructure - Part Document - Create an axis system 체크 - Hybrid Design 체크 박스 해제

Read more »

Day 12

Constructor of Inheritance

constructor.cpp

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 <iostream>
using namespace std;

class TV{
int size;
public:
TV(){size = 20;}
TV(int size):size(size){}
int getSize(){return size;}
};

class WideTV : public TV{
bool videoIn;
public:
WideTV(int size, bool videoIn) : TV(size){
this->videoIn = videoIn;
}
bool getVideoIn(){return videoIn;}
};

class SmartTV : public WideTV{
string ipAddr;
public:
SmartTV(string ipAddr, int size) : WideTV(size, true){
this->ipAddr = ipAddr;
}
string getipAddr(){return ipAddr;}
};

int main(){
SmartTV htv("192.0.0.1", 32);
cout << "size = " << htv.getSize() << endl;
cout << "videoIn = " << htv.getVideoIn() << endl;
cout << "IP = " << htv.getipAddr() << endl;

return 0;
}

Output

1
2
3
4
5
size = 32
videoIn = 1
IP = 192.0.0.1

Process finished with exit code 0

Virtual Function & Overriding

  • Virtual function
    • virtual 키워드로 선언된 멤버 함수
    • 동적 바인딩 지시어
    • 컴파일러에게 함수에 대한 호출 바인딩을 실행 시간까지 미루도록 지시
  • Function overriding
    • 파생 클래스에서 기본 클래스의 가상 함수와 동일한 이름의 함수 선언
    • 기본 클래스 : 가상 함수의 존재감 상실
    • 파생 클래스 : 오버라이딩한 함수가 호출되도록 동적 바인딩
    • 함수 재정의라고도 부름
    • 다형성의 한 종류
  • 조건
    • virtual으로 함수 선언(파생 클래스는 생략 가능)
    • upcasting
    • 함수 동일

overriding.cpp

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
#include <iostream>
using namespace std;

class Base{
public:
virtual void f(){cout << "Base" << endl;}
};

class Derived : public Base{
public:
void f(){cout << "Derived" << endl;}
};

class GrandDerived : public Derived{
public:
void f(){cout << "GrandDerived" << endl;}
};

int main(){
Base *bp = new GrandDerived;
bp->f();
Derived *dp = new GrandDerived;
dp->f();

return 0;
}

Output

1
2
3
4
GrandDerived
GrandDerived

Process finished with exit code 0

destructor1.cpp

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
#include <iostream>
using namespace std;

class Base{
public:
~Base(){cout << "~Base" << endl;}
};

class Derived : public Base{
public:
~Derived(){cout << "~Derived" << endl;}
};

class GrandDerived : public Derived{
public:
~GrandDerived(){cout << "~GrandDerived" << endl;}
};

int main(){
Base *bp = new GrandDerived;
Derived *dp = new GrandDerived;
GrandDerived *gp = new GrandDerived;

delete bp;
delete dp;
delete gp;

return 0;
}

Output

1
2
3
4
5
6
7
8
~Base
~Derived
~Base
~GrandDerived
~Derived
~Base

Process finished with exit code 0

destructor2.cpp

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
#include <iostream>
using namespace std;

class Base{
public:
virtual ~Base(){cout << "~Base" << endl;}
};

class Derived : public Base{
public:
~Derived(){cout << "~Derived" << endl;}
};

class GrandDerived : public Derived{
public:
~GrandDerived(){cout << "~GrandDerived" << endl;}
};

int main(){
Base *bp = new GrandDerived;
Derived *dp = new GrandDerived;
GrandDerived *gp = new GrandDerived;

delete bp;
delete dp;
delete gp;

return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
11
~GrandDerived
~Derived
~Base
~GrandDerived
~Derived
~Base
~GrandDerived
~Derived
~Base

Process finished with exit code 0

Overloading vs. Overrding

  • Overloading
    • 이름만 같은 함수 중복 작성
    • 하나의 클래스
  • Overriding
    • 모든 것이 완벽히 같은 함수 재작성
    • 상속

Quiz

quiz.cpp

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
#include <iostream>
using namespace std;

class BaseArray{
int capacity;
int *mem;
public:
BaseArray(int capacity = 100):capacity(capacity){mem = new int[capacity];}
~BaseArray(){delete [] mem;}
void put(int index, int val);
int get(int index);
int getCapacity(){return capacity;}
};

class MyStack : public BaseArray{
int tos;
public:
MyStack(int capacity) : BaseArray(capacity){tos = 0;}
void push(int n);
int pop();
int capacity(){return getCapacity();}
int length(){return tos;}
};

int main(){
MyStack mStack(100);
int n;
cout << "스택에 삽입할 5개의 정수를 입력하라>> ";
for(int i = 0; i < 5; i++){
cin >> n;
mStack.push(n);
}
cout << "스택 용량:" << mStack.capacity() << ", 스택 크기:" << mStack.length() << endl;
cout << "스택의 모든 원소를 팝하여 출력한다>> ";
while(mStack.length() != 0){
cout << mStack.pop() << ' ';
}
cout << endl << "스택의 현재 크기 : " << mStack.length() << endl;
}

void BaseArray::put(int index, int val){
mem[index] = val;
}
int BaseArray::get(int index){
return mem[index];
}

void MyStack::push(int n){
put(tos, n);
tos++;
}
int MyStack::pop(){
tos--;
return get(tos);
}

Output

1
2
3
4
5
6
스택에 삽입할 5개의 정수를 입력하라>> 34 52 41 12 78
스택 용량:100, 스택 크기:5
스택의 모든 원소를 팝하여 출력한다>> 78 12 41 52 34
스택의 현재 크기 : 0

Process finished with exit code 0

Day 13

Interface

  • 인터페이스만 선언하고 구현을 분리하여 작업자마다 다양한 구현 가능
  • 사용자는 구현의 내용을 모르지만 인터페이스에 선언된 순수 가상 함수가 구현되어있기 때문에 호출하여 사용하기만 하면 됨

shape.cpp

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 <iostream>
using namespace std;

class Shape{
Shape *next;
protected:
virtual void draw() = 0; // 순수 가상 함수
public:
Shape(){next = NULL;}
virtual ~Shape(){}
void paint(){draw();}
Shape *add(Shape *p){this->next = p; return p;};
Shape *getNext(){return next;}
};

class Circle : public Shape{
protected:
void draw(){cout << "Circle" << endl;}
~Circle(){cout << "del Circle" << endl;}
};

class Rect : public Shape{
protected:
void draw(){cout << "Rect" << endl;}
~Rect(){cout << "del Rect" << endl;}
};

int main(){
Shape *pStart = NULL;
Shape *pLast;
pStart = new Circle();
pLast = pStart;
pLast = pLast->add(new Rect());
pLast = pLast->add(new Rect());
pLast = pLast->add(new Circle());
pLast = pLast->add(new Rect());
Shape *p = pStart;
while(p != NULL){
p->paint();
p = p->getNext();
}
p = pStart;
while(p != NULL){
Shape *q = p->getNext();
delete p;
p = q;
}

return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
11
12
Circle
Rect
Rect
Circle
Rect
del Circle
del Rect
del Rect
del Circle
del Rect

Process finished with exit code 0

Abstract Class

  • 최소한 하나의 순수 가상 함수를 가진 클래스
  • 온전한 클래스가 아니므로 객체 생성 불가능
  • 추상 클래스의 포인터는 선언 가능
  • 순수 가상 함수를 통해 파생 클래스에서 구현할 함수의 형태(원형)을 보여주는 인터페이스 역할

calculator.cpp

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 <iostream>
using namespace std;

class Calculator{
void input(){
cout << "정수 2개를 입력하세요>>";
cin >> a >> b;
}
protected:
int a, b;
virtual int calc(int a, int b) = 0;
public:
void run(){
input();
cout << "계산된 값은 " << calc(a, b) << endl;
}
};

class Adder : public Calculator{
protected:
int calc(int a, int b){return a + b;}
};

class Subtract : public Calculator{
protected:
int calc(int a, int b){return a - b;}
};

int main(){
Calculator *c;
c = new Adder;
c->run();
c = new Subtract;
c->run();
delete c;

return 0;
}

Output

1
2
3
4
5
6
정수 2개를 입력하세요>>4 3
계산된 값은 7
정수 2개를 입력하세요>>4 3
계산된 값은 1

Process finished with exit code 0

Generalization of Function

  • Generic 혹은 일반화
    • 함수나 클래스를 일반화시키고, 매개변수 타입을 지정하여 틀에서 찍어내듯이 함수나 클래스 코드를 생산하는 기법
  • Template
    • 함수나 클래스를 일반화하는 C++ 도구
    • template 키워드로 함수나 클래스 선언
    • Generic type - 일반화를 위한 Data type

sum.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

template<class T>
T Sum(T a, T b){
return a + b;
}

int main(){
cout << Sum(1, 2) << endl;
cout << Sum(1.1, 2.2) << endl;
cout << Sum('1', '2') << endl;

return 0;
}

Output

1
2
3
4
5
3
3.3
c

Process finished with exit code 0

search.cpp

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
#include <iostream>
using namespace std;

template<class T>
bool search(T one, T arr[], int size){
for(int i = 0; i < size; i++){
if(arr[i] == one)
return true;
}
return false;
}

int main(){
int x[] = {1, 10, 100, 5, 4};
if(search(100, x, sizeof(x) / 4))
cout << "100이 배열 x에 포함되어 있다.";
else
cout << "100이 배열 x에 포함되어 있지 않다.";
cout << endl;

char c[] = {'h', 'e', 'l', 'l', 'o'};
if(search('e', c, 5))
cout << "e가 배열 x에 포함되어 있다.";
else
cout << "e가 배열 x에 포함되어 있지 않다.";
cout << endl;

return 0;
}

Output

1
2
3
4
100이 배열 x에 포함되어 있다.
e가 배열 x에 포함되어 있다.

Process finished with exit code 0

Generalization of Class

  • 선언 : template<class T>
    • class의 정의 앞에 선언
    • 선언부, 구현부 - 멤버 함수 앞 선언
    • T class<T>::function(T param);
  • 일반화할 변수만 T로 선언

generic.cpp

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 <iostream>
using namespace std;

class Point{
int x, y;
public:
Point(int x = 0, int y = 0):x(x), y(y){}
void show(){cout << '(' << x << ',' << y << ')' << endl;}
};

template<class T>
class MyStack{
int tos;
T data[100];
public:
MyStack();
void push(T element);
T pop();
};

int main(){
MyStack<int *> ipStack;
int *p = new int[3];
for(int i = 0; i < 3; i++)
p[i] = i * 10;
ipStack.push(p);
int *q = ipStack.pop();
for(int i = 0; i < 3; i++)
cout << q[i] << '\t';
cout << endl;
delete [] p;

MyStack<Point> pointStack;
Point a(2, 3), b;
pointStack.push(a);
b = pointStack.pop();
b.show();

MyStack<Point *> pStack;
pStack.push(new Point(10, 20));
Point *pPoint = pStack.pop();
pPoint->show();

MyStack<string> stringStack;
string s = "C++";
stringStack.push(s);
stringStack.push("Zerohertz");
cout << stringStack.pop() << '\t';
cout << stringStack.pop() << endl;

return 0;
}

template<class T>
MyStack<T>::MyStack(){
tos = -1;
}
template<class T>
void MyStack<T>::push(T element){
if(tos == 99){
cout << "Stack full" << endl;
return;
}
tos++;
data[tos] = element;
}
template<class T>
T MyStack<T>::pop(){
T Data;
if(tos == -1){
cout << "Stack empty" << endl;
return 0;
}
Data = data[tos--];
return Data;
}

Output

1
2
3
4
5
6
0	10	20	
(2,3)
(10,20)
Zerohertz C++

Process finished with exit code 0

gclass.cpp

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
#include <iostream>
using namespace std;

template<class T1, class T2>
class GClass{
T1 data1;
T2 data2;
public:
GClass(){data1 = 0; data2 = 0;};
void set(T1 a, T2 b){
data1 = a; data2 = b;
}
void get(T1 &a, T2 &b){
a = data1; b = data2;
}
};

int main(){
int a;
double b;
GClass<int, double> x;
x.set(2, 0.5);
x.get(a, b);
cout << "a = " << a << "\tb = " << b << endl;

char c;
float d;
GClass<char, float> y;
y.set('m', 12.5);
y.get(c, d);
cout << "c = " << c << "\td = " << d << endl;

return 0;
}

Output

1
2
3
4
a = 2	b = 0.5
c = m d = 12.5

Process finished with exit code 0

Quiz

quiz.cpp

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 <iostream>
#include <string>
using namespace std;

class Shape{
protected:
string name;
int width, height;
public:
Shape(string n = "", int w = 0, int h = 0){name = n; width = w; height = h;}
virtual double getArea(){return 0;}
string getName(){return name;}
};

class Oval : public Shape{
public:
Oval(string n = "", int w = 0, int h = 0):Shape(n, w, h){}
double getArea(){return 3.14 * width * height;}
};

class Rect : public Shape{
public:
Rect(string n = "", int w = 0, int h = 0):Shape(n, w, h){}
double getArea(){return width * height;}
};

class Triangular : public Shape{
public:
Triangular(string n = "", int w = 0, int h = 0):Shape(n, w, h){}
double getArea(){return width * height / 2;}
};

int main(){
Shape *p[3];
p[0] = new Oval("빈대떡", 10, 20);
p[1] = new Rect("찰떡", 30, 40);
p[2] = new Triangular("토스트", 30, 40);
for(int i = 0; i < 3; i++)
cout << p[i]->getName() << " 넓이는 " << p[i]->getArea() << endl;
for(int i = 0; i < 3; i++) delete p[i];

return 0;
}

Output

1
2
3
4
5
빈대떡 넓이는 628
찰떡 넓이는 1200
토스트 넓이는 600

Process finished with exit code 0

Day 14

STL

  • STL(Standard Template Library)
    • 표준 템플릿 라이브러리
    • 많은 제네릭 클래스와 제네릭 함수 포함
  • STL의 구성
    • 컨테이너 : 템플릿 클래스
      • 데이터를 담아두는 자료 구조를 표현한 클래스
      • 리스트, 큐, 스택, 맵, 셋, 벡터
    • iterator : 컨테이너 원소에 대한 포인터
      • 컨테이너의 원소들을 순회하면서 접근하기 위해 만들어진 컨테이너 원소에 대한 포인터
    • 알고리즘 : 템플릿 함수
      • 컨테이너 원소에 대한 복사, 검색, 삭제, 정렬 등의 기능을 구현한 템플릿 함수
      • 컨테이너의 멤버 함수 아님

STL 컨테이너의 종류

컨테이너 클래스 설명 헤더 파일
vector 동적 크기의 배열을 일반화한 클래스 <vector>
deque 앞뒤 모두 입력 가능한 큐 클래스 <deque>
list 빠른 삽입/삭제 가능한 리스트 클래스 <list>
set 정렬된 순서로 값을 저장하는 집합 클래스, 값은 유일 <set>
map (key, value)쌍으로 값을 저장하는 맵 클래스 <map>
stack 스택을 일반화한 클래스 <stack>
queue 큐를 일반화한 클래스 <queue>

STL iterator의 종류

iterator의 종류 iterator에 ++ 연산 후 방향 read/write
iterator 다음 원소로 전진 read/write
const_iterator 다음 원소로 전진 read
reverse_iterator 지난 원소로 후진 read/write
const_reverse_iterator 지난 원소로 후진 read

STL 알고리즘 함수들

  • copy
  • merge
  • random
  • rotate
  • equal
  • min
  • remove
  • search
  • find
  • move
  • replace
  • sort
  • max
  • partition
  • reverse
  • swap

Vector

  • 가변 길이 배열을 구현한 Generic 클래스
  • 원소의 저장, 삭제, 검색 등 다양한 멤버 함수 지원
  • 벡터에 저장된 원소는 인덱스로 접근 가능

vector.cpp

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
#include <iostream>
#include <vector>
using namespace std;

int main(){
vector<int> v;

v.push_back(1);
v.push_back(2);
v.push_back(3);

for(int i = 0; i < v.size(); i++)
cout << v[i] << '\t';
cout << endl;

v[0] = 10;
int n = v[2];
v.at(2) = 5;

for(int i = 0; i < v.size(); i++)
cout << v[i] << '\t';
cout << endl;

return 0;
}

Output

1
2
3
4
1	2	3	
10 2 5

Process finished with exit code 0

Iterator

  • 반복자라고도 부름
  • *, ++ 연산자 사용 가능
  • 컨테이너의 원소를 가리키는 포인터

iterator.cpp

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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
vector<int> v;
int n;

cout << "5개의 정수를 입력하시오." << endl;
for(int i = 0; i < 5; i++){
cin >> n;
v.push_back(n);
}

sort(v.begin(), v.end()); // sort(v.begin() + a, v.begin() + b) -> a에서 b - 1까지

vector<int>::iterator it;

for(it = v.begin(); it != v.end(); it++)
cout << *it << '\t';
cout << endl;

return 0;
}

Output

1
2
3
4
5
5개의 정수를 입력하시오.
30 -7 250 6 120
-7 6 30 120 250

Process finished with exit code 0

Algorithm

  • 탐색(find) : 컨테이너 안에서 특정한 자료를 찾음
  • 정렬(sort) : 자료들을 크기 순으로 정렬
    • param1 : 정렬을 시작한 원소의 주소
    • param2 : 소팅 범위의 마지막 원소 다음 주소
  • 반전(reverse) : 자료들의 순서 역순
  • 삭제(remove) : 조건이 만족되는 자료 삭제
  • 변환(transform) : 컨테이너 요소들을 사용자가 제공하는 변환 함수에 따라 변환

algorithm.cpp

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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Circle{
string name;
int radius;
public:
Circle(int radius = 1, string name = ""):radius(radius), name(name){}
double getArea(){return 3.14 * radius * radius;}
string getName(){return name;}
bool operator<(Circle b);
friend ostream &operator<<(ostream &os, vector<Circle> &b);
};

void printVector(vector<Circle> vec);

int main(){
vector<Circle> v;
v.push_back(Circle(2, "waffle"));
v.push_back(Circle(3, "pizza"));
v.push_back(Circle(1, "donut"));
v.push_back(Circle(5, "pizzaLarge"));
printVector(v);
// int it = v.size() - 1;
sort(v.begin(), v.end()); // sort(&v[0], &v[it]);
printVector(v);
cout << endl << "프렌드함수 operator<<로 출력하는 경우" << endl;
cout << v << endl;

return 0;
}

bool Circle::operator<(Circle b){
if(this->radius < b.radius)
return true;
else
return false;
}

ostream &operator<<(ostream &os, vector<Circle> &b){
vector<Circle>::iterator it;
os << "모든 원소를 출력한다.>>";
for(it = b.begin(); it != b.end(); it++)
os << it->getName() << '\t';
os << endl;
return os;
}

void printVector(vector<Circle> vec){
cout << "모든 원소를 출력한다.>>";
for(auto it = vec.begin(); it != vec.end(); it++) // auto는 자동형변환
cout << it->getName() << '\t';
cout << endl;
}

Output

1
2
3
4
5
모든 원소를 출력한다.>>waffle   pizza   donut   pizzaLarge
모든 원소를 출력한다.>>donut waffle pizza pizzaLarge

프렌드함수 operator<<로 출력하는 경우
모든 원소를 출력한다.>>donut waffle pizza pizzaLarge

Day 9

Polymorphism(다형성)

  • Overloading
    • 함수 중복
    • 연산자 중복
    • Default Parameter
  • Overriding
    • 함수 재정의

Function Overloading

  • 다른 함수로 인식
  • 함수의 이름 동일
  • 함수의 매개변수 type, 개수 다름
  • return type 무관
  • 소멸자 불가 - 매개변수 X
  • 모호하지 않게 선언
Read more »

Day 5

Object

oval.cpp

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
#include <iostream>
using namespace std;

class Oval{
int width, height;
double getArea();
public:
Oval();
Oval(int w, int h);
~Oval();
int getWidth();
int getHeight();
void set(int w, int h);
void show();
};

int main(){
Oval a, b(3, 4);
a.set(10, 20);
a.show();
b.show();

return 0;
}

Oval::Oval(){
width = 1;
height = 1;
}

Oval::Oval(int w,int h){
width = w;
height = h;
}

Oval::~Oval(){
cout << "Oval 소멸 ";
show();
}

double Oval::getArea(){
return 3.14*width*height;
}

int Oval::getWidth(){
return width;
}

int Oval::getHeight(){
return height;
}

void Oval::set(int w, int h){
width = w;
height = h;
}

void Oval::show(){
cout << "width = " << width << ", height = " << height << ", Area = " << getArea() << endl;
}

Output

1
2
3
4
5
6
width = 10, height = 20, Area = 628
width = 3, height = 4, Area = 37.68
Oval 소멸 width = 3, height = 4, Area = 37.68
Oval 소멸 width = 10, height = 20, Area = 628

Process finished with exit code 0

Object Pointer

  • 객체에 대한 포인터
    • 객체의 주소 값을 가지는 변수
  • 포인터로 멤버를 접근할 때
    • 객체포인터->멤버

circle1.cpp

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 <iostream>
using namespace std;

class Circle{
int radius;
public:
Circle(){radius = 1;}
Circle(int r){radius = r;}
double getArea();
};

int main(){
Circle donut;
double d = donut.getArea();

Circle *p;
p = &donut;
double b = p->getArea();

cout << d << "==" << b << endl;

return 0;
}

double Circle::getArea(){
return radius*radius*3.14;
}

Output

1
2
3
3.14==3.14

Process finished with exit code 0

circle2.cpp

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
#include <iostream>
using namespace std;

class Circle{
int radius;
public:
Circle(){radius = 1;}
Circle(int r){radius = r;}
double getArea();
};

int main(){
Circle donut;
Circle pizza(30);

cout << donut.getArea() << endl;

Circle *p;
p = &donut;
cout << p->getArea() << endl;
cout << (*p).getArea() << endl;

p = & pizza;
cout << p->getArea() << endl;
cout << (*p).getArea() << endl;

return 0;
}

double Circle::getArea(){
return radius*radius*3.14;
}

Output

1
2
3
4
5
6
7
3.14
3.14
3.14
2826
2826

Process finished with exit code 0

Object Array

  • 객체 배열 선언 가능
    • 기본 타이 배열 선언과 형식 동일
      • Circle c[3];
  • 객체 배열 선언
    • 객체 배열을 위한 공간 할당
    • 배열의 각 원소 객체마다 생성자 실행
      • 매개변수 없는 생성자 호출
      • 매개변수 있는 생성자를 한번에는 호출할 수 없음
    • Circle c[3] = {Circle(10), Circle(20), Circle()};
  • 배열 소멸
    • 배열의 각 객체마다 소멸자 호출
    • 생성의 반대순으로 소멸

circle1.cpp

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
#include <iostream>
using namespace std;

class Circle{
int radius;
public:
Circle(){radius = 1;}
Circle(int r){radius = r;}
double getArea();
void setRadius(int r){radius = r;}
};

int main(){
Circle circleArray[3];
circleArray[0].setRadius(10);
circleArray[1].setRadius(20);
circleArray[2].setRadius(30);

for(int i = 0; i < 3; i++){
cout << "Circle " << i << "의 면적은 " << circleArray[i].getArea() << endl;
}

Circle *p;
p = circleArray;
for(int i = 0; i < 3; i++){
cout << "Circle " << i << "의 면적은 " << p->getArea() << endl;
p++;
}

return 0;
}

double Circle::getArea(){
return radius*radius*3.14;
}

Output

1
2
3
4
5
6
7
8
Circle 0의 면적은 314
Circle 1의 면적은 1256
Circle 2의 면적은 2826
Circle 0의 면적은 314
Circle 1의 면적은 1256
Circle 2의 면적은 2826

Process finished with exit code 0

circle2.cpp

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 <iostream>
using namespace std;

class Circle{
int radius;
public:
Circle(){radius = 1;}
Circle(int r){radius = r;}
void setRadius(int r){radius = r;}
double getArea(){return 3.14*radius*radius;}
};

int main(){
Circle c[3] = {Circle(10), Circle(20), Circle()};
Circle *p = c;

for(int i = 0; i < 3; i++){
cout << "c[" << i << "]의 면적은 " << c[i].getArea() << endl;
}

for(int i = 0; i < 3; i++){
cout << "(c+" << i << ")의 면적은 " << (c+i)->getArea() << endl;
}

for(int i = 0; i < 3; i++){
cout << "*(c+" << i << ")의 면적은 " << (*(c+i)).getArea() << endl;
}

for(int i = 0; i < 3; i++){
cout << "p[" << i << "]의 면적은 " << p[i].getArea() << endl;
}

for(int i = 0; i < 3; i++){
cout << "(p+" << i << ")의 면적은 " << (p+i)->getArea() << endl;
}

for(int i = 0; i < 3; i++){
cout << "*(p+" << i << ")의 면적은 " << (*(p+i)).getArea() << endl;
}

for(int i = 0; i < 3; i++){
cout << "p->" << i << "의 면적은 " << p->getArea() <<endl;
p++;
}

return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
c[0]의 면적은 314
c[1]의 면적은 1256
c[2]의 면적은 3.14
(c+0)의 면적은 314
(c+1)의 면적은 1256
(c+2)의 면적은 3.14
*(c+0)의 면적은 314
*(c+1)의 면적은 1256
*(c+2)의 면적은 3.14
p[0]의 면적은 314
p[1]의 면적은 1256
p[2]의 면적은 3.14
(p+0)의 면적은 314
(p+1)의 면적은 1256
(p+2)의 면적은 3.14
*(p+0)의 면적은 314
*(p+1)의 면적은 1256
*(p+2)의 면적은 3.14
p->0의 면적은 314
p->1의 면적은 1256
p->2의 면적은 3.14

Process finished with exit code 0

Dynamic Memory

1
2
3
4
5
6
7
8
9
10
11
12
13
데이터타입 *포인터변수 = new 데이터타입;
데이터타입 *포인터변수 = new 데이터타입(초기값); // 배열의 초기화는 for문
delete 포인터변수;

데이터타입 *포인터변수 = new 데이터타입[배열의크기]; // 배열의 동적 할당
for(int i = 0; i < 배열의크기; i++){
포인터변수[i] = 값;
}
delete [] 포인터변수;

클래스이름 *포인터변수 = new 클래스이름; // 객체의 동적 할당
클래스이름 *포인터변수 = new 클래스이름(생성자매개변수리스트);
delete 포인터변수;
  • 정적 할당
    • 변수 선언을 통해 필요한 메모리 할당
    • 많은 양의 메모리는 배열 선언을 통해 할당
  • 동적 할당
    • 필요한 양이 예측되지 않는 경우, 프로그램 작성 시 할당 받을 수 없음
    • 실행 중에 운영체제로부터 할당 받음
      • 힙(heap)으로부터 할당
      • 힙은 운영체제가 소유하고 관리하는 메모리, 모든 프로세스가 공유할 수 있는 메모리
  • C의 동적 메모리 할당
    • malloc()
    • free()
  • C++의 동적 메모리 할당, 반환
    • new 연산자
      • 기본 타입 메모리 할당, 배열 할당, 객체 할당, 객체 배열 할당
      • 객체의 동적 생성 - 힙 메모리로부터 객체를 위한 메모리 할당 요청
      • 객체 할당 시 생성자 호출
    • delete
      • new로 할당 받은 메모리 반환
      • 객체의 동적 소멸 - 소멸자 호출 뒤 객체를 힙에 반환
Data Type 정적 할당 동적 할당
Integer int a = val; int *pa = &a; int *p = new int(val);
Array int arr[num]; int *parr = new int[100];
Object Class instance(val); Class *pc = &instance; Class *pc = new Class;

circle.cpp

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 <iostream>
using namespace std;

class Circle{
int radius;
public:
Circle(){radius = 1; cout << "생성자 실행" << endl;}
Circle(int r){radius = r; cout << "생성자 실행" << endl;}
~Circle(){cout << "소멸자 실행" << endl;}
double getArea(){return 3.14*radius*radius;}
};

int main(){
int radius;
while(true){
cout << "정수 반지름 입력(음수이면 종료 >> ";
cin >> radius;
if(radius < 0) break;
Circle *p = new Circle(radius);
cout << "원의 면적은 " << p->getArea() << endl;
delete p;
}
return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
11
정수 반지름 입력(음수이면 종료 >> 5
생성자 실행
원의 면적은 78.5
소멸자 실행
정수 반지름 입력(음수이면 종료 >> 9
생성자 실행
원의 면적은 254.34
소멸자 실행
정수 반지름 입력(음수이면 종료 >> -1

Process finished with exit code 0

This Pointer

  • 현재의 실행중인 객체를 가리키는 pointer 변수
  • 포인터, 객체 자신 포인터
  • 클래스의 멤버 함수 내에서만 사용
  • 개발자가 선언하는 변수가 아닌 컴파일러가 선언한 변수
  • 용도
    • 매개변수 이름 == 멤버 변수 이름
    • 매개변수가 자신의 객체주소를 return
  • 사용범위
    • 멤버 함수

Quiz

quiz1.cpp

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
#include <iostream>
using namespace std;

class Sample{
int *p;
int size;
public:
Sample(int n){
size = n; p = new int[n];
}
void read();
void write();
int big();
int getSize(){return size;}
~Sample(){delete [] p;}
};

int main(){
Sample s(10);
s.read();
cout << "동적배열 정수 " << s.getSize() << "개를 출력합니다. ";
s.write();
cout << "가장 큰 수는 " << s.big() << endl;

return 0;
}

void Sample::read(){
cout << "입력하려는 정수의 개수는? ";
cin >> size;
cout << size << "개의 정수를 입력하시오. ";
for(int i = 0; i < size; i++)
cin >> p[i];
}
void Sample::write(){
for(int i = 0; i < size; i++)
cout << p[i] << ' ';
cout << endl;
}
int Sample::big(){
int b = p[0];
for(int i = 0; i < size; i++){
b = (b < p[i]) ? p[i] : b;
}
return b;
}

Output

1
2
3
4
5
6
입력하려는 정수의 개수는? 5
5개의 정수를 입력하시오. 11 22 44 55 23
동적배열 정수 5개를 출력합니다. 11 22 44 55 23
가장 큰 수는 55

Process finished with exit code 0

quiz2.cpp

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
#include <iostream>
using namespace std;

class Circle{
int radius;
public:
Circle(){radius = 1;}
Circle(int r){radius = 1;}
void setRadius(int r){radius = r;}
int getRadius(){return radius;}
double getArea(){return 3.14 * radius * radius;}
};

class Sample{
Circle *p;
int size;
public:
Sample(int n){
size = n; p = new Circle[n];
}
void read();
void write();
Circle big();
int getSize(){return size;}
~Sample(){delete [] p;}
};

int main(){
Sample s(10);
s.read();
cout << "각 원 객체의 반지름 " << s.getSize() << "개를 출력합니다. ";
s.write();
Circle big = s.big();
cout << "가장 큰 원의 넓이 : " << big.getArea() << "\t 가장 큰 원의 반지름 : " << big.getRadius() << endl;

return 0;
}

void Sample::read(){
int num;
cout << "입력하려는 원의 개수는? ";
cin >> size;
cout << size << "개의 원의 반지름을 입력하시오. ";
for(int i = 0; i < size; i++){
cin >> num;
p[i].setRadius(num);
}
}
void Sample::write(){
for(int i = 0; i < size; i++)
cout << p[i].getRadius() << ' ';
cout << endl;
}
Circle Sample::big(){
Circle b = p[0];
for(int i = 0; i < size; i++){
b.setRadius((b.getRadius() < p[i].getRadius()) ? p[i].getRadius() : b.getRadius());
}
return b;
}

Output

1
2
3
4
5
6
입력하려는 원의 개수는? 3
3개의 원의 반지름을 입력하시오. 3 16 2
각 원 객체의 반지름 3개를 출력합니다. 3 16 2
가장 큰 원의 넓이 : 803.84 가장 큰 원의 반지름 : 16

Process finished with exit code 0

Day 6

string

  • 문자열 생성
    • string str0("name");
    • string str1 = "name";
    • string str2(str); - 복사생성자
    • 주의 : 문자열 끝에 '\0(NULL)가 없음
  • 문자열 연산자
    • 산술 연산자
      • +=, +
    • 관계 연산자
      • >=, <=, >, <, !=, ==
      • 비교는 사전식
    • 배열처럼 사용 가능
  • 문자열 변환 함수
    • 문자열 -> 숫자
      • stoi();
      • stof();
      • stod();
    • 숫자 -> 문자열
      • to_string();
    • 문자열 -> C언어 문자열(\0)
      • str.c_str();
  • 문자열 크기 함수
    • str.size();
    • str.length();
    • str.capacity(); - 시스템이 정해줌
  • 문자열 조작 함수
    • str.append(string);
      • 문자열 뒤에 파라미터의 문자열 추가
    • str.substr(시작 index, 크기);
      • 시작부터 크기만큼 추출
    • str.replace(index, length, string);
      • index에서 length만큼 string으로 대체
    • str.find(string, index);
      • index부터 string이 시작하는 위치 int값으로 return
    • str.resize(unsigned, char);
      • 숫자만큼의 크기로 바꾸며 남는다면 char으로 채움

string1.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main(){
string str;
string address("서울시 서울시 서울시");
string copyAddress(address);

char text[] = {'L', 'O', 'V', 'E', ' ', 'C', '+', '+', '\0'};
string title(text);

cout << str << endl;
cout << address << endl;
cout << copyAddress << endl;
cout << title << endl;

return 0;
}

Output

1
2
3
4
5
6

서울시 서울시 서울시
서울시 서울시 서울시
LOVE C++

Process finished with exit code 0

string2.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main(){
string str;

cout << "문자열을 입력하세요 : ";

getline(cin, str, '\n');
int len = str.length();

for(int i = 0; i < len; i++){
string first = str.substr(0, 1);
string sub = str.substr(1, len-1);
str = sub + first;
cout << str <<endl;
}

return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
문자열을 입력하세요 : asdfasdfasdfasdf
sdfasdfasdfasdfa
dfasdfasdfasdfas
fasdfasdfasdfasd
asdfasdfasdfasdf
sdfasdfasdfasdfa
dfasdfasdfasdfas
fasdfasdfasdfasd
asdfasdfasdfasdf
sdfasdfasdfasdfa
dfasdfasdfasdfas
fasdfasdfasdfasd
asdfasdfasdfasdf
sdfasdfasdfasdfa
dfasdfasdfasdfas
fasdfasdfasdfasd
asdfasdfasdfasdf

Process finished with exit code 0

string3.cpp

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 <iostream>
using namespace std;

class Date{
string year;
string month;
string day;
public:
Date(int y, int m, int d){year = to_string(y); month = to_string(m); day = to_string(d);}
Date(string when);
void show();
string getYear(){return year;}
string getMonth(){return month;}
string getDay(){return day;}
};

int main(){
Date birth(2014, 3, 20);
Date independenceDay("1945/8/15");
independenceDay.show();
birth.show();
cout << birth.getYear() << ',' << birth.getMonth() << ',' << birth.getDay() << endl;

return 0;
}

Date::Date(string when){
int where1;
int where2;
where1 = when.find('/');
year = when.substr(0, where1);
where2 = when.find('/', where1 + 1);
month = when.substr(where1 + 1, where2 - where1 - 1);
day = when.substr(where2 + 1, when.size());
}
void Date::show(){
cout << year << "년" << month << "월" << day << "일" << endl;
}

Output

1
2
3
4
5
1945815
2014320
2014,3,20

Process finished with exit code 0

Call by Reference

1
2
3
4
5
int n = 0;
int &refn = n;

class c;
class &refc = c;
  • 참조 변수
    • 참조자 &의 도입
    • 이미 존재하는 변수에 대한 다른 이름(별명)을 선언
      • 참조 변수는 이름만 존재
      • 참조 변수에 새로운 공간 할당 X
      • 초기화로 지정된 기존 변수 공유

reference1.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main(){
cout << 'i' << '\t' << 'n' << '\t' << "refn" << endl;

int i = 1;
int n = 2;
int &refn = n;
n = 4;
refn++;
cout << i << '\t' << n << '\t' << refn << endl;

refn = i;
refn++;
cout << i << '\t' << n << '\t' << refn << endl;

int *p = &refn;
*p = 20;
cout << i << '\t' << n << '\t' << refn << endl;

return 0;
}

Output

1
2
3
4
5
6
i	n	refn
1 5 5
1 2 2
1 20 20

Process finished with exit code 0

reference2.cpp

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 <iostream>
using namespace std;

class Circle{
int radius;
public:
Circle(){radius = 1;}
Circle(int radius){this->radius = radius;}
void setRadius(int radius){this->radius = radius;}
double getArea(){return 3.14*radius*radius;}
};

void readRadius(Circle &c);

int main(){
Circle donut;
readRadius(donut);
cout << "donut의 면적 = " << donut.getArea() << endl;

return 0;
}

void readRadius(Circle &c){
int r;
cout << "정수 값으로 반지름을 입력하세요 >> ";
cin >> r;
c.setRadius(r);
}

Output

1
2
3
4
정수 값으로 반지름을 입력하세요 >> 3
donut의 면적 = 28.26

Process finished with exit code 0

reference3.cpp

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
#include <iostream>
using namespace std;

class MyintStack{
int p[10];
int tos;
public:
MyintStack();
bool push(int n);
bool pop(int &refn);
};

int main(){
MyintStack a;
for(int i = 0; i < 11; i++){
if(a.push(i))
cout << i << '\t';
else
cout << endl << i + 1 << "번째 stack full" << endl;
}
int n;
for(int i = 0; i < 11; i++){
if(a.pop(n))
cout << n << '\t';
else
cout << endl << i + 1 << "번째 stack empty" << endl;
}

return 0;
}

MyintStack::MyintStack(){
tos = 0;
}
bool MyintStack::push(int n){
if(tos == 10)
return false;
p[tos] = n;
tos++;
return true;
}
bool MyintStack::pop(int &refn){
if(tos == 0)
return false;
tos--;
refn = p[tos];
return true;
}

Output

1
2
3
4
5
6
0	1	2	3	4	5	6	7	8	9	
11번째 stack full
9 8 7 6 5 4 3 2 1 0
11번째 stack empty

Process finished with exit code 0

Quiz

quiz.cpp

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
77
78
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

class Player{
string name;
public:
Player(string name = ""){
this->name = name;
}
void setName(string name){this->name = name;}
string getName(){return name;}
void getEnterKey(){
char buf[100];
cin.getline(buf, 99);
}
};

class GamblingGame{
Player p[2];
int num[3];
bool matchAll();
public:
GamblingGame();
void run();
};

int main(){
GamblingGame game;
game.run();

return 0;
}

GamblingGame::GamblingGame(){
cout << "***** 갬블링 게임을 시작합니다. *****" << endl;
srand((unsigned int) time(0));
for(int i = 0; i < 3; i++)
num[i] = 0;
cout << "첫번째 선수 이름>>";
string name;
cin >> name;
p[0].setName(name);
cout << "두번째 선수 이름>>";
cin >> name;
p[1].setName(name);
}
void GamblingGame::run(){
string n;
int i = 0;
for(;;){
cout << p[i % 2].getName() << " : <Enter>";
if(i == 0)
cout << endl;
p[i % 2].getEnterKey();
if(this->matchAll()){
cout << p[i % 2].getName() << "님 승리!!" << endl;
break;
}
else
cout << "아쉽군요!" << endl;
i++;
}
}
bool GamblingGame::matchAll(){
cout << "\t";
for (int i = 0; i < 3; i++){
int n = rand() % 3;
num[i] = n;
cout << num[i] << "\t";
}
if (num[0] == num[1] && num[0] == num[2])
return true;
else
return false;
}

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
***** 갬블링 게임을 시작합니다. *****
첫번째 선수 이름>>Kim
두번째 선수 이름>>Park
Kim : <Enter>
0 1 1 아쉽군요!
Park : <Enter>
2 0 2 아쉽군요!
Kim : <Enter>
1 2 1 아쉽군요!
Park : <Enter>
1 1 1 Park님 승리!!

Process finished with exit code 0

Day 7

Dynamic Memory

stack.cpp

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 <iostream>
using namespace std;

class MyintStack{
int *parr;
int tos;
public:
MyintStack(int size);
bool push(int n);
bool pop(int &refn);
~MyintStack(){delete [] parr;}
};

int main(){
MyintStack a(10);
for(int i = 0; i < 11; i++){
if(a.push(i))
cout << i << '\t';
else
cout << endl << i + 1 << "번째 stack full" << endl;
}
int n;
for(int i = 0; i < 11; i++){
if(a.pop(n))
cout << n << '\t';
else
cout << endl << i + 1 << "번째 stack empty" << endl;
}

return 0;
}

MyintStack::MyintStack(int size){
tos = 0;
parr = new int[size];
}
bool MyintStack::push(int n){
if(tos == 10)
return false;
parr[tos] = n;
tos++;
return true;
}
bool MyintStack::pop(int &refn){
if(tos == 0)
return false;
tos--;
refn = parr[tos];
return true;
}

Output

1
2
3
4
5
6
0	1	2	3	4	5	6	7	8	9	
11번째 stack full
9 8 7 6 5 4 3 2 1 0
11번째 stack empty

Process finished with exit code 0

Return Reference

return_ref1.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

char c = 'a';

char &find(){
return c;
}

int main(){
char a = find();
cout << a << endl;

char &ref = find();
cout << ref << endl;

find() = 'b';
cout << find() << endl;

return 0;
}

Output

1
2
3
4
5
a
a
b

Process finished with exit code 0

return_ref2.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

char &find(char s[], int index){
return s[index];
}

int main(){
char name[] = "Zerohertz";
cout << name << endl;

find(name, 0) = '5';
cout << name << endl;

char &ref = find(name, 2);
ref = 't';
cout << name << endl;

return 0;
}

Output

1
2
3
4
5
Zerohertz
5erohertz
5etohertz

Process finished with exit code 0

reference.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

void addConst(int &x, int y){
x = x + 200;
y = y + 200;
cout << "addConst" << endl;
cout << "&x = " << &x << "\tx = " << x << endl;
cout << "&y = " << &y << "\ty = " << y << endl;
}

int main(){
int a = 100;
int b = 100;
addConst(a, b);
cout << "Main" << endl;
cout << "&a = " << &a << "\ta = " << a << endl;
cout << "&b = " << &b << "\tb = " << b << endl;

return 0;
}

Output

1
2
3
4
5
6
7
8
addConst
&x = 0x7ffee84d09a8 x = 300
&y = 0x7ffee84d0964 y = 300
Main
&a = 0x7ffee84d09a8 a = 300
&b = 0x7ffee84d09a4 b = 100

Process finished with exit code 0

return_ref3.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int &addConst(int &x, int y){
x = x + 200;
y = y + 200;
cout << "addConst" << endl;
cout << "&x = " << &x << "\tx = " << x << endl;
cout << "&y = " << &y << "\ty = " << y << endl;
return x;
}

int main(){
int a = 100;
int b = 100;
addConst(a, b) = 555;
cout << "Main" << endl;
cout << "&a = " << &a << "\ta = " << a << endl;
cout << "&b = " << &b << "\tb = " << b << endl;

return 0;
}

Output

1
2
3
4
5
6
7
8
addConst
&x = 0x7ffee834b9a8 x = 300
&y = 0x7ffee834b964 y = 300
Main
&a = 0x7ffee834b9a8 a = 555
&b = 0x7ffee834b9a4 b = 100

Process finished with exit code 0

Shallow Copy & Deep Copy

  • Shallow copy
    • 객체 복사 시, 객체의 멤버를 1:1로 복사
    • 객체의 멤버 변수에 동적 메모리가 할당된 경우
      • 사본은 원본 객체가 할당 받은 메모리를 공유하는 문제 발생
  • Deep copy
    • 객체 복사 시, 객체의 멤버를 1:1로 복사
    • 객체의 멤버 변수에 동적 메모리가 할당된 경우
      • 사본은 원본이 가진 메모리 크기만큼 별도로 동적 할당
      • 원본의 동적 메모리에 있는 내용을 사본에 복사
    • 완전한 형태의 복사
      • 사본과 원본은 메모리를 공유하는데 문제 없음

shallow1_copy.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main(){
int *a = new int(3);
int *b = new int(5);
cout << "a의 주소(복사전) : " << a << endl;
cout << "b의 주소(복사전) : " << b << endl;

a = b;

cout << "a의 주소(복사후) : " << a << endl;
cout << "b의 주소(복사후) : " << b << endl;

cout << "a의 값 : " << *a << endl;
cout << "b의 값 : " << *b << endl;

delete a;
delete b;

return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
untitled(997,0x11451edc0) malloc: *** error for object 0x7fefdc405980: pointer being freed was not allocated
untitled(997,0x11451edc0) malloc: *** set a breakpoint in malloc_error_break to debug
a의 주소(복사전) : 0x7fefdc405970
b의 주소(복사전) : 0x7fefdc405980
a의 주소(복사후) : 0x7fefdc405980
b의 주소(복사후) : 0x7fefdc405980
a의 값 : 5
b의 값 : 5

Process finished with exit code 6

shallow2_copy.cpp

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
#include <iostream>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;

class Person{
char *name;
int id;
public:
Person(Person &p){
this->name = p.name;
this->id = p.id;
}
Person(int id, const char *name);
~Person();
void changeName(const char *name);
void show(){cout << id << ',' << name << endl;}
};

int main(){
Person zerohertz(1, "zerohertz");
Person zhz(zerohertz);

cout << "***** zhz 객체 생성 후 *****" << endl;
zerohertz.show();
zhz.show();

zhz.changeName("0Hz");
cout << "***** zhz 이름 변경 후 *****" << endl;
zerohertz.show();
zhz.show();

return 0; // zhz, zerohertz 순으로 소멸, zerohertz 소멸 시 오류
}

Person::Person(int id, const char *name){
this->id = id;
int len = strlen(name);
this->name = new char[len + 1];
strcpy(this->name, name);
}
Person::~Person(){
delete [] name;
}
void Person::changeName(const char *name){
if(strlen(name) > strlen(this->name))
return;
strcpy(this->name, name);
}

Output

1
2
3
4
5
6
7
8
9
10
untitled(1347,0x11c7dddc0) malloc: *** error for object 0x7f9184c05970: pointer being freed was not allocated
untitled(1347,0x11c7dddc0) malloc: *** set a breakpoint in malloc_error_break to debug
***** zhz 객체 생성 후 *****
1,zerohertz
1,zerohertz
***** zhz 이름 변경 후 *****
1,0Hz
1,0Hz

Process finished with exit code 6

Copy Constructor

copy_constructor1.cpp

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
#include <iostream>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;

class Person{
char *name;
int id;
public:
Person(Person &p);
Person(int id, const char *name);
~Person();
void changeName(const char *name);
void show(){cout << id << ',' << name << endl;}
};

int main(){
Person zerohertz(1, "zerohertz");
Person zhz(zerohertz);

cout << "***** zhz 객체 생성 후 *****" << endl;
zerohertz.show();
zhz.show();

zhz.changeName("0Hz");
cout << "***** zhz 이름 변경 후 *****" << endl;
zerohertz.show();
zhz.show();

return 0;
}

Person::Person(Person &p){
this->id = p.id;
int len = strlen(p.name);
this->name = new char[len + 1];
strcpy(this->name, p.name);
cout << "복사 생성자 실행, 원본 객체의 이름 : " << this->name << endl;
}
Person::Person(int id, const char *name){
this->id = id;
int len = strlen(name);
this->name = new char[len + 1];
strcpy(this->name, name);
}
Person::~Person(){
delete [] name;
}
void Person::changeName(const char *name){
if(strlen(name) > strlen(this->name))
return;
strcpy(this->name, name);
}

Output

1
2
3
4
5
6
7
8
9
복사 생성자 실행, 원본 객체의 이름 : zerohertz
***** zhz 객체 생성 후 *****
1,zerohertz
1,zerohertz
***** zhz 이름 변경 후 *****
1,zerohertz
1,0Hz

Process finished with exit code 0

copy_constructor2.cpp

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
#include <iostream>
#pragma warning(disable:4996)
using namespace std;

class MyString{
char *pBuf;
public:
MyString(const char *s = NULL);
MyString(MyString &MyStr);
~MyString();
void print();
int getSize();
};

int main(){
MyString str1;
MyString str2("Hello");
MyString str3("World!");
MyString str4(str3);
str1.print();
str2.print();
str3.print();
str4.print();

return 0;
}

MyString::MyString(const char *s){
if(s == NULL){
pBuf = new char[1];
pBuf[0] = NULL;
}
else{
pBuf = new char[strlen(s) + 1];
strcpy(pBuf, s);
}
}
MyString::MyString(MyString &MyStr){
int len = MyStr.getSize();
this->pBuf = new char[len + 1];
strcpy(this->pBuf, MyStr.pBuf);
}
MyString::~MyString(){
if(pBuf) delete [] pBuf;
}
void MyString::print(){
cout << pBuf << endl;
}
int MyString::getSize(){
return strlen(pBuf);
}

Output

1
2
3
4
5
6

Hello
World!
World!

Process finished with exit code 0

객체에서 동적 메모리 사용 시 복사 생성자 직접 생성

Day 1

C와의 차이점

  • #include <stdio.h> -> #include <iostream>
    • 형식지정자 필요없어짐
  • .h 사라짐
    • 호환은 가능
  • class의 개념
    • object
  • bool의 개념
    • C
      • 참 : 0 이외의 값
    • C++
      • true : 1
      • false : 0
  • string의 개념

Practice

practice.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main(){
int a = 5;
int b = 10;
cout << "1. a + b / 3 * 3 = " << a + b / 3 * 3 << endl;
cout << "2. b << 2 = " << (b << 2) << endl;
cout << "3. a != b = " << (a != b) << endl;
cout << "4. b % a = " << (b % a) << endl;
cout << "5. (a > b) ? a : b = " << ((a > b) ? a : b) << endl;
cout << "6. sizeof(a) = " << sizeof(a) << endl;
int c;
c = a++;
cout << "7. C = a++ 이후 c의 값 : " << c << endl;
a += b;
cout << "8. a += b 이후 a의 값 : " << a << endl;
cout << "9. a & b = " << (a & b) << endl;
c = (a + b, a - b);
cout << "10. c = (a + b, a - b) 이후 c의 값 : " << c << endl;
return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
11
12
1. a + b / 3 * 3 = 14
2. b << 2 = 40
3. a != b = 1
4. b % a = 0
5. (a > b) ? a : b = 10
6. sizeof(a) = 4
7. C = a++ 이후 c의 값 : 5
8. a += b 이후 a의 값 : 16
9. a & b = 0
10. c = (a + b, a - b) 이후 c의 값 : 6

Process finished with exit code 0
Read more »

제어문

  • 제어문(Control statement)을 사용하여 조건에 의해 문장들의 실행 순서 변경
    • 조건문 : 조건에 따라서 여러 개의 실행 경로 가운데 하나를 선택
      • if-else 문장
      • switch 문장
    • 반복문 : 조건이 유지되는 한, 혹은 정해진 횟수만큼 처리를 되풀이하는 문장
      • for 문장
      • while 문장
Read more »

변수와 상수

  • 변수(Variable) : 프로그램이 실행되는 동안 저장된 값이 변경될 수 있는 공간
  • 상수(Constant) : 프로그램이 실행되는 동안 값이 변경되지 않는 공간

자료형

데이터의 종류를 자료형(Data type) 또는 데이터 타입이라고 한다.

  • 변수는 일단 하나의 자료형으로 정의되면 해당되는 종류의 데이터만 저장할 수 있음
  • 자료형을 크게 나누면 정수형(Interger type)과 문자형(Charater type), 부동 소수점형(Floating-point type)으로 나눌 수 있음
    • 정수형 : 정수 타입의 데이터
    • 문자형 : 하나의 문자 데이터
    • 부동 소수점형 : 실수 타입의 데이터
Read more »

C++?

  • C언어에 객체 지향 프로그래밍 기능을 추가하여 만든 언어
  • C언어만큼 효율적이고 속도가 빠르며 이식성이 좋은 절차적 프로그래밍(Procedural programming) 가능
  • 복잡한 프로그램을 작성하는데 매우 효과적인 방법으로 알려진 객체 지향 프로그래밍(Object-oriented programming) 가능
  • 템플릿을 이용하는 일반화 프로그래밍(Generic programming) 가능
Read more »

Introduction

Media? Mass Media?

  • Media
    • 매체(Medium) : 어떤 작용을 한 쪽에서 다른 쪽으로 전달 하는 물체, 또는 그런 수단
    • 메세지를 수용자에게 전달하기 위한 물체 또는 수단
  • Mass media
    • 대중매체
    • 많은 사람들에게 대량으로 (정보와 사상 등을) 전달하는 것

프레임 효과(Framing Effect)

  • 1978년, Tuchman의 편집국 참여관찰 연구
  • 뉴스 생산자인 뉴스 조직이 현실의 사건을 선택, 가공, 편집하여 수용자에게 현실을 바라보는 하나의 틀(frame)을 제공

정치적 사안들에 대한 수용자들의 인식이 그 사안에 대한 매스미디어의 해석적 관점에 영향을 받는 현상

Read more »

Introdunction

Resources are scarce(자원은 희소하다.)

희소성(Scarcity) : 사회가 가진 자원은 제한적이라는 속성

경제학(Economics)

한 사회가 자신의 희소한 자원들을 관리(Manage)하는 방법을 연구하는 학문

  • 사람들은 무엇을 살 것인지, 얼마나 일하고, 저축하고, 지출할 것인지를 어떻게 결정하나?
  • 기업은 무엇을 얼마나 생산하고, 노동자들을 몇 명이나 고용할 것인지 어떻게 결정하나?
  • 사회는 제한된 자원을 국방비, 소비재, 환경보호, 기타 여러 필요한 일에 어떻게 배분하나?
Read more »

Introduction

Optimization - 시험

  • Define manufacturing and describe the technical and economic consideration : A kind of optimization
  • Relationship among product design(functionality & costs) and engineering(processes) and factors(materials)
  • Important trends in modern manufacturing and minimizing of production costs(optimization)

Manufacturing(production)

  • The process of converting raw materials into products
    • Design of desires
    • Realization of goods
    • Through various production methods
  • Raw material became a useful product : Maximizing Added value

Product Design & Concurrent Engineering

  • Product design(제품 설계)
    • 70~80%의 개발 비용
    • 제품에 대한 기능 및 성능의 이해 필요
    • CAD, CAM, CIM을 통해 설계와 제작이 순차적으로 이루어짐
    • 하지만 현대는 즉각적으로 이뤄짐 : 동시 공학
  • Concurrent engineering(동시 공학)
    • 설계 및 제조를 통합하는 즉각적이고 체계적인 접근
    • 제품의 수명주기와 관련된 모든 요소 최적화
Read more »