Dav*_*zar 0 c++ constructor class
这是从同学教授吹嘘正确的代码,我无法理解为什么它需要一个双重构造出我需要的是其中两个滞后我的进步作为一个专业的原本只有第一功能和想不出图
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;
}
它不需要两个构造函数,这就是类的定义方式.这样,您可以通过两种方式创建对象:
Studentrecords s(15);
这将创建一个Studentrecords大小为15 的对象,或
Studentrecords s;
它将调用默认构造函数,并创建一个类型Studentrecords和大小为25 的对象.
我必须注意到这是不好的代码:
Studentrecords()可以替换默认构造函数Studentrecords(int size = 25)以避免代码重复.std::vector.