Need Help to wirte the code that will Overload the relational operators in the f
ID: 3863881 • Letter: N
Question
Need Help to wirte the code that will Overload the relational operators in the following code:
#ifndef H_personType
#define H_personType
#include <iostream>
#include <string>
using namespace std;
class personType
{
//Overload the stream insertion and extraction operators.
friend istream& operator>>(istream&, personType&);
friend ostream& operator<<(ostream&, const personType&);
public:
const personType& operator=(const personType&);
//Overload the assignment operator.
void setName(string first, string last);
//Function to set firstName and lastName according to
//the parameters.
//Postcondition: firstName = first; lastName = last
string getFirstName() const;
//Function to return the first name.
//Postcondition: The value of firstName is returned.
string getLastName() const;
//Function to return the last name.
//Postcondition: The value of lastName is returned.
personType(string first = "", string last = "");
//constructor with parameters
//Set firstName and lastName according to the parameters.
//Postcondition: firstName = first; lastName = last
//Overload the relational operators.
bool operator==(const personType& right) const;
bool operator!=(const personType& right) const;
bool operator<=(const personType& right) const;
bool operator<(const personType& right) const;
bool operator>=(const personType& right) const;
bool operator>(const personType& right) const;
protected:
string firstName; //variable to store the first name
string lastName; //variable to store the last name
};
#endif
Explanation / Answer
//============ add these functions in you personType.cpp file =====================//
bool personType::operator==(const personType& right) const {
if(firstName.compare(right.firstName)==0 && lastName.compare(right.lastName)==0)
return true;
return false;
}
bool personType::operator!=(const personType& right) const {
if(firstName.compare(right.firstName)!=0 || lastName.compare(right.lastName)!=0)
return true;
return false;
}
bool personType::operator<=(const personType& right) const {
if(firstName.compare(right.firstName)<=0 && lastName.compare(right.lastName)<=0)
return true;
return false;
}
bool personType::operator<(const personType& right) const {
if(firstName.compare(right.firstName)<0 && lastName.compare(right.lastName)<0)
return true;
return false;
}
bool personType::operator>=(const personType& right) const {
if(firstName.compare(right.firstName)>=0 && lastName.compare(right.lastName)>=0)
return true;
return false;
}
bool personType::operator>(const personType& right) const {
if(firstName.compare(right.firstName)>0 && lastName.compare(right.lastName)>0)
return true;
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.