Can someone help me with my c++ code? I am trying to overload the operator== to
ID: 641981 • Letter: C
Question
Can someone help me with my c++ code?
I am trying to overload the operator== to compare the first_name, last_name, and SSN of different customers but im not sure how to access those elements. I also want to overload the operator to print out the customer using an ostream. If someone could feel in some of the holes for me that would be awesome.
#include <iostream>
using namespace std;
class customer
{
string last_name,first_name;
int SSN;
friend void compare_customer(customer x,customer y);
friend void print_customer(customer x);
public:
customer(string first_name, string last_name, int SSN){}
};
void compare_customer(customer x, customer y)
{
cout<<x.first_name;
}
void overload(ostream& os,customer x)
{
}
void print_customer(customer x)
{
cout<<"Customer:"<<endl;
cout<<x.last_name;
}
int main()
{
customer customer1("Brian","Bonds",78765421);
customer customer2("Jim","Smith",987654321);
print_customer(customer1);
compare_customer(customer1, customer2);
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class customer{
private:
string last_name, first_name;
int SSN;
public:
customer(string fn, string ln, int ssn){
first_name = fn;
last_name = ln;
SSN = ssn;
}
bool operator==(const customer &x){
return x.last_name == last_name && x.first_name == first_name && x.SSN == SSN;
}
friend ostream &operator<<(ostream& out, customer &x);
};
ostream &operator<<(ostream& out, customer &x){
out << "Name: " << x.first_name << " " << x.last_name << endl;
out << "SSN: " << x.SSN << endl;
return out;
}
int main(){
customer customer1("Brian","Bonds",78765421);
customer customer2("Jim","Smith",987654321);
cout << "Customer1 " << customer1 << endl;
cout << "Customer2 " << customer2 << endl;
if(customer1 == customer2){
cout << "Both customers are equal" << endl;
}
else{
cout << "Both customers are not equal" << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.