Purchase Solution

C++ stack class with inheritance storing integers, strings

Not what you're looking for?

Ask Custom Question

Extend the improved stack class stack2 so that the derived class stack would be also capable to store integers, strings, or pairs of integers. Model the pair by a structure with two integer-valued fields x and y.

In the main program, define several integers, strings, and pairs and demonstrate pushing them to and popping from your stack.

Attachments
Purchase this Solution

Solution Summary

This solution provides a simple stack class that will store integers, strings and pairs of numbers. The program involves inheritance, where a more complicated stack2 class is inherited from base stack class.

Solution Preview

Attached is C++ file plus an image of the results produced by the program. I have defined structure "Pair" to store pairs of integers (x,y). This is a simple realization of the stack and that in this type of stack it is a requirement that the pop command sequence is exact opposite of push command sequence. That is to say, if you pushed 1,"ABC",{1,2} you must pop {1,2},"ABC",1.

#include <iostream>
using namespace std;

class stack // a simple stack class
{
protected:
int top; // stack pointer
static const int MAX = 10; // stack size
private:
int st[MAX]; // stack array
public:
stack() // default constructor
{
top = -1;
}
void push(int n) // push method
{
st[++top] = n;
}
int pop() // pop method
{
return(st[top--]);
...

Purchase this Solution


Free BrainMass Quizzes
Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

Javscript Basics

Quiz on basics of javascript programming language.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.