Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone comment this code so I can explain it? I wrote the code from the boo

ID: 3825983 • Letter: C

Question

Can someone comment this code so I can explain it? I wrote the code from the book but have been unable to understand how it exactly is working. The book only shows the steps "7.2, 7.3 etc". I wrote some comments but I am still trying to learn this stuff.

Main.cpp

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

#include "stackType.h"

using namespace std;
int main()
{
   //Step 1
   double GPA;
   double highestGPA;
   string name;
   stackType<string> stack(100);
   ifstream infile;
   infile.open("Data.txt"); //Step 2
   if (!infile) //Step 3
   {
       cout << "The input file does not "
           << "exist. Program terminates!"
           << endl;
       return 1;
   }
   cout << fixed << showpoint; //Step 4
   cout << setprecision(2); //Step 4
   infile >> GPA >> name; //Step 5
   highestGPA = GPA; //Step 6
   while (infile) //Step 7
   {
       if (GPA > highestGPA) //Step 7.1
       {
           stack.initializeStack(); //Step 7.1.1
           if (!stack.isFullStack()) //Step 7.1.2
               stack.push(name);
           highestGPA = GPA; //Step 7.1.3
       }
       else if (GPA == highestGPA) //Step 7.2
       if (!stack.isFullStack())
           stack.push(name);
       else
       {
           cout << "Stack overflows. "
               << "Program terminates!"
               << endl;
           return 1; //exit program
       }
       infile >> GPA >> name; //Step 7.3
   }
   cout << "Highest GPA = " << highestGPA
       << endl; //Step 8
   cout << "The students holding the "
       << "highest GPA are:" << endl;
   while (!stack.isEmptyStack()) //Step 9
   {
       cout << stack.top() << endl;
       stack.pop();
   }
   cout << endl;
   system("pause");
}

stackType.h

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>
#include "stackADT.h"
using namespace std;
template <class Type>
class stackType : public stackADT<Type>
{
public:
   const stackType<Type>& operator=(const stackType<Type>&);
   void initializeStack();
   bool isEmptyStack() const;
   bool isFullStack() const;
   void push(const Type& newItem);
   Type top() const;
   void pop();
   stackType(int stackSize = 100);
   stackType(const stackType<Type>& otherStack);
   ~stackType();
private:
   int maxStackSize; //variable to store the maximum stack size
   int stackTop; //variable to point to the top of the stack
   Type *list; //pointer to the array that holds the
   //stack elements
   void copyStack(const stackType<Type>& otherStack);
};
template <class Type>
void stackType<Type>::initializeStack()
{
   stackTop = 0;
}//end initializeStack
template <class Type>
bool stackType<Type>::isEmptyStack() const
{
   return (stackTop == 0);
}//end isEmptyStack
template <class Type>
bool stackType<Type>::isFullStack() const
{
   return (stackTop == maxStackSize);
} //end isFullStack
template <class Type>
void stackType<Type>::push(const Type& newItem)
{
   if (!isFullStack())
   {
       list[stackTop] = newItem; //add newItem to the
       //top of the stack
       stackTop++; //increment stackTop
   }
   else
       cout << "Cannot add to a full stack." << endl;
}//end push
template <class Type>
Type stackType<Type>::top() const
{
   assert(stackTop != 0); //if stack is empty,
   //terminate the program
   return list[stackTop - 1]; //return the element of the
   //stack indicated by
   //stackTop - 1
}//end top
template <class Type>
void stackType<Type>::pop()
{
   if (!isEmptyStack())
       stackTop--; //decrement stackTop
   else
       cout << "Cannot remove from an empty stack." << endl;
}//end pop
template <class Type>
stackType<Type>::stackType(int stackSize)
{
   if (stackSize <= 0)
   {
       cout << "Size of the array to hold the stack must "
           << "be positive." << endl;
       cout << "Creating an array of size 100." << endl;
       maxStackSize = 100;
   }
   else
       maxStackSize = stackSize; //set the stack size to
   //the value specified by
   //the parameter stackSize
   stackTop = 0; //set stackTop to 0
   list = new Type[maxStackSize]; //create the array to
   //hold the stack elements
}//end constructor
template <class Type>
stackType<Type>::~stackType() //destructor
{
   delete[] list; //deallocate the memory occupied
   //by the array
}//end destructor
template <class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
   delete[] list;
   maxStackSize = otherStack.maxStackSize;
   stackTop = otherStack.stackTop;
   list = new Type[maxStackSize];
   //copy otherStack into this stack
   for (int j = 0; j < stackTop; j++)
       list[j] = otherStack.list[j];
} //end copyStack

template <class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
   list = nullptr;
   copyStack(otherStack);
}//end copy constructor
template <class Type>
const stackType<Type>& stackType<Type>::operator=
(const stackType<Type>& otherStack)
{
   if (this != &otherStack) //avoid self-copy
       copyStack(otherStack);
   return *this;
} //end operator=
#endif

stackADT.h

template <class Type>
class stackADT
{
public:
   virtual void initializeStack() = 0;
   virtual bool isEmptyStack() const = 0;
   virtual bool isFullStack() const = 0;
   virtual void push(const Type&) = 0;
   virtual Type top() const = 0;
   virtual void pop() = 0;
};

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "stackType.h"
using namespace std;

int main()
{
//Declration of variable
double GPA;
double highestGPA;
string name;
  
   // stack(100) is object of class stackType
stackType<string> stack(100);
   ifstream infile;
  
   // opening data.txt file using open()
infile.open("Data.txt");
  
if (!infile) // checking of weather data.txt file exist or not
{
cout << "The input file does not "
<< "exist. Program terminates!"
<< endl;
return 1;
}
cout << fixed << showpoint; // to set manipulator for on output screen
cout << setprecision(2); // to set precision for on output screen
infile >> GPA >> name; // data from file is taken to GPA and name variable
highestGPA = GPA; //GPA is assigned to highestGPA
while (infile) //loop continues untill we dont get EOF Character
{
if (GPA > highestGPA) // if GPA is high than highestGPA
{
stack.initializeStack(); // call initializeStack() of stackType class
if (!stack.isFullStack()) // if stack is not full
stack.push(name);
highestGPA = GPA; //GPA is assigned to highestGPA
}
else if (GPA == highestGPA) // if GPA is equal to highestGPA
if (!stack.isFullStack())
stack.push(name);
else                // if GPA is less than highestGPA
{
cout << "Stack overflows. "
<< "Program terminates!"
<< endl;
return 1; //exit program
}
infile >> GPA >> name; //data from file is taken to GPA and name variable
}
cout << "Highest GPA = " << highestGPA
<< endl; //Step 8
cout << "The students holding the "
<< "highest GPA are:" << endl;
while (!stack.isEmptyStack()) // loop continues untill stack dont become empty
{
cout << stack.top() << endl; //print top element of stack
stack.pop(); //pop for stack
}
cout << endl;
system("pause");
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------