Answer to Question #213951 in C++ for Maryam

Question #213951

Create a class template for a class named GeneralStackthat holds

  • A single data member as an array named stack of size 50 to store certain elements.
  • Three member functions i.e. push(type) to add elements in the Stack, pop() to remove elements from the stack, and currentStatus() to check whether the array is filled or not. (A filled array is an array that has non-zero value at all of its indexes).

In the main() function, create three objects with different data types of class General Stack and test the functionality of member functions for various values of data members for these objects.


1
Expert's answer
2021-07-08T02:04:56-0400
#include <iostream>
using namespace std;
template<typename type>
class GeneralStack{
    type stack[50];
    int size;
    public:
    GeneralStack(){
        for(int i = 0; i < 50; i++) stack[i] = (type) 0;
        size = 0;
    }
    void push(type data){
        if(size == 50){
            return;
        }
        else stack[size++] = data;
    }
    void pop(){
        if(size == 0) return;
        else{
            cout<<stack[--size]<<" ";
            stack[size] = (type)0;
        }
    }
    bool currentStatus(){
        if(size == 50) return true;
        return false;
    }
};
int main(){
    GeneralStack<int> integers;
    GeneralStack<float> floats;
    GeneralStack<char> chars;


    for(int i = 0; i < 52; i++){
        integers.push(i);
        floats.push(i);
        chars.push(i + 65);
    }
    if(integers.currentStatus()) cout<<"\nArray is full!\n";
    floats.pop();


    cout<<endl;
    for(int i = 0; i < 45; i++){
        integers.pop();
    }


    if(!chars.currentStatus()) cout<<"\nArray is NOT full";
    cout<<endl;
    chars.pop();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS