Professional Documents
Culture Documents
Re: What Is "Mutable" Keyword? Answer Roshanpr
Re: What Is "Mutable" Keyword? Answer Roshanpr
Re: What Is "Mutable" Keyword? Answer Roshanpr
Class my
{
public:
my(){age=0;}
void plusplus(int b)const
{
age+=b;
}
};
int main()
{
const my obj;
obj.plusplus(40);
}
class XYZ
{
public:
int i;
mutable int cc;
public:
XYZ();
};
int main()
{
const XYZ obj;
obj.cc = 100; // modify obj object's
member "cc" which has
been declared as
mutable.
}
Copy
explicit CSid(LPCTSTR pszAccountName, LPCTSTR pszSystem = 0);
We need to write a managed proxy of the constructor above that creates an instance
of ManagedCSid from an object of String. The proxy constructor should also only be
able to be called explicitly; however, the explicit keyword is not part of Managed
Extensions.
This affects how the operator can be called by clients of the wrapper class that are
written in other languages such as Visual Basic and Visual C#. The operator
op_Explicit is for explicit calls only and should be used when there can be a loss of
information; for example, a conversion from double to int. The operator
op_Implicit is for implicit and explicit calls and should be used when there cannot
be a loss of information.
In this example, we use the op_Explicit form of the convert-to operation. This
ensures that the proxy constructor can only be called explicitly by clients of the
wrapper class, as required.
Copy
static ManagedCSid __gc* op_Explicit(String __gc* pszAccountName);
Here, t is created on the common language runtime heap and is therefore accessible
even when this function returns.
If the unmanaged unary constructor had not been declared explicit, it could be
used implicitly within Managed Extensions code only. Other common language
specification (CLS)-compliant languages cannot use unary constructors for implicit
conversions. For interoperability reasons, you should wrap each unary-argument
constructor within the class it is defined in with a suitable conversion operator.
Copy
static ManagedCSid __gc* FromString(String __gc* pszAccountName);
C++ Manipulators
In this C++ tutorial, you will learn what a manipulator is, endl manipulator, setw
manipulator, setfill manipulator and setprecision manipulator explained along with syntax
and examples.
What is a Manipulator?
Manipulators are operators used in C++ for formatting output. The data is manipulated by
the programmer’s choice of display.
There are numerous manipulators available in C++. Some of the more commonly used
manipulators are provided here below:
endl Manipulator:
This manipulator has the same functionality as the ‘\n’ newline character.
For example:
Exforsys
Training
setw Manipulator:
This manipulator sets the minimum field width on output. The syntax is:
setw(x)
Here setw causes the number or string that follows it to be printed within a field of x
characters wide and x is the argument set in setw manipulator. The header file that must
be included while using setw manipulator is <iomanip.h>
#include <iostream.h>
#include <iomanip.h>
void main( )
{
int x1=12345,x2= 23456, x3=7892;
cout << setw(8) << ”Exforsys” << setw(20) << ”Values” <<
endl
<< setw(8) << “E1234567” << setw(20)<< x1 << end
<< setw(8) << “S1234567” << setw(20)<< x2 << end
<< setw(8) << “A1234567” << setw(20)<< x3 << end;
}
setw(8) setw(20)
Exforsys Values
E1234567 12345
S1234567 23456
A1234567 7892
setfill Manipulator:
This is used after setw manipulator. If a value does not entirely fill a field, then the
character specified in the setfill argument of the manipulator is used for filling the
fields.
#include <iostream.h>
#include <iomanip.h>
void main( )
{
cout << setw(10) << setfill('$') << 50 << 33 << endl;
}
$$$$$$$$5033
This is because the setw sets 10 width for the field and the number 50 has only 2
positions in it. So the remaining 8 positions are filled with $ symbol which is specified in
the setfill argument.
setprecision Manipulator:
The setprecision Manipulator is used with floating point numbers. It is used to set
the number of digits printed to the right of the decimal point. This may be used in two
forms:
• fixed
• scientific
These two forms are used when the keywords fixed or scientific are appropriately used
before the setprecision manipulator. The keyword fixed before the
setprecision manipulator prints the floating point number in fixed notation. The
keyword scientific before the setprecision manipulator prints the floating point
number in scientific notation.
#include <iostream.h>
#include <iomanip.h>
void main( )
{
float x = 0.1;
cout << fixed << setprecision(3) << x << endl;
cout << sceintific << x << endl;
}
0.100
1.000000e-001
#include <iomanip>
I/O manipulators that take parameters are in the <iomanip> include file.
I/O Manipulators
The following output manipulators control the format of the output stream. Include
<iomanip> if you use any manipulators that have parameters. The Range column tells
how long the manipulator will take effect: now inserts something at that point, next
affects only the next data element, and all affects all subsequent data elements for the
output stream.
Manip. Rng Description
General output
Sets minimum field width on output. This sets the minimum size of the field
- a larger number will use more columns. Applies only to the next element
inserted in the output. Use left and right to justify the data appropriately
setw(n) next
in the field. Output is right justified by default. Equivalent to cout.width(n);
To print a column of right justified numbers in a seven column field:
cout << setw(7) << n << endl;
width(n) next Same as setw(n).
left next Left justifies output in field width. Only useful after setw(n).
Right justifies output in field width. Since this is the default, it is only used
right next
to override the effects of left. Only useful after setw(n).
Only useful after setw. If a value does not entirely fill a field, the character
ch will be used to fill in the other characters. Default value is blank. Same
setfill(ch) all effects as cout.fill(ch) For example, to print a number in a 4 character
field with leading zeros (eg, 0007):
cout << setw(4) << setfill('0') << n << endl;
Sets the number of digits printed to the right of the decimal point. This
applies to all subsequent floating point numbers written to that output
setprecision(n) all stream. However, this won't make floating-point "integers" print with a
decimal point. It's necessary to use fixed for that effect. Equivalent to
cout.precision(n);
bool output
boolalpha Uses alphabetic representation (true and false) for bool values. Turned
noboolalpha all
off with noboolalpha.
Input
For most input values (eg, integers and floating-point numbers), skipping
initial whitespace (eg, blanks) is very useful. However, when reading
characters, it is often desired to read the whitespace characters as well as
skipws
noskipws all the non-spacing characters. The these I/O manipulators can be used to
turn whitespace skipping off and on. Eg,
cin >> noskipws;
turns whitespace skipping off for all subseqent cin input.
Other
cout << "A. " << tenth << ", " << one << ", " <<
big << endl;
cout << "B. " << fixed << tenth << ", " << one << ", " <<
big << endl;
cout << "C. " << scientific << tenth << ", " << one << ", " <<
big << endl;
cout << "D. " << fixed << setprecision(3) << tenth << ", " << one
<< ", " << big << endl;
cout << "E. " << setprecision(20) << tenth << endl;
cout << "F. " << setw(8) << setfill('*') << 34 << 45 << endl;
cout << "G. " << setw(8) << 34 << setw(8) << 45 << endl;
return 0;
}
produces this output (using DevC++ 4.9.8.0):
A. 0.1, 1, 1.23457e+009
B. 0.100000, 1.000000, 1234567936.000000
C. 1.000000e-001, 1.000000e+000, 1.234568e+009
D. 0.100, 1.000, 1234567936.000
E. 0.1000000014901161
F. ******3445
G. ******34******45
Lines F and G show the scope of setw() and setfill().
One of the most useful classes you will ever have. Use it whenever you need an
IStream or to stream any text or binary data to memory. Implements the IStream
interface.
Stream any kind of data to memory. This is one of the most-used classes I have
ever written. It implements the IStream interface, and therefore has many
applications in COM programming. But I find it useful in many other situations.
For example, as a "string builder", where I need to stream text in a loop (or
otherconstruct) and then get direct access to the pointer afterwards without a
copy operation.
The class uses VirtualAlloc internally, and so is lightning fast at allocating more
memory. This is a real value class - you won't be disappointed!