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

Using the PhoneData class below, overload the ostream operator (<<). You need to

ID: 3627838 • Letter: U

Question

Using the PhoneData class below, overload the ostream operator (<<). You need to make this a friend function of the PhoneData class.

For example, in your PhoneData header file under the public section put this:
friend ostream& operator<<(ostream&, const PhoneData&);
Then define it in the implementation file.

To test your class create a main file that does the following:

Creates 2 PhoneData objects and assigns values to data members.
For example, when creating your objects do the following:

PhoneData pd1("Susan Meyers", "414 444-4444");
PhoneData pd2("Joy Rodgers", "888 888-8888");


Then use the overloaded ostream function to display the values. For example:
cout << pd1;
cout << pd2;

Attached is an example of the programs output.

Files to turn in:

PhoneData.h // phoneData header file
PhoneData.cpp // phoneData implementation file
PhoneDataMain.cpp // main file




-------------------------------------------------------------------------------------------------------

Header File:
// PhoneData.h
#ifndef PHONE_DATA_H

#define PHONE_DATA_H

#include <iostream>

#include <string>

using namespace std;

/******* Class PhoneData Specification ********/

class PhoneData

{

private:

string name;

string phoneNumber;

public:

// 2 parameter constructor

PhoneData(string, string);

// defualt constructor

PhoneData();

// mutator functions

void setName(string);

void setPhoneNumber(string);

// accessor functions

string getName() const;

string getPhoneNumber() const;

};

#endif


Implementation File
// PhoneData.cpp
#include "PhoneData.h"

//no argument constructor

PhoneData ::PhoneData()

{

//initializes both data members to empty strings

name = "";

phoneNumber = "";

}

//two argument constructor

PhoneData ::PhoneData(string name,string number)

{

//initializes both variables with appropriate

//passed arguments

this->name = name;

phoneNumber = number;

}

//setter method for name

void PhoneData :: setName(string name)

{

this->name = name;

}

//setter method for phoneNumber

void PhoneData :: setPhoneNumber(string number)

{

this->phoneNumber = number;

}

//getter method for name

string PhoneData :: getName() const

{

return name;

}

//getter method for PhoneNumber

string PhoneData :: getPhoneNumber() const

{

return phoneNumber;

}



Main File:
// PhoneDataMain.cpp
#include "PhoneData.h"

//main function

int main()

{

//two PhoneData objects

PhoneData pd1("Susan Meyers", "414 444-4444");

PhoneData pd2("Joy Rodgers", "888 888-8888");

//displaying objects details using getter functions

//PhoneData1

cout<<" Phone Data 1: "<<endl;

cout<<" Name: "<<pd1.getName()<<endl;

cout<<"Phone Number: "<<pd1.getPhoneNumber()<<endl;

//PhoneData2

cout<<" Phone Data 2: "<<endl;

cout<<" Name: "<<pd2.getName()<<endl;

cout<<"Phone Number: "<<pd2.getPhoneNumber()<<endl;

cout<<endl;

system("pause");

return 0;

} //end of the main

Explanation / Answer

added lines are in blue
// PhoneData.h #ifndef PHONE_DATA_H #define PHONE_DATA_H #include <iostream> #include <string> using namespace std; /******* Class PhoneData Specification ********/ class PhoneData { private: string name; string phoneNumber; public: //overloaded output friend ostream& operator<<(ostream&, const PhoneData&); // 2 parameter constructor PhoneData(string, string); // defualt constructor PhoneData(); // mutator functions void setName(string); void setPhoneNumber(string); // accessor functions string getName() const; string getPhoneNumber() const; }; #endif // PhoneData.cpp #include "PhoneData.h" //no argument constructor ostream& operator<<(ostream &stream, const PhoneData& pd) { stream<<" Name: "<<pd.getName()<<endl; stream<<"Phone Number: "<<pd.getPhoneNumber()<<endl; return stream; } PhoneData ::PhoneData() { //initializes both data members to empty strings name = ""; phoneNumber = ""; } //two argument constructor PhoneData ::PhoneData(string name,string number) { //initializes both variables with appropriate //passed arguments this->name = name; phoneNumber = number; } //setter method for name void PhoneData :: setName(string name) { this->name = name; } //setter method for phoneNumber void PhoneData :: setPhoneNumber(string number) { this->phoneNumber = number; } //getter method for name string PhoneData :: getName() const { return name; } //getter method for PhoneNumber string PhoneData :: getPhoneNumber() const { return phoneNumber; } // PhoneDataMain.cpp #include "PhoneData.h" //main function int main() { //two PhoneData objects PhoneData pd1("Susan Meyers", "414 444-4444"); PhoneData pd2("Joy Rodgers", "888 888-8888"); //displaying objects details using getter functions //PhoneData1 cout<<" Phone Data 1: "<<endl; cout<<pd1; //cout<<" Name: "<<pd1.getName()<<endl; //cout<<"Phone Number: "<<pd1.getPhoneNumber()<<endl; //PhoneData2 cout<<" Phone Data 2: "<<endl; cout<<pd2; //cout<<" Name: "<<pd2.getName()<<endl; //cout<<"Phone Number: "<<pd2.getPhoneNumber()<<endl; cout<<endl; system("pause"); return 0; } //end of the main
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