Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Code Snippets Code Examples

Floating-point values in system default, scientific, and fixed formats.

/* Floating-point values in system default, scientific, and fixed formats. */ #include <iostream> using std::cout; using std::endl; using std::ios; int main() { double x = .001234567, y = 1.946e9; cout << "Displayed in default format:\n" << x << '\t' << y << '\n'; cout.setf( ios::scientific, ios::floatfield ); cout << "Displayed in scientific format:\n" << x << '\t' << y << '\n'; cout.unsetf( ios::scientific ); cout << "Displayed in default format after unsetf:\n" << x << '\t' << y << '\n'; cout.setf( ios::fixed, ios::floatfield ); cout << "Displayed in fixed format:\n"<< x << '\t' << y << endl; return 0; }

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

Set specific format flags. The function setf() sets the io stream format flags of the current stream to fmtfl. The optional mask argument specifies that only the flags that are in both fmtfl and mask should be set. The return value is the previous configuration of io stream format flags. Member type fmtflags is a bitmask type (see ios_base::fmtflags). Function returns the format flags selected in the stream before the call. The first form (1) sets the stream's format flags whose bits are set in fmtfl, leaving unchanged the rest, as if a call to flags(fmtfl|flags()).

A return statement ends the processing of the current function and returns control to the caller of the function. A value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non-void return type, the compiler issues an error message. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

Clear specific format flags. Clears the format flags selected in mask. Function unsets the formatting flags identified by flags. The parameterized manipulator resetiosflags behaves in a similar way as this member function. This function does not return any value.

The C++ comments are statements that are not executed by the compiler. The comments in C++ programming can be used to provide explanation of the code, variable, method or class. If we write comments on our code, it will be easier for us to understand the code in the future. Also, it will be easier for your fellow developers to understand the code. By the help of comments, you can hide the program code also. There are two types of comments in C++: • Single Line comment. • Multi Line comment









The only difference is that, the return type of operator function is Check in this case which allows to use both codes ++obj; obj1 = ++obj;. It is because, temp returned from "operator"