본문 바로가기

언어/C++

박스 상속 스택 박스에 공넣고 빼기!

#include <iostream>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

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


class MyStack: public BaseArray{
		int top;
public:
	MyStack(int size):BaseArray(size){
		top=0;
	}
	void push(int n){
		put(top, n);
		top++;
	}
	int pop(){
		top--;
		return get(top);
	}
	int capacity(){
		return getCapacity();
	}
	int length(){
		return top;
	}
	
};

int main(int argc, char** argv) {
	
	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;	
	

	return 0;
}