Answer to Question #201126 in C++ for Root

Question #201126

Write a class implementation for a class named PhoneNumber giving the specification below: Data that is associated with this class are:  First name of type string.  Last name of type string.  Phone number of type string. Functions:  Constructor: that initializes any object of type “PhoneNumber”.  Overloaded function for the insertion operator (<<) to print any object of type PhoneNumber.  Overloaded function for extraction operator (>>) to read in for any object of type PhoneNumber all the values of its data members. Write a driver program that test the class as follow:  Declare an object of type “PhoneNumber”, read in its values, and then print it (using operator<<, operator>>).  Declare an array of three objects of type “PhoneNumber”, read in their values and then print their values (using operator<<, operator>>).


1
Expert's answer
2021-05-31T05:39:24-0400
#include <iostream>
#include <string>
using namespace std;
class PhoneNumber{
    string first_name, last_name, phone_number;
    public:
    PhoneNumber(){}
    friend ostream &operator<<(ostream &out, const PhoneNumber &phone){
        out<<"\nFirst Name: "<<phone.first_name<<"\nLast Name: "<<phone.last_name<<"\nPhone Number: "<<phone.phone_number;
        return out;
    }
    friend istream &operator>>(istream &in, PhoneNumber &phone){
        in>>phone.first_name>>phone.last_name>>phone.phone_number;
        return in;
    }
};
int main(){
    PhoneNumber phone;
    cout<<"Enter first name, last name and phone number respectively\n";
    cin>>phone;
    cout<<phone;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog