nakka soft world !

making stack 본문

프로그래밍언어/C++

making stack

nakka 2017. 3. 16. 22:47
728x90


1. 전역변수와 일반함수

#incude <iostream>

using namespace std;


int buff[10] = { 0 };

int idx = 0;


void push(int a) ( buff[idx++] = a; }

int pop() {return buff[--idx]; }


int main()

{

push(10);

push(20);


cout << pop() << endl;

cout << pop() << endl;

}

Stack이 2개 필요하다면 배열과 인덱스가 2개씩 필요함.


이를 개선하기위해 구조체 선언.


2. 구조체로 Stack타입 만들기

struck Stack

{

int buff[10];

int idx = 0;

};

void push(Stack* s, int a) ( s->buff[(s->idx)++] = a; }

int pop(Stack* s) {return s->buff[--(s->idx)]; }


int main()

{

Stack s1, s2;

s1.idx = 0;

s2.idx = 0;

push(&s1, 10);

push(&s1, 20);

cout << pop(&s1) << endl;

cout << pop(&s1) << endl;

}


함수 부분이 너무 복잡함.


3. 상태를 나타내는 데이터와 상태를 조작하는 함수 묶기


구조체 안에 함수를 넣으면 훨씬 깔끔한 구조가 됨.

struck Stack

{

int buff[10];  // 멤버데이터

int idx = 0;


void push(int a) ( buff[idx++] = a; }    // 멤버함수

int pop() {return buff[--idx]; }

};


int main()

{

Stack s1, s2;

s1.idx = 0;

s2.idx = 0;

s1.push(10); // 객체의 함수를 사용함. 객체 중심적 프로그램.

s1.push(20);

cout << s1.pop() << endl;

cout << s1.pop() << endl;

}


4. 접근지정자의 도입 - 정보은닉(information Hiding)

사용자는 index에 접근 할일이 없음.

//struck Stack  // struct의 경우에는 디폴트가 public

class Stack  // class의 경우에는 디폴트가 private

{

private:

int buff[10];  // 멤버데이터

int idx = 0;

public:

void init() { idx = 0; }

void push(int a) ( buff[idx++] = a; }    // 멤버함수

int pop() {return buff[--idx]; }

};

int main()

{

Stack s1;

//s1.idx = 0; // error. init함수를 통해 init함.

s1.init();

s1.push(10);

s1.push(20);

cout << s1.pop() << endl;

cout << s1.pop() << endl;

}


5. 객체 초기화를 자동으로  - 생성자

class Stack

{

private:

int buff[10];

int idx = 0;

public:

Stack() { idx = 0; }   // 생성자 : 객체를 생성하면 자동으로 호출 됨

void push(int a) ( buff[idx++] = a; }

int pop() {return buff[--idx]; }

};

int main()

{

Stack s1;

s1.push(10);

s1.push(20);

cout << s1.pop() << endl;

cout << s1.pop() << endl;

}


6. 소멸자

class Stack

{

private:

int* buff;

int idx = 0;

public:

Stack(int sz = 100)

{

buff = new int[sz];

idx = 0;

}

~Stack()  // 소멸자 : 객체가 파괴될때 호출된다.

{

delete[] buff;

}

void push(int v)

{

buff[idx++] = v;

}

int pop()

{

return buff[--idx];

}

};

int main()

{

Stack s1;

s1.push(10);

cout << s1.pop() << endl;

}


7. 선언과 구현의 분리

// Stack.h

class Stack

{

private:

int* buff;

int idx = 0;

public:

Stack(int sz = 10);

~Stack();

void push(int v);

int pop();

};

// Stack.cpp

Stack::Stack(int sz /*= 100*/)

{

buff = new int[sz];

idx = 0;

}

Stack::~Stack()

{

delete[] buff;

}

void Stack::push(int v)

{

buff[idx++] = v;

}

int Stack::pop()

{

return buff[--idx];

}


8. 코딩 관례

 1. 멤버 변수(Private) 가 멤버 함수 보다 뒤에 나옴

class Stack

{

public:

Stack(int sz = 10);

~Stack();

void push(int v);

int pop();

private:

int* buff;

int idx = 0;

};


9. 클래스 템플릿

데이터 타입만 바뀌고 구현 부가 동일하다면. Template.

주의 사항. 구현부와 선언부가 모두 하나의 파일에 있어야 함.

template<template T>class Stack

{

public:

Stack(int sz = 10)

{

idx = 0;

buff = new T[sz];

}

~Stack() { delete[] buff; };

void push(T v) { buff[idx++] = v; };

T pop() { return buff[--idx]; };

private:

T* buff;

int idx = 0;

};

int main()

{

Stack<int> s1;

Stack<double> s2;

s1.push(10);

cout << s1.pop() << endl;

}


10. STL Stack

STL에 Stack이 이미 구현되어있음

int main()

{

stack<int> s;


s.push(10);

s.push(20);

s.push(30);


cout << s.top() << endl; // OK. 단, 리턴만 하고 제거 안됨. // 30

cout << s.top() << endl; // OK. 단, 리턴만 하고 제거 안됨. // 30


s.pop();


cout << s.top() << endl; //20

}


728x90

'프로그래밍언어 > C++' 카테고리의 다른 글

생성자 (construct)  (0) 2017.03.16
접근 지정자  (0) 2017.03.16
OOP Concept  (0) 2017.03.16
C++ Explicit Casting  (0) 2017.03.15
reference  (0) 2017.03.15
Comments