簡體   English   中英

具有多態數據的鏈接列表

[英]linkedLists with polymorphic data

我正在嘗試制作一個具有 personType、studentType 和professionalType 的程序,其中后兩者是從personType 繼承的。 在同一個程序中,我有一個 nodeType 結構,其中包含一個名為“data”的 personType 變量來保存學生、教授和人員。 然后我想調用他們自己的 print() function 以及我需要區分人、學生和教授。 我在此處看到了如何執行此操作的示例,但是我遇到了一些問題,並且可能做錯了。

Class 和結構代碼:

    class courseType {
    public:
        void setSectionID(string id);
        void setCourseName(string name);
        void setTitle(string tit);
        void setDays(string dys);
        void setTime(string tme);
        void setRoom(string rm);
        void setStatus(bool stat);
        void setCap(int cp);
        string getSectionID() const;
        string getCourseName() const;
        string getTitle() const;
        string getDays() const;
        string getTime() const;
        string getRoom() const;
        bool getStatus() const;
        int getCap() const;
        int getEnrolled() const;
        static int getCensus();
        bool addStudent();
        void print() const;
        courseType& operator= (const courseType &course);

    private:
        void setEnrolled(int enroll);
        string sectionID;
        string courseName;
        string title;
        string days;
        string time;
        string room;
        bool status;
        int cap;
        int enrolled;
        static int census;

};

class personType{

    public:
        personType();
        personType(string first, string last, string addr, double hght, string dob, char gend);
        personType(string first, string last);
        personType(personType &other);
        ~personType();
        string getFirstName() const;
        string getLastName() const;
        string getAddress() const;
        string getDOB() const;
        double getHeight() const;
        char getGender() const;
        void setFirstName(string first);
        void setLastName(string last);
        void setAddress(string addr);
        void setDOB(string dob);
        void setHeight(double hght);
        void setGender(char gend);
        virtual void print() const;
        bool equals(const personType &other) const;
        personType& operator= (const personType &person);

    private:
        char gender;
        string DOB;
        double height;
        string address;
        string fName;
        string lName;

};

class studentType: public personType{

    public:
        studentType();
        studentType(string first, string last);
        studentType(string first, string last, double gpa, string classification, string id); 
        ~studentType();
        void setGPA(double gpa);
        void setID(string id);
        void setClassification(string classification);
        void setCoursesEnrolled(int num);
        double getGPA() const;
        string getID() const;
        string getClassification() const; 
        int getCoursesEnrolled() const;
        void print() const;
        bool equals(const studentType &other) const;
        bool addCourse(courseType *course);
        studentType& operator= (const studentType &student);

    private:
        string ID; //added every id 'should' be unique
        double GPA;
        string Classification;
        courseType *courses[3];
        int coursesEnrolled;

};

class professorType: public personType{
    public:
        professorType();
        professorType(string first, string last);
        professorType(string first, string last, string addr, double hght, string dob, char gend, string id, string depar, string deg);
        ~professorType();
        void setEmployeeID(string id);
        void setDepartment(string depar);
        void setDegree(string deg);
        void setCoursesTaught(int courses);
        string getEmployeeID() const;
        string getDepartment() const;
        string getDegree() const;
        int getCoursesTaught() const;
        void print() const;
        bool equals(const professorType &other) const;
        bool assignCourse(courseType *course);
        professorType& operator= (const professorType &professor);

    private:
        string employeeID;
        string department;
        string degree;
        courseType *courses[5];
        int coursesTaught;

};

struct nodeType{
    personType data;
    courseType data2;
    nodeType *next;
};

const int maxInt = 2147483647;

void addCourse(int &courseCount, nodeType *&head, nodeType *&tail);
void addPersonType(int &personCount, int &studentCount, int &professorCount, nodeType *&head, nodeType *&tail);
void addStudent(int &studentCount, int &personCount, nodeType *&head, nodeType *&tail);
void addProfessor(int &professorCount, int &personCount, nodeType *&head, nodeType *&tail);
void addPerson(int &personCount, nodeType *&head, nodeType *&tail);

int lookUpStudent(int personCount, int studentCount, nodeType *head, nodeType *tail);
int lookUpProfessor(int personCount, int professorCount, nodeType *head, nodeType *tail);
int lookUpCourse(int courseCount, nodeType *head, nodeType *tail);

string makeLower(string a);

void initializeList(nodeType *& head, nodeType *&tail, int &count);
bool isEmptyList(const nodeType *head);
void printPersonList(nodeType *head);
void printCourseList(nodeType *head);
int lengthList(nodeType *head);
void destroyList(nodeType *&head, nodeType *&tail, int &count);
void insertPersonFirst(personType &newItem, nodeType *&head, nodeType *&tail, int &count);
void insertPersonLast(personType &newItem, nodeType *&head, nodeType *&tail, int &count);
void insertCourseFirst(courseType &newItem, nodeType *&head, nodeType *&tail, int &count);
void insertCourseLast(courseType &newItem, nodeType *&head, nodeType *&tail, int &count);
nodeType* getNode(nodeType *head, int count, int index);

實例:

template<typename Base, typename T>
inline bool instanceof(const T *ptr) {
    return dynamic_cast<const Base*>(ptr) != nullptr;
}

insert 和 addStudent 函數(addProfessor 基本相同,所以不包括在內):

studentType newStudent;
newStudent.setFirstName(First);
newStudent.setLastName(Last);
newStudent.setGender(Gender);
newStudent.setDOB(DOB);
newStudent.setAddress(Address);
newStudent.setID(studentID);
newStudent.setClassification(Classification);
newStudent.setHeight(Height);
newStudent.setGPA(GPA);
insertPersonLast(newStudent, head, tail, personCount);

void insertPersonLast(personType &newItem, nodeType *&head, nodeType *&tail, int &count){
    nodeType *newNode = new nodeType;
    newNode->data = newItem;
    newNode->next = NULL;
    if (head == NULL) {
        head = newNode;
        tail = newNode;
    }else {
        tail->next = newNode;
        tail = newNode;
    }
    count++;
}

我如何檢查它的學生類型:

personType *holderPtr;
holderPtr = &(getNode(head, personCount, currentIndex)->data);
cout << "Arr[" << currentIndex << "] is studentType: " << instanceof<studentType>(holderPtr) << endl

我希望這是有道理的。 摘要:我有一個名為 addStudent() 的 function 在這里列出,它添加了一個使用 insertPersonLast() 插入列表的學生和一個 function ,它通過鏈接列表查找學生,但是上面的 cout 我得到了所有錯誤的。 此外,當調用我的鏈表打印 function 時,它只打印基本 class print(),即使它是學生。 任何幫助,將不勝感激!

關於 print() function 不調用派生:

你的行newNode->data = newItem; 按值復制。 也許您想在其中保留指向personType的指針,而不是實例。

data未定義為指針。 所以它永遠不能存儲派生類型,這將具有比要放置在那里的項目更大的表示。 多態性和按值復制不能一起工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM