how can I place a student object that holds information such as name, test avera
ID: 3859084 • Letter: H
Question
how can I place a student object that holds information such as name, test average, address, and Id onto a stack? Here is my code so far for my student class and StackStack header
#ifndef STATICSTACK_H_
#define STATICSTACK_H_
#include "Student3.h"
using namespace std;
class staticStack
{
private:
int *stackArray;
public:
int stackSize;
int top;
//Constructor
staticStack(int);
// Copy constructor
staticStack(const staticStack &);
//Destructor
~staticStack();
//stack operations
void push (student);
void pop (int &);
bool isFull() const;
bool isEmpty() const;
};
staticStack::staticStack(int size){
stackArray = new int[size];
stackSize = size;
top = -1;
}
staticStack::staticStack(const staticStack &obj){
//create the stack array
if(obj.stackSize > 0)
stackArray = new int[obj.stackSize];
else
stackArray = nullptr;
//copy the stackSize atribute.
stackSize = obj.stackSize;
//Copythe stack contents
for (int count = 0; count < stackSize; count++){
stackArray[count] = obj.stackArray[count];
}
//set the top of the stack
top = obj.top;
}
//destructor
staticStack::~staticStack(){
delete [] stackArray;
}
void staticStack::push(student obj){//??
if (isFull()){
cout << "The stack is full. ";
}
else{
top++;
stackArray[top]= obj ; //??
}
}
void staticStack::pop(int &num){
if (isEmpty()){
cout << "The stack is empty. ";
}
else{
num = stackArray[top];
top--;
}
}
bool staticStack::isFull() const {
bool status;
if (top == stackSize - 1){
status = true;
}else{
status = false;
}
return status;
}
bool staticStack::isEmpty() const{
bool status;
if(top == -1){
status = true;
}else{
status = false;
}
return status;
}
#endif /* STATICSTACK_H_ */
////////////////////////////////////////////////
StaticStack
#ifndef STATICSTACK_H_
#define STATICSTACK_H_
#include "Student3.h"
using namespace std;
class staticStack
{
private:
int *stackArray;
public:
int stackSize;
int top;
//Constructor
staticStack(int);
// Copy constructor
staticStack(const staticStack &);
//Destructor
~staticStack();
//stack operations
void push (student);
void pop (int &);
bool isFull() const;
bool isEmpty() const;
};
staticStack::staticStack(int size){
stackArray = new int[size];
stackSize = size;
top = -1;
}
staticStack::staticStack(const staticStack &obj){
//create the stack array
if(obj.stackSize > 0)
stackArray = new int[obj.stackSize];
else
stackArray = nullptr;
//copy the stackSize atribute.
stackSize = obj.stackSize;
//Copythe stack contents
for (int count = 0; count < stackSize; count++){
stackArray[count] = obj.stackArray[count];
}
//set the top of the stack
top = obj.top;
}
//destructor
staticStack::~staticStack(){
delete [] stackArray;
}
void staticStack::push(student obj){//??
if (isFull()){
cout << "The stack is full. ";
}
else{
top++;
stackArray[top]= obj ; //??
}
}
void staticStack::pop(int &num){
if (isEmpty()){
cout << "The stack is empty. ";
}
else{
num = stackArray[top];
top--;
}
}
bool staticStack::isFull() const {
bool status;
if (top == stackSize - 1){
status = true;
}else{
status = false;
}
return status;
}
bool staticStack::isEmpty() const{
bool status;
if(top == -1){
status = true;
}else{
status = false;
}
return status;
}
#endif /* STATICSTACK_H_ */
Explanation / Answer
//create student class with these members
class student
{
private:
string name, address;
int id;
float average;
public:
// parameterized constructor
student(string n, sring addr, int i, float avg)
{
name=n;
address=addr;
id=i;
average=avg;
}
// getters
int getId()
{
return id;
}
float getAverage()
{
return average;
}
string getName()
{
return name;
}
string getAddress()
{
return address;
}
}
//Then you can use this object of thil class to push on stack
//code for main function
// Include all header files first
void main()
{
// create student objects
student s1("Alebex", "Florida",1,87.5);
student s2("Morgan", "UK",2,89.7);
// create stack object with 10 elements
staticStack stack(10);
// push students on stack
stack.push(s1);
stack.push(s2);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.