[December] 5.
Greetings and salutations!
My first visit here a few weeks ago was very helpful and friendly thanks to dkalita.
I’ve been moving forward on my own since then, but it appears I have run into another dilemma.
I have a short program listed below. It compiles fine.
The problem is that I would like the program to do one more thing, but I am at a loss on how to make it happen.
Here is the problem: I have an overloaded equality operator member function for the Student class. It currently does a fine job comparing the “myStudentId” data member of the two Student objects in the program. When I compare these two student objects, I would like it to compare the “myName” strings that are inherited from the Person class as well. Will someone please show me how I can compare “myName” & “myStudentId” of two Student objects in one fell swoop?
I have an overload equality function in the Person class, but it is currently empty.
Thank you!
// William Byrd II
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(const string name)
{
myName = name;
}
bool operator==(const Person& p) const;
private:
string myName;
};
// declares Student class as publicly inheriting from the Person class
class Student : public Person
{
public:
// implemented constructor for the Student class
Student(const string name,
const int studentId)
: Person( name )
{
myStudentId = studentId;
}
// implemented overloaded equality operator Student class member function
bool operator==(const Student& s) const
{
if ( myStudentId == s.myStudentId )
return true;
else
return false;
}
private:
int myStudentId;
};
int main()
{
Student app1( "William Byrd II", 8765309 );
Student app2( "William Byrd II", 8765309 );
// test overloaded == operator for Student class
cout << "app1" << ( ( app1 == app2 )
? " == " : " != " ) << "app2"
<< " according to the overloaded == operator\n";
system("pause");
return 0;
}