Operation Overloading

How do I overload the function so that Correct appears?

Student constructor is firstName and lastName.


I've been looking up operator overloading and it's mostly been + and haven't really found anything relating to this.


1
2
3
4
5
6
7
  Student s1("a","b");
   Student s2("x","y");
   if (s1<s2) {
    cout<<"Correct: s1 is less than s2"<<endl;
   } else {
    cout<<"Error: s1 is not less than s2"<<endl;
   }.
Assuming firstName, lastName are type std::string, you need to overload the operator < for Student:
1
2
3
4
5
//sorting by lastName
bool operator < (const Student& lhs, const Student& rhs)
{
    return lhs.lastName < rhs.lastName;// operator < already overloaded for std::string
}
Topic archived. No new replies allowed.