user12829237
user12829237

Reputation:

beginner questions about class and objects

I'm working on a small program for school that is an intro to using objects. I'm new to class and still trying to wrap my head around it. I'm not really asking how to fix my code, I'm just looking to get a better understanding of things.

In the code below, in my implementation file student.cpp, there is a function called isLessThanByID. It is supposed to compare the current object to the object being passed in. I'm having a hard time understanding what the "current" object is and how the function knows what the current object is. There are two objects s1 and s2. My instructor says assume s1 is the current object and s2 is the passed in object. I asked my instructor how the function knows about s1 if it's not being passed in and she said it's because s1 is an object of class and the function is a class member. This made sense for a minute and then I thought on it more and realized I'm still confused. If s1 doesn't need to be passed in, then why does s2 need to be passed in?

I'm getting an error on that function that says

"error: extra qualification ‘Student::’ on member ‘isLessThanByID’ [-fpermissive] bool Student::isLessThanByID(const Student &s2) const;".

I'm not sure what it means by "extra qualification".

I really appreciate any help in wrapping my head around this! Thank you!

Here's my code:

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 - implementation file


//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- header file

#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

Upvotes: 3

Views: 122

Answers (1)

Daniel Langr
Daniel Langr

Reputation: 23537

How the function knows about s1 if it's not being passed in?

Each member function has an implicit (hidden) first parameter that passes a pointer to the "current" object (available inside that function as this). Imagine that

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

is actually something like

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

Similarly, imagine that the call

s1.isLessThanByID(s2);

is actually

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

Note that there is no s1 inside isLessThanByID available. That object exist outside of the function definition, so, you cannot use it inside. Instead of s1.id just write this->id, or, shortly, just id (which will be interpreted as this->id implicitly in your case).

Upvotes: 1

Related Questions