【问题标题】:beginner questions about class and objects关于类和对象的初学者问题
【发布时间】:2020-04-25 16:52:54
【问题描述】:

我正在为学校编写一个小程序,介绍如何使用对象。我是新来的班级,但仍在努力解决它。我并不是真的在问如何修复我的代码,我只是想更好地理解事物。

在下面的代码中,在我的实现文件 student.cpp 中,有一个名为isLessThanByID 的函数。它应该将当前对象与传入的对象进行比较。我很难理解“当前”对象是什么以及函数如何知道当前对象是什么。有两个对象 s1 和 s2。我的导师说假设 s1 是当前对象,而 s2 是传入的对象。我问我的导师,如果函数没有被传入,它是如何知道 s1 的,她说这是因为 s1 是类的对象,而函数是类成员。这有点道理,然后我想了更多,意识到我仍然很困惑。如果 s1 不需要传入,那为什么 s2 需要传入呢?

我在那个函数上收到一个错误,上面写着

“错误:成员‘isLessThanByID’上的额外资格‘Student::’ [-fpermissive] bool Student::isLessThanByID(const Student &s2) const;”。

我不确定“额外资格”是什么意思。

我非常感谢任何帮助我解决这个问题!谢谢!

这是我的代码:

app.cpp


const double minGpaForHonor = 3.75;

int main()
{
        Student s1("G10", 3.9);
        Student s2("G20", 3.5);


        s1.print();
        s2.print();

        //write code to test Student::isLessThanByID
        s1.isLessThanByID(s2);

        if (true)
                 cout << "s2 is less than s1" << endl;

        else
                cout << "s1 is less than s2" << endl;

    //write code to test Student::qualifyForHonor
    s1.qualifyForHonor(minGpaForHonor);
        if (true)
            cout << "s1 qualifies for honors" << endl;

    s2.qualifyForHonor(minGpaForHonor);
        if (true)
            cout << "s2 qualifies for honors" << endl;

        return 0;
}

student.cpp - 实现文件


//implement the required 3 functions here

Student::Student(const char initId[], double gpa)
{
        //initialize ID
        for (int i = 0; i < strlen(initId); i++)
        {
        id[i] = initId[i];
    }

        gpa = gpa;
        //initialize gpa
}

bool Student::isLessThanByID(const Student &s2) const
{
        //compare current student object to passed in object}
        if (strcmp(s1.id, s2.id) < 0)
                return true;

        else
                return false;

}

bool Student::qualifyForHonor(double minGpaForHonor) const
{
//return true if gpa is higher than "minGpaForHonor"
        if(gpa >= minGpaForHonor)
                return true;
}
void Student::print() const
{
  cout << id << '\t' << gpa << endl;
}

student.h-头文件

#define STUDENT_H

#include <iostream>
#include <cstring>

using namespace std;

class Student
{
public:
        Student(const char initId[], double gpa);
        bool Student::isLessThanByID(const Student &s2) const;
        bool qualifyForHonor(double minGpaForHonor) const;
        void print()const;
private:
        const static int MAX_CHAR = 100;
        char    id[MAX_CHAR];
        double  gpa;

};
#endif

【问题讨论】:

  • if (true) do_something 将始终为 do_something,因为 true 始终为真。我认为您需要更多地学习有关函数的知识。并确保您承诺返回值的函数始终有效。
  • ... 只需从 .h 中的函数定义中删除 Student::
  • 要么你的导师不知道事情是如何运作的,要么你们相互误解了。
  • 我肯定我误会了她。

标签: c++


【解决方案1】:

如果没有传入,函数如何知道s1

每个成员函数都有一个隐含的(隐藏的)第一个参数,它传递一个指向“当前”对象的指针(在该函数中以this 的形式提供)。想象一下

bool Student::isLessThanByID(const Student &s2) const
{
   return strcmp( /* s1. */ id, s2.id) < 0;
}

其实是这样的

bool Student::isLessThanByID(const Student* this, const Student &s2) // not real code
{
   return strcmp(this->id, s2.id);
}

同样,想象一下调用

s1.isLessThanByID(s2);

其实是

Student::isLessThanByID(&s1, s2); // not real code

请注意,isLessThanByID 内没有可用的s1。该对象存在于函数定义之外,因此您不能在内部使用它。而不是s1.id,只写this-&gt;id,或者,简单地说,只是id(在你的情况下,这将被隐式解释为this-&gt;id)。

【讨论】:

  • 感谢您的周到回复。不过,我仍然被困住了。我们还没有了解这个->,所以我不被允许使用它。另一个答案是只传入两个对象吗?
  • @newGirl 你根本不需要this。我只是试图说明成员函数是如何工作的——s1 实际上是通过隐式 this 参数传入的,s2 需要通过显式参数传递。因此,是的,两个对象都需要传入,但方式不同。
猜你喜欢
  • 2021-03-04
  • 2011-01-12
  • 2018-01-08
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
相关资源
最近更新 更多