Escolar Documentos
Profissional Documentos
Cultura Documentos
Revision Tour
C ++ Programming language was developed
at AT & T Bell LAB early in 1980’s by
BJARNE STROUSTRUP.
\a Bell
\b Back Space
\f Form Feed
\n New Line
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab
\’ Single Quote
\” Double Quote
\? Question Mark
\on Octal Number ( On represents
the number in octal)
\xHn Hexadecimal number
\0 Null
These escape sequences are used in output
statement cout.
EX:
cout<<“\n\t Wel Come”; // Right statement
1. Mantissa
2. Exponent
Mantissa must be either an integer or proper
real constant. The mantissa is followed by
letter E or e and the exponent must be an
integer.
The floating point number 5.8 can be written
as,
0.58 x 101=0.58E01 or 0.58e01, here E01 or e01
represents 101
0.58 E01
Mantissa part Exponent part
|| Logical OR
= Assignment Operator
*= Assign Product
/= Assign Quotient
%= Assign Remainder
+= Assign Sum
-= Assign Minus
<<= Assign Left Shift
>>= Assign Right Shift
&= Assign Bitwise AND
^= Assign Bitwise XOR
|= Assign Bitwise OR
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
Direct component selector
#include<iostream.h>
void main()
{
int n=45;
cout<<“the integer value is “<<n;
getch();
}
Char data type (for characters):
Characters can store any member of the C++
implementation’s basic character set.
Char type is often said to be an integer type.
#include<iostream.h>
void main()
{
float n=45.123;
cout<<“the float value is “<<n;
getch();
}
The data type double is also used for handling
floating-point numbers.
It occupies twice as much memory as type float, and
stores floating-point with much larger range and
precision (significant numbers after decimal point).
Type double is slower than float.
The void type specifies an empty set of
values. It does not return a value.
A modifier is used to alter the meaning of base
type to fit various situations more precisely.
List of modifiers
i) signed
ii) unsigned
iii) long
iv) short
By using different number of bytes to store values,
C++ offers three types of integers:
Type Approximate size Minimal Range
(in bytes)
Class Structure
Statement 1
Statement 2
Statement 3
The Selection construct means the execution of
statement(s) depending upon a condition-test.
If a condition evaluates to true, a course-of-action
(a set of statements) is followed otherwise another
course-of-action (a different set of statements).
Statement 1
Statement 2
Iteration construct means repetition of set of
statements depending upon a condition test. Till the
time of condition is true.
A set of statements are repeated again and again.
Condition ?
False
True
Statement 1
Exit Statement
Statement 2
An if statement tests a particular condition, if the
condition evaluates to true, a course of action is
followed, i.e., a statement or a set of statement is
executed. Otherwise if the condition evaluated to
false then the course of action is ignored.
Syntax: if (condition)
statement 1;
Eg: if(a>b)
cout<<“ a is big”;
false
if
Condition ?
true
Statement 1
Statement 2
End statement
An if-else statement tests a particular condition, if the
condition evaluates to true, a course of action is
followed, i.e., a statement or a set of statement is
executed. Otherwise if the condition evaluated to
false then the another course of action is executed.
Syntax: if (condition)
statement 1;
else
statement 2;
Eg: if(a>b)
cout<<“ a is big”;
else
cout<<“b is big”;
false
if Condition ? Statement 1 Statement 2
true
Statement 1
Statement 2
End Statement
A nested if is an if that has another if in its body or in its
else body. The nested if can have one of the following three
forms
Form 1 Form 2 Form 3
if (expression 1) if (expression 1) if (expression 1)
{ { {
if(expression 2) body of if if (expression 2)
}
statement 1 statement 1
else
else { else
statement 2 if (expression 2) statement
} statement 1 ……….
else else }
{ statement 2 else
body of else ………. {
} if (expression 2)
}
statement 1
else
statement 2
……….
}
#include<iostream.h> if(ch=='*')
#include<conio.h> result=a*b;
void main() else
{
if(ch=='/')
clrscr();
result=a/b;
char ch;
float a,b, result; else
cout<<"Enter the two values" ; cout<<"Unknown Operation ";
cin>>a>>b; cout<<"\nThe Resultis : "<<result;
cout<<"Enter the Operator [ + - * / ] :"; getch();
cin>>ch; }
if(ch=='+')
result=a+b;
else
if(ch=='-')
result=a-b;
else
A common programming construct in C++ is the if-
else-if ladder.
Syntax:
if (expression 1) statement 1;
else
if (expression 2) statement 2;
else
if (expression 3) statement 3;
……….
else
statement 4;
C++ has an operator that can be alternative to if
statement. The conditional operator ? :
This operator can be used to replace the if statement
of C++.
Syntax Example
if(expression) if(a>b)
statement 1; cout<<a;
else else
statement 2; cout<<b;
gets()
String Functions
puts()
getchar() - It reads a character
putchar() – It writes a character
void main()
{
char ch,next;
cout<<“\nEnter any character:”;
ch=getchar();
next=ch+1;
cout<<“\nThe next character is:”;
putchar(ch);
getch();
}
Output:
Enter any character: a
The next character is: b
gets() - It reads a string of characters from
the keyboard.
puts() – It writes a string on the screen
void main()
{
char str[80];
Int I,count=0;
cout<<“\nEnter any string(max.80 chars) : ”;
gets(str);
cout<<endl<<puts(str);
getch();
}
Output:
Enter any string(max. 80 chars): Welcome to all
Welcome to all
get() Single character Functions
put()
getline() Line oriented I/O Functions
write()
get()
It reads a single character and stores it in a
character variable.
put()
It prints a single character at a time.
void main()
{
char ch;
cout<<“\nEnter any one character”;
cin.get(ch);
cout<<endl;
cout.put(ch);
getch();
}
Output:
Enter any one character : s
s
getline()
It reads a line of text that ends with a new-line
character.
write()
It can display an entire line.
void main()
{
char str1[50],str2[50];
cout<<“\nEnter strings”;
cin.getline(str1,50);
cin.getline(str2,50);
int i,j;
for(i=0;str1[i]!=‘\0’;i++)
for(j=0;str2[j]!=‘\0’;j++)
If(i==j)
cout<<“\nEqual number of characters”;
else
cout<<“\nDifferent number of characters”;
getch(); }
One
Two
Equal number of characters
An array is a collection of variables of
same type that are referenced by a common
name .
C++ allows more type of data i.e its
derived data type.(integer and floating
point).
1.Single dimensional arrays
2. Multi-dimensional arrays(two-dimensional arrays)
It is simplest type of array. C++ array’s
index numbering starts with 0.
An array must be defined before it can be
used to store information . Ex.
Type array-name[size] ;
Base Type:
The data type of array elements is known
as the base type of the array.
void main()
{
float marks[10];
for(int i=0;i<10;i++)
{ cout<<“Enter marks of student”<<i+1<<“\n”;
cin>>marks[i];
}
cout<<“\n”;
for(i=0;i<10;i++)
cout<<“marks[“<<i<<“]=“<<marks[i]<<“\n”;
getch();
}
C++ does not have string data type rather
it implements string as a single –dimension
character array. A string is defined as a
character array that is terminated by a null
character.
Ex.
To declare an character array that hold 10
character string we would write
char str[10];
A two dimensional array is an array in which
each element is itself an array. Ex
An array A[m][n] is an m by n table with m rows
and n columns containing m*n table.
0 N-1
M-1
An array of string is a two dimensional
character array . The size of the first index
determines the number of strings and size of
second index determines maximum length of
each string.
Eg:
char str[60];
Character arrays can also be initialized like:
Char string [10]=“program”;
Will initialize it as
char string [10]={‘p’ , ‘r’ , ‘o’ , ‘g’ , ’r’ , ‘a’ ,’m’, \0}
Two dimension will do it in:
int cube [ 5 ] [2]= { 1 ,1,
2,8
3, 27
4, 68
5, 25};
char string[2][7]={“Sunday”,”Monday”};
C++ allows you to skip the size of an array is
the initialization . This is called an unsized array.
C++ calculates is size and creates it as required.
char s1 [ ] =“first string”;
int val[ ]={2, 3, 4, 5, 6, 7, 8, 9};
float amount [ ]={2341.57, 1900.70};
FUNCTIONS
A function is a sub program that acts on data
and often returns a value
Program
Sub prgm
Sub prgm Sub prgm
Syntax:
type function-name(parameters);
Eg:
cube(x);
void main()
{ float cube(float);
float x,y;
cout<<“\nEnter any one number”;
cin>>x;
y=cube(x);
cout<<“\nThe cube of”<<x<<“is”<<y;
getch();
}
float cube(float a)
{
float n;
n=a*a*a;
return(n);
}
When declaring a function we can specify a
default value to a function’s parameter(s).
Syntax:
type fn-name(pmtr1=value, pmtr2=value);
Eg:
float cube(float x=10.5);
The function can not modify the constant
arguments.
In order to make the argument constant to the
function we can use the keyword const.
Syntax:
type fn-name(const type pmtr1,const type
pmtr2);
Eg:
int sum(const int a , const int b);
In this method copies the values of actual
parameters into the formal parameters, that is , the
function creates its own copy of argument values and
then uses them.
Eg:
int change(int x)
{
}
vol=change(5);
void main()
{ int change(int);
int orig=10;
cout<<“\nThe original value is”<<orig<<endl;
cout<<“\nReturn value of function change()
is”<<change(orig)<<endl;
cout<<“\nThe value after function change() is
over”<<orig<<endl;
getch();
}
int change(int a)
{ a=20;
return(a);
}
The original value is 10
Return value of function change() is 20
The value after function change() is over 10
In this method copies the reference of actual
parameters into the formal parameters, that is , the
same variables value can be accessed by any of the
two names: the original variable’s name and the
reference name.
Eg:
int change(int &x)
{
}
vol=change(a);
void main()
{ int change(int &);
int orig=10;
cout<<“\nThe original value is”<<orig<<endl;
cout<<“\nReturn value of function change()
is”<<change(orig)<<endl;
cout<<“\nThe value after function change() is
over”<<orig<<endl;
getch();
}
int change(int &a)
{ a=20;
return(a);
}
The original value is 10
Return value of function change() is 20
The value after function change() is over 20
C++ interprets an array name as the address
of its first element. C++ treats the name of an
array as if it were a pointer.
When an array is used as an argument to a
function, only the address of the array gets
passed not a copy of the entire array.
void main()
{ void display(int age[10]);
int age[10],i;
cout<<“Enter elements:\n”;
for(i=0;i<10;i++)
cin>>age[i];
display(age);
getch();
}
void display(int a[10])
{ for(int i=0;i<10;i++)
cout<<“\n”<<a[i];
}
void main()
{ void display(int age[]);
int age[10],i,n;
cin>>n;
cout<<“Enter elements:\n”;
for(i=0;i<n;i++)
cin>>age[i];
display(age);
getch();
}
void display(int a[])
{ for(int i=0;i<n;i++)
cout<<“\n”<<a[i];
}
void main()
{ void display(int age[10]);
int age[10],i;
cout<<“Enter elements:\n”;
for(i=0;i<10;i++)
cin>>age[i];
display(age);
getch();
}
void display(int *a)
{ for(int i=0;i<10;i++)
cout<<“\n”<<a[i];
}
A function calling itself in its own body is a recursive
function.
Eg:
fac(int n)
{
if(n==1) return 1;
else
return n*fac(n-1);
}
Return statement: (a value is return from a function)
The function terminates when either a return
statement is encountered or the last statement in the
function is executed.
A function can return only a single value.
Syntax:
return (value);
Eg:
int add( )
{
c=a+b;
return (c);
}
The scope rules of a language are the rules that
decide, in which part(s) of the program, a particular
piece of code or data item would be known and can
be accessed therein.
There are four kinds of scopes in C++:
i) Local scope
ii) Function scope
iii) File scope
iv) Class scope
A structure is a collection of variables referenced
under one name.