Seminar 2 POO

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.6 kB
3
Indexable
Never
#include <iostream>

using namespace std;
/**
 * Clasa Stiva:
 *  - constructor fara parametri
 *  - destructor
 *  - constructor cu parametri
 *  - constructor de copiere
 *  - metoda adaugare element in stiva
 *  - metoda scoatere element din stiva
 */

class Stiva{
    int *vect;
    int top;
    int max_dimension;

    public:

        Stiva(); // constructor fara parametri
        ~Stiva(); // destructor
        Stiva(int, int); // constructor cu parametri
        Stiva(Stiva &); // constructor de copiere

        // metode
        void push(int);
        int pop();
};

void Stiva::push(int valoare)
{
    if(top == max_dimension - 1)
    {
        int* aux;
        aux = new int[max_dimension + 50];
        for(int i=0; i<max_dimension;i++)
        {
            aux[i] = vect[i];
        }
        delete []vect;
        max_dimension += 50;
        vect = aux;
    }
    vect[++top] = valoare;
}

Stiva::Stiva(){
    top = -1;
    max_dimension = 10;
    vect = new int[max_dimension];
}

Stiva::~Stiva(){
    delete []vect;
    top = -1;
    max_dimension = 0;
    vect = nullptr;
}

Stiva::Stiva(int dimensiune, int valoare){
    top = dimensiune - 1;
    max_dimension = dimensiune;
    vect = new int[max_dimension];
    for(int i=0;i<max_dimension;i++)
        vect[i] = valoare;
}

Stiva::Stiva(Stiva &x)
{
    max_dimension = x.max_dimension;
    top = x.top;
    vect = new int[max_dimension];
    for(int i=0;i<=top;i++)
    {
        vect[i] = x.vect[i];
    }
}

int Stiva::pop()
{
    return vect[top--];
}

int main()
{
    Stiva s;
    s.push(34);
    s.push(55);
    cout<<s.pop();
    return 0;
}