CH 3-Programming in C++
CH 3-Programming in C++
3 : Programming in C++
• Introduction :-
• The C++ Program is made up of sequence of characters. The characters are the
most basic building blocks of the C++ language.
• Characters set is a set of valid characters recognized by a language. A character
represents any letter ,digit, or any other sign.
• The C++ character set consists of upper and lower case letters, digits, special
characters and white spaces. The letters and digits together constitute the
alphanumeric set.
• Token is a sequence of characters from the character set of C++, which is
identified by the complier.
• Keywords are used for specific purposes in C++ and compiler can interprets these
words.
Data Types :-
• A data type determines the type and the operations that can be
performed on the data.
• A data type is defined as, “the set/collection of values and the set of
operations that operate on those values “.
• In short , data type is analyzed as follows :
Data Type = Permitted Data values + Operations
• The various data types provided by C++ are built-in data types,
derived data types and user defined data types ,As shown Fig. :
Hierarchy of C++ Data Types :-
1. Built-In Data Types
• The basic (fundamental) data types provided by c++ are integral, floating point and void data type.
Among these data types, the integral and floating-point data types can be preceded by several type
modifiers. These modifiers (also known as type qualifiers) are the keywords that alter either size or range
or both of the data types. The various modifiers are short, long, signed and unsigned. By default the
modifier is signed.
• In addition to these basic data types, ANSI C++ has introduced two more data types namely, bool and
wchar_t.
• Integral Data Type: The integral data type is used to store integers and includes char (character) and int
(integer) data types.
• Char: Characters refer to the alphabet, numbers and other characters (such as {, @, #, etc.) defined in the
ASCII character set. In C++, the char data type is also treated as an integer data type as the characters are
internally stored as integers that range in value from -128 to 127. The char data type occupies 1 byte of
memory (that is, it holds only one character at a time).
The modifiers that can precede char are signed and unsigned. The various character data types with their
size and range are listed in Table.
Int: Numbers without the fractional part represent integer data. In C++, the int data type is used to store integers
such as 4, 42, 5233, -32, -745. Thus, it cannot store numbers such as 4.28, -62.533. The various integer data
types with their size and range are listed in Table.
• Floating-point Data Type: A floating-point data type is used to store
real numbers such as 3 .28, 64. 755765, 8.01, -24.53. This data type
includes float and double’ data types. The various floating -point data
types with their size and range are listed in Table.
Void: The void data type is used for specifying an empty parameter list to a function and return type for
a function. When void is used to specify an empty parameter list, it indicates that a function does not
take any arguments and when it is used as a return type for a function, it indicates that a function does
not return any value. For void, no memory is allocated and hence, it cannot store anything. As a result,
void cannot be used to declare simple variables, however, it can be used to declare generic pointers.
Bool and wchar_t : The boo1data type can hold only Boolean values, that is; either true or false, where
true represents 1 and false represents O. It requires only one bit of storage, however, it is stored as an
integer in the memory. Thus, it is also considered as an integral data type. The bool data type is most
commonly used for expressing the results of logical operations performed on the data. It is also used as
a return type of a function indicating the success or the failure of the function.
In addition to char data type, C++ provides another data type wchar_t which is used to store 16- bit
wide characters. Wide characters are used to hold large character sets associated with some non-
English languages.
2. User-defined Data types : -
• User has the facility in the programming languages to create data
types according to his/her own needs.
• The data types defined by user according to his/her requirements
known as user-defined data types.
i. Enumerated :-
o In C++ , enumerated data type helps in enhancing the reliability of
the code.
o Enumerations are a way of defining constants.
o Syntax :-
enum tag {member1,member2,-------,membern};
o Here, enum is the keyword and tag is the name of enumeration and
member1,member2,------------member are identifiers which
represents integer constant values. The member should be unique.
Once, the enumeration is defined then variable or object of that type
is defined.
o Syntax :-
enum tag{member1,member2,--------} Variables;
OR
enum tag{member1, member2-------}object_name;
o Here, all members take integer values starting with zero i.e. member1
is assigned 0,member2 is assigned 1 ………and so on.
• For example :-
• enum colors {black,green,red,yellow,blue};
• Enum colors background;
• Here,
• black+=0,green=1,red=2………
• Enumeration variables are particularly useful as flags to indicate
various options are to identify various conditions. It increases clarity
of the program. If we want to have blue colors for the background, it
is easier to understand with,
• background = blue Than with background =4;
• It makes program more readable.
Structure :-
o In ‘C’ structure is a collection of data items of different data types.
o In C++ structure is a collection of variables of different data types in a
single name.
o Syntax :-
struct name of structure
{
data members;
functions
}
o For example,
struct student
{
private ;
char name[20];
long phone;
public :
void display(void)
{
cout<<phone;
}
} ;
o By default members of the C++ structure are public.
• For example,
struct student s;
printf(“%ld”,s.phone); // Invalid
• We cannot print phone like this, we have to invoke public function
display to print phone i.e. Instead of printf statement we can write,
• s.display();
iii. Union:-
• Union in C++ is a user defined data type that uses the same memory as
other objects from a list of objects. At an instance it contains only a single
object.
• Syntax :-
union tag
{
member1;
member2;
member n;
} objectname;
• For example :-
union example
{
int a;
char b;
float c;
}e;
• There are three elements of different types int, char and float respectively. Here, int
takes two bytes memory, char takes one byte memory and float takes four bytes.
• Union allows the same storage is shared with its members .
• Hence, Total memory required is four bytes.
• Unions are very useful for applications involving multiple members, where values need
not be assigned to all members at one time .
• Whereas , in case structure for same example , memory required is 2+1+4= 7 bytes to
store its contents.
iv. Class :-
• But in C++, the array size is one larger than the number of characters present in the
string.
• Here,”\0” is treated as a one character.
• For example :-
char string[5] = “abcde”; //Invalid in C++
char string[5] = “abcd”; //valid in C++
• Functions :
• A Function is a discrete block of statements that perform certain task.
• Once, a function has been written to play a particular role it can be
called upon repeatedly throughout the program.
• Function is the named part of a program that can be declared before
its use, such a declaration is known as a function prototype it ends
with semicolon.
• Syntax : return_type function_name(parameters);
• For example : int max(int x, int y);
• Pointers :-
• A variable in C++ that holds a memory address is called a pointer.
• This address is usually the location of another variable in memory.
• If one variable contains the address of another variable, the first
variable is said to point to the second.
• A pointer declaration consists of a base type, an *(asterisk) and the
variable name.
• Syntax :- datatype *ptvar;
• Where, ptvar is pointer variable.
• For example : float *I; //float pointer
• Program: Write a program which demonstrates the declaration of pointer.
• #include<iostream.h>
using namespace std;
main()
{
int l=2; Output :-
Address of l = 6242
int *m; Value of l = 2
m = &l;
printf(“\n Address of l = %u”,m);
printf(“\n Value of l = %d”, *m);
}
Accessing variable through its
pointer:-
• Once, pointer has been assigned the address of a variable then we can
access the value of the variable using the pointer. If the above program ,we
add a statement , c = *m
• Where, c is int and it access value of address
*m=30; //30 assigned to l through indirection
• In addition to this c++ also have constant pointer declaration and pointer to
constant declaration.
• For example;
• int *const m = “xyz”; //Constant pointer
• int const *m = &l; //pointer to a constant
• In constant pointer we cannot change address of ‘m’ and in pointer to a
constant, the contents of ‘l’ cannot change.
void pointer :-
• void pointer used to point the other data type of variable in C++.
• If we define,
int *m;
float *l;
m=&l; //results in compilation error.
• For such type compatibility void pointers are used.
• For example,
• void *v;
int *i;
float *f;
v=&i;
v=&f;
Here, void pointer v will hold pointer of any type.
void Data Type:-
• The void data type also known as empty data type. void is used for
following two purposes:-
• If the function is not having any arguments, functionname(void);
• To specify the return type of function when function is not returning
any value.
• Void functionname(void);
Operators in C++ can be classified into 6 types: Increment and Decrement Operators
++ increases the value of the operand by 1
Arithmetic Operators -- decreases it by 1
Assignment Operators
Relational Operators 2. C++ Assignment Operators
Logical Operators Operator Example Equivalent to
Bitwise Operators = a = b; a = b;
Other Operators += a += b; a = a + b;
1. C++ Arithmetic Operators -= a -= b; a = a - b;
Operator Operation *= a *= b; a = a * b;
/= a /= b; a = a / b;
+ Addition %= a %= b; a = a % b;
- Subtraction
3. C++ Relational Operators
* Multiplication Operator Meaning Example
== Is Equal To 3 == 5 gives us false
/ Division != Not Equal To 3 != 5 gives us true
> Greater Than 3 > 5 gives us false
Modulo < Less Than 3 < 5 gives us true
Operation >= Greater Than or Equal To 3 >= 5 give us false
%
(Remainder after
division) <= Less Than or Equal To 3 <= 5 gives us true
4. C++ Logical Operators 5. C++ Bitwise Operators
Operator Example Meaning Operator Description
Binary One's
Logical OR. ~
Complement
expression1 True if at
|| || least one of
expression2 the operands << Binary Shift Left
is true.
>> Binary Shift Right
Logical NOT.
True only if
! !expression
the operand
is false.
• Unary Operators :- -, ++, --, sizeof, !
• Conditional Operator :-
• This is the only ternary operator used in C and C++. It is defined as (?:) where
the first symbol is ? and second is : (colon).
• Syntax :
• condition expression? expression 1 : expression 2;
• The condition expression is evaluated first and if true then expression1 is
evaluated otherwise expression2 is evaluated.
• For example,
mfloat a = 15;
float b,c;
(a<20)?b =50 :c=100;
All these above operators are valid in C and C++.
6. Other C++ Operators The scope resolution operator ( :: ) is used for several reasons.
For example: If the global variable name is same as local
Operator Description Example
variable name, the scope resolution operator will be used to
sizeof
returns the size of data
type
sizeof(int); // 4 call the global variable. It is also used to define a function
outside the class and used to access the static variables of class.
returns value based on string result = (5 > 0) ?
?:
the condition "even" : "odd"; // "even"
new
accesses members of
. struct variables or class s1.marks = 92; delete
objects
new operator: It is used for dynamic storage allowance. The
used with pointers to new operator allows you to create objects of any type.
-> access the class or struct ptr->marks = 92;
variables
pointer variable = new datatype;
<< prints the output value cout << 5;
C++ also provides new meaning of some built-in operators. This is Operator Overloading.
1. Scope Resolution Operator(::) :-
cin cout
Input Output
Devices Devices
Extraction i
Operator
Keyboard
Fig. :- Extraction Operator
Input Device
• The << (insertion) operator in C++ is used with the cout statement, which is used
to send the result to an output device (monitor).
• The << operator is used to insert the contents of a variable on its right to the
object on its left.
• The operator << also called as put to operator.
• For example :
int avg = 10;
Cout<<“Average is :”<<avg<<endl;
• In the above example, the << operator first sends the string Average is: to cout.
• Next, the content of the variable avg is sent to cout and finally the keyword endl is
sent, which transfers the control to the end of the line, as shown in Fig. :-
Monitor Insertion
Object Operator
Average is: << avg
y 20 b 20 y
after execution
10 y
20 x
Classes And Access Specifiers :-
• Access specifiers are used to restrict (limits) the accessibility of class
members.
• Access specifiers defines how the members of the class can be accessed.
• Each member of a class is associated with an access specifiers.
• The access specifiers of a member control its accessibility as well as
determines the part of the program that can directly access the member of
the class.
• In the class declaration syntax, the keyword private , public and protected
are known as access specifiers (also known as visibility modes).
• An access specifier specifies the access rules for members following it until
the end of the class or until another access specifier is encountered.
• When a member is declared private, it can be accessed only inside the class .
• While a public member is accessible both inside and outside the class.
• Protected members are accessible both inside the class in which they are declared as
well as inside the derived classes of the same class.
• Once, an access specifier has been used, it remains in effect until another access specifier
is encountered or the end of the class declaration is reached.
• An access specifier is provided by writing the appropriate keyword (private , public or
protected) followed by a colon ‘:’.
• Note that :-
• The default access specifier of the member of a class is private.
• i.e. ,If no access specifier is provided in the class definition, the access specifier is
considered to be private.
• In C++, We can set the access of a class’s members using the access specifiers. We can
make members private (visible only to code in the same class), protected (visible only
inside the same class and classes derived from it.) ,or public(visible to code inside and
outside the class).
• By default, all the class has private access.
• The general syntax of a class declaration that uses the private and public access
specifiers are given below :-
• Class ClassName
• For example :-
{
class book
private : {
//Declarations of private //private by default
// members appear here …. char title[30]; // variable
declaration
public :
float price;
//Declarations of public public :
// members appear here …. void getdata(char [], float);
}; //function declaration
void putdata();
};
• In above example, a class name book with two data
members title and price and two member functions getdata()
and putdata() is created. As no access specifier is provided
for data members, they are private by default, whereas, the
member functions are declared as public. It implies that the
data members are accessible only through the member
functions while the member functions can be accessed
anywhere in the program.
Defining Data Members And Member Functions :-
• All the variables inside the class are called data members. These variables are of any basic data
type, derived data type, user defined data type or objects of other class.
• Any function declare inside a class is called a member function of that class. Only the member
functions can have access to the private data members and private functions. By default all
members of class are private.
• The general syntax of a class declaration is shown below :-
class class _name
{
private:
variable declarations;
function declarations;
public :
variable declarations;
function declarations;
};
class : Name
• Private area
Data members
Function members
Public area
Data members
Function members
Roll
Student[1]
Name
Roll
Student[2]
Name
Roll
Student[3]
Name
|
|
|
Roll
Student[99]
Name
void putdata(void)
• Program for array of objects. {
• #include<iostream.h> cout<<“roll :”<<roll<<endl; 3. Student Information
cout<<“name”<<name<<“\n”; Enter roll : 202
using namespace std;
} Enter name : Anita
class student roll : 200
int main()
{ { name : Nirali
int roll; int i; roll : 201
student s[3]; //array of size 3 name : Prajakta
char name[20]; roll : 202
for(i=0;i<3;i++)
public : name : Anita
{
void getdata(void); s[i].putdata();
void putdata(void); }
}; return 0;
}
void student :: getdata(void)
Output :
{ 1. Student Information
cout<<“Enter rollno :”; Enter roll : 200
cin>>roll; Enter name : Nirali
2. Student Information
cout<<“Enter name :” ;
Enter roll : 201
cin>>name; Enter name : Prajakta
}
Usage of Namespace :-
• Consider a situation, when we have two persons with the same name, Seema, in the same class.
• Whenever, we need to differentiate them definitely we should have to use some additional
information along with their name, like either the area, if they live in different area or their
mother’s or father’s name, etc.
• Same situation can arise in the C++ applications.
• For example :- We might be writing some code that has a function called abc() and there is
another library available which is also having same function abc(). Now the compiler has no way
of knowing which version of abc() function we are referring to within the code.
• A namespace is designed to overcome this difficulty and is used as additional information to
differentiate similar functions, classes, variables etc. with the same name available in different
libraries. Using namespace, we can define the context in which names are defined. A namespace
defines a scope.
• A namespace is a declarative region that provides a scope to the identifiers(names of the types,
function, variables etc. ) inside it.
• Namespaces are used to group related names and to avoid clashes between similar names in
different parts of large programs.
• Defining a Namespace :
• A namespace definition begins with the keyword ‘namespace’ followed by
the namespace name as follows :-
• namespace namespace_name
{
//code declaration……..
}
To call the namespace enabled version of either function or variable we use ,
name ::code //code could be variable or function
• Program for namespace accept
• #include<iostream.h>
using namespace std;
//first namespace
int main()
namespace space_one {
{ //calls function from first name space
void func() space_one::func();
// Calls function fron second name space
{
space two::func();
cout<<“Inside space_one”; return 0;
} }
}
OUTPUT :
Inside space_one
//second namespace Inside space_two
namespace space_two
{
void func()
{
cout<<“Inside space_two”;
}
}
Managing Console I/O :-
• C++ defines its own object oriented I/O systems; it has its own object
oriented way of handling data input and output.
• These Input/Output operations are carried out using different stream
classes and their related functions.
• Until now we have used the object of the istream class (cin) and
object of ostream class until (cout); with operators >> and <<
respectively.
• For formatting purpose we use different streams and their functions
in C++.
C++ Streams :-
• A stream is sequence of bytes.
• Stream acts either as a source from which the input data can be
obtained or as a destination to which the output data can be sent.
• In C++, to manage the data flow we have different stream classes,
• Fig. shows stream classes for console I/O.
ios
istream.withassign ostream.withassign
iostream
iostream.withassign
#include<iostream.h>
1. istream class :
• It is a derived class of ios and hence, inherits the properties of ios.
• It defines input functions such as get(), getline() and read().
• In addition, it has an overloaded member function, stream extraction
operator >>, to read data from a standard input device to the memory
items.
2. ostream class :
• It is a derived class of ios and hence inherits the properties of ios.
• It defines output functions such as put() and write().
• In addition, it has an overloaded member function, stream insertion
operator <<, to write data from memory to a standard output device.
• cerr object is of ostream class used for displaying error messages.
3. iostream class :-
• It is derived from multiple base classes, istream and ostream, which
are inturn inherited from class ios.
• It supports both input and output stream operations.
• These classes istream_withassign, ostream_withassign and
iostream_withassign add the assignment operators to their parent
classes.
Usage of Manipulators :
• Manipulators are the functions used in Input/Output statement.
• All the manipulators are defined in header file iomanip.h.
• We can use chain of manipulator that is one or more manipulators in
statement.
Table : Manipulators
Manipulators Equivalent I/O function
setw() width()
setprecision() precision()
setfill() fill()
setioflags() setf()
resetioflags() unsetf()
1. Setting of field width :
• setw() is used to define the width of field necessary for the output for a
variable .
• For example: setw(5); This function will reserve 5 digits for a number.
setw(5);
cout<<123;
Output : 1 2 3
b= 1 8 1
c= 1 7
• Program :Program to display student data.
#include<iostream.h>
void student:: read_data()
using namespace std; {
class student int i;
cout<<“Enter student name :”;
{ cin>>name;
int marks[5]; cout<<“Enter 5 subject marks :”
int total = 0; for(i=0;i<5;i++)
{
char name[20]; cin>>marks[i];
public : total = total + marks[i];
}
void read_data();
void display(); }
};
void student:: display_data()
{ Output :-
cout<<“\n Name :”<<name; Enter student name : Rama
cout<<“\n Marks in 5 subjects :” ; Enter 5 subjects marks : 20
for(int i=0; i<5; i++) 50
cout<<“\t”<<marks[i]; 30
60
cout<<“Total=”<<total;
40
} Name : Rama
int main() Marks in 5subjects : 20 50 30 60
{ 40
student s; Total = 200
s.read_data();
s.display_data();
return 0;
}
• Program : Program to declare class account having data members
principle,rate_of_interest, no_of_years. Accept this data for one object and find out
simple interest.
#include<iostream.h>
using namespace std;
class account
{
private :
int p, n, r;
float si;
public :
void getdata()
{
cout<<“Enter Principle, Rate _of_interest and number_of_years :\n ”;
cin>>p>>r>>n;
}
void calculate()
int main()
{ {
si = (p*n*r)/100; account a;
a.getdata();
} a.calculate();
void display() a.display();
return 0;
{
}
cout<<“simple Interest = ”; Output :
cout<<si; Enter Principle, Rate_of_interest and
number_of_years :
} 5400 12 5
}; Simple Interest : 3240
• Program: Program to declare a class rectangle having data members length
and breadth. Accept this data for one object and display area and perimeter
of rectangle.
void rectangle::accept()
#include<iostream.h> {
cout<<“Enter length and breadth :”;
using namespace std; cin>>len>>bred;
Class rectangle }
void rectangle::calculate()
{ {
private : area = len * bred;
peri = 2*(len + bred);
int len,bred,area,peri; }
public : void rectangle::display()
{
void accept(); cout<<“Area of rectangle :”<<area;
void calculate(); cout<<“\n Perimeter of
Rectangle :”<<peri;
void display(); }
};
int main()
{ Output :-
Enter length and breadth : 9 5
rectangle r1; Area of rectangle : 45
r1.accept(); Perimeter of Rectangle :28
r1.calculate();
r1.display();
return 0;
}
• Program : Program to declare a class book having data members as title
and author. Accept this data for five books and display this accepted data.
Void book :: accept()
#include<iostream.h> {
cout<<“Enter Title of the book :”<<endl;
using namespace std; cin>>title;
cout<<“\n Enter author name :”<<endl;
class book cin>>auth;
{ }
void book::display()
private : {
cout<<“Book Name :”<<title;
char auth[20]; cout<<“\n Author :” << auth;
}
char title[50]; int main()
public : {
int i;
void accept(); for(i=0;i<5;i++)
{
void display(); b[i].accept();
b[i].display();
}b[2]; }
return 0;
}
• Program to display book information using class object.
#include<iostream.h>
using namespace std; void book :: getdata()
class book
{
cout<<“Enter Book number :”;
{
cin>>bookno;
int bookno; cout<<“Enter book name :”;
char bookname[20]; cin>>nbookname;
char author[20] ; cout<<“Enter Author name :”;
float price; cin>>author;
public : cout<<“Enter Book Price”;
void getdata(); cin>>price;
void display(); }
};
void book::display() Output :
{ Book number : 2504
Book name : C++
cout<<“Book number :”<<bookno<<endl; Author : Bharambe
Price : 120.29 Rs.
cout<<“Book name :”<<bookname<<endl;
cout<<“Author :”<<author<<endl;
cout<<“Price”<<price<<endl;
}
int main()
{
book b;
b.getdata();
b.display();
}
Type Casting Explicit Type Casting or Explicit Type Conversion
Implicit Type Casting or Implicit Type Conversion It is also known as the manual type casting in a program.
It is manually cast by the programmer or user to change from
It is known as the automatic type casting. one data type to another type in a program. It means a user
It automatically converted from one data type to another can easily cast one data to another according to the
without any external intervention such as programmer or user. requirement in a program.
It means the compiler automatically converts one data type to It does not require checking the compatibility of the variables.
another. In this casting, we can upgrade or downgrade the data type of
All data type is automatically upgraded to the largest type one variable to another in a program.
without losing any information. It uses the cast () operator to change the type of a variable.
It can only apply in a program if both variables are compatible
with each other. Syntax of the explicit type casting
int main () (type) expression;
{
short x = 200; int num;
int y; num = (int) 4.534; // cast into int data type
y = x; cout << num;
cout << " Implicit Type Casting " << endl;
cout << " The value of x: " << x << endl;
cout << " The value of y: " << y << endl;
}
int main ()
{
// declaration of the variables
int a, b;
float res;
a = 21;
b = 5;
cout << " Implicit Type Casting: " << endl;
cout << " Result: " << a / b << endl; // it loses some
information
return 0;
}
Manipulators are helping functions that can modify the • ends: It is also defined in ostream and it inserts a null
input/output stream. It does not mean that we change the character into the output stream. It typically works with
value of a variable, it only modifies the I/O stream using std::ostrstream, when the associated output buffer needs to
insertion (<<) and extraction (>>) operators. be null-terminated to be processed as a C string.
• flush: It is also defined in ostream and it flushes the output
Manipulators are special functions that can be included in the stream, i.e. it forces all the output written on the screen or
I/O statement to alter the format parameters of a stream. in the file. Without flush, the output would be the same, but
Manipulators are operators that are used to format the data may not appear in real-time.
display.
To access manipulators, the file iomanip.h should be included in
the program.
Types of Manipulators There are various types of
manipulators:
Manipulators without arguments: The most important
manipulators defined by the IOStream library are provided
below.
• endl: It is defined in ostream. It is used to enter a new
line and after entering a new line it flushes (i.e. it forces
all the output written on the screen or in the file) the
output stream.
• ws: It is defined in istream and is used to ignore the
whitespaces in the string sequence.
Manipulators with Arguments: Some of the manipulators are setw () is a function in Manipulators in C++:
used with the argument like setw (20), setfill (‘*’), and many The setw() function is an output manipulator that
more. These all are defined in the header file. If we want to use inserts whitespace between two variables. You must enter
these manipulators then we must include this header file in our an integer value equal to the needed space.
program. For Example, you can use following manipulators to Syntax:
set minimum width and fill the empty space with any character setw ( int n)
you want: std::cout << std::setw (6) << std::setfill (’*’); As an example,
Some important manipulators in <iomanip> are: int a=15; int b=20;
• setw (val): It is used to set the field width in output cout << setw(10) << a << setw(10) << b << endl;
operations.
• setfill (c): It is used to fill the character ‘c’ on output stream. setfill() is a function in Manipulators in C++:
• setprecision (val): It sets val as the new value for the It replaces setw(whitespaces )’s with a different character. It’s
precision of floating-point values. similar to setw() in that it manipulates output, but the only
• setbase(val): It is used to set the numeric base value for parameter required is a single character.
numeric values. Syntax:
• setiosflags(flag): It is used to set the format flags specified setfill(char ch)
by parameter mask. Example:
• resetiosflags(m): It is used to reset the format flags specified int a,b;
by parameter mask. a=15; b=20;
cout<< setfill(‘*’) << endl;
cout << setw(5) << a << setw(5) << a << endl;
setprecision() is a function in Manipulators in C++:
It is an output manipulator that controls the number of digits to Reference Variable :- Example
display after the decimal for a floating point integer.
Syntax: #include <iostream>
setprecision (int p) using namespace std;
Example:
float A = 1.34255; int main()
{
cout <<fixed<< setprecision(3) << A << endl; int x = 10;
istream and ostream serves the base classes for iostream class.
The class istream is used for input and ostream for the output.