Found 34488 Articles for Programming

What is the difference between ++i and i++ in C++?

V Jyothi
Updated on 08-Sep-2023 23:00:20

32K+ Views

There is a big distinction between the suffix and prefix versions of ++.In the prefix version (i.e., ++i), the value of i is incremented, and the value of the expression is the new value of i. So basically it first increments then assigns a value to the expression.In the postfix version (i.e., i++), the value of i is incremented, but the value of the expression is the original value of i. So basically it first assigns a value to expression and then increments the variable.                               ... Read More

What is the difference between cerr and cout streams in c++?

Akshaya Akki
Updated on 30-Jul-2019 22:30:21

3K+ Views

cout is an object of the stdout stream, while cerr is an object of the stderr stream.stdout and stderr are different streams, even though they both refer to console output by default. Redirecting (piping) one of them (e.g. program.exe >out.txt) would not affect the other.Generally, stdout ought to be used for actual program output, whereas all information and error messages should be printed to stderr, in order that if the user redirects output to a file, information messages are still printed on the screen and not to the output file.

When should you use 'friend' in C++?

Krantik Chavan
Updated on 11-Feb-2020 04:58:06

209 Views

A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows −class Box {    double ... Read More

What is the difference between cerr and clog streams in c++?

Fendadis John
Updated on 30-Jul-2019 22:30:21

611 Views

cerr and clog are both objects of the stderr stream. Following are the differences between them. You can also read about the cout object to get a clearer picture.Un-buffered standard error stream (cerr)cerr is the standard error stream which is used to output the errors. This is also an instance of theostream class. As cerr is un-buffered therefore it's used when we need to display the error message instantly. It doesn't have any buffer to store the error message and display later.Buffered standard error stream (clog)This is also an instance of ostream class and used to display errors but unlike ... Read More

How to initialize memory with a new operator in C++?

Nishtha Thakur
Updated on 19-Jun-2020 05:22:14

808 Views

The new operator in C++ is defined for allocating memory and not initializing. If you want to allocate an array of type int with the new operator, and you want to initialize them all to the default value(ie 0 in case of ints), you can use the following syntax −Syntaxnew int[10]();Note that you simply must use the empty parentheses - you can't, for example, use (0) or the other expression that is why this is only helpful for default initialization.There are other methods of initializing the same memory using fill_n, memset, etc which you can use to initialize objects to ... Read More

What are cin, cout and cerr streams in C++?

Jai Janardhan
Updated on 11-Feb-2020 04:52:35

2K+ Views

cin, cout, cerr, and clog are streams that handle standard inputs and standard outputs. These are stream objects defined in iostream header file.std::cin is an object of class istream that represents the standard input stream oriented to narrow characters (of type char). It corresponds to the C stream stdin. The standard input stream is a source of characters determined by the environment. It is generally assumed to be input from an external source, such as the keyboard or a file.std::cout is an object of class ostream that represents the standard output stream oriented to narrow characters (of type char). It corresponds ... Read More

What is the difference between the dot (.) operator and -> in C++?

Smita Kapse
Updated on 11-Feb-2020 04:49:15

2K+ Views

The dot and arrow operator are both used in C++ to access the members of a class. They are just used in different scenarios. In C++, types declared as class, struct, or union are considered "of class type". So the following refers to all three of them.a.b is only used if b is a member of the object (or reference[1] to an object) a. So for a.b, a will always be an actual object (or a reference to an object) of a class.a→b is essentially a shorthand notation for (*a).b, ie, if a is a pointer to an object, then ... Read More

What are the basic rules and idioms for operator overloading in C++?

Anvi Jain
Updated on 19-Jun-2020 05:21:04

319 Views

When it comes to operator overloading in C++, there are 3 basic rules you should follow. like all such rules, there are so exceptions. These 3 rules are −1.  Whenever the meaning of an operator is not obviously clear and undisputed, it should not be overloaded. Instead, provide a function with a well-chosen name. Basically, the first and foremost rule for overloading operators, at its very heart, says:Don’t do it.That might seem strange, but there are only a few cases where operator overloading is appropriate. The reason is, it is hard to understand the semantics behind the application of an ... Read More

What is C++ Standard Error Stream (cerr)?

Arushi
Updated on 10-Feb-2020 13:41:41

4K+ Views

std::cerr is an object of class ostream that represents the standard error stream oriented to narrow characters (of type char). It corresponds to the C stream stderr. The standard error stream is a destination of characters determined by the environment. This destination may be shared by more than one standard object (such as cout or clog).As an object of class ostream, characters can be written to it either as formatted data using the insertion operator (operator

Standard Input Stream (cin) in C++

Manikanth Mani
Updated on 10-Feb-2020 13:39:48

5K+ Views

std::cin is an object of class istream that represents the standard input stream oriented to narrow characters (of type char). It corresponds to the C stream stdin. The standard input stream is a source of characters determined by the environment. It is generally assumed to be input from an external source, such as the keyboard or a file.As an object of class istream, characters can be retrieved either as formatted data using the extraction operator (operator>>) or as unformatted data, using member functions such as read. The object is declared in header with external linkage and static duration: it ... Read More

Advertisements