This is correct code from a classmate the professor bragged about and I cant understand why it requires a double constructor i originally only had the first function and couldnt figure out it required two which lagged my progress as a professional
class Studentrecords
{
private:
struct student
{
string name;
string address;
int ID;
double gpa;
};
student *stackArray;
int stackSize;
int top;
public:
Studentrecords();
Studentrecords(int size);
~Studentrecords();
void push(string name, string address, int id, double gpa);
void pop();
bool isFull() const;
bool isEmpty() const;
void display();
};
Studentrecords::Studentrecords(int size)
{
stackArray = new student[size];
top = 0;
}
Studentrecords::Studentrecords()
{
stackSize = 25;
stackArray = new student[stackSize];
top = 0;
}
Studentrecords::~Studentrecords()
{
delete [] stackArray;
}
It doesn’t require two constructors, that’s just how the class is defined. That way, you can create an object in two ways:
which will create a
Studentrecordsobject of size 15, orwhich will call the default constructor, and create an object of type
Studentrecordsand size 25.I must note that this is bad code though:
Studentrecords()constructor can be replaced withStudentrecords(int size = 25)to avoid code duplication.std::vector.