Professional Documents
Culture Documents
Sushant Paudel
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the value of x on screen
cout << "This " << " is a " << "single C++ statement";
Chaining insertions is especially useful to mix literals and variables in a single statement:
cout << "I am " << age << " years old and my zipcode is " << zipcode;
Assuming the age variable contains the value 24 and the zipcode variable contains 90064, the output of the
previous statement would be:
I am 24 years old and my zipcode is 90064
For formatted input operations, cin is used together with the extraction
operator, which is written as >> (i.e., two "greater than" signs). This
operator is then followed by the variable where the extracted data is stored.
For example:
int age;
cin >> age;
The first statement declares a variable of type int called age, and the second extracts from cin a value
to be stored in it. This operation makes the program wait for input from cin; generally, this means
that the program will wait for the user to enter some sequence with the keyboard. In this case, note
that the characters introduced using the keyboard are only transmitted to the program when the
ENTER (or RETURN) key is pressed.
#include<iostream>
using namespace std;
int main()
{
int age;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}
• Extractions on cin can also be chained to request more than one datum
in a single statement:
cin >> a;
cin >> b;
#include<iostream>
#include<conio.h>
//Main Function
int main()
{
//Standard Ouput Statement
cout<<"My First C++ Program";
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
#include<iostream>
#include<conio.h>
//Main Function
int main()
{
//Standard Ouput Statement
cout<<"My First C++ Program"<<endl;
cout<<"Hello world";
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
#include<iostream>
#include<conio.h>
int main()
{
int val=200;
std :: cout<<"value of val is"<<val;
getch();
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
int val=500;
int main()
{
int val=200;
The endl Manipulator This manipulator causes a linefeed to be inserted into the
output stream. It has the same effect as using the newline character ‘\n’.
for example, the statement
cout<<“First value =”<<first<<endl<<“Second value =”<<second;
will cause two lines of output
The setw Manipulator This manipulator causes the output stream that follows it to be
printed within a field of n characters wide, where n is the argument to setw(n). The
output is right justified within the field. For example,
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(11)<<"Bhaktapur"<<endl<<setw(11)<<"Multiple"<<endl<<setw(11)<<"Ca
mpus";
getch();
return 0;
}
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
cout<<setw(5)<<1<<endl;
cout<<setw(5)<<10<<endl;
cout<<setw(5)<<10000<<endl;
getch();
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
cout<<"1\n10\n100\n";
getch();
return 0;
}
The setprecision Manipulator This manipulator sets the n digits of
precision to the right of the decimal point to the floating point output,
where n is the argument to setprecision(n).
For example, float a = 42.3658945, b = 35.24569, c = 58.3214789, d =
49.321489;
cout<<a<<endl <<setprecision(3)<<b<<endl<<c<<endl<<setprecision(2)<<d
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
float e=22/7.0;//3.142857
cout<<e<<endl;
cout<<setprecision(2)<<e;
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
float e=22.0/7;
clrscr();
cout<<e<<endl;
cout<<setprecision(2)<<e<<endl;
cout<<setprecision(2)<<129;
getch();
return 0;
}
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
cout<<setfill('*');
cout<<setw(5)<<1<<endl;
cout<<setw(5)<<10<<endl;
cout<<setw(5)<<100<<endl;
getch();
return 0;
}
Reference Variable: Reference variable is an alias (another name) given
to the already existing variables of constants. When we declare a
reference variable memory is not located for it rather it points to the
memory of another variable.
Normal
variable
Pointer
variable
Reference
variable
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x=500;
int &y=x;
cout<<"value of x="<<x;
cout<<"value of y="<<y;
cout<<"address of x="<<&x;
cout<<"address of y="<<&y;
getch();
return 0;
}
Tokens: Tokens are the smallest individual units in a program. Keywords,
identifiers, constants, strings and operators are tokens in C++.
Function Call Function call is the process of making use of function by providing it
with the parameters it needs. We call a function as follows.
function-name (argument1, argument2, .. ,argument n);
Function Definition Function definition is a process of defining how it does what
it does or in other words, during function definition, we list the series of codes
that carry out the task of the function. A function is defined as follows,
If a function is inline, the compiler places a copy of the code of that function at each point where the function is
called at compile time.
• C++ provides an inline functions to reduce the function call overhead. Inline function is a function that is
expanded in line when it is called. When the inline function is called whole code of the inline function gets
inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler
at compile time. Inline function may increase efficiency if it is small.