Professional Documents
Culture Documents
Neethu krishna
C++ is an object oriented programming
language.
Digits 0-9
1. Keywords
2. Identifiers
3. Literals
4. Punctuators
5. Operators
Identifiers are fundamental building blocks of a
program
E.g.:-
break, int, float, new, public etc.
Literals are data items that never change
their value during the program run.
integer-constant
character-constant
floating-constant
string-literal
Whole numbers without any fractional part.
E.g.:-
‘z’ or ‘A’
Numbers having fractional parts.
Written in 2 forms:
fractional form
exponent form
A real constant in fractional form must have
at least one digit before and after the
decimal point.
E.g..:-
2.0, 17.5, -13.0
A real constant in exponent form has two
parts : a mantissa and an exponent
Exponent integer
E.g..:-
2.5E05, -0.172e-3
A string literal is a sequence of characters
surrounded by double quotes.
E.g.:-
“abc” size 4
The following characters are used as
punctuators :
( ) Parentheses
[ ] Brackets
{ } Braces
, Comma
; Semicolon
: Colon
* Asterisk
… Ellipsis
= Equal to
# Pound
Operators are tokens that trigger some
kind of computation .
Two types :
unary operators
binary operators
Require only one operand to operate upon.
& Address operator
* Indirection operator
+ Unary plus
- Unary minus
~ Bitwise complement
++ Increment operator
-- Decrement operator
! Logical negation
Require two operands to operate upon.
Arithmetic operators +, -, *, /, %
Conditional operator ? :
// Program to print a string
#include<iostream.h>
void main( )
{
}
OUTPUT
#include<iostream.h> preprocessor
directive
Two types :
Fundamental data types
Derived data types
Fundamental data types are those that are
not composed of other data types.
5 types:
int
char
float
double
void
Integers are represented in C++ by int data
type.
E.g.:-
enumeration
array
pointers
An array is a finite collection of n elements
of same type referenced under one name.
Arrays can be
one dimensional
two dimensional
multi dimensional
E.g.:-
int a[10];
float b[3][4];
Enumeration is an alternative method for naming
integer constants.
E.g.:-
enum { start, pause, go};
Variable declaration
type var_name;
Variable initialization
int sum = 0;
There are 2 values associated with a
variable :
lvalue
rvalue
rvalue
data value stored in the memory.
lvalue
the address of the memory at which the
data value is stored
Memory 1051 1052 1053 1054 1055
address
Data value of 10 25
the variable
Variable’s A B
name
++a or a++ a = a + 1
E.g.:-
sum = 0;
count =1;
sum = sum + (++ count);
sum 2 count 2
When the increment/decrement operator follows
its operand, C++ performs the
increment/decrement operation after using the
value of the operand.
E.g.:-
sum = 0;
count =1;
sum = sum + (count++);
sum 1 count 2
Evaluate x = ++y+ 2y if y = 6.
int n = 7;
cout<<“n++ =“<<n++;
cout<<“n=“<<n;
Operator Meaning
== Equal to
!= Not equal to
Relational operator determine the relation among
different operands.
Comparison true result 1
false result 0
0 1 1 1 0 0 0 1
1 0 0 0 1 1 0 1
3 3 0 1 0 1 1 0
2 6 1 1 0 0 0 1
Operator Definition Example Result
E.g.:-
General form is :
sizeof var_name; or
sizeof (type);
E.g.:-
int a;
sizeof a returns 2
sizeof (float) returns 4
A comma operator is used to string together
several expressions.
E.g.:-
b = (a=3, a+1);
Two types
E.g.:-
If either operand is float the other is converted to
float.
If either operand is double the other is converted
to double.
Explicit conversion is user-defined.
General form:
(type) expression
E.g.:-
(float)(x+y/2)
{
statement_1;
statement_2;
.
.
.
.
statement_n;
}
In a program statements may be executed
sequentially, selectively or iteratively.
Two types :
1) if
2) switch
General form :
if(expression)
{
statement block
}
else statement_4;
#include<iostream.h>
#include <conio.h>
void main()
{
clrscr();
char ch;
float a, b, result;
if(ch = = ‘+’)
result = a + b;
else if(ch = = ‘-’)
result = a - b;
else
cout<<“wrong operator \n”;
getch();
}
OUTPUT
switch(dow)
{
case 1 : cout<<“Sunday \n”;
break;
case 2 : cout<<“Monday \n”;
break;
case 3 : cout<<“Tuesday \n”;
break;
case 4 : cout<<“Wednesday \n”;
break;
case 5 : cout<<“Thursday \n”;
break;
case 6 : cout<<“Friday \n”;
break;
case 7 : cout<<“Saturday \n”;
break;
default : cout<<“wrong number of day”;
break;
}
getch();
}
OUTPUT
3 types of loops:
1) for loop
2) while loop
3) do-while loop
1) Initialization expression
gives initial value to the control variable before
entering the loop the loop
2) Test expression
the loop body is executed only if the test
expression evaluates true, otherwise loop is
terminated.
3) Update expression
change the value of the loop control variable.
4) Loop body
the statements that are executed repeatedly
General form is:
E.g.:-
for(j = 1; j<=10; j++)
{
cout<<j<<“\n”;
}
prints numbers from 1 to 10
General form is:
E.g.:-
j = 1;
while(j<=10)
{
cout<<j<<“\n”;
j++;
}
General form is:
do
{
loop body
} while (test expression) ;
E.g.:-
for(i=1; i<5; i++)
{
for(j=1;j<=i; j++)
{
cout<<“*”;
}
cout<<“\n”;
}
OUTPUT
*
**
***
****
*****
The jump statements unconditionally
transfer the program control within a
function.
5 types
1) goto
2) break
3) continue
4) return
5) exit()
The goto statement can transfer the
program control anywhere in the program.
goto label ;
……..
……..
……..
label : ……..
The break statement enables a program to
skip over part of the code.
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, i;
cout<<“enter the number”;
cin>>num;
for(i=2; i<=num-1; i++)
{
if(num% i == 0)
{
cout<<“not prime”;
exit(0);
}
}
cout<<‘prime”;
getch(); }
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
if(n= =1)
cout<<first;
do
{
rem = num% 10;
rev = rev * 10 + rem;
num = num / 10;
} while (num!=0);
if (org = = rev)
{
cout<<"Given number is a palindromic number";
}
else
{
cout<<"Given number is not a palindromic number";
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, num, sum = 0, avg;
Input / Output
Processing
Decision box
Flow of control
Connector
1. start
start
2. Read length and breadth
3 area l*b;
Read l and b
4. print area
Print area
stop
Write the algorithm and draw the flowchart
to find the maximum of 3 nos
Arrays can be
one dimensional
two dimensional
multi dimensional
E.g.:-
int a[10];
float b[3][4];
One dimensional
type array_name[size];
E.g.:-
int a[5];
a 11 12 13 14 15
a 0 0 0 0 0
getch();
}
10 8 3 2 1
8 10 3 2 1
3 10 8 2 1
2 10 8 3 1
1 10 8 3 2
1 10 8 3 2
1 8 10 3 2
1 3 10 8 2
1 2 10 8 3
1 2 8 10 3
1 2 8 10 3
1 2 3 10 8
1 2 3 10 8
1 2 3 8 10
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, a[100], temp;
cout<<"Enter the limit ";
cin>>n;
cout<<“enter the numbers”;
for(i=0; i<n; i++)
{
cin>>a[i];
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout<<“the sortrd array is”;
for(i=0; i<n; i++)
{
cout<<a[i];
}
getch(); }
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, a[100], small;
cout<<"Enter the limit ";
cin>>n;
cout<<“enter the numbers”;
for(i=0; i<n; i++)
{
cin>>a[i];
}
small=a[0];
getch();
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int length, i, j, str[50], flag=0;
cout<<"Enter the string";
cin>>str;
length = strlen(str);
for(i=0, j=length-1; i<=length/2; i++,j- -)
{
if(str[i] != str[j])
{
flag=1;
break;
}
}
if(flag= = 1)
cout<<“not palindrome”;
else
cout<<“palindrome”;
getch();
}
A two dimensional array is an array in
which each element is itself is a single
dimensional array.
type array_name[rows][columns];
E.g.:-
int a[3][4];
0 1 2 3
0
a[1][2]
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
cin>>a[i][j];
}
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
2) user-defined functions
- are created by the user as per the
requirements of the program
A function has 3 parts :
1. function declaration/prototype
2. function definition
3. function call
It is the declaration of the function that tells the
program about the type of the value returned by
the function an the number and type of its
arguments.
return_type function_name(arguments);
E.g.:-
int add(int a, int b);
A function can be defined anywhere in the
program.
General form is:
return_type function_name(arguments)
{
function body
}
E.g.:-
int add(int a, int b)
{
int sum;
sum = a + b;
return sum;
}
A function is called or invoked by providing
the function name, followed by the
parameters being sent enclosed in
parantheses.
add(x,y);
#include<iostream.h> int cube(int x)
#include<conio.h> {
int cube(int x);
int s;
void main()
{ s = x * x * x;
clrscr(); return s;
int n, c; }
cout<<"Enter the number";
cin>>n;
c = cube(n);
cout<<“cube =“<<c;
getch();
}
#include<iostream.h> int diff = sub(a,b);
#include<conio.h>
int add(int x, int y); int pdt = mul(a,b);
int sub(int x, int y);
int mul(int x, int y); float qout = div(a,b);
float div(int x, int y);
void main() cout<<“sum=“<<sum;
{ cout<<“diff=“<<diff;
clrscr(); cout<<“pdt=“<<pdt;
int a, b; cout<<“quot=“<<quot;
cout<<"Enter 2 numbers";
cin>>a>>b; getch();
}
int sum = add(a,b);
float div(int x, int y)
int add(int x, int y) {
{ return x/y;
return x+y; }
}
In
such cases, the function assigns a
default value to the parameter which
does not have a matching argument in
function call.
area= area(x);
cout<<“area =“<<area;
The use of the same function name to
create functions that perform a variety
of different tasks - Function
overloading.
We can define a family of functions
with one function name but with
different argument lists.
The correct function to be invoked is
determined by checking the number
and type of the arguments and not on
function type.
E.g. :- overloaded add() function
c = cube(n);
cout<<“cube =“<<c;
getch();
}
Situations where inline functions does
not work
For functions returning values , if a
loop, a switch, or a goto exists.
For functions not returning values, if a
return statement exists.
If functions contain static variables.
If inline functions are recursive.
A function that calls itself is called a
recursive function.
f = factorial(n);
cout<<“factorial =“<<f;
getch();
}
A pointer is a variable that holds a memory
address.
a 5 5006
There are 2 unary pointer operators.
& -ampersand
* - asterisk
a 5 5006
The arithmetic operations on pointers are
1) increment/decrement
2) add/subtract with an unsigned integer
number
10 40 50 60 2 100
2000 2002 2004 2006 2008 2010
p++
The name of the array is actually the
starting address of the array. So
x 1 5 7 8 10
0 1 2 3 4
*(x+1) 5
*(x+3) 8
We can access the array using pointers as
follows:
int x[5] = {1, 5, 7, 8, 10}
int *p = x;
p
*(p+1) *(5002)5
*(p+3) *(5006)8
#include<iostream.h>
#include<conio.h> cout<<“length =“<<length;
void main()
{ getch();
char a[20], *p; }
int length=0;
cout<<“enter the string”;
cin>>a;
p = a;
while(*p != ‘\0’)
{
length++;
p++;
}
The are 2 methods to invoke a function.
1) call by value
2) call by reference
Actual arguments
The arguments in the function call are
called actual arguments.
add(a, b);
actual arguments
Formal arguments
The arguments in the function definition are
called formal arguments.
formal arguments
In call by value method a copy of the actual
arguments are sent to the called function.