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

时间:2018-10-14 00:07:32

标签: c++ overloading operator-keyword this-pointer

在代码的2个注释部分中,我们得到了以下2个分配:

在此处为运算符<添加运算符重载功能,以允许 两个NEPerson对象的比较。如果第一个 一个人的名字按字母顺序位于第二个人的名字之前。

在此处添加运算符<的运算符重载功能以允许 两个NEStudent对象的比较。仅在以下情况下返回true 第一个学生的GPA小于第二个学生的GPA。

当我编译代码时,我会收到很多错误,所以我很确定自己不知道自己在做什么,这对我来说是很新的。我将不胜感激任何帮助。如果现在还不清楚,我主要需要帮助的是运算符“ <”的重载。

        #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;
    }

1 个答案:

答案 0 :(得分:0)

比您想象的要容易得多–只需比较成员即可。
并且不要忘记bool operator< (const NEPerson &p2) const { return pname < p2.pname; }

{{1}}

(“ pname”是一个奇怪的名称。为什么不使用“ name”?)