Question: Please help me to find the problem for c + + thank you. I wrote the code out but it says in my user.cpp and

Please help me to find the problem for c++ thank you.
I wrote the code out but it says in my user.cpp and main.cpp have some mistakes. Please help me fix it thank you
The Question: Implement THE Operator Overloading for class User
1) Complete the implementation of the operator overloading for your user.h and user.cpp.
bool operator==(const User&) const;
bool operator!=(const User&) const;
bool operator>(const User&) const;
bool operator<(const User&) const;
bool operator>=(const User&) const;
bool operator<=(const User&) const;
2) Implement the istream and ostream for your user.h and user.cpp
friend istream& operator >>(istream& in, User& user);
friend ostream& operator<<(ostream& out, User& user);
my answere:
user.h
#include
using namespace std;
class User {
public:
bool operator==(const User&) const;
bool operator!=(const User&) const;
bool operator>(const User&) const;
bool operator<(const User&) const;
bool operator>=(const User&) const;
bool operator<=(const User&) const;
friend std::istream& operator>>(std::istream& in, User& user);
friend std::ostream& operator<<(std::ostream& out, const User& user);
private:
std::string name;
int age;
};
user.cpp
#include "user.h"
User::User(const std::string& name, int age) : name(name), age(age){}
bool User::operator==(const User& other) const {
return name == other.name && age == other.age;
}
bool User::operator!=(const User& other) const {
return !(*this == other);
}
bool User::operator>(const User& other) const {
return age > other.age ||(age == other.age && name > other.name);
}
bool User::operator<(const User& other) const {
return age < other.age ||(age == other.age && name < other.name);
}
bool User::operator>=(const User& other) const {
return !(*this < other);
}
bool User::operator<=(const User& other) const {
return !(*this > other);
}
std::istream& operator>>(std::istream& in, User& user){
std::string name;
int age;
in >> name >> age;
if (in){
user = User(name, age);
}
return in;
}
std::ostream& operator<<(std::ostream& out, const User& user){
out << user.name <<""<< user.age;
return out;
}
main.cpp
#include
#include "user.h"
int main(){
User user1("Eason",21);
User user2("jason",20);
std::cout << "User1: "<< user1<< std::endl;
std::cout << "User2: "<< user2<< std::endl;
if (user1== user2){
std::cout << "User1 and User2 are equal." << std::endl;
}
else if (user1> user2){
std::cout << "User1 is older than User2."<< std::endl;
}
else {
std::cout << "User1 is younger than User2."<< std::endl;
}
std::cout << "Enter a new user: ";
User newUser;
std::cin >> newUser;
std::cout << "New user: "<< newUser << std::endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!