Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C++ Course Question.. Overload << using member. func


  • Please log in to reply

#1
mhb22079

mhb22079

    New Member

  • Member
  • Pip
  • 2 posts
Hello:
Hoping someone can answer a question for me regarding overloading the << operator using a member function instead of a friend class. Current code is as follows:


Class PhoneNumber {
private:
char areaCode[4];
char exchange[4];
char line[5];
};

ostream &operator<<(ostream &output, const PhoneNumber &num)
{
output << "(" << num.areaCode << ")"
<< num.exchange << "-" << num.line;
return output;
}

int main ()
{
PhoneNumber phone;

cout << phone << endl;


Text says to rewrite using overloaded << operator as member functions and use:"somePhoneNumber.operator<<(cout);"
and
"somePhoneNumber << cout;"
to call.
I have beat myself to death for a while to figure this out but cannot seem to understand how to do it. If someone is willing to show the code required and explain how 'cout' being the left operand works I'd be most appreciative.

  • 0

Advertisements


#2
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Hi mhb,

First thing I would do is start with some working code using the friend method. Then you can easily modify that to work with the member function method.

To get the friend method working, follow these steps:

Step 1:

Declare within your PhoneNumber class that the "ostream &operator<<(ostream &output, const PhoneNumber &num)" function is actually a friend. If you don't do that, the function will not have access to the private members.

Step 2:

You either need some way to initialize the PhoneNumber class or you need a member function to set the private fields (or both). I think it's good to have initialized classes, so I'd recommend adding a constructor to the class that takes an initial area code, exchange, and line and puts those values in its private members. Then, down in your main function you can use your new constructor to get some valid data in the instance of the class referenced by the "phone" variable.

Step 3:

Finish off that "int main()" function by adding a return statement and closing with a "}".

Hopefully you are familiar with how to carry out these three steps. If you have questions about them, definitely ask. Once you do this, you should actually be able to compile and run your app and see how it works. Then you'll want to change your friend function to a member function. The following are a list of things to consider while doing that:

1. You'll need to change the friend declaration within your PhoneNumber class to a public member function declaration, and where you define the "ostream &operator<<(ostream &output, const PhoneNumber &num)" function, you will need to add the "PhoneNumber" class name and scope resolution operator to indicate that it is a member of the PhoneNumber class.

2. You will need to consider the parameter list for the operator<< function. Now that it is a member of the PhoneNumber class, do you really need both of those parameters? (hint, hint)

3. In the main function, the "cout << phone << endl;" line will have to be modified. The " << endl;" part should still be fine, if you have done the member function correctly, but the "cout << phone" part will have to change to the two versions indicated in the assignment text that you included in your original post.

I hope this helps. I'm trying not to just give everything away - that won't help you get anything out of the assignment - so if you run into any problems, just post your questions.

-Ricci
  • 0

#3
mhb22079

mhb22079

    New Member

  • Topic Starter
  • Member
  • Pip
  • 2 posts
I believe by adding it to the PhoneNumber class means that the variables are available to it, so I can drop the PhoneNumber reference leaving only ostream. However, the compiler says that ostream is not part of the PhoneNumber class.

class PhoneNumber
{
// friend ostream &operator<<(ostream&, const PhoneNumber &);
friend istream &operator>>(istream&, PhoneNumber & );
public:
PhoneNumber :: ostream &operator<<(ostream&) ;

private:
char areaCode[4];
char exchange[4];
char line[5];

};


PhoneNumber :: ostream &operator<<(ostream &output)
{
output << "(" << areaCode << ")"
<< exchange << "-" << line;
return output;
}

//ostream &operator<<(ostream & output, const PhoneNumber &num)
//{
// output << "(" << num.areaCode << ")"
// << num.exchange << "-" << num.line;
// return output;
//}
  • 0

#4
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Ok, you've got two problems here.

1. The PhoneNumber class name and scope resolution operator are not necessary on the function declaration inside the class.

2. You are attaching the PhoneNumber class name and scope resolution operator to the return type (ostream&) of the function, and not the function itself. To attach it to the function itself, put them on the function name - "operator<<". Remember that the "&" that you have before "operator<<" is actually part of the return type, so the PhoneNumber class name and scope resolution operator belong between the "&" and "operator<<".

-Ricci
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP