Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
Question
Book Icon
Chapter 16, Problem 5PP
Program Plan Intro

  • Include required library files.
  • Define a class named “StackOverflowException”.
    • Inside the access specifier “public”,
      • Define a constructor to assign the message
      • Declare a parameterized constructor to assign “msg” to message.
      • Define a function “display” to return message.
    • Inside the access specifier “private”.
      • Declare a string variable “message”.
  • Define a class named “StackEmptyException”.
    • Inside the access specifier “public”,
      • Define a constructor to assign the message
      • Declare a parameterized constructor to assign “msg” to message.
      • Define a function “display” to return message.
    • Inside the access specifier “private”.
      • Declare a string variable “message”.
  • Define a class named “Stack”.
    • Declare an integer array and variable.
    • Inside the access specifier “public”,
      • Define a constructor to assign “-1” to “top”.
      • Define a “push()” function.
        • “try” block to check the top is equal to “3”.
          • The condition is true, throw exception.
          • Otherwise increment the top and the value is assigned to stack.
        • “catch” block to display the error message.
      •  Define a “pop()” function.
        • “try” block to check the top is equal to “-1”.
          • The condition is true, throw exception.
        • Otherwise decrement the top and return the value.
        • “catch” block to display the error message.
  • Define a “main()” function.
    • Create an object for class “Stack”.
    • Then check the “push()” and “pop()” function.

Blurred answer
Students have asked these similar questions
stacktype.h stacktype.cPP #ifndef STACKTYPE_H_INCLUDED tdefine STACKTYPE H_INCLUDED #include "StackType.h" template StackType::StackType () const int MAX_ITEMS - 5; top - -1; class Fulistack // Exception class thrown // by Push when stack is full. template bool StackType: :IsEmpty () class EmptyStack // Exception class thrown // by Pop and Top when stack is emtpy. return (top = -1); template bool StackType: :ISFull () template class StackType return (top -- MAX_ITEMS-1): template void StackType: :Push (ItemType newItem) public: StackType () : bool IsFull (): bool IsEmpty (): void Push (ItemType); void Pop () i if( IsFull ()) throw Fullstack (); top++: items (top]- newItem; template void StackType:: Pop () ItemType Top (): private: int top: ItemType items [MAX_ITEMS ]; if( IsEmpty ()) throw EmptyStack (); top-- }; tendif // STACKTYPE_H_INCLUDED template ItemType StackType::Top () if (IsEmpty ()) throw Emptystack (): return items [top]; Generate the driver file (main.cpp) where…
2. Write an application utilizing JAVA-ready Stack class and perform the following. Create a new instance of the Stack of type double. Use a loop to get 5 input from the user and insert into the stack created. The inputs are 3.4, 10.1, 10.3, 11.5, 3.5. Write a statement to display the content of the stack. Perform pop() all elements and display on the screen if the number is greater than 5.0. Use the output sample given to guide your code. Sample output: [3.4, 10.1, 10.3, 11.5, 3.5] 11.5 10.3 10.1
Stack: Stacks are a type of container with LIFO (Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only. Your Stack should not be of the fixed sized. It should be able to grow itself. So using the class made in task 1, make a class named as Stack, having following additional functionalities: bool empty() : Returns whether the Stack is empty or not. Time Complexity should be: O(1) bool full() : Returns whether the Stack is full or not. Time Complexity should be: O(1)int size() : Returns the current size of the Stack. Time Complexity should be: O(1)Type top () : Returns the last element of the Stack. Time Complexity should be: O(1) void push(Type) : Adds the element of type Type at the top of the stack. Time Complexity    should be: O(1) Type pop() : Deletes the top most element of the stack and returns it. Time Complexity    should be: O(1) Write non-parameterized constructor for the above class. Write Copy…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning