C++

[C++] 명품 C++ 프로그래밍 실습문제 7장 11번

Hs’s Coding vlog 2023. 1. 11. 21:53
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;
class Stack{
    int *p;
    int index=-1;
public:
    Stack(){
        p = new int [10];
    }
    Stack &operator<<(int k){
        this->p[++index] = k;
        return  *this;
    }
    
    void operator>>(int &k){ //pop
        k=this->p[index];
        this->index--;
    }

    bool operator!(){
        if(this->index==-1) return true;
        else return false;
    }
    
};



int main() {
    Stack stack;
    stack << 3 << 5 << 10;
    
    while (true) {
        if(!stack) break;
        
        int x;
        stack >> x;
        cout << x << ' ';
    }
    cout << endl;
    return 0;
}