Professional Documents
Culture Documents
2. Basics of C and C++
2. Basics of C and C++
/ )
1. Arrays
2. Structure
'
! :p'd:{In:* .
passing coueai.no .
/STRUCTURE
↳ collection different
of
datatypes
Stout rectangle
{
int length ; 2
-
Main memory
int breath ; z
-
7
-
/rT€¥
-
3 4
;
bytes but till here , it does not
heap
in
occupy any space J
memory -
FIFI? ! zeaangier-qio.si
"
"
main
→ here it occupies memory code
length main
!
Z - = to
5
= section
2- bredtn =
←
-
Struct card
{
int face ;
int shape ;
int colour ;
3;
int main C )
{
strut
"
card
"
deck [52] -
f - - - -
3
pointfl Td
,
deck -63 -
face) ;
printfl
"
"
Xd ,
deck -103 -
shape) ;
}
Pointer
↳ address variables
1.
why pointers
2 . Deceleration int a -_ 10 ;
print fl ) ;
"
allocation
5 .
Dynamic y d
-
,
a
Ap) ;
" ''
print fl 1. d
,
→
derefezencing
USES0FP0ER
I.
Accessing heap
2 .
3 Parameter
.
passing .
X -
x -
€ =
¥#
#incwdecstdlib header file for Malbec )
int main Cl
1¥
7
Heap
-
{
Stack
HEINE's) manors -
size .ec .my ,
} ↳ code
as
for returning
malloc function V/ -
I section
for 5
returns void pointer creating space
( type casting) integer values in heap .
x -
I
REFERENCEIN
-
int maine )
-
{
int a -40 ; air
in:*
.
.
=
(out car ;
=
}
POINTERTOSTR-ULTURES.to
l .tl
vet rectangle
{
int length ;
int bredth ;
3 ;
int main
: :÷÷÷÷÷÷÷÷÷÷÷÷÷
} memory
I te
✓
p
→
length
=
20
;
4 bytes of
Op
¥
.
| /
int main
{
*
Struct
rectangle p ;
I ! !!!!! !
size of '
FUNCTIONS
Parameters of
passing functions
Pass value
by ]
→
Only in C
→
→
pass
pass
by address
) In c++
by reference
t T
MONOLITHIC MODULAR
PROGRAMING PROGRAMING
FUNCTIONEXAMPLEPRO
"
'TPeintaddlinta,ir
{
-
inte ;
( = atb ;
teoonafoaemeter
imma ii:
int Maine )
{
int ;
n
, y ,z
10 ;
@Actupa lameterz-addtmy7jprintfVt.d
n=
y=5 ;
"
,
z );
} W
15
CALLBLUE
t.e.in/EiE#:::y--
Void ( int )
swap n , inty
{ -
iii.
temp ; Tap = Code
} section
main
=
int main )
{
int a
,
b; because it will create a copy of values
a 10
-_
;
b = 20 ;
swap ( a ,b ) ;
'
pnhtfl
"
I. d
'
I.
'
d ;a,b ) ;
} 1020
CALLBYADDRESSSE
void swap ( int n , inty )
A
{ n Y temp
int temp ; ⑤ Do
* A
FEE
! Ib
''
mm
}
a
int main ) Et Et -
{ 200/201 202/203
b;
-
int a
,
s ::÷
.
swap ( a ,b ) ;
swap
'
= Code
pnhtfl
"
I. d
'
'
I. d ; a ,b ) ;
main section
} 20 10 #
CALL BY REFERENCE ( Only in Ctt )
-
/
{
int temp
-
III 'm
,
"
:r S
WAPI
main
Code
200/201 202/203
main section
int main ) I
{ temp
int a
,
b; ⑦
a 10
;
-
-
b = 20 ;
only 2
bytes of memory is used
swap ( b)
; as in reference another
a,
'
, only
pnhtfl
"
I. d
'
I.
'
d ; a, b ); name is
given to the pre
-
enlisting
} 20 to variable
ARRAYASPARAMETERVOID
fun ( int AEI , int n)
{ ↳ ARRAYS
or int sea as can
only
int i; be
passed as call
by
for Li =o ;
ien ; it -17 address .
print fl [ i) ) ;
''
"
1. d
'
,
A-
g
int maine )
{
int a[ 5) =
{ 2,4 , 6) 8,103 ;
fun ( A
,
5 )
;
}
for
*
int fun ( int n )
int E]
fun (int n ) -
/af§no#
¥
{ - it returns an
array
④
-
*
int p ; "
" " " " " "" " "
Ewtn :
" "" "
{ main -
=
*
int A ; I
A- =
fun ( 5 ) ;
-
STRVCTUREASPARAMETE#
JCALLBYREFERE.NU#
③
/ If
CALLBYVALU
VI.
length tt ; -
.int/
*
return VI.
length 01 boedth ;
.
} CALL BY ADDRESS
:c:÷::÷t
:÷÷÷:÷:÷ .
int maine )
int main l ) {
{ Struct rectangle r={ 10,5 } ;
Strutt rectangle r =
{ 10,53 ; changed ( Ir , 20 ) ;
f l
"
Id
"
cereals ) ) ; 3
point ,
-
PASS1NGARRAYSASCALLBYVALVEUSlNhSTRUCTURE#
th )
Void
fun ( struct test struct test
{ {
t1 .
A -103=10 ; int ALT ] ;
Et
-
} int n
;
⑤ 3
/
int maine )
t-fqzm.gs 103,53 ;
sgtynugt test,
,
g
V
is call by value
STRUCTURESANDFUNCTIONSS
tout
rectangle
{
int main Cl
int length ;
int breath ; {
} Strut rectangle r
;
*
void initialize ( strut rectangle r
,
int 1
,
int b) initialize Hr , 10,5 ) ;
Srf
{ area co ) ;
=L (
r →
length changel 207 ;
!
,
-
✓ → breath = b , V
be changed , so call
by
int area ( Strutt rectangle r ) I address .
l
{ 10
- b
return r -
length r -
bred th ;
,
*
void change l ( struct
rectangle r
,
int D
{
=L ;
r →
length
g
CLASSANDCONSTRUCTORCIASS
rectangle
{ int maine )
E
private :
int
length ; rectangle 8110,5 ) ;
int bredth; r . areal )
{
l;
length -_
breath -_ b;
int areal )
{
return length breath ;
}
void ( int e)
change length
{
=L ;
length
}
};
Class rectangle
-
int
length ;
Accessor
/
-
{
}
return length ;
int breath; -
public Mutator
) {
length
)
rectangle C ) =L ;
Constructors { -
3
( overloaded) length -_ bzedthel ; Destructor ~
Rectangle C)
;
}
int b ) ;
-
rectangle ( int l
,
facilitators
I
int areal ) ;
bred th = b ;
}
int area Cl
rectangle : :
{
*
breath ;
return
length
,
int rectangle : :
perimeter C)
{
*
return 2 ( length tbredlh) ;
}
rectangle rectangle C)
~
: :
{
}
int main C )
{
rectangle r ( lo , 5 );
Couto r . area
;
( out ca
perimeter ;
r -
r set
length 120) ;
.
Coutu r
getlength C );
.
3
TEMPLATE CLASS
-
class
{
arithmetic
↳ for using various datatypes .
private :
Using the same class for
T nfta ; different datatypes .
T int b;
public :
T T
arithmetic intfa.int/b )
T int add C ) ;
T int sub C ) ;
3;
}
template < class TS
7 Tint arithmetic : : add C)
CT >
{
Tiny c ;
(= at b ;
return c;
/ !
T int c
; int main C )
(= a -
b; E
÷÷÷:÷÷÷÷:::÷
return "
.
.
(out cc
art add C )
.