0% found this document useful (0 votes)
23 views

Formatting Using The Ios Members

The document discusses formatting output using ios members in C++. It describes the various format flags defined in the ios_base class that control output formatting, such as boolalpha, showbase, uppercase, dec, hex, oct, fixed, and scientific. It explains that the setf() and unsetf() member functions of ios can be used to set and unset these format flags on a stream. Some examples are given showing how to set flags like showpoint and showpos to control floating point output formatting.

Uploaded by

Pranav Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Formatting Using The Ios Members

The document discusses formatting output using ios members in C++. It describes the various format flags defined in the ios_base class that control output formatting, such as boolalpha, showbase, uppercase, dec, hex, oct, fixed, and scientific. It explains that the setf() and unsetf() member functions of ios can be used to set and unset these format flags on a stream. Some examples are given showing how to set flags like showpoint and showpos to control floating point output formatting.

Uploaded by

Pranav Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Formatting Using the ios Members

Formatting Using the ios Members


 Each stream has associated with it a set of format flags that control the way
information is formatted. The ios class declares a bitmask enumeration called fmtflags in
which the following values are defined. (Technically, these values are defined within
ios_base, which is a base class for ios.)

 adjustfield, basefield ,boolalpha, dec


 fixed ,floatfield ,hex, internal
 left, oct, right, scientific
 showbase, showpoint ,showpos, skipws
 unitbuf, uppercase

 These values are used to set or clear the format flags.


Formatting Using the ios Members
member
field effect when set
constant
boolalpha read/write bool elements as alphabetic strings true and false).
showbase write integral values preceded by their corresponding numeric base prefix.
independent showpoint write floating-point values including always the decimal point.
flags
showpos write non-negative numerical values preceded by a plus sign (+).
uppercase write uppercase letters replacing lowercase letters in certain insertion operations.
numerical dec read/write integral values using decimal base format.
base  hex read/write integral values using hexadecimal base format.
(basefield) oct read/write integral values using octal base format.
float format  fixed write floating point values in fixed-point notation.
(floatfield) scientific write floating-point values in scientific notation.
the output is padded to the field width by inserting fill characters at a specified
internal
internal point.
adjustment 
left the output is padded to the field width appending fill characters at the end.
(adjustfield)
the output is padded to the field width by inserting fill characters at the
right
beginning.
Setting the Format Flags

 To set a flag, use the setf() function. This function is a member of ios.
 To unset a flag, use the unsetf() function. This function is a member of ios.

fmtflags setf(fmtflags flags);

stream.setf(ios::showpos);

Here, stream is the stream you wish to affect.

Example:
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout << 100.0; // displays +100.0
int x=3656;
cout.setf(ios::dec);
cout.setf(ios::hex , ios::basefield);
Some new features of C++
Namespaces in C++
Namespaces in C++
 The namespace keyword allows you to partition the global namespace by creating a
declarative region.
 A namespace defines a scope.
 Anything defined within a namespace statement is within the scope of that
namespace.
Syntax:

namespace <namespace-name> {
// declarations
}
RTTI-(Run-Time Type Information) and Type
Casting
 Converting an expression of a given type into another type is known as type-casting.
 The functionality of these explicit conversion operators is enough for most needs with
fundamental data types.
 However, these operators can be applied indiscriminately on classes and pointers to
classes, which can lead to code that while being syntactically correct can cause runtime
errors.
 Traditional explicit type-casting allows to convert any pointer into any other pointer
type, independently of the types they point to. The subsequent call to member
 result will produce either a run-time error or a unexpected result.
 In order to control these types of conversions between classes, we have four specific
casting operators:
1.dynamic_cast
2.reinterpret_cast
3.static_cast
4.const_cast
dynamic_cast
 dynamic_cast can be used only with pointers and references to objects. Its purpose is
to ensure that the result of the type conversion is a valid complete object of the requested
class.
 Therefore, dynamic_cast is always successful when we cast a class to one of its base
classes.
 Syntax:
dynamic_cast <new_type> (expression)

Example:
Base b; Base* bptr;
Derived d; Derived* dptr;
bptr = dynamic_cast<Base*>(&d); // ok: derived-to-base
dptr = dynamic_cast<Derived*>(&b); // wrong: base-to-derived
static_cast
 static_cast can perform conversions between pointers to related classes, not only from
the derived class to its base, but also from a base class to its derived.
 This ensures that at least the classes are compatible if the proper object is converted, but
no safety check is performed during runtime to check if the object being converted is in
fact a full object of the destination type.
 Therefore, it is up to the programmer to ensure that the conversion is safe. On the other
side, the overhead of the type-safety checks of dynamic_cast is avoided.
 Syntax:
static_cast <new_type> (expression)
Example:
Base b; Base* bptr;
Derived d; Derived* dptr;
bptr = static_cast<Base*>(&d); // ok: derived-to-base
dptr = static_cast<Derived*>(bptr); // ok: base to derived
dptr = static_cast<Derived*>(&b); // ok: base-to-derived
 static_cast can also be used to perform any other non-pointer conversion that could also
be performed implicitly, like for example standard conversion between fundamental
types.
reinterpret_cast
 reinterpret_cast converts any pointer type to any other pointer type, even of unrelated
classes. The operation result is a simple binary copy of the value from one pointer to the
other. All pointer conversions are allowed: neither the content pointed nor the pointer
type itself is checked.
 It can also cast pointers to or from integer types. The format in which this integer value
represents a pointer is platform-specific.
 Syntax:
reinterpret_cast <new_type> (expression)
Example:

class A {}; class B {};


A * a = new A;
B * b = reinterpret_cast<B*>(a);
const_cast
 This type of casting manipulates the constness of an object, either to be set or to be
removed. For example, in order to pass a const argument to a function that expects a
non-constant parameter:
 Syntax:
const_cast <new_type> (expression)
Example:
void show (char * str)
{
cout << str << endl;
}
int main ()
{
const char * data = “cdac india";
show( const_cast<char *> (data) );
return 0;
}
Type inference for initialized variables/functions
 When initializing a variable, the auto keyword can be used in place of the type to tell
the compiler to infer the variable’s type from the initializer’s type. This is called type
inference (also sometimes called type deduction).
 Example:

auto d{ 5.0 }; //double


auto i{ 1 + 2 }; //int

//with function return value


auto add(int x, auto y)
{
    return x + y;
}
Init statement for if/switch : New version of the if and switch in c++:
if( init, condition)
{ }

switch( init,condition)
{ }

Extended for /forearch loop:


for(type identifier : collection-name)
{ }

Structured Binding:

int array[3]={1,2,3};
auto[a,b,c]=array;
cout<<a<<"\t"<<b<<"\t"<<c<<endl;
Lambdas Expression
 A lambda expression allows us to define an anonymous function inside another
function. The nesting is important, as it allows us both to avoid namespace naming
pollution, and to define the function as close to where it is used as possible .
 The syntax for lambdas is one of the odd things in C++, and takes a bit of getting
used to.
 Lambdas syntax:
[ captureClause ] ( parameters ) -> returnType { statements; }
Example:

auto add { [](int x, int y) {


return x+y;
}
};
cout << add(3, 2) << '\n';
Command line arguments
 We can pass arguments to main() in the way we pass to other functions.
 For this specify the arguments at command prompt at execution time of
the program.
 Variables which are used to collect passed arguments in main() are called
argc and argv
 Variable argc contains the count( number) of arguments being passed to
main(), whereas argv contains addresses of string passed to main().
Passing arguments to main()

/* A program Using Command Line Arguments */


int main( int argc, char *argv[])
{
int i;
for( i=0; i<argc; i++ )
printf(“ \n %s ”, argv [i]);
return 0;
}

Note: The variable argv[] has been declared as an array of pointers to string.
Conversion functions

stoi(string)-Convert string to integer 


stol (string) -Convert string to long int 
stoul (string)- Convert string to unsigned integer 
stoll (string) -Convert string to long long  int
stoull (string) -Convert string to unsigned long long 
stof (string)-Convert string to float 
stod (string) -Convert string to double 
stold (string) -Convert string to long double 
to_string (number) -Converts numerical value to string 
Thank You

You might also like