Академический Документы
Профессиональный Документы
Культура Документы
HINDU COLLEGE,
UNIVERSITY OF DELHI
C++ LANGUAGE
MUKESH KUMAR
Student of The Hindu college
University of Delhi
2
Dedicated to my best teacher
Mr. S.K.Chauhan and Mr. Amit sir
3
Contents
1 C++ Fundamentals 3
1.1 Character set . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 C++ Token . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Identifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6.1 Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6.2 Float . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6.3 character . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6.4 void . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 Varables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.8 operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.8.1 Assignment operators . . . . . . . . . . . . . . . . . . . 6
1.8.2 Logical operator . . . . . . . . . . . . . . . . . . . . . . 7
1.8.3 Relational operator . . . . . . . . . . . . . . . . . . . . 7
1.8.4 Arithmetic operators . . . . . . . . . . . . . . . . . . . 7
1.8.5 Auto increment / Decrement operators . . . . . . . . . 8
1.9 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Control Stetements 13
3.1 Conditional Structure . . . . . . . . . . . . . . . . . . . . . . . 14
3.1.1 If and else . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.2 Itration Structures(Loop) . . . . . . . . . . . . . . . . . . . . 15
3.2.1 While Loop . . . . . . . . . . . . . . . . . . . . . . . . 15
1
3.2.2 The do-while Loop . . . . . . . . . . . . . . . . . . . . 17
3.2.3 for loop . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.3 Jumping Statement . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3.1 The break Statement . . . . . . . . . . . . . . . . . . . 20
3.3.2 The Continue Statement . . . . . . . . . . . . . . . . . 20
3.3.3 The goto Statement . . . . . . . . . . . . . . . . . . . . 21
3.3.4 The exit function . . . . . . . . . . . . . . . . . . . . . 22
3.4 The Selective Structure . . . . . . . . . . . . . . . . . . . . . . 22
3.4.1 Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2
Chapter 1
C++ Fundamentals
Like all programming language c++ has its own vocabulary and grammar.
The sequence instruction that are given to execute a specific task is called a
program. The instructions are formed of certain words, symbols and num-
bers which strictly follow that c++ grammar or sentence.
3
1.3 Identifier
The identifier maybe the name of a variable or a constant of a function. usu-
ally for all programming languages, the first character of an identifier will
be an alphabet followed by either by numbers or alphabets. unlike, C++
does not impose any restriction on identifier length. Also not that all C++
compilers are case sensitive; underscores are allowed for battery readability,
and no is special characters are allowed while defining an identifier.
1.4 keywords
Keywords are words that have specific purpose in c++ language. So , we
can not use them as our variable name or function name. there are about 63
keywords in C++ .
1.5 constants
The value of a quantity that does not vary throughout, the program is called
a constant .
1.6.1 Integer
These are numbers without decimal part, which may or maynot be prefixed
with minus(-) sign.
4
e.g.
The modifiers when applied to integers and integer range are as fol-
lows.
short integer range is -32768 to +32767 and occupies 2 bytes.
integer range is from -2147483648 to 2147483647and occupies 4 bytes.
long integer range is -9223372036854775808L to 9223372036854775807 and
occupies 8 byte.
1.6.2 Float
float are numbers with decimal part. In C++, floats are stored with and
digits of decimal precision.
1.6.3 character
Any single letter enclosed within single quotes, can be defined as a character
type data.
Like integer, we can also specify a variable of char using the mod-
ifiers signed or unsigned, which will affect the range of integers that can be
represented.
1.6.4 void
It is valueless. It has two uses: One to explicitly declare a function as
returning no value: second to create generic pointers.
5
1.7 Varables
A variable is the storehouse for constants. (variables are seperated by comma).All
C++ variable must be declared They are used. The general form of variable
declaration is ,
Datatype variablename;
declaration statement must end with a semicolon.
1.8 operators
c++ is very rich in built-in operators . An operator is a symbol that tells
the compiler to perform specific mathematical or logical manipulation.lke C
,C++ also has three general classes of operators: airthmetic,relational and
logical etc.
(e.g.)
a=15; simple
a=b=c=2; multiple
a+=3; compound
In the third example += means add 15 to a (or) increment a by 15
6
1.8.2 Logical operator
C++ has three logical operators as
&& means Logical AND
|| means Logical OR
! means Logical NOT
These are used when we want to test more than one condition and
make dicisions.
(e.g.) a>b&& x==10;
this expression is true only if a>b is true and x==10 is also true. if either(or
both) of them is false, the expression is also false.
7
*multiplication
- subtraction
/ division
A postfix operator first assigns the value to the variable on the left
and then assigned to the variable on the left. (e.g.)
a=5
b=++a
then the result will be
a=b=6
A postfix operator first assigns the value to tjhe variable on the left
and then increment/ decrement the oprend.
(e.g.)
a=5
b=a++
here b=5 and a=6
1.9 Comments
C++ provides two styles of comments. The first comment style supports
traditional c-language comments. All text appearing between /*and */ is
treated as a comment. comments of this type can span multiple lines
8
e.g.
/*multiline comment*/
The second comment style supports single line C++ comments. All text
follow // untile the end of the line is treated as a comment
e.g.
//single line comment
9
Chapter 2
before writing a program in any language one must know about its structure
for basic it is line number and FORTRAN it is format statements likewise
on C++ program must adhere to the structure it is designed for. in simple
C++ program should have the following details
2.1 explanation
The program lines have been indicated for explanation purpose only and they
do not form part of the program.
10
<iostream> include in this program. Also note that there are no space be-
tween the angled brackets and iostream. If we insert any blank inside, most
compliers (which is compile our program in machine language ) consider it
as a serious error and the program wont compile.
Line 3. The main difference between old style C++ program and
new style C++ program is inclusion of this statement, using namespace
std;. According to ANSI C++ definition, all the classes, objects and func-
tions of the standard C++ library are defined within namespace std. Thus,
this statement used to indicate all the functions used in the program belongs
to the standard library. This using directive indicates that we want to refer
to things within the namespace called std.
cout<<All is well;
this statement displayed massage on the output screen.
Line 8. the closing brace } is used to indicate the end of the func-
tion main().
The standard input and output statement avialable in c++ are cin
and cout. The cin refers keyword and cout refer display screen respactivly.In
C++ we use two operators namely extraction operator (>>) and insertion
11
operator(<<) for input and output purposes. The header file iostream con-
sists of all necessary definition for the operators, cin and cout. Hence it is
essential to include the header file iostream in all the programs requiring
input and output operations.
For example, if we want to enter 5,2,3 for a,b,c and all belong to int
type, then the declaration and input statements might look like this ,
int a, b, c;
cin>>a>>b>>c;
and this way of giving input is called cascading of input,then we have to
enter data follows
523
note that there is space between two values.
12
Chapter 3
Control Stetements
Most of the control structures that we will see in this section re-
quire a generic statement as part of its syntax. A statement can be either a
simple statement (a simple instruction ending with a semicolon) or a com-
pound statement (several instructions grouped in a block), like the one just
described. In the case that we want the statement to be a simple statement,
we do not need to enclose it in braces ({ }). But in the case that we want the
statement to be a compound statement it must be enclosed between braces
({ }), forming a block.
13
3.1 Conditional Structure
3.1.1 If and else
The if keyword is used to execute a statement or block only if a condition is
fulfilled. Its form is:
if (condition) statement
if (x == 100)
cout << x is 100;
If we want more than a single statement to be executed in case that the
condition is true we can specify a block using braces :
if (x == 100)
{
cout<< x is ;
cout << x;
}
if (condition)
statement1
else statement2
For example:
if (x == 100)
cout << x is 100;
else
cout << x is not 100;
14
prints on the screen x is 100 if indeed x has a value of 100, but if it
has not -and only if not- it prints out x is not 100. The if + else structures
can be concatenated with the intention of verifying a range of values. The
following example shows its use telling if the value currently stored in x is
positive, negative or none of them (i.e. zero):
if (x >0)
cout << x is positive;
else if (x <0)
cout << x is negative;
else
cout << x is 0;
15
{
int n;
cout << Enter the starting number > ;
cin >>n;
while (n>0)
{
cout << n << , ;
n;
}
cout << FIRE!;
return 0;
}
16
continue looping forever. In this case we have included n; that decreases
the value of the variable that is being evaluated in the condition (n) by one -
this will eventually make the condition (n0) to become false after a certain
number of loop iterations: to be more specific, when n becomes 0, that is
where our while-loop and our countdown end.
Of course this is such a simple action for our computer that the
whole countdown is performed instantly without any practical delay between
numbers.
Its functionality is exactly the same as the while loop, except that
condition in the do-while loop is evaluated after the execution of statement
instead of before, granting at least one execution of statement even if con-
dition is never fulfilled. For example, the following example program echoes
any number you enter until you enter 0.
// number echoer
#include <iostream>
using namespace std;
int main ()
{
unsigned long n;
do {
cout << Enter number (0 to end): ;
cin >> n;
cout << You entered: << n << ;
} while (n != 0);
return 0;
}
17
Enter number (0 to end): 160277
You entered: 160277
Enter number (0 to end): 0
You entered: 0
The do-while loop is usually used when the condition that has to
determine the end of the loop is determined within the loop statement itself,
like in the previous case, where the user input within the block is what is
used to determine if the loop has to end. In fact if you never enter the value
0 in the previous example you can be prompted for more numbers forever.
18
{
for (int n=10; n>0; n)
{
cout << n << , ;
}
cout FIRE!;
return 0;
}
10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
FIRE!
The initialization and increase fields are optional. They can remain
empty, but in all cases the semicolon signs between them must be written.
For example we could write: for (;n<10;) if we wanted to specify no initializa-
tion and no increase; or for (;n<10;n++) if we wanted to include an increase
field but no initialization (maybe because the variable was already initialized
before). Optionally, using the comma operator (,) we can specify more than
one expression in any of the fields included in a for loop, like in initialization,
for example. The comma operator (,) is an expression separator, it serves to
separate more than one expression where only one is generally expected. For
example, suppose that we wanted to initialize more than one variable in our
loop:
19
3.3 Jumping Statement
3.3.1 The break Statement
Using break we can leave a loop even if the condition for its end is not ful-
filled. It can be used to end an infinite loop, or to force it to end before its
natural end. For example, we are going to stop the count down before its
natural end (maybe because of an engine check failure?):
10, 9, 8, 7, 6, 5, 4, 3,
countdown aborted!
20
// continue loop example
#include <iostream>
using namespace std;
int main ()
{
for (int n=10; n>0; n)
{
if (n==5) continue;
cout << n << , ;
}
cout << FIRE!;
return 0;
}
10, 9, 8, 7, 6, 4, 3, 2, 1,
FIRE!
21
n;
if (n>0) goto loop;
cout << FIRE!;
return 0;
}
10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
FIRE!
switch (expression)
{
case constant1:
group of statements 1;
break;
case constant2:
group of statements 2;
break;
22
.
.
.
default:
default group of statements
}
switch (x)
{
case 1:
cout << x is 1;
break;
case 2:
cout << x is 2;
break;
default:
cout << value of x unknown;
}
if (x == 1)
{
cout << x is 1;
}
23
else if (x == 2)
{
cout << x is 2;
}
else
{
cout << value of x unknown;
}
The switch statement is a bit peculiar within the C++ language be-
cause it uses labels instead of blocks. This forces us to put break statements
after the group of statements that we want to be executed for a specific con-
dition. Otherwise the remainder statements including those corresponding
to other labels- will also be executed until the end of the switch selective
block or a break statement is reached. For example, if we did not include a
break statement after the first group for case one, the program will not auto-
matically jump to the end of the switch selective block and it would continue
executing the rest of statements until it reaches either a break instruction
or the end of the switch selective block. This makes unnecessary to include
braces { } surrounding the statements for each of the cases, and it can also be
useful to execute the same block of instructions for different possible values
for the expression being evaluated. For example:
switch (x)
{
case 1:
case 2:
case 3:
cout << x is 1, 2 or 3;
break;
default:
cout << x is not 1, 2 nor 3;
}
24
constants, use a concatenation of if and else if statements.
some programs
//fibonacci series
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,first=0,second=1,next;
cout<<enter the no. of terms of you want<<endl;
cin>>n;
for(i=0;i<n;i++)
{
if(i<=1)
next=i;
else
{
next=first+second;
first=second;
second=next;
}
cout<<next<<endl;
}
getch();
}
// ascending order
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x)
void main()
{
clrscr();
int n,i,j,a[100],temp;
cout<<how many numbers you want to arrange in ascending order;
cin>>n;
cout >>please enter those numbers ;
25
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j <n-i;j++)
{
if(a[i]>a[i+])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
cout<<your entered numbers,in ascending order,are: n;
for(i=0;i<n;i++)
{
cout<<a[i]<< t;
}
getch();
}
// descending order
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j,a[100],temp;
cout<<how many numbers you want to arrange in descending order;
cin>>n;
cout<<please enter those numbers;
for(i=0;in;i++)
{
cin>>a[i];
}
26
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]<a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
coutyour entered number ,in descending order, are:;
for(i=0;i<n;i++)
{
cout<<a[i]<< t;
}
getch();
}
// secant method
#include<iostream.h>
#include<conio.h>
#include<math.h> #define f(x) ((x-cos(x))
void main()
{
clrscr();
float x,x2,x1,accuracy;
cout<<please input boundary :<<endl;
cout<<please input lower boundary: ;
cin>>x;
cout<<please input upper boundary: ;
cin>>x1;
cout<<please input accuracy: ;
cin>>accuracy;
do
{
x2=x1-f(x1)*(x1-x)/(f(x1)-f(x));
27
x=x1;
x1=x2;
}
while(accuracy<fabs(f(x2)));
cout<<solution is = <<x2;
getch();
}
//simpson 1/3
#include<iostream.h>
#include<conio.h>
#define f(x) (2*(x)-2)
void main()
{
clrscr();
float a,b,h,x,sum,I,n;
cout<<please enter limits : ;
cout<<please enter lower limit: ;
cin>>a;
cout<<please enter upper limit:;
cin>>b;
cout<<input number of iteration : ;
cin>>n;
h=(b-a)/n;
for(int c=1;c<=n;c++)
{
x=a+h*c;
if(c%2 == 0)
sum=sum+2*f(x);
else
sum=sum+4*f(x);
}
I=h*(sum+f(b))/3;
cout<<solution is =<<I;
getch();
}
//bisection method
28
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) ((x)-cos(x))
void main()
{
clrscr();
float a,b,c,accuracy,solu ;
cout<<enter boundary b/w solution you want;
cin>>a>>b;
cout<<enter accuracy=;
cin>>accuracy;
if(f(a)*f(b)<0)
{
do
{
c=(a+b)/2;
if(f(c)*f(b)>0)
b=c;
else
a=c;
}
while(accuracy<fabs(f(c)));
cout<<solution is =<<c;
}
else
cout<<solution does not lies b/w boundary;
getch();
}
29
cout<<how many numbers you want to enter ;
cin>>n;
cout<<input those number n;
for(i=0;i <=n;i++)
{
cin>>a[i];
}
max=a[0];
for(i=0;i<=n;i++)
{
if(max<a[i])
{
max=a[i];
poss=i+1;
}
}
cout<<larger number is=<<max;
cout<< n position=<<poss;
getch();
}
30
x=x1;
}
while(accuracy<fabs(f(x)));
cout<<solution is =<<x;
getch();
}
31
cout<<enter upper limit=;
cin>>b;
h=(b-a)/n;
for(c=2;c<=n+1;c++)
{
x=(a+h*c);
sum=sum+2*f(x);
}
I=h/2*(f(a)+sum);
cout<<integration is =<<I;
getch();
}
32
Index
33