Operators with programm

 Operators with Program.


→All 'C'-Operature are valid in C++ also in addition c++ introduce some new operators they are

Operator Symbol Operator Name
<<Insertion
 >>Extraction
:: Scope resolution
::*Pointer to member declaration
→*Pointer to member operator memory allocation operator
newMemory allocation operator
delete memory deallocation operator
endlline feed operator new line
setwfield width operator

i】Insertion operator(<<) & Extraction operator(>>):-
This two operators are used to insert and display the values as like as printf () and scanf().
Syntax:-
Extraction operator:-
cin>>variable-name;
Insertion
cout<<variable_name;
  • Program on Insertion & Extrration operatore.

#include<iostream.h>
int main()
{
int a;
cout<<"Enter a value\n";//Insertion operator
cin>>a; //Extraction operator
cout<<"a="<<a;
return 0;
}
Out put:-
Enter a value
8
a=8

ii】Scope Resolution Operator

This operator is used to access the Global variable but also has a local variable with a same variable name.

Syntax:-
::Variable_name;
 Return type<class_name>::method_name()
{
State_1;
State_2;
:
:
}
↣It used to define number of functions outside of the class.                          
Ex:-
#include<iostream.h>
int a=30;
int main()
{
int a=20;
cout<<"::a="<<::a<<"\n";
cout<<"a="<<a;
return 0;
}
Out put:-
::a=30
a=20

New operator:-
It is used to allocate memory dynamically

Syntax:-
Pointer variable= new operator data_type;
⇒ The new operator can be used an object of any data type
⇒ In the pointer variable is a point of type data type then new operator allocate memory to hold a data objects of type data type.
Ex:-p=new int;
Program on new operator
#include<iostream.h>
#include<conio.h>
int main()
{
int *p,n,i;
clrscr();
cout<<"Enter size\n";
cin>>n;
p=new int[n];
cout<<"Enter"<<n<<"value\n";
for(i=0;i<n;i++)
cin>>*(p+i);
for(i=0;i<n;i++)
cout<<"\n Enter value is"<<*(p+i);
getch();
}
Out put:-
Enter size
3
Enter 3 value
5
7
9

 Enter value is5
 Enter value is7
 Enter value is9
[Program finished]

Delete operator
:-
⇒It is used to release dynamic allocate memory.

Syntax:-
detete [] variable;
Ex:-delete []a;
Program on delete operator
#include<iostream.h>
#include<conio.h>
int main()
{
int *p,n,i;
clrscr();
cout<<"Enter size\n";
cin>>n;
p=new int[n];
cout<<"Enter"<<n<<"value\n";
for(i=0;i<n;i++)
cin>>*(p+i);
for(i=0;i<n;i++)
cout<<"\n Enter value is"<<*(p+i);
delete [] n;
getch();
}
Out put:-
Enter size
3
Enter 3 value
5
7
9

 Enter value is5
 Enter value is7
 Enter value is9
[Program finished]

Manipulators in CPP:-
⇒ manipulators are used to manipulate or change the output
⇒ manipulators are endl,setw()
The head file for manipulators is iomanip.h
endl:-
endl is used to new line fetch
#include<iostream.h>
#include<conio.h>
int main()
{
int a,b;
clrscr();
cout<<"Enter a,b values"<<endl;
cin>>a>>b;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
getch();
}
Out put:-
Enter a,b values
4
7
a=4
b=7

[Program finished]
Setw ():-
setw() sets the number of characters to be used as the field width for the next enseration operation.
⇒setw() is declared inside iomaip.h
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"with out setw()"<<endl;
cout<<"1"<<endl<<"11"<<endl;
cout<<"111"<<endl;
cout<<"with setw()"<<endl;
cout<<setw(4)<<"1"<<endl;
cout<<setw(4)<<"11"<<endl;
cout<<setw(4)<<"111"<<endl;
return 0;
}
Out put:-
with out setw()
1
11
111
with setw()
     1
   11
 111

[Program finished]

Comments

Popular posts from this blog

History of C language

Lab programs of C

Algorithm Pseudopods Flow-chat