Re: What Is "Mutable" Keyword? Answer Roshanpr

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

Re: What is "mutable" keyword?

Answer mutable key word is used when u want 0 Roshanpr


# 1 to make any member
variable of a const object modifyable.

Basically when u make a object


constant u cannot modify its
data members. But during the
declaration of the class if a
data member is declared as mutable it
can changed.

Class my
{

mutable int age;

public:

my(){age=0;}
void plusplus(int b)const
{
age+=b;
}
};

int main()
{
const my obj;
obj.plusplus(40);
}

Is This Answer 16 Yes


Correct ? 2 No

Re: What is "mutable" keyword?


Answer Mutable keyword is used to modify a
# 2 data member of an object
which has declared as constant. for
example:

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.
}

The explicit Keyword


The explicit keyword in unmanaged C++ is used to declare a single-argument
constructor that can only be called explicitly. In our example, there is a constructor
for CSid that can be called just with the pszAccountName parameter because its
second parameter has a default value of 0:

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.

Managed Extensions provides convert-from operators: static unary member functions


that convert from an object of some class to an object of the class in which the
operator is defined. The user can choose to name the operator either op_Explicit or
op_Implicit.

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);

ManagedCSid __gc* ManagedCSid::op_Explicit(String __gc* pszAccountName)


{
ManagedCSid __gc* t = new ManagedCSid(pszAccountName);
return t;
}

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.

For interoperability with .NET Framework-compliant languages that do not use


op_Explicit we need to provide a similar member function called FromString as
follows:

Copy
static ManagedCSid __gc* FromString(String __gc* pszAccountName);

ManagedCSid __gc* ManagedCSid::FromString(String __gc* pszAccountName)


{
ManagedCSid __gc* t = new ManagedCSid(pszAccountName);
return t;
}
C++ Manipulators
Category: C++
Comments (10)

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:

cout << "Exforsys" << endl;


cout << "Training";
produces the output:

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;
}

The output of the above example is:

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;
}

The output of the above program is

$$$$$$$$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;
}

The above gives ouput as:

0.100
1.000000e-001

C++ Notes: I/O Manipulators


Manipulators are the most common way to control output formating.

#include <iomanip>
I/O manipulators that take parameters are in the <iomanip> include file.

Default Floating-point Format


Unless you use I/O manipulators (or their equivalent), the default format for each
floating-point number depends on its value.

• No decimal point: 1.0000 prints as 1


• No trailing zeros: 1.5000 prints as 1.5
• Scientific notation for large/small numbers: 1234567890.0 prints as 1.23457e+09

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

endl now Write a newline ('\n') and flush buffer.

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;

Floating point output

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);

Used fixed point notation for floating-point numbers. Opposite of


fixed all scientific. If no precision has already been specified, it will set the
precision to 6.

scientific all Formats floating-point numbers in scientific notation. Opposite of fixed.

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.

ws now Reads and ignores whitespace at the current position.

Other

showpoint, noshowpoint, uppercase, nouppercase, dec, oct, hex,


setbase(8|10|16), showbase, noshowbase, ends, showpos, noshowpos,
internal, flush, unitbuf, nounitbuf, setiosflags(f), resetiosflags(f)
Example
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const float tenth = 0.1;
const float one = 1.0;
const float big = 1234567890.0;

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().

C++ Stream class

Submitted By: Brett Goodman


Rating: (Not rated) (Rate It)
Share: By Email

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!

You might also like