Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this problem, I have to use overloading to edit a c++ class I have used befo

ID: 3713112 • Letter: F

Question

For this problem, I have to use overloading to edit a c++ class I have used before.   

first, overload << and >> operators for the class you have written that has ouput and input functions. The << and >> will behave the same as the output and input member functions. Those operators must be overloaded as friends. Change every use of the output and input functions to use these operators.

second. Overload <, >, ==, !=, <=, >= operators as members in the class Contact. The comparison should be done in alphabetically order on the last name and then first name of the contact.

THE CLASS CODE IS AS FOLLOWED:

#include <iostream>

using namespace std;

class Contact {

private:

        string last, first, address, email;

        string phone;

public:

        Contact();

        Contact(string l, string f, string a, string p, string e);

        string getLast();

        string getFirst();

        string getAddress();

        string getPhone();

        string getEmail();

        void setLast(string l);

        void setFirst(string f);

        void setAddress(string a);

        void setPhone(string p);

        void setEmail(string e);

        void input();

        void output();

};

string Contact::getLast() {

        return last;

Explanation / Answer

#include <iostream>
using namespace std;

class Contact {
private:
        string last, first, address, email;
        string phone;
public:
        Contact();
        Contact(string l, string f, string a, string p, string e);
        string getLast();
        string getFirst();
        string getAddress();
        string getPhone();
        string getEmail();
        void setLast(string l);
        void setFirst(string f);
        void setAddress(string a);
        void setPhone(string p);
        void setEmail(string e);
        friend std::ostream& operator<<( std::ostream &output, const Contact &C );
        friend std::istream& operator>>( std::istream &input, Contact &C );
        bool operator ==(Contact obj);
        bool operator !=(Contact obj);
        bool operator >=(Contact obj);
        bool operator <=(Contact obj);
        bool operator >(Contact obj);
        bool operator <(Contact obj);
};
Contact::Contact() {
        setLast("Default");
        setFirst("Default");
        setAddress("Default");
        setPhone("1234567890");
        setEmail("default@company.com");
}
Contact::Contact(string l, string f, string a, string p, string e) {
        setLast(l);
        setFirst(f);
        setAddress(a);
        setPhone(p);
        setEmail(e);
}
string Contact::getLast() {
        return last;
}
string Contact::getFirst() {
        return first;
}
string Contact::getAddress() {
        return address;
}
string Contact::getPhone() {
        return phone;
}
string Contact::getEmail() {
        return email;
}
void Contact::setLast(string l) {
        last = l;
}
void Contact::setFirst(string f) {
        first = f;
}
void Contact::setAddress(string a) {
        address = a;
}
void Contact::setPhone(string p) {
        if (p.length() != 10) {
                cout << "Invalid phone number" << endl;
                exit(1);
        }
        phone = p;
}
void Contact::setEmail(string e) {
if (e.find("@") == (int) -1) {
                cout << "Invalid email" << endl;
                exit(1);
        }
        email = e;
}

     std::ostream& operator<<( std::ostream &output, const Contact &C ) {
         output << C.last << " " << C.first<<" "<<C.address<<" "<<C.email<<" "<<C.phone;
         return output;           
      }
     std::istream& operator>>( std::istream &input, Contact &C ) {
         input >> C.last >> C.first>>C.address>>C.email>>C.phone;
         return input;
     }
    
     bool Contact::operator ==(Contact obj)
     {
     if(this->last == obj.last && this->first == obj.first)
     return true;
     else
     return false;
     }
     bool Contact::operator !=(Contact obj)
     {
     if(this->last != obj.last || this->first != obj.first)
     return true;
     else
     return false;
     }
     bool Contact::operator >=(Contact obj)
     {
     if(this->last >= obj.last && this->first >= obj.first)
     return true;
     else
     return false;
     }
     bool Contact::operator <=(Contact obj)
     {
     if(this->last <= obj.last && this->first <= obj.first)
     return true;
     else
     return false;
     }
     bool Contact::operator >(Contact obj)
     {
     if(this->last > obj.last || this->first > obj.first)
     return true;
     else
     return false;
     }
     bool Contact::operator <(Contact obj)
     {
     if(this->last < obj.last || this->first < obj.first)
     return true;
     else
     return false;
     }
int main () {       
        Contact c1; // default constructor
       Contact c2("Doe", "Jane", "123 Elm Street", "7189974444",
                       "jane@somewhere.com");

cout<<"Enter a new contact with last name,first name,address,email,phone";
       cin>>c1;
        cout<<c1;
       
        if(c1== c2)
        cout<<" Both contacts are same";
       
        if(c1 > c2)
        cout<<" c1 comes first alphabetically than c2";
       
return 0;
}

Output:

Enter a new contact with last name,first name,address,email,phone

Doe Jane 123ElmStreet 7189974444 jane@somewhere.com

Doe Jane 123ElmStreet 7189974444 jane@somewhere.com
Both contacts are same

Do ask if any doubt. Please upvote.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote