You are on page 1of 53

Compiled By:

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 << "Hello"; // prints Hello


cout << Hello; // prints the content of variable Hello

Multiple insertion operations (<<) may be chained in a single statement:

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;

cout << "Enter your age:";


cin >> age;
cout << "\nYour age is: "<<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 >> b;


This is equivalent to:

cin >> a;
cin >> b;
#include<iostream>
#include<conio.h>

//Standard namespace declaration


using namespace std;

//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>

//Standard namespace declaration


using namespace std;

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

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;

cout<<"value of val is"<< :: val;


getch();
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
int val=500;
int val=700;
int main()
{
int val=200;

cout<<"value of val is"<< :: val;


getch();
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
namespace variable1{
int val=500;
}
namespace variable2{
int val=100;
}
int main()
{
int val=200;

cout<<"value of val is"<<variable1 :: val;


cout<<"\nvalue of val is"<<variable2:: val;
getch();
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
namespace first
{
void display()
{
cout<<"from namespace\n";
}
}
void dispaly()
{
cout<<"this is global scope\n";
}
int main(){
first :: display();
dispaly(); getch();
return 0; }
Manipulators: Manipulators are the operators used with the insertion operator
(<<) to modify or manipulate the way data is displayed. The most commonly
used manipulators are endl, setw, and setprecision.
Note: The header file for setw and setprecision manipulators is iomanip.h

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

Keywords: Keywords are explicitly reserved identifiers and can not be


used as names for the program variables or other user-defined program
elements. Some keywords are int,
auto, switch, case, do, else, public, default, continue etc.
Type Conversion There are two types of type conversion: automatic
conversion and type casting.
Automatic Conversion (Implicit Type Conversion) When two operands
of different types are encountered in the same expression, the lower
type variable is converted to the type of the higher type variable by the
compiler automatically. This is also called type promotion. The order of
types is given below:
Data Type Order long double (highest) double float long int char
(lowest)
Type Casting Sometimes, a programmer needs to convert a value from
one type to another in a situation where the compiler will not do it
automatically. For this C++ permits explicit type conversion of variables
or expressions as follows:
(type-name) expression //C notation
type-name (expression) //C++ notation
Functions A function is a single comprehensive unit that performs a specified task.
This specified task is repeated each time the function is called. Functions break
large programs into smaller tasks. They increase the modularity of the programs
and reduce code redundancy.
Function Prototype (Function declaration) Function prototype lets the compiler
know the structure of function in terms of its name, number and type of arguments
and its return type.
Syntax: return-type function-name(datatype1, datatype2, …,datatype n);

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,

return-type function-name(datatype1 variable1, datatype2 var2, …., datatype n var n)


{
……………… ;
………………….; //body of the function
……………….;
}
#include<iostream>
#include<conio.h>
using namespace std;
void display(void);
int main()
{
display();
getch();
return 0;
}
void display(void)
{
cout<<"hello BMC";
}
Default Arguments In C++, a function can be called without specifying all its arguments.
But it does not work on any general function. The function declaration must provide
default values for those arguments that are not specified. When the arguments are
missing from function call, default value will be used for calculation.
#include<iostream>
using namespace std;
float interest(int p, int t = 5, float r = 5.0 );
main()
{
float rate, i1,i2,i3;
int pr , yr;
cout<<"Enter principal, rate and year";
cin>>pr>>yr>>rate; //5000 2 10
i1=interest(pr ,yr ,rate);
i2=interest(pr , yr);
i3=interest(pr);
cout<<i1<<endl<<i2<<endl<<i3; //1000 500 1250
return(0);
}
float interest(int p, int t,float r)
{
return((p*t*r)/100);
}
#include<iostream>
using namespace std;
int sum(int w, int x= 5, int y = 3);
main()
{
int s1,s2,s3;
int pr , yr;
s1=sum(10 ,20 ,30);//60
s2=sum(10 , 20);//33
s3=sum(10);//18
cout<<s1<<endl<<s2<<endl<<s3;
return(0);
}
int sum(int w, int x,int y)
{
return (w+x+y);
}
NOTE: The default arguments are specified in function declaration only and not
in function definition. Only the trailing arguments can have default values. We
must add defaults from right to left. We cannot provide a default value to a
particular argument at the middle of an argument list. Default arguments are
used in the situation where some arguments have same value. For eg., interest
rate in a bank remains same for all customers for certain time.
Inline Function
Inline Functions:
We say that using functions in a program is to save some memory space because all the calls to the functions
cause the same code to be executed. However, every time a function is called, it takes a lot of extra time in
executing a series of instructions. Since the tasks such as jumping to the function, saving registers, pushing
arguments into the stack and returning to the calling function are carried out when a function is called. When
a function is small, considerable amount of time is spent in such overheads. C++ has a solution for this
problem. To eliminate the cost of calls to small functions, C++ proposed a new feature called INLINE function.
When a function is defined as inline, compiler copies its body where the function call is made, instead of
transferring control to that function. A function is made inline by using a keyword “inline” before the function
definition.

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.

The syntax for defining the function inline is:


inline return-type function-name(parameters)
{
// function code
}
#include<iostream>
#include<conio.h>
using namespace std;
inline int max(int x, int y)
{
return ((x>y)?x:y);
}
int main()
{
int a,b;
cout<<"enter the value for a & b"<<endl;
cin>>a>>b;
cout<<"larger is:"<<max(a,b);
getch();
return 0;
}
Function that share the same name are said to be overloaded functions and the process is referred
to as function overloading. i.e. function overloading is the process of using the same name for
two or more functions. Each redefinition of a function must use different type of parameters, or
different sequence of parameters or different number of parameters. The number, type or sequence
of parameters for a function is called the function signature.
When we call the function, appropriate function is called based on the parameter passed. Two
functions differing only in their return type can not be overloaded.
For eg int add(int , int ) and
float add(int,int)
A function is overloaded when same name is given to different function.
The two functions with the same name will differ at least in one of the following.
a) The number of parameters
b) The data type of parameters
c) The order of appearance
#include<iostream> }
using namespace std;
//function declaration float perimeter(float r)
float perimeter(float); {
int perimeter(int,int); return(2*3.14*r);
int perimeter(int,int,int); }
int main() int perimeter(int l,int b)
{ {
cout<<"Perimeter of a circle:“ ; return(2*(l+b));
cout<<perimeter(2.0)<<endl; }
cout<<"Perimeter of a rectangle: “ ; int perimeter(int a,int b,int c)
cout<<perimeter(10,10)<<endl; {
cout<<"Perimeter of a triangle: “ ; return(a+b+c);
cout<<perimeter(5,10,15); }
return (0);
Recursive function Recursion is a powerful technique of writing complex
algorithms in an easy way. It defines the problem in terms of itself. In this
technique , a large problem is divided into smaller problem of similar nature
as original problem.so that the smaller problem is easy to solve and in the
most case they can be solved easily. Hence to implement this technique, a
programming language support the function that is capable of calling itself.
C++ support such function and these function are called recursive functions
//For example: to find the factorial cout<<"the factorial of given number is"<<f;
of a given number getch();
#include<iostream> return 0;
#include<conio.h> }
using namespace std; int fact(int num)
int fact(int num); {
int main() if(num==0)
{ return 1;
int num; else
cout<<"Enter a number"; return (num*fact(num-1));
cin>>num; }
int f=fact(num);
#include<stdio.h>//example of C structure
#include<conio.h>
int main()
{
struct employee
{
char name[20];
int age;
float salary;
}e1,e2;
printf("enter the record of two employee");
scanf("%s%d%f", e1.name, &e1.age,&e1.salary);
scanf("%s%d%f", e2.name, &e2.age,&e2.salary);
printf("Employee Datails");
printf("%s%d%f", e1.name,e1.age,e1.salary);
printf("%s%d%f",e2.name,e2.age,e2.salary);
getch();
return 0;
}
#include<iostream>//program using structure in c++
#include<conio.h>
using namespace std;
struct student int main()
{ {
char name[30];
int roll; struct student s1;
void getdata() s1.getdata();
{
cout<<"enter the name"<<endl;
s1.display();
cin>>name; return 0;
cout<<"enter the roll no."<<endl; }
cin>>roll;
}
void display()
{
cout<<"name of student: "<<name<<endl;
cout<<"roll no."<<roll<<endl;
}
};
C Structure C++ Structure

1. Collection of Variables/Data 1. Collection of Variables/ Data and Data


Function.

2. Member are public 2. Member are Public/private/protected

3. User Defined 3. User Defined

You might also like