Question

When enabling a class to work with iostreams by adding overloaded    operator<< and operator>> functions, should...

When enabling a class to work with iostreams by adding overloaded

   operator<< and operator>> functions, should they be friends

   of the class or member functions, and why?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

The stream operators:

  • operator << output
  • operator >> input

When you use these as stream operators (rather than binary shift) the first parameter is a stream. Since you do not have access to the stream object (its not yours to modify) these can not be member operators they have to be external to the class. Thus they must either be friends of the class or have access to a public method that will do the streaming for you.

It is also traditional for these objects to return a reference to a stream object so you can chain stream operations together.

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
When enabling a class to work with iostreams by adding overloaded    operator<< and operator>> functions, should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ questions T/F The compiler will report an error if the + operator is overloaded in...

    C++ questions T/F The compiler will report an error if the + operator is overloaded in a way so that it performs subtraction. T/F When a unary operator is overloaded using a member function, it accepts one explicit argument and returns a class object. T/F The size of an operator cannot be overloaded. T/F Operator overloading also allows creation of new operators. T/F To convert a class object to basic data type, a conversion operator needs to be included publicly...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • Overload a relational operator for the Job class: Assume that this operator is not overloaded for...

    Overload a relational operator for the Job class: Assume that this operator is not overloaded for the Salary class. a) Write how the function will be declared in your code. b) Write an external function definition (not inside the class). solve it in C++10 to solve this you nedd question number 1. Create a composition between the classes Job and Salary. Class Salary a. data member:    1. money b. constructors:    1. default constructor    2. user defined constructor with a parameter...

  • c++)Which situation would require the operator to be overloaded as a non-member function? provide an example...

    c++)Which situation would require the operator to be overloaded as a non-member function? provide an example of your answer choice of when this would occur. a. The overloaded operator is =. b. The left operand is an int. c. The operator returns a reference. d. The left operand must not be a class object (or a reference to a class object).

  • Design a class called NumDays. The class’ purpose is to store a value that represents a...

    Design a class called NumDays. The class’ purpose is to store a value that represents a number of work hours and convert it to a number of days. For example, 8 hours would be converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours would be converted to 2.25 days. The class should have a constructor that accepts a number of hours, as well as member functions for storing and retrieving the hours and days....

  • This homework involves ADDING MEMBER FUNCTIONS to ArrayBag.h. This homework is important because ...

    This homework involves ADDING MEMBER FUNCTIONS to ArrayBag.h. This homework is important because we do some thing like this on exams. All you will need to submit is your member function code BUT your code should compile and run if I were to add it to ArrayBag.h. That means you should put your code in ArrayBag.h and make sure it compiles before submitting. You should test with a simple main. DO NOT submit main or your ArrayBag.h file just submit...

  • In the space provided, answer each of the given questions. Your answers should be concise; aim fo...

    In the space provided, answer each of the given questions. Your answers should be concise; aim for two or three sentences. 1. Why is choosing not to overload the assignment operator and using default memberwise copy a potentially dangerous thing to do? 2. Why are some operators overloaded as member functions while others are not? 3. Describe precisely how the overloaded addition operator for HugeInt operates. 4. What restrictions does the class have? In the space provided, answer each of...

  • URGENT! In the following implementation for an overloaded + operator for English class objects: def _lt_(self,...

    URGENT! In the following implementation for an overloaded + operator for English class objects: def _lt_(self, other): return (self.totalinches() < other.totalinches() where totalinches is the English Length method to convert the years, feet and inches to inches, Why was it OK to use the operator in a method those purpose is defining the less than comparison operation for English Length objects?

  • Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to...

    Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to store a value that represents a number of work hours and convert it to a number of days. For example, 8 hours would be converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours would be converted to 2.25 days. The class should have a constructor that accepts a number of hours, as well as member functions for storing...

  • 27. Suppose the unary ! operator is an overloaded member function of class String. For a...

    27. Suppose the unary ! operator is an overloaded member function of class String. For a String object s, which function call is generated by the compiler when it finds the expression Is? a. s.operator!0 b. s.operator!(default valuel, default value?,...) c. operator!(s d. A compiler error results because no arguments are given 28. Select the false statement regarding inheritance. a. A derived class can contain more attributes and behaviors than its base class b. A derived class can be the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT