Would like to destroy the stack that I made

  abstract-data-type, c++, class, destructor

So, in class, we learnt about the implementation of an array abstract data structure, and using the array class we made, we implemented a stack abstract data structure as a class.

#include <iostream>
#ifndef ARRAYADT1_H
#define ARRAYADT1_H

using namespace std;

class ArrayADT1
{
    public:
        ArrayADT1();
        ArrayADT1(int);
        virtual ~ArrayADT1();
        bool setElement(int, int);
        int getElement(int);
        int getCapacity();
    protected:

    private:
        int capacity;
        int* elements;

};

#endif // ARRAYADT1_H


ArrayADT1::ArrayADT1(){
    capacity=0;
    elements=NULL;
}

ArrayADT1::ArrayADT1(int arraySize){
    capacity=arraySize;
    elements=new int[arraySize];
}

bool ArrayADT1::setElement(int index, int value){
    elements[index]=value;
    return(true);
}

int ArrayADT1::getElement(int index){
    return(elements[index]);
}

int ArrayADT1::getCapacity(){
    return(capacity);
}

ArrayADT1::~ArrayADT1(){
    delete[] elements;
}
#ifndef STACKADT1_H
#define STACKADT1_H

using namespace std;

class StackADT1
{
    public:
        StackADT1()=delete; //disable creation of stack without specifying capacity
        StackADT1(int); //create stack of capacity
        bool push(int);
        int pop();
        bool isFull();
        bool isEmpty();
        int length();
        virtual ~StackADT1();

    protected:

    private:
        int ValueCount;
        ArrayADT1 members;

};

#endif // STACKADT1_H

#include <iostream>

StackADT1::StackADT1(int stackCapacity): members(stackCapacity){
    ValueCount=0;
}

int StackADT1::length(){
    return(ValueCount);
}

bool StackADT1::isEmpty(){
    if(ValueCount==0)
        return(true);
    return(false);
}

bool StackADT1::isFull(){
    if(ValueCount==members.getCapacity())
        return(true);
    return(false);
}

int StackADT1::pop(){
    if(isEmpty()){
        cout<<"The Stack is empty"<<"n";
        return(-1);
    }
    ValueCount--;
    return(members.getElement(ValueCount));
}

bool StackADT1::push(int value){
    if(isFull()){
        cout<<"The stack is full"<<"n";
        return(false);
    }
    members.setElement(ValueCount, value);
    ValueCount++;
    return(true);
}

StackADT1::~StackADT1(){
    //I would like to know what happens here
    //dtor



}

I was wondering about the destructor function in both cases. In the ArrayADT1 class, we explicitly used the delete method, but we do no such thing in the StackADT1 class. Does the stack also get destroyed after

return 0;

is called?

Source: Windows Questions C++

LEAVE A COMMENT