Professional Documents
Culture Documents
-i
C^udt &-<*.@
>p 3\ toA,
TV nef
eUv, JUi me^ ^^- ^ ^
yoLd- Luxxf-v [ \ vdt x, cn 5f)
? . .. .
ivA \ ^? j'
J^
1
J
J/ Ca <b^ ',
Suxip
tjbpX^ @ @ @ - @ - b J
a ofjQJb^ vasJ^Jdi Jyd J^jo JUgjs a Jm^I MaX^bM.
G _i
nMcJL \ oua1Ai
a
/
J^k maxv\ O
PuRlc ;CJ
9. *
. 1
-^
@Space ^*a jtta j2-A M cLa^W ^k e^ccf
ebri w -^^ i>^^L^n.
*Ay | CU
\ U
C-i C mms OdhcC^ -n t ,c O t
e alU c^
'GUuM.
: i/ w V.
jU^AiM^^ vOAl
: l&COM^-*l
l^Mfyvt
0OC/L
r-,
.@@*
-pdUhjy^^JM
& of Nie ^o_
\ v& rnaxr) O
v_/
i* ^9
pqaA^ M)
~!t:
; *> i
inline runuuuns
@ In general, functions save memory because all the calls to the function cause the same
code to be executed.
@ The function body is not duplicated in memory. When the compiler sees a function call,
it jumps to the function definition. At the end of the function, it jumps back to the calling
function.
@ While this sequence of events may save memory space, it takes some extra time.
o To make a function inline, we simply put keyword inline before the function definition.
@ The significant feature of an inline function is that the run-time overhead to link the
Syntax :
inline return_type function_name(arguments)
{
function definition coding
.%
Program : Calculate the square of a number using INLINE FUNCTION.
I. #include<iostream.h>
2.
8. void mainQ
9. {
1. #include<iostream.h>
4. void main()
5. {
6. int s1 = square(2+3);
7. int s2 = square(3*4-5+6/2);
8. int s3 = square(++s1);
12. }
(i j ja/> AjJxiV coil Pt
e**~
^u^w V- "^ ^ ^
?
^\ ^o (ftro i 0[ 0^) : |O f 0
kvL @^ Wf
fWua . dak- 4 ^oA -k,
-=?
~^ chM
3 I 31
@n @
^^1 dm^ -~ ^^ .
yJ
f - s Tv
^ ^ 1l tu> ^^ ^
t^'VsWA*- ^o^ai- Y ^ ^ ^
^ ^ JUUx. f ^cKuJf
t* button,
-, WWXe ^a. 4 t f
t** ^^^^ ^
(W C^captl Of OOPS
-?
=%
Potu f McarpK
y a*
3 olA 9
-=)
^ xt 4^ t
o ^
~5
J @41
S&r\
I - ": A
Syntax :
class <class_name>
{
[private] :
data_members;
member_functions;
public :
data_members;
member_functions;
protected :
datajmembers;
member_f unctions;
};
@ Where -
objectname.public_data_member_name;
objectname.public_function_member_name (arguments);
@ Onlv the Dublic members can be accessed outside the class specifications.
Class Methods Definitions
@ The data members of a class must be declared within the body of the class whereas the
member functions of a class can be defined in two places :
function.
@ When a function is defined inside a class, it is treated as an inline function.
@ Normally, only small functions are defined inside the class definition.
@ The syntax f
or defining a member function is same as of a normal function except that it
normal functions.
@ It is written as -
returnjype classname:: function_name(arguments)
{
Function body
}
@ The membership label classname:: tells the compiler that the function functionjname
oXl
OAjL
@C-
i;
pAMfcCXL '.
,%
V
CuJtl - - @' c^]i (Vi
jjint
Qhf
ep qmmXmm> / -,
r
i
n
I;
t
X ot
y*-; i
7C *M a.j
I
r rnojjn. C")
?CJUid&cnlLJl Cf ^
4
@" 1-.S!
paWXc * .%
/rn cox.
r @ *
I jj <zc m ^^ ^Mj,'
. |AA U-,n
O '
I
ZJbnc @ &$&, Q >
I a )a @ -M 6-tro -T
; i
@.. .. . $r.
1 1 @ 11 , I
A
ex
Ajfr
ft rnbr\ '
OUTPUT
f\ LS>T TiHf-
1- I3>
Friend Function
@ The private and protected data of a class might need to be shared with some non-
member of the class.
protected members.
@ A friend class is a class whose member functions can access another class's private or
protected members.
@ It can be invoked like a normal function without the help of any object.
@ Unlike member functions, it cannot access the member names directly and has to use
an object name and dot membership operator with each member name (Eg. A.x)
@ For example, to make an outside function check() as a friend to the class sample, the
class definition may look like as follows :
class sample
{
public:
Remember that the function definition of this function does not use the keyword friend.
Program : Demonstration of the use of friend function.
1. #include<iostream.h>
2. classABC
3.
{
4. intx, y;
5.
public:
6.
void getvalue()
7.
{
8.
cout "Enterthe values ofxand y : ";
9.
cin x y;
10.
}
11. friendfloatavg(ABC); //Friend prototype
12. };
13.
14. floatavg(ABCA)
15.
{
16. return float(A.x +A.y)/2.0;
17.
}
18.
19. void main()
20.
{
21. ABCobj;
22. obj.getvalue();
23. cout "Average = " avg(obj);
24.
}
Extending the use of f
riends
@ One possible solution is to make this function a member function of every class.
@ The other alternative, and better in design, is to define the function as global function
and make it a friend of the class so that it can gain access to class's private data.
1. #include<iostream.h>
2. classtwo; //forward declaration
3. class one
4.
{
5. inta;
6. public:
7. @ void setone(intx)
8. { a = x; }
9. void showboth(two); //Fi
// Function prototype
10. };
11.
12. class two
13.
{
14. intb;
15. public:
16. void settwo(inty)
17. { b=y; }
18. void showtwo()
19.
{
20. cout "No. 2 = " b;
21.
}
22. friend void one::showboth(two);
23. };
24.
25. void one :: showboth(two obj)
26.
{
27. cout endl "No. 1 : " a;
28. cout endl "No. 2 : " obj.b;
29.
}
30.
31. void main()
32.
{
33. one aa;
34. two bb;
35. aa.setone(5);
36. bb.settwo(IO);
37. aa.showboth(bb);
38.
}
NOTE The member function, which you '
only be defined after the definitions of both the classes.
iV Demonstration ofaGLOBAL FUNCTION ASA FRIEND OF A CLASS
Prograrr
3.
4.
{
5.
6.
7.
8.
9.
{ cout "One -> a = " a; }
10.
11.
12. };
13.
14. class
15. {
16.
17.
18.
19.
20.
{ cout "Two -> b = " b; }
21.
22.
23. };
24.
25.
26.
27. {
28.
29.
30.
31. }
32.
33.
34.
{
35.
36.
37.
38.
39.
40.
41.
42.
43.
. ,(T
(D^lf
l ^ikX2u^ ^m&mijUA, JlMAxf
rA^ J '
7/ 0
UaoJc Jhd v
K . 1.1 : . .
1;
Ux
c . . ,.
0
: / ' .
CXn >-? inaAru. -j
(UXv kaJL @
s
c i - @@
5.
1 Cuxtonax/u ^x^t @ y
0 j J
J
Con " v^ilJ iovoi2 -
tint- pi/C/i) @
c :
QudryuJr
qjjuo voJml f^Lf
i \ o
ITO
TQ-t
e@'@
X '' Zoo
(J <\
. it
- -i
CAM >jck );
V
fan cixon jjjUtu . x; zfauix ^>- -
, w^ o. -M^ ^7 i X, ^ ^""^
-> @**-
1 ( I 1W or^ ^^
y- skxyoJ {jp, vr)
t @@ @ @ @ -
Foe B<OMf)^-
(JL mCu-i^C^
vot
cvxd^Y'1
hRoe, wo. & :
^y
?
, u fUjooJt Uak^f
t . f^O
*
N/ 0*
to -
A* Lat
er v^^
i*
f
Com^kwtoki |_-U
@ A JWr
tf
oujdJV^ li O, SLptual rrwwhvc -h^cLo^
St
y -cW.
?;
die:; oisLcCvac.30
m .= 0 -
1
r\ dz> 0 @
HaJL,
-^
@)
^
0
^ - .t..j . , . .*. , ^ GjO^Km^oW U
I
in - o ;
easeful <x)
p ~ ~;
t p- a\
S OUW^ ^ P^ ^
C n^ ^TSlo .W-^^
^ I i-
-^ y v old m ^\ O^
^^ . 1 1 -2 A%- s)^*0^)-
WAP -*b ~2<^piAaiL TtLx J<j15l1 4 -W^*-
?
c^jb l< ^a .put-11 ^ + *p-^:
- p^
?
?
Cvdt nnoan C )
K^
]>f
rs tr u c/ro R^_
n i l. lit: t
o ^ " Jj^Mtk ^vxeA^^
@ W? M tOM* @ * ..^^ @j CI<XM J^ ^^
A \ \
(%J^ @ 0
I'll
(xdd*^
13 IZI iS
JunJb ^M @
^'*JMrwur
n C^*,W>, ^)
*n
V
i ' i @
OhlO/f')
5, ^iW-O ;
i
!* i o 5
~jl^ PauJa
tot^l f^J^g^Ke^
_^ JkJj J* @f
y
JA%
JU O~ (\ t
->) ITuJ
,>af
eu^cx
-) 6itdlmi^X\ &^ K& CAJIa, HW^ Small box-
1 a..
@ @- .
I 1
^CWw (7-^
A+ h-* Qd^ HamaIuM^
CJjL -
?) V1* 1 dt
e.^ eW^t : .- ^^-uj^;
"V V' JXa, &takc vOUsJJk. Ja ^vSf
r s^AH)^hxL
o o ,. @. -^ j$
? @ @ @
$
0u
^^c D^Ke^i; i-io
Q/-c
oiiJk.C t^ * )
@ : qA>
no =- >@ )
Vv^. ^ "*
. s
-;
JjjJMh^' ^ "
s
/oJjJJL J
*i- 1U 0J ' O
30
- - ^
/
J (J
T22
^y
Containers and Iterators & Namespaces
!@ Containers
@ Containers are objects that hold other objects.
@ Standard Template Library (STL) has number of container classes that allow
programmers to perform common tasks. .
@ These container classes support generic programming and can be used for handling
data of different data types.
@ Each container class defines a set of functions that may be applied to the container. For
example, a list container includes functions that insert, delete and merge elements. A
stack container includes functions to push and pop values.
@ Containers ere further classified as sequence containers and associative containers.
@ In sequence containers, the elements are stored in a sequence and only can be
2. Iterat
ors
@ Iterators are objects that allow to cycle through the contents of a container in much the
same way that you would use a pointer to cycle through a linked list.
@ Iterators are of five types - random access, bidirectional, forward, input and output.
Defining
@ We can a Namespace
define our own namespace. The syntax for defining a namespace is -
namespace namespace_name
@ To
Using use a namespace member, the member's name must be qualified with the
Namespace
namespace name and the scope resolution operator as shown below -
namespace_name :: member;
or a using statement must occur before the member is used. The using statements are
1 xfJax\
$cu4> t kat
t do iuul rweuv &jj dbudcuuvy &J< meMu^
3. What f* ^^^ ^
(S, . vxhxt ch ^ ^^ ^
,*