If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Certainly! Here's an example that demonstrates the use of template classes in C++:
cppCopy code
#include <iostream>
// Template class representing a Stack
template <typename T>
class Stack {
private:
    T* data;        // Array to store stack elements
    int capacity;   // Maximum capacity of the stack
    int top;        // Index of the top element
public:
    // Constructor
    Stack(int stackCapacity) {
        capacity = stackCapacity;
        data = new T[capacity];
        top = -1;
    }
    // Destructor
    ~Stack() {
        delete[] data;
    }
    // Function to check if the stack is empty
    bool isEmpty() const {
        return top == -1;
    }
    // Function to check if the stack is full
    bool isFull() const {
        return top == capacity - 1;
    }
    // Function to push an element onto the stack
    void push(const T& element) {
        if (isFull()) {
            std::cout << "Stack Overflow. Cannot push element.\n";
            return;
        }
        data[++top] = element;
    }
    // Function to pop an element from the stack
    void pop() {
        if (isEmpty()) {
            std::cout << "Stack Underflow. Cannot pop element.\n";
            return;
        }
        --top;
    }
    // Function to retrieve the top element of the stack
    T& getTop() const {
        if (isEmpty()) {
            std::cout << "Stack is empty. No top element.\n";
            exit(1);
        }
        return data[top];
    }
};
int main() {
    // Create a stack of integers
    Stack<int> intStack(5);
    // Push elements onto the stack
    intStack.push(10);
    intStack.push(20);
    intStack.push(30);
    // Get and display the top element
    std::cout << "Top element of intStack: " << intStack.getTop() << std::endl;
    // Pop an element from the stack
    intStack.pop();
    // Get and display the new top element
    std::cout << "Top element of intStack after popping: " << intStack.getTop() << std::endl;
    // Create a stack of characters
    Stack<char> charStack(3);
    // Push elements onto the stack
    charStack.push('A');
    charStack.push('B');
    // Get and display the top element
    std::cout << "Top element of charStack: " << charStack.getTop() << std::endl;
    // Pop an element from the stack
    charStack.pop();
    // Get and display the new top element
    std::cout << "Top element of charStack after popping: " << charStack.getTop() << std::endl;
    return 0;
}
In this example, we create a template class called Stack that represents a stack data structure. The template parameter T represents the type of elements stored in the stack.
The Stack class includes member variables data (an array to store stack elements), capacity (the maximum capacity of the stack), and top (the index of the top element).
The class provides member functions for basic stack operations such as isEmpty, isFull, push, pop, and getTop.
In the main function, we create two instances of the Stack class: intStack (a stack of integers) and charStack (a stack of characters). We push elements onto the stacks, retrieve and display the top elements, and perform pop operations.
By running the program, you will see the output that demonstrates the stack operations for both integer and character stacks.
Start the conversation!
Be the first to share your thoughts
Quick answers to common questions about our courses, quizzes, and learning platform
Didn't find what you're looking for?
Contact Support