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>>).
#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;
}
Comments
Leave a comment