You are on page 1of 75

0 x/ 6- 6 VJUuJkib @

-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

Sw^f- ^C^-rO ',

tjbpX^ @ @ @ - @ - b J
a ofjQJb^ vasJ^Jdi Jyd J^jo JUgjs a Jm^I MaX^bM.

UyJiM XUS, 40-iMlL Y\ A^ .

G _i
nMcJL \ oua1Ai
a
/

J^k maxv\ O

C^L c^ ' b' - )\ uLu>l yaj^ddt

PuRlc ;CJ

9. *
. 1

_) fo7rccc^c^- o/oboiL vomxaJJjl

-^
@Space ^*a jtta j2-A M cLa^W ^k e^ccf
ebri w -^^ i>^^L^n.

*Ay | CU

c^u oMvcaMcI cub -ruuAdLx . (y /"^

O'OA, -Wi. , A^M -UULd j^yju s^JXL ^M^jo^l

\ U
C-i C mms OdhcC^ -n t ,c O t
e alU c^
'GUuM.

: i/ w V.

: w up^v ^uj^unjs ^mmajomaj, qL^ o^^cjJM,


I

jU^AiM^^ vOAl

: l&COM^-*l
l^Mfyvt

Xnt x pbo - C-^*)

0OC/L

r-,
.@@*
-pdUhjy^^JM
& of Nie ^o_

\ v& rnaxr) O

Ink a , *?&*$&- ,C)


sc)
v\

v_/

i* ^9
pqaA^ M)

^^relu** "-**"* ^ <T" <wl\ \ @

~!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.

@ To save execution time in shor


t functions, we may put the code in the function body

directly in line with the code in the calling function.


@ That is, each time there's a function call in the source file, the actual code from the

function is inserted, instead of a jump to function.

o This is generally done with functions having small coding.

o Inline functions are functions which are expanded inline.

o To make a function inline, we simply put keyword inline before the function definition.

@ An inline function executes faster than normal functions.


@ Inline function is a request to the compiler. Thus inline function may not work as inline
sometimes. In this regard, they differ from macros, which work as macro always.
Although a warning message is flashed when an inline request is not fulfilled.

@ All inline functions must be defined prior to their use.


@ Inline function does not work as inline when the code contains loops, recursions, goto,

switch, static variables, etc.


@ Hence, they are generally used where function definitions are generally small.

@ The significant feature of an inline function is that the run-time overhead to link the

function with the function call is reduced.

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.

3. inline int square(int x)


4- {
5. return x * x;
6. }
7.

8. void mainQ
9. {

10. int s1 = square(2+3);


II. int s2 = square(3*4-5+6/2);
12. int s3 = square(++s1);
13. cout "S1 = " s1 endl;
14. cout "S2 = " s2 endl;
15. cout "S3 = " s3;
16. } '

Program : Calculate the square of a number using MACROS.

1. #include<iostream.h>

2. #define square(x) (x * x) //defining macro


3.

4. void main()
5. {

6. int s1 = square(2+3);
7. int s2 = square(3*4-5+6/2);

8. int s3 = square(++s1);

9. cout "S1 = " s1 endl;

10. cout "S2 = " s2 endl;

11. cout "S3 = " s3;

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

sriOvL S^jtk-2 Si Kb,3 obf^tcb

[&a^[ [ttdJb ) li^tJ


taw :

3 I 31
@n @

^^1 dm^ -~ ^^ .
yJ
f - s Tv

^ ^ 1l tu> ^^ ^
t^'VsWA*- ^o^ai- Y ^ ^ ^

^ ^ JUUx. f ^cKuJf
t* button,

Jl >^iidlL *U^ rv.

^ ' TiZl*^ * ^ ^ "v **

-, WWXe ^a. 4 t f
t** ^^^^ ^
(W C^captl Of OOPS

-?

^ juwwt*. (v^ **~mjLi h r~


aCnS>xuA , ^\ jjM* <xl- v cm- road^ "^H.

=%
Potu f McarpK

^ void ocR ^^. ^O

y a*
3 olA 9

-=)

^3., + M. 2_ _, Poo* ojdi*"


~gisjH^e'T ft>OC^

Pt- moo* * pjd&r^ JomdMv^ ^m >t&rv*M


rpt m^o^ -^

^ xt 4^ t
o ^

~5

J @41

S&r\
I - ": A

Declaration of a class and creation of object & Member function msiae


and outside the class

Syntax :
class <class_name>

{
[private] :
data_members;

member_functions;

public :

data_members;

member_functions;

protected :

datajmembers;

member_f unctions;

};
@ Where -

Class - keyword (similar to struct)


Private/public/protected - Access specifiers for data and functions of class

Data Members - Actual data is stored here only.

Member Functions - Functions declared within a class.

@ By default, members of a class are private.

Creating Class Objects


@ Objects are variables for a class. Like any other variable, they must also be declared.

Syntax : classname objectname;


@ The syntax for accessing a class member is similar to that of accessing a member of a
structure variable, i.e., using the dot operator.

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 :

o Inside the class definition

o Outside the class definition


@ In both the cases, the code for the function body is identical.

@ There is a difference in the way the function header is defined.

Member functions defined inside the class


@ A function declared within the definition of a class is called a method or member

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

is enclosed within the body of a class.


@ Function prototype is not needed as it is defined within a class.
@ Usually, the data within a class is private and the functions are kept public.

Member functions defined outside the class


@ The function definitions of functions defined outside the class are very much like the

normal functions.

@ They should have a function header and a function body.


@ The only difference is that the name of the function is the full name of the function.

@ It is written as -
returnjype classname:: function_name(arguments)

{
Function body

}
@ The membership label classname:: tells the compiler that the function functionjname

belongs to the class classname.


I

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,'

JV ^^^ - ' ' a- - r \ @ @- @ -

. |AA U-,n
O '

I
ZJbnc @ &$&, Q >
I a )a @ -M 6-tro -T

; i

@.. .. . $r.

JtloU C)^ . ... . . .

1 1 @ 11 , I
A

ex

Ajfr
ft rnbr\ '

void tMn :: m&xmC)>


(1WX ^ tt W@^ -. ll ^^uu ;

ixW ' : ad B>Y*if


o>"i tl , h*^+A
@qjLcL
Vi
JLr& WOjuaO

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.

@ Normally, a non-member function cannot access an object's private or protected data.

@ A friend function is a non-member function that is granted access to a class's private or

protected members.

@ A friend class is a class whose member functions can access another class's private or

protected members.

Special characteristics of a friend function

@ A function may be declared friend of more than one class.

@ 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)

@ It can be declared either in the public or the private part of a class.

@ Usually, it has the objects as arguments.


@ If A is declared as a friend of B, this does not give B the right to access the private
members of class A. Thus we can say, friendship is not mutual.

Declaring a friend of a class

@ The prototype declaration star


ts with the keyword friend.

@ 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:

friend void check(void); //function prototype


};
The friend function definition may appear elsewhere in a program like a normal C++
function.

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

@ Suppose we want a function to operate on objects of two or more classes in order to


manipulate their private data.

@ 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.

@ For making friends, the following situation can arise -

o Global function as friend of a class.

o Member function of a class as friend of another class.

o Class as a friend of another class.


Program : Demonstration of a MEMBER FUNCTION AS FRIEND OF A CLASS.

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

j)oA OaJ< (K J^aaaX^w ^

c . . ,.
0

: / ' .
CXn >-? inaAru. -j

QjbuJtZ <c c ^ \ \ Q>alcKAACL ^ - ' @/

(UXv kaJL @
s

c i - @@

5.
1 Cuxtonax/u ^x^t @ y

5 ioMAA jc; .- i^^^


t
@ '-tic @ @

. Ey'/^L QwMxdx, f/MA^A Jum* ;-

Jjj^l m tn, NoJaa) e/ (X <%t>^ *m~Jma


[^ Id

0 j J

\ Mjb vod ) vaio? '


! ' J

J
Con " v^ilJ iovoi2 -

JWkjI JtoaJ? meiwn f W

SutllXtAO Stood* f@iy. v<d\ 4 c^'V^Xz/)/2- '


dia qjkM Oxa) 4 (ljX^^ Mjf&oH y^Jm. ^ujia^

tint- pi/C/i) @

c :

.puW '^cL - <uj^ (jVi! / axjLl).


VQld >W (^ g^'I / &x5 vJLjL)

CjQuk ^C " T<sf


cd pd^d (i<MUsmJLslli ^2C
I, _
f l - pl -f* oUA - pL ^ &wlL j'

Omh ^'}T^kl cLUJmuam dv<X"db& -


<C <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 ^>- -

-> ln ^+ , ^ / pcx^tWp. -jW ^ -^ c^v


not duo <r
t- ^ ^ ^-;^-

, 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 @

J> WiiiJL (kJtojjdt -fWWM -

^ Mo re. XC^ma (&^ul cuyvul^aM ca*v U o(^<>

^ oJbc roJkcO ^ ^ ^ eWW*


r

^ *U ^WW^ jou& ^ ^~ o^n^

HaJL,
-^

^"lu x c^- iso,^


JsJUb(UtJ>/> ^
; n ^^-
.@ '

@)
^
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^*-

Je-tovn o^ o^cw^a J^oor


f vciix^ .

?
c^jb l< ^a .put-11 ^ + *p-^:

n ^ d^MttXt L^^ 95) "-^J


i
\

- 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 \ \

iklKl CAvh Ml QaaJUi Qam d^XjMJM>

(%J^ @ 0
I'll

(xdd*^

13 IZI iS

JunJb ^M @
^'*JMrwur
n C^*,W>, ^)
*n
V

^@' b.-^WA ^a-


=. ^^
J

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-

^@^ JU ,o wJ5 ^^ ^^^ 1f ^ ^ @


' @ \

dgak dburvvo '

1 a..

@ @- .

I 1

f ' "@ @ '

^CWw (7-^
A+ h-* Qd^ HamaIuM^

oJL c&w^ Job alL <mu^ -


alP

@5 - it (^d^iJ^M!^ *"^ mU*~ "^ ^

CJjL -
?) V1* 1 dt
e.^ eW^t : .- ^^-uj^;
"V V' JXa, &takc vOUsJJk. Ja ^vSf
r s^AH)^hxL

XdU, Xta >** **fL tj tU cM* m v^bzt*


. @_ 7^ ^

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

retrieved in that sequence.


@ In associative containers the elements can be retrieved according to keys.

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.

Iterator Access Allowed

Stores and retrieve values. Elements can be accessed randomly.


Random Access

Stores and retrieve values. Elements can be accessed in forward


Bidirectional
and backward order.

Stores and retrieve values. Elements can be accessed only in


Forward
forward direction.

Input Retrieve, but notstore values. Forward moving only.

OutputStoresbutnotretrievevalues. Forward movingonly.


3; Namespaces
@ A program may include many identifiers defined in different scopes.
@ Sometimes a variable of one scope may collide with a variable of same name in a

different scope and thus creating a problem.


@ To handle this problem, a new feature in the form of namespace is added. Each

namespace defines a scope where all related identifiers are placed.

Defining
@ We can a Namespace
define our own namespace. The syntax for defining a namespace is -

namespace namespace_name

{ //declarations of variables, functions and classes

} there is no semicolon after the terminating brace.


@ Note that

@ 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

generally placed in the beginning of the program.


using namespace namespace_name;
@ The above statement specifies that members of namespace namespace_name can be
used in the program without preceding each member with namespace_name and the
AW)r> M&Adt_UV_^d

1 xfJax\

$cu4> t kat
t do iuul rweuv &jj dbudcuuvy &J< meMu^

Oa^L *ycfdbtiur\ H k^K Wt


onf
iJU,
I
4b-

1- Whcdt ^ ^Du. ^iia^ W- :


Sep. * tiU op^jr

3. What f* ^^^ ^
(S, . vxhxt ch ^ ^^ ^

,*

You might also like