Formatting Using The Ios Members
Formatting Using The Ios Members
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.
stream.setf(ios::showpos);
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:
switch( init,condition)
{ }
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:
Note: The variable argv[] has been declared as an array of pointers to string.
Conversion functions