0% found this document useful (0 votes)
20 views21 pages

Unit 4

Uploaded by

ssmtbharmal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views21 pages

Unit 4

Uploaded by

ssmtbharmal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit -4

POINTER, VIRTUAL FUCTIONs and POLYMORPHISM, RTTI


❖ INTRODUCTION OF POLYMORPHISM

➢ After classes and inheritance, polymorphism is the third essential features of an object
oriented programming language. It is simply one thing with several multiple forms. We
already know that function overloading and operator overloading are one type of
polymorphism.
➢ A polymorphism are two types, (1) Compile time Polymorphism (2) Run Time
Polymorphism. Function overloading and Operator overloading are example of Compile
time Polymorphism.

Polymorphism

Compile time Polymorphism Run time Polymorphism


(Early Binding) (Late Binding)
Example: (1) Function Overloading Example: Virtual Functions
Operator Overloading
➢ Compile time polymorphism:
In function overloading, overloaded member functions are called by matching the number
of arguments and type of arguments. This procedure done at time of compilation, so a
compiler is able to select the matching function for particular call.
So in this type of Polymorphism, the selection of function depending on compile at time
of compilation and not at the time of running a program. That’s why it is called Compile
time Polymorphism or Early Biding.
➢ Run time polymorphism:
In run-time polymorphism, selection of appropriate function is called at run time not at compile
time
❖ Let us consider the situation for a member function name and its prototype (number of
arguments and type of arguments) are same for both Base and Derived classes.
❖ For example,
class base
{
public: void show()
{
cout<<” I am base class”;
}
};
class derived : public base
{
public: void show()
{
cout<<” I am derived class”;
}
};

❖ In above situation both member function name show( ) are same, and if you want to call a
member function of both base and derived classes? What happen, prototype are also same
for both the functions.
❖ So in this type of situation, at the time of compilation, compiler does not know what to
do? Which functions choose for calling, so appropriate member function could be
selected while program is running. This is known as Run Time Polymorphism.
❖ In run time polymorphism, member function is linked with a particular class at the time
of running which is much latter after the compilation, that’s why this process is also
called Late Binding.
❖ It is also called Dynamic Binding, because the selection of appropriate function is done
dynamically at run time.
❖ This dynamic and late binding is achieved with virtual function, and this concept must
require a pointer to object. So a next topic is use of pointer to object in a program.

➢ POINTER TO OBJECTS :
➢ We already know that, a member function of class can be called with help of object and
dot operator. So now we have to call a member function of a class with help of pointer to
object. We can also use an object pointer for access the public data members of an object.
➢ Let us consider example for calling a member function through pointer to object, here
class Student is there and S is the object of class Student,
Student S ;
Similarly we can define a pointer S1 of type Student as follows:
Student *S1 ; Which is called pointer to objects.
➢ Pointer objects are useful in creating objects at run time.
Example: Calling a member function of class with help of object pointer.
#include<iostream.h>
class student
{
private: int regno, age;
char name[20];
public:
void getdata(int x, char name1[],int y)
{
regno = x ;
strcpy (name, name1);
age = y ;
}
void display()
{
cout<<"Reg no. : "<<regno<<endl;
cout<<"Name : "<<name<<endl;
cout<<"Age : "<<age<<endl;
}
};
int main()
{
student s;
student *s1;
s1 = &s;
s1->getdata(10,"Akashy",48);
s1->display();
return 0 ;
}
Output:
Reg no. : 10
Name : Akashy
Age : 48

➢ We can also create the objects using pointer and new operator as follows:
Student *S1 = new Student ;
➢ Above statement allocates enough memory for the data members in the object structure
and assign the address of the memory space to S1.
➢ We can also create an array of objects using pointers. For example,
Student *S1 = new Student [10] ; // array of 10 objects
➢ Above statement will create memory space for an array of 10 objects of class Student.
❖ this POINTER :
➢ Every object in C++ has access to its own address through an important pointer called
this pointer. The this pointer is an implicit parameter to all member functions.
Therefore, inside a member function, this may be used to refer to the invoking object.

➢ Friend functions do not have a this pointer, because friends are not members of a class.
Only member functions have a this pointer.
❖ Application of this pointer:
Using this pointer, any member function can find out the address of the object of which
it is member.
Accessing members of class with this pointer.
This pointer is also return the object it points to.
➢ Let us try the following example to understand the concept of this pointer:
Example: Find out the address of object of particular class by using this pointer

#include<iostream.h>
class test
{
public:
void show()
{
cout<<"My object's address is:= "<<this<<endl;
}
};
int main()
{
test t1,t2,t3;
t1.show();
t2.show();
t3.show();
return 0 ;
}
Output:
My object’s address is:= 0×8f80fff4
My object’s address is:= 0×8f80fff2
My object’s address is:= 0×8f80fff0
➢ In above program the show( ) function ask the address of three objects t1, t2, and t3.
➢ So this function prints out the value of this pointer which is shown in above output.
➢ Let us see another example of this pointer which initialize and accessing data members.
Example: Accessing Data members and member function with this pointer

#include<iostream.h>
#include<conio.h>
class test
{
int x,y,z;
public:
void sum()
{
this->x = 50 ;
this->y = 40 ;
this->z = x + y ;
}
void show()
{
this->sum();
cout << "The value of x is:= "<< this->x <<endl;
cout << "The value of y is:= "<< this->y <<endl;
cout << "Sum of x and y is:= "<< this->z <<endl;
}
};
int main()
{
clrscr();
test t1 ;
t1.show();
getch();
return 0;
}
Output:
The value of x is:= 50
The value of x is:= 40
Sum of x and y is:= 90
➢ In above program we just initialize and accessing the value of x and y using this pointer
and arrow (->) operator.
➢ Inside the void show( ) function we just accessing or calling sum( ) function by using this
pointer and arrow(->) operator.
➢ So, this is how we can access the members of class by using this pointer.
❖ POINTERS TO DERIVED CLASSES :

➢ We can use pointers not only to the base class objects but also the objects of derived
classes. Pointers to objects of a base class are type compatible with pointers to objects of
a derived class.
➢ Therefore, a single pointer variable can be made to point to objects belonging to different
classes.
➢ For example, if B is base class and D is a derived class from base class B, then pointer
declared as a pointer to base class B can also be a point to D.

➢ So we can declare, B *ptr ; // pointer to class B type variable


B b ; // base class object
D d ; // derived class object
ptr = &b ; // ptr points to object b, put address of b in pointer
➢ We can make ptr to point to the object d also,
ptr = &d ; // ptr points to object d, put address of d in pointer

Example: Pointer to Base class (Ordinary member function accessed with Base class pointers).

#include<iostream.h>
#include<conio.h>
class Base
{
public:
void show()
{
cout<<"I am base class" << endl ;
}
};
class Derived : public Base
{
public:
void show()
{
cout <<"I am derived class"<<endl ;
}
};
int main()
{
clrscr();
Base b ;
Derived d;
Base *bptr ;
bptr = & b ;
bptr -> show() ;
bptr = & d ;
bptr -> show() ;
getch();
return 0;
}
Output:
I am base class
I am base class

➢ In above program, both Base class and Derived class function name show( ) is same, and
if we create a base class pointer and assign address of Base class object to it using
statement,
bptr = &b ; bptr -> show( ) ;
➢ Then it will call the show( ) function of base class.
➢ If we create a base class pointer and assign address of Derived class object to it using
statement,
bptr = &d ; bptr -> show( ) ;
➢ Then it will also call the show( ) function of Base class, not call a Derived class function.
The function in the Base class is always executed. The compile ignores the contents of
the pointer bptr and choose the member function that matches the type of the pointer.
➢ So, if we have to call a Derived class function when function name is same for both base
and derived class, we have to create p pointer to derived class and assign address of
object to a pointer
This problem can also be solved by making base class member function virtual, instead
of creating pointer to derived class.
❖ VIRTUAL FUNCTION :
In above program (pointer to base class), both base and derived class function name
show( ) are same, and if we create a base class pointer and assign address of derived class
object to it. Then also it will call a base class function, not call a derived class function.
This problem can be solved by using virtual function.
Virtual means existing in effect but not in reality, when virtual function are used in a
program, that appears to be calling a function of one class may in reality be calling a
function of a different class.
A virtual function a member function which is declared within base class and is re-
defined (Overridden) by derived class. When you refer to a derived class object using a
pointer or a reference to the base class, you can call a virtual function for that object and
execute the derived class’s version of the function.
The keyword virtual tells the compiler that it should not perform early binding (static).
Instead, it should automatically install all the mechanism necessary to perform late
binding (dynamic binding).
Rules for Virtual Function :
• Virtual functions must be declared in public section of class.
• Virtual functions cannot be static.
• It cannot be a friend function of another class.
• Virtual functions should be accessed using pointer or reference of base class type to
achieve run time polymorphism.
• The prototype of virtual functions should be same in base as well as derived class.
• They are always defined in base class and overridden in derived class. It is not
mandatory for derived class to override (or re-define the virtual function), in that
case base class version of function is used.
• A class may have virtual destructor but it cannot have a virtual constructor.
Example 1: Virtual function (Virtual member function accessed with Base class pointer).
#include<iostream.h>
#include<conio.h>
class Base
{
public:

virtual void show()


{
cout<<"I am base class" << endl ;
}
};

class Derived : public Base


{
public:
void show()
{
cout <<"I am derived class"<<endl ;
}
};
main()
{
clrscr();
Base b ;
Derived d;
Base *bptr ;
bptr = & b ;
bptr -> show() ;
bptr = & d ;
bptr -> show() ;
getch();
return 0;
}
Output:
I am base class
I am derived class
➢ In above example the function call bptr -> show( ) executes the function that corresponds
to the contents of the pointer bptr, and not on the type pointer.

Example 2: Run time polymorphism (Dynamic binding of member function according


to user choice).
#include<iostream.h>
#include<conio.h>
class Base
{
public:
virtual void show()
{
cout<<"I am base class" << endl ;
}
};
class Derv1 : public Base
{
public:
void show()
{
cout <<"I am derived class 1"<<endl ;
}
};
class Derv2 : public Base
{
void show()
{
cout<<"I am derived class 2"<<endl;
}
};
int main()
{
Base *bptr ;
int ch;
cout << "1. call function of derv1 class" << endl;
cout << "2. call function of derv2 class" << endl;
cout << "3. call function of Base class" << endl;
cout << " Enter your choice 1, 2 or 3: ";
cin>>ch;
if (ch==1)
bptr = new Derv1;
else if(ch == 2)
bptr = new Derv2;
else if(ch == 3)
bptr = new Base;
bptr -> show() ;
return 0;
}

Output :

• call function of derv1 class


• call function of derv2 class
• call function of Base class
Enter your choice 1, 2 or 3: 2
I am derived class 2
➢ DEFAULT ARGUMENT TO VIRTUAL FUNCTION

❖ Default arguments are entirely compile-time feature. I.e. the substitution of default
arguments in place of missing arguments is performed at compile time. For this reason,
obviously, there's no way default argument selection for member functions can depend on
the dynamic (i.e. run-time) type of the object. It always depends on static (i.e. compile-
time) type of the object.
❖ Check the output of following C++ program.
Example: Default argument to virtual function

#include<iostream.h>
class B
{
public:
virtual void print(int a=10)
{
cout << "Base Class data = " << a << endl;
}
};
class D : public B
{
public:
void print(int a=20)
{
cout <<"Derived class data = " << a <<endl;
}
};

int main()
{
B *bp = new D();
bp->print();
return 0;
}
Output :
Derived class data = 10

➢ If we take a closer look at the output of above example, we observe that print( ) function
of derived class is called and default value of base class print( ) function is used.
➢ The reason is the default value is substituted at compile time. The print( ) function is
called on the basis of bp which is a pointer of Base type at run time. So compiler
substitutes 10 (not 20).
❖ PURE VIRTUAL FUNCTION :

➢ A pure virtual function in C++ is a virtual function for which we don’t have
implementation, we only declare it. A pure virtual function is declared by assigning 0 in
declaration.
➢ See the following example.
class Test
{
// Data members of class
public:
virtual void show() = 0; // Pure Virtual Function

/* Other members */
};

➢ In above example, the show( ) function is known as pure virtual function. Thus, a pure
virtual function is a virtual function with no body and = 0 in its declaration.
❖ ABSTRACT CLASS
➢ Sometimes implementation of all functions cannot be provided in a base class because we
don’t know the implementation.
➢ If we declare a pure virtual function in the base class, then user should never be able to
create an object of the base class.
➢ A class from which we would never want to create objects is called and abstract class.
Such a class exists only as a parent for the derived classes. So an abstract base class must
have at least one pure virtual function.
➢ Anybody who tries to create an object from such a base class would be reported an error
by the compiler and give complain that you are trying to create and object of the abstract
class.
➢ In abstract class, you can create pointer and references. This allows abstract classes to
support run time polymorphism.

A complete example: Pure virtual function / Abstract class


➢ A pure virtual function is implemented by classes which are derived from an Abstract
class. Following is a simple example to demonstrate the same.

#include<iostream>
class Base
{
int x;
public:
virtual void show() = 0;
int getX()
{
return x;
}
};
class Derived: public Base
{
int y;
public:
void show()
{
cout << "show() function called";
}
};
int main()
{
Derived d;
d.show();
return 0;
}

Output:
show() function called

➢ In above example the virtual function show( ) in the base class never gets executed,
because this show( ) function is virtual with no body and = 0 in its declaration.
❖ RTTI (Run-Time Type Identification)
➢ RTTI stands for Run-time Type Identification.
➢ RTTI is useful in applications in which the type of objects is known only at run-time.
➢ RTTI allows programs that manipulate objects or references to base classes to
retrieve the actual derived types to which they point to at run-time.
➢ Two operators are provided in C++ for RTTI.
(i) dynamic_cast operator:
The dynamic_cast operator can be used to convert a pointer that refers to an object of
class type to a pointer to a class in the same hierarchy. On failure to cast the
dynamic_cast operator returns 0.
(ii) typeid operator:
➢ The typeid operator allows the program to check what type an expression is. When a
program manipulates an object through a pointer or a reference to a base class, the
program needs to find out the actual type of the object manipulated.
➢ The operand for both dynamic_cast and typeid should be a class with one or more virtual
functions.

CONSOLE I/O OPERATIONs


❖ INTRODUCTION
➢ Every program generally takes some input for which it generates the processed data as
output.
➢ C++ provides a rich set of functions and operations to perform Input / Output (I / O).
C++ also supports all I/O functions available in C.
❖ C++ STREAMs and STREAM
CLASSES C++ STREAMs
➢ A stream is a general name given to a flow of data. Different streams are used to
represent different kinds of data flow.
➢ A stream is a collection of bytes. Each stream is associated with a particular class which
contains member functions and definition for dealing with that particular kind of data
flow.
➢ The input stream is called the keyboard or any other input device while the output stream
is generally the screen or any other storage device.
C++ STREAM CLASSES
➢ I / O system of C++ contains hierarchy of classes that are used to define various streams
to deal with both console and disk files. These classes are called stream classes.
➢ In below figure show the hierarchy of the stream classes used for input and output
operations with the console unit. These classes are declared in the header file iostream.h.

➢ In above figure istream and osteam classes are derived from the ios class and used to
support input and output operations respectively.
➢ The iostream class is derived from both istream and ostream by multiple inheritances.
➢ The classes used for standard I/O are declared in the header file iostream.h.
➢ The member functions of istream and ostream classes perform both formatted and
unformatted operations.
➢ The istream class contain such member functions are get( ), getline( ), read( ) and
overloaded extraction operator >>.
➢ The ostream class contains functions like put( ), and write( ) as well as overloaded
insertion operator <<.

❖ UNFORMATTED I/O OPERATIONs:


❖ Overloaded Operator >> and << :

➢ The object cin and cout are used for input and output of data of various types. This is
done by overloading the operator >> and << to recognize all the basic C++ data types.
➢ The general format of reading data from keyboard is:
cin >> variable1 >> variable2 >> ……… >> variableN ;
➢ The operator >> reads the data character by character and assign it to the identical
location.
For example: int temp = 25 ;

cin >> temp ;


➢ The operator >> will read the character 2 and 5 and assign to variable temp.
➢ The general format of print the output data on as screen is:
cout << variable1 << variable2 << ……… << variableN ;
➢ The operator << print the data on screen.
For example: char name[25] ;
cin >> name ;
cout << name ;
➢ The operator >> will read the characters and stores it in name variable and operator <<
will print the characters on the screen
❖ get( ) and put( ) functions :
➢ The get( ) and put( ) functions handle character input and output respectively.
❖ get( ) function :
➢ There are two types of get( ) function: (1) get( void ) and (2) get( char )
➢ The get( char ) assigns the input character to its argument and get( void ) returns
the input character.
Example: get( ) function with no parameters.
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
char c;
while((c=cin.get())!='\n') // get character from keyboard and assign to c
{ // until user not press enter key
cout<<c; // display character on screen
}
getch();
return 0;
}
➢ In above program output is a entering a line of text (enter one by one character until
entering a new line character).
➢ In above program we can also use operator >> to read a character but it will skip the
white space and newline character.
Example: get( char ) function with parameters.
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
char c;
while(c!='\n')
{
cin.get(c); // get character from keyboard and stores into c
cout<<c; // display character on screen
}
getch();
return 0;
}
➢ The output of above program is same as previous program, to print a line of text until
user not press enter key.
❖ put( ) function :

➢ The put( ) function is a member of ostream class and writes a single character to the
output device. It can be used for print a line of text on screen.
➢ The statement cout.put(„M‟) print a M character on the screen, which is same as used as
cout << „M‟ ; statement.
➢ If we write a numeric value inside the argument of put( ), then it will display a ASCII
code for that character. For example, if we write a statement,
cout.put ( 66 ) ;
it will print a character B, because the ASCII value of 66 is B.
➢ We can also write the put statement in previous example like get( ).
Example: put( ) function with parameters.
#include<iostream.h>
#include<conio.h>
main()
{
clrscr( );
char c; // get character from keyboard and assign to c
while((c!='\n'))
{
cin.get(c);
cout.put(c); // used to display character instead of cout<<c;
}
getch();
return 0;
}
➢ The output of above program is same as previous program, to print a line of text until
user not press enter key. We use cout.put( c ) statement in place of cout << c.
❖ getline( ) and write( ) functions :
❖ getline( ) function :

➢ The getline( ) function reads the character input in to a character array until the new line
character is encountered. This function can be invoked by using cin object as follows:
cin.getline( line, size ) ;
• The above statement reads the character input into the variable line and terminated with
entering a new line character „\n‟. The new line character is read but not saved.
• If we write a code,
char name[25] ;
cin.getline( name, 10 ) ;
If we enter following input through keyboard,
Christ College < press enter >
So it will take 10 character in the variable name and output will be
Christ Col
❖ write( ) function :
➢ The write( ) function allows moves flexibility. It has following format:
write (char * line , int len ) ;
❖ The first argument is the string to be displayed and second argument indicates the
number of character to be display.
❖ The syntax of write function is,
cout.write( line, size ) ;
➢ It is possible to concatenate two wirte( ) statement in single statement which is same as
get( ) and getline( ) in previous topic,
cout.wirte( string1, size1).write( string2, size2) ;
Example: Use of getline( ) and write( ) functions.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50];
cout<<"Enter String: ";
cin.getline(str,50);
cout.write(str,6)<<endl;
cout.write(str,14)<<endl;
cout.write(str,7).write(str,15).write(str,18)<<endl;
int len=strlen(str);
cout.write(str,len)<<endl;
getch();
}
➢ The output of above program is: Enter string: Christ College Rajkot
Christ
Christ College
Christ Christ College Christ College Raj
Christ College Rajkot
❖ FORMATTED I/O OPERATIONS:
➢ C++ provides a number of features for formatting the output. The features include:
• ios class functions and
• manipulators
➢ The ios class provides a large number of member functions for formatting the output in
number of ways. List of ios format functions in C++:
(1) width( ): To specify the required field size for printing an output value on screen.
(2) precision( ): To specify the number of digits to be displayed after the decimal point
of a float or double value.
(3) fill( ): To specify a character that is used to fill the unused portion of a field.
(4) setf( ): To specify format flags that can control the form of output display (for
example left justification and right juctification).
(5) unsetf( ): To clear the flags specified by the setf( ) function.
➢ Now we will see some important manipulators that are frequently used in our program.
To use these manipulators in our program, we have to insert iomanip.h header file. The
header file iomanip.h provides standard manipulators corresponding to the various
format.
➢ List of standard manipulators:
(i) oct : use octal notation
(ii) hex : use hexadecimal notation
(iii) dec : use decimal notation
(iv) endl : insert new line and flush stream
(v) ends : insert „\0‟ and flush stream
(vi) flush : flush stream
(vii) setw( ) : set the field width, which is same as width( )
(viii) setprecision( ) : set the floating point precision, which is same as
precision( )
(ix) setiosflags( ) : set the format flag, which is same as setf( )
(x) unsetiosflags( ) : clear the flags, which is same as unsetf( )

➢ Defining field width with the width( ) function:


➢ The width( ) function specifies the minimum field width to be used for the next numeric,
character or string output operations.
➢ The syntax of width( ) function is:
cout.width (w ) ;
❖ Where w is the field width (number of columns). The output will be printed right justified
and remaining width will be consider as a white space.
Example: width( ) function
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout.width(8) ;
cout << "CHRIST" << endl ;
getch();
}
➢ The output of above program is:

C H R I S T

➢ The string “ CHRIST ” will be print on right justified, the width of the value is 8, so
insert two blank space before the string start.
❖ Setting precision with precesion( ) function:
➢ The precision( ) function specifies the number of digits to be displayed while printing the
floating point number. By default, six digits are permitted for all floating point numbers.
➢ The syntax of precision( ) function is:
cout.precision(p ) ;
➢ Where p specifies the number of digits to be used after decimal point while displaying
floating point number.
Example: precision( ) function

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout.precision(3) ;
cout << 24.5678 << endl ;
getch();
}

➢ The output of above program is:


24.6

❖ Filling and padding with fill( ) function:

➢ When field size is larger than the stored value, the unused position of field are filled with
the white space, by default. By using function fill( ), to fill the unused position by any
desired character indicated in the argument of fill( ) function.
➢ The syntax of fill( ) function is:
cout.fill(ch ) ;
➢ Where ch represents the character which is used for filling the unused positions.
Example: fill( ) function

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout.fill(„*‟) ;
cout.width(8) ;
cout << “CHRIST”<< endl ;
getch();
}
➢ The output of above program is:

* * C H R I S T

➢ The string “ CHRIST ” will be print on right justified, the width of the value is 8, so fill
two * character in place of two blank space.

❖ Formatting flags: Using setf( ) and unsetf( ) functions:


➢ The function width( ) print a value in right justified, but a text always print left justified.
Another thing is if we have to print floating point number in scientific format using
precision( ), this thing can be possible by using setf( ) function.
➢ Using setf( ) function, we can set the ios flags and ios bitfield.
➢ The syntax of setf( ) function is:
cout.setf (arg1, arg2 ) ;
➢ The arg1 is one of the formatting flags defined in the class ios. The formatting flag
specifies the format action required for the output. Another argument is arg2 indicate the
bit fields.
➢ Now below table shown the list of some formatting ios flags and bit fields of it:
Format to be print ios formatting flag (arg1) Bit – field (arg2)
Left justified output ios :: left ios :: adjustifield

Right justified output ios :: right ios :: adjustifield

Scientific notation ios :: scientific ios :: floatfield

Fixed point natation ios :: fixed ios :: floatfield

Decimal base ios :: dec ios :: basefield

Octal base ios :: oct ios :: basefield

Hexadecimal base ios :: hex ios :: basefield


Example: Use of setf( ) function to print a text on left justified using width( ) function
and fill the remaining space with fill( ) function.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout.fill('$');
cout.setf( ios::left, ios::adjustfield ); // set flag for justification
cout.width(10);
cout << "Christ" << endl;
getch();
}
➢ The output of above program is:

C H R I S T $ $ $ $

➢ In above program setf( ) function is set the ios flags for left justification for output, so
text will print form left justified and using fill( ) function remaining four space are filled
by $.
➢ In above program if we want to remove the flags set by the setf( ) function used unsetf( )
function.
➢ The following flags that do not have bit fields, so in setf( ) function only one argument
arg1 is used.
➢ List of flags that do not have bit fields are:
(1) ios :: showbase : use base indicator on output
(2) ios :: shopos : print + before positive numbers
(3) ios :: showpoint : show trailing decimal point and zeros
(4) ios :: uppercase : upper case letter for hex output
❖ MANAGING OUTPUT WITH MANIPULATORS:
➢ We already discuss the list of standard manipulators, here we just see the example of that
manipulators which are frequently used, such are:
(i) endl
(ii) setw( )
(iii) setprecision( )
(iv) setfill( )
(v) setiosflags( )
(vi) resetiosflags( )
➢ To use these manipulators in program, we must include iomanip.h header file.
Example: Formatting with manipulators

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
main()
{
clrscr();
float x = 12.34567 ;
cout << setw(4) << 15 << endl;
cout << setw(6) << 22 << endl;
cout << setprecision(3) << 12.3456 << endl;
cout << setprecision(4) << 11.67895 << endl;
cout << setw(8) << setiosflags(ios::showpos) << x <<endl;
getch();
return 0;
}

➢ The output of above program is:

1 5

22

12.3
11.68
+12.35

You might also like