answersLogoWhite

0


Best Answer

Writing statements in a class member function that use cout or cin is not recommended because it binds your class to the (global) standard IO streams, which is rarely a good idea. It goes against the OOP paradigm which encourages generalisation and where independent classes should express independent notions independently.

Class member functions should only be implemented when those functions operate solely upon the class itself, which primarily refers to class mutators (assignment operators and other "setters") as well as accessors ("getters"). If a class member function mutates a stream, then that function is really operating upon two classes simultaneously and should really be declared external to both, perhaps as a friend function if it also requires private access to the class representation.

With a well-designed class hierarchy, overloading the operator<< and operator>> operators usually suffices for most streaming requirements. If you require something more specialised, consider exposing a protected interface and allow users to provide their own implementations rather "hard wiring" your own.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you think of a good reason to avoid writing statements in a class member function that use cout or cin?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the reason for making Data as Private Member of a Class?

this is the reason for security purpose


What statements indicate the start and end of a procedure?

A procedure is started by calling the function that represents that procedure. The function call must include any and all required arguments.The procedure ends whenever a return statement is encountered anywhere within the function body, or execution falls off the end of the function (assuming no return value is expected from the procedure), or a non-return function is invoked by the function (such as the abort() function) or an unhandled exception is thrown by the function. Apart from a non-returning function call, execution always returns to the calling code (the caller). If an unhandled exception is thrown by a function, the call stack automatically "unwinds" until a suitable exception handler is found. If no handler is found on the call stack, the global main function will unwind, terminating the program with an unhandled exception error. Hence the reason all non-trivial programs should provide a "catch-all" exception handler in the global main function.


Why we make a function friend in c plus plus?

A "normal" function is just a function. Even a friend function is just a normal function. However, when a class declares an external function or an external class method to be a friend of the class, the friend function gains access to the private members of the class. class foo { friend void bar(foo&amp;); int m_data; }; void bar(foo&amp; f) { f.m_data=42; } In the example above, the foo class declares the bar function to be a friend function. As such, the bar function has unrestricted access to the private members of foo. In this case, foo::m_data is private (by default) and would therefore be inaccessible to bar were it not declared a friend of foo. Other than that, the bar function is no different to any other function. Note that you cannot declare friendship from outside of a class. The class itself must declare its own friends. However, the same function can be declared friends in more than one class, which can be a useful feature when two or more classes work closely together, as the friend function can be used to provide the "glue" that binds them together.


Why static variables in a function have global extent?

A variable declared static outside of a function has a scope the of the source file only. It is not a global variable. A variable declared outside of a function and without the static qualifier would be a global variable.


How will you overload a template function?

The only reason to overload a template function is to provide an overload that differs in the number and type of arguments that cannot be addressed by the template function alone. You simply declare them as you would any other overload. The overloads may themselves be template functions, but there must be no ambiguity: every template function must generate an unique function signature. Remember that template functions generate overloads at compile time on an as-required basis.

Related questions

Why is there a need to create the function in a program?

There are many reasons, One simple reason, is that functions group a certain sequence of code statements under one function name. So that if you need to run these set of code statements multiple times and at various occasions, instead of copy/pasting that whole set at various places in your source code, you can simply call the function using its name.


What is the reason why you do not see any of the expected output from the DBMS OUTPUT statements.?

You are using the wrong output statements.


This is an authors intention reason or drive for writing the piece?

This is an author's intention, reason, or drive for writing the piece.


Which kind of proof combines statements and reason into proof?

paragraph proof


Impact of finance on financial statements?

Finance are the reason for financial statements. Without financial information, financial statements can't be created. Investors use this information to make decisions about investing in a business.


What is the reason for writing the book twilight?

im going to answer your question with another question....what is the reason for writing any book? think about it.


What is it called when a writer is writing with a specific reason in mind?

Writing with intention.


What information is contained in the right column of a two-column proof?

The reason.


What is the objective of the paper?

It is the reason why you are writing.


What is the reason for making Data as Private Member of a Class?

this is the reason for security purpose


How do you start a All about you letter?

The way you start a letter depends upon the reason you are writing it. If you are writing an all about you letter for a job or class, then you would want to start it with a very formal introduction including your name and the reason you are writing the letter. If you are writing the letter for a more casual reason relax your voice and have fun with it.


Who taught the importance of reason in his writing?

Aristotle