본문 바로가기

언어/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 MyQueue: public BaseArray{
		int enindex;
		int deindex;
public:
	MyQueue(int size) : BaseArray(size){
		enindex=0;
		deindex=-1;
	}
	void enqueue(int n){
		put(enindex,n);
		enindex++;
	}
	int capacity(){
		return getCapacity();
	}
	int length(){
		return enindex;
	}
	int dequeue(){
		enindex--;
		deindex++;
		return get(deindex);
	}
	
};

int main(int argc, char** argv) {
	
	MyQueue mQ(100);
	int n;
	cout << "박스에 삽입할 5개의 정수를 입력하라 >> ";
	
	for (int i=0; i<5; i++){
		cin >>n;
		mQ.enqueue(n);
	} 
	
	cout << "큐의 용량: " << mQ.capacity() << ", 큐의 크기 : " << mQ.length() << endl; 
	
	cout << "큐의 원소를 순서대로 제거하여 출력한다 >> ";
	
	while(mQ.length() !=0){
		cout << mQ.dequeue() << ' ';
	}
	
	cout << endl << "큐의 현재 크기 : " << mQ.length() << endl;	
	

	return 0;
}