Found 33676 Articles for Programming

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

V Jyothi
Updated on 02-Dec-2024 00:26:59

34K+ Views

There is a big distinction between the suffix and prefix versions of ++. In this article, we will see the differences between them and will go through it's examples. Prefix version (++i) In the prefix version (i.e., ++i), the value of i first increments, and then the value of the expression becomes the new value of i. So basically it first increments and then assigns a value to the expression. Postfix version (i++) In the postfix version (i.e., i++), the value of I first increments, but the value ... Read More

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

Akansha Kumari
Updated on 02-May-2025 18:48:58

4K+ 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. These are both provided by the library in C++. In this article, we will learn the difference between these two output streams in more detail. Character Output Stream (cout) The character output stream is used to display the data or information to the console (standard output device, basically the screen), like printing messages, results, ... Read More

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

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

309 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++?

Akansha Kumari
Updated on 05-May-2025 18:39:52

983 Views

cerr and clog are both objects of the stderr(standard error) stream, which is used to display error messages or diagnostics. In this article, we will learn the difference between these two in more detail. Further, the description of the cout object is also given to get a clearer picture. Unbuffered standard error stream (cerr) The cerr is the standard error stream, which is used to output the errors. This is also an instance of the ostream (as iostream means input/output stream) class. As cerr is unbuffered, therefore it's used when we need to display the error message instantly. It doesn't ... Read More

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

Akansha Kumari
Updated on 06-May-2025 18:36:44

1K+ Views

In C++, the new operator is mainly used for allocating memory on the heap, but to initialize that memory, you need to explicitly declare and provide a value to it.Here, the new operator dynamically allocates memory for a variable or object during runtime and returns a pointer to the allocated memory.  Here are the following ways you can initialize memory using the new operator: For built-in types For arrays For objects new Operator in Built-in Types The built-in types are the basic data types in C++, which ... Read More

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

Akansha Kumari
Updated on 06-May-2025 19:07:14

3K+ Views

The cin, cout, cerr, and clog are streams that handle standard input and output stream objects, which are defined in an header file. Standard Output Stream (std::cout) The cout is an object of class ostream that represents the standard output stream oriented to narrow characters (of type char). It corresponds to the C stream stdout. The standard output stream is the default destination of characters determined by the environment. This destination may be shared with more standard objects (such as cerr or clog). Syntax Here is the following syntax for cout in C++: cout

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

Akansha Kumari
Updated on 07-May-2025 19:14:19

3K+ Views

In C++, both dot (.) and arrow (->) operators are used to access members of classes, structures, and unions. But they are used in different scenarios based on how the object is being accessed. In this article, we will learn the differences and uses of these two operators in C++. Dot Operator (.) The dot(.) operator is used to access members (variables and functions) of a class, structure, or union when working with its object (not a pointer). It allows you to access and manipulate the object's properties by directly interacting with members of an object. Syntax Here is the ... Read More

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

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

455 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

5K+ 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

6K+ 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