Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 코딩독학
- 자바정리
- Reflex
- C++
- 개인프로젝트
- backend
- SNS
- 모의 주식
- java
- 개발자
- 사물인터넷
- 코테
- 코스닥
- 코딩테스트
- 주가예측
- 프로그래밍
- frontend
- 자바독학
- Project
- 백준
- 프로그래머스
- 프로그램
- Python
- java정리
- 이웃한 칸
- 코딩
- 풀스택프레임워크
- 빅데이터
- 개발
- 자바
Archives
- Today
- Total
Creative Code
stack3(stack,array클래스 연결) 본문
※main.cpp파일
#include <iostream>
#include "stack.h"
int main()
{
Stack s1(10);
Stack s2(100);
s1.push(100); //구조체 자료는 인자를 전달할 때 오버헤드를 줄이기 위해 포인터를 쓴다.
s1.push(200);
s1.push(300);
std::cout << "s1 1st pop() : " << s1.pop() <<std::endl;
std::cout << "s1 2nd pop() : " << s1.pop() <<std::endl;
std::cout << "s1 3rd pop() : " << s1.pop() <<std::endl;
s2.push(900);
s2.push(800);
s2.push(700);
std::cout << "s2 1st pop() : " << s2.pop() <<std::endl;
std::cout << "s2 2nd pop() : " << s2.pop() <<std::endl;
std::cout << "s2 3rd pop() : " << s2.pop() <<std::endl;
return 0;
}
※stack.h파일
#ifndef STACK_H
#define STACK_H
#include "array.h"
class Stack {
private:
static const int STACKSIZE;
Array arr_; //클래스안에 클래스타입의 멤버를 가지는 관계를 'has-a'관계라 한다.
int tos_;
Stack(const Stack& rhs);
Stack& operator=(const Stack& rhs);
public:
explicit Stack(int size = Stack::STACKSIZE);
~Stack();
bool isFull() const;
bool isEmpty() const;
void push(int data);
int pop();
};
#endif
※stack.cpp파일
#include "stack.h"
#include <cassert>
const int STACKSIZE = Array::getDefaultSize();
Stack::Stack(int size)
:arr_(size), tos_(0)
{
}
Stack::~Stack()
{
}
bool Stack::isFull() const {
return tos_ == arr_.size();
}
bool Stack::isEmpty() const {
return tos_ == 0;
}
void Stack::push(int data) {
assert(!isFull());
arr_[tos_] = data;
++tos_;
}
int Stack::pop() {
assert(!isEmpty());
--tos_;
return arr_[tos_];
}
'C++ Programming' 카테고리의 다른 글
queue2(생성자 초기화 리스트, static member, explicit) (0) | 2023.09.06 |
---|---|
safeArray(배열 크기) (0) | 2023.09.06 |
stack2(extern,explicit사용) (0) | 2023.09.06 |
array(array 클래스) (0) | 2023.09.05 |
list2(linked list 클래스 연결) (0) | 2023.09.05 |