Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:01:44+00:00 2026-06-14T05:01:44+00:00

c++ novice here. I’m currently trying to write a program involving templated stacks that

  • 0

c++ novice here. I’m currently trying to write a program involving templated stacks that can handle two separate data types, int and Student objects. While the stack logic of the program works fine, I’m not sure how to go about printing the value at the top of the stack for each data type. Right now I have two possible ideas, overload the ‘<<‘ operator for a class (which one, I’m not sure) or write overloaded functions for TopStack() (i.e. template <> int Stack<int>::TopStack() const). I have the latter implemented, but not correctly. Does anyone know what the most efficient way to accomplish this is? I’m open to any suggestions.

I’ll post the relevant parts of my code so you can see what I’m talking about.

template <class DataType>
struct StackNode
{
    DataType data;                      // data can be of any type
    StackNode<DataType> *next;          // point to the next node
};

template <class DataType>
class Stack
{

private:
    StackNode<DataType> *top;                                 // point to the top node of the stack
    int maxSize;                                              // maximum stack size
    int numNodes;                                             // number of nodes in the stack


public:
    Stack();                                                  // constructor, create a stack with size 10
    ~Stack();                                                 // destructor

    bool isEmpty() const { return (top == 0); }               // check if the stack is empty
    bool isFull() const { return (numNodes == maxSize); }     // check if the stack is full
    void Push(const DataType &elem);                          // push a node onto the top of the stack
    void Pop();                                               // pop a node from the top of the stack
    int TopStack() const;                                // return data from the top of the stack
};

struct Students
    {
        char lastName[20];               // student's last name
        char firstName[20];              // student's first name
        int IDNumber;                    // student ID #
        Students();                      // constructor
        void PrintStudent();             // print a student's information
    };

void Students::PrintStudent()
{
    cout << "\nID# " << this->IDNumber << " - " << this->lastName << ", "
         << this->firstName << endl;
}

// in main() snippet
// if the user asks for top of stack
case 3:
    if (!intStack)            // I use a boolean to switch the stack being accessed
        sstack.TopStack();    // Student stack
    else if (intStack)
        istack.TopStack();    // Int stack
    break;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:01:45+00:00Added an answer on June 14, 2026 at 5:01 am
    int TopStack() const;                                // return data from the top of the stack
    

    should be

    DataType TopStack() const;                                // return data from the top of the stack
    

    because the type of the data varies with the type of the stack.

    Implemented something like this:

    template<typename DataType>
    DataType Stack<DataType>::TopStack() const
    {
       Assert(top != nullptr);
       if (top == nullptr)
         return DataType();
       return top->data;
    }
    

    However, what does your class do that std::stack<T> does not do? Or even std::vector<T> (which is, amusingly enough, usually a better stack than stack).

    The code that is using your Stack<DataType> will know the type of the data, either because of the type of the variable it is talking to, or a template parameter it itself has. Add a operator<< overload for your Student class, so you can print them without jumping through any special hoops.

    If you find overloading operator<< to be scary, you could write a free-standing Print function with int and Student const& overloads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can Scala be used to write GWT applications? (NOTE: Java/Scala novice here...)
Novice Programmer here - I have an application with two threads that I start,
Novice programmer here. I'm writing a program that analyzes the relative spatial locations of
I'm a jquery novice trying to write my first app here and one thing
SQL novice here. I'm trying to generate a costing query that outputs employee time
SQL novice here. I'm trying to get a list of invoice numbers. There are
Web-development novice here. I want to try implementing a simple web site that uses
Programming novice here. I'm trying to allow a user to enter their name, firstName
Novice programmer here. trying to learn static methods and recursion. No idea why I
Java novice here! I'm developing a small game that's just simply moving around the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.