简体   繁体   English

类的函数重载时应该传递什么参数

[英]What arguments are you supposed to pass when function overloading for a class

There are 2 commented sections of code where we were given the following 2 assignments: 在代码的2个注释部分中,我们得到了以下2个分配:

Add here an operator overloading function for operator < to allow a comparison of two NEPerson objects. 在此处添加运算符<的运算符重载函数,以允许比较两个NEPerson对象。 It will return true if the first person's name alphabetically precedes the second person's name. 如果第一人称名字按字母顺序位于第二人称名字之前,它将返回true。

Add here an operator overloading function for operator < to allow a comparison of two NEStudent objects. 在此处添加运算符<的运算符重载函数,以允许比较两个NEStudent对象。 It will return true only if the first student's GPA is less than the second student's GPA. 仅当第一个学生的GPA小于第二个学生的GPA时,它才会返回true。

When I compile the code I receive a WALL of errors so I'm pretty sure I just have no idea what I'm doing this is all fairly new to me. 当我编译代码时,我会收到很多错误消息,所以我很确定自己不知道自己在做什么,这对我来说是相当新的。 I would appreciate any help. 我将不胜感激任何帮助。 If it wasn't clear already what I mainly need help with here is the operator overloading of "<". 如果现在还不清楚,我主要需要帮助的是运算符“ <”的重载。

        #include <iostream>
        #include <string>
        using namespace std;

        class NEPerson {
        private:
                int id;
                string pname;
                string address;
        public:
                NEPerson(int d, string n, string a)
                { id = d;  pname = n; address = a;}
                virtual void printInfo() {
                   cout << "Name: " << pname;
                   cout << "\t ID: " << id;
                   cout << "\t Address: " << address << endl;
        }

        // Q1
        bool operator< (const NEPerson &p2) const
{
    return pname < p2.pname;
}

        };

        class NEStudent : public NEPerson {
        private:
                string major;
                float gpa;
                int CurCoursesNum;
                string* CurEnrolledCourses;
        public:
                NEStudent(int d, string n, string a, string m, float g, int cnum):NEPerson(d, n, a)
        { major = m; gpa = g; CurCoursesNum = cnum; }
                void addEnrolledCourses() {
                        CurEnrolledCourses = new string[CurCoursesNum];
                        cout << "Enter the code of " << CurCoursesNum;
                        cout << " course(s) for the student:\n";
                        for (int i = 0; i < CurCoursesNum; i++) cin >> CurEnrolledCourses[i];            }

                virtual void printInfo() {
                        NEPerson::printInfo();
                        cout << "\t Student Major: " << major;
                        cout << "\t GPA = " << gpa;
    cout << "\n\t Taking Courses:\n";
                    for (int i = 0; i < CurCoursesNum; i++)
                        cout << "\t   " << CurEnrolledCourses[i] << endl;
                     }
    // question 2 here asks the same as question 1 but for student.
   bool operator< (const NEStudent &s2) const
{
    return gpa < s2.gpa;
}

    ~NEStudent()
        { if (!CurEnrolledCourses) delete[] CurEnrolledCourses; }
    };


    class NETeacher : public NEPerson {
    private:
            string rank;
            string department;
            int CurCoursesLoad;
            string* CurCoursesTeaching;
    public:
            NETeacher(int d, string n, string a, string r, string p, int cnum) :NEPerson(d, n, a)
    { rank = r; department = p; CurCoursesLoad = cnum;}
            virtual void printInfo() {
             NEPerson::printInfo();
             cout << "\t Teacher Rank: " << rank;
             cout << "\t Department = " << department;
             cout << "\n\t Teaching Courses:\n";
             for (int i = 0; i < CurCoursesLoad; i++)
             cout << "\t   " << CurCoursesTeaching[i] << endl;
            }

            void addCoursesTeaching() {
                CurCoursesTeaching = new string[CurCoursesLoad];
                cout << "Enter the code of " << CurCoursesLoad;
                cout << " course(s) for the teacher:\n";
                        for (int i = 0; i < CurCoursesLoad; i++)
                             cin >> CurCoursesTeaching[i]; }
    ~NETeacher()
    { if (!CurCoursesTeaching) delete[] CurCoursesTeaching; }
    };
    //A template function that prints the info of two objects based on their < relation.
    template <class T>
    void PrintOrdered(T &v1, T &v2) {
            if (v1 < v2) { v1.printInfo(); v2.printInfo(); }
            else { v2.printInfo(); v1.printInfo(); }
    }

    int main() {
      NEStudent s1(111, "John", "Boston", "CE", 3.2, 2);
      NEStudent s2(222, "Emily", "Cambridge", "CS", 3.6, 3);
      NETeacher t1(888, "Adam", "Dedham", "Assistant Professor", "EECE", 3);
      NETeacher t2(999, "Mary", "Foxboro", "Associate Professor", "CS", 2);

      s1.addEnrolledCourses(); s2.addEnrolledCourses();

      t1.addCoursesTeaching(); t2.addCoursesTeaching();

      NEPerson* ps1 = &s1;
      NEPerson* ps2 = &s2;
      NEPerson* pt1 = &t1;
      NEPerson* pt2 = &t2;

      ps1‐>printInfo();
      ps2‐>printInfo();
      pt1‐>printInfo();
      pt2‐>printInfo();

      cout << "\n Students ordered by GPA (Ascending):\n";
      PrintOrdered(s1, s2);
      cout << "\n Teachers ordered alphabetically (Ascending):\n";
      PrintOrdered(t1, t2);
      cout << "\n Students ordered alphabetically:\n";

    //    PrintOrdered(???, ???);
    return 0;
    }

It's much easier than you think – just compare the members. 这比您想像的要容易得多,只需比较成员即可。
And don't forget the const . 并且不要忘记const

bool operator< (const NEPerson &p2) const
{
    return pname < p2.pname;
}

("pname" is a strange name. Why not "name"?) (“ pname”是一个奇怪的名称。为什么不使用“ name”?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM