Está en la página 1de 207

Ramanathan

Digitally signed by Ramanathan


DN: cn=Ramanathan, c=IN, o=Commercial Taxed Dept
Staff Training Institute,, ou=Computer Lecturer,,
email=ctdsti@gmail.com
Location: Commercial Taxes Staff Training Institute,
Computer Lecturer,Ph:9442282076
Date: 2008.03.13 11:55:56 +05'30'

1
C++ NJN G

(Object Oriented Concepts using C++)


1.1 G N
(Object Oriented Programming Language)

C b M
EP MA. C b EP G
(Programs) (Application
Software) AA. G
G NJ A.
EP G, k O
e A. k
, EP GL Of
A.
G NO k Of
P (Variables) . k
(Data Types) .
O e G EP
P (Control Statements) .
G N J A.
CL bP A.
b C
I CA , G N
O F A A.
F G N .
EP P O N
.
H, C bM, (data),
(operations) HK , Q
J C b FM
.
C++ G N,

J C HC b, Q
1

. , Q HC
b , b Q
A. H
A.
F
P. K O F
O
K A F
J C b F I P
AA. Oe G
, C b N
G A.
O Q, O F,
H , G
C HKA.
- - (object)
A. , O H A.
EP GL
A.
, EH (Calculator) .
G (State), H (Behaviour) A . G ,
c , , CF, P
PA. H , , N,
GF I, G N
A.

FO

F, ,

M
C F

1.1 EH

G NJ EH
A:
- EH
:
Number1,result, operator, Number_backup

:
Additon()
Subtraction()
Erase_Memory()
Display_Result()

 ,
A .
 , PH ,
G .

F
(Encapsulation ) N A.

H F
A.
A , , , , L
G H A
O . M
EP GL
P F A.
GO A E
(Objects) OF.

A
:

Account number long int


Name char[15]
Opening balance float;
Account type char

:
Accept_Details()
Display_Details()
Update_opening_balance()
Withdrawls()
Deposit()

Date_of_birth char[10]
Name char[15];
Class, sec char[4];
Marks float

:
Accept_Details()
Display_Details()
Total()
Average()
Grading()

1.2 (Polymorphism)
, , ,
E .
J.
:
Draw_Square()
Read side
Drawrequired
lines

Draw_Circle()
Read radius

Draw_Rectangle()
Read length,
breadth Draw
required lines

Draw

Draw_Arc()
Read Start_angle,
End_angle,radius
draw

W :
Draw (side) -
Draw (length, breadth) -
Draw (radius) -
Draw(radius, start_angle, end_angle) -
Draw( ) k A.
. J
k Draw()
P . Draw( ) kO
J O
(Polymorphism) A.
F O
F A.

1.3 K (Inheritance)
(Class) M A G
AA. H
O (Template) . , JKO
MA A. , M
A. M , ,
G-c , C, HK
P. M
. , , N,
A MO
A. , ,
MO . MO
, .
J K Q E
. , , G-c , C,
A , M M
A A. A:
Class animal :
Features :
eyes, skin, habitat, food
Functions :
Reproduction()
living_style()
prey_Style()

Birds:
Young_ones;
Feeding_Style;
Skeleton_hollow;

Mammals
Young_ones
Mammary_glands
Functions:
Parenting();

Functions:
Migration();

Reproduction_style()

1.1 K

M (base class) A.
, M (derived
classes) A. M F I.
K Q
M .

M K (Inheritance)
A.
K M M
IMA. G PJ (Code Reusability)
FA.

GF :
 GO,
(Class) M
A.
 H O ( O
)
.
O I O
A. (Data Hiding)
(Data Abstraction)
A.
 (Function) P (Operator)
F
(Polymorphism) C (Complexity)
A.
 K (Inheritance), M
J M
NA. P (Code
Resuability) FA.
O, H
P A F N
A.
1.4 : i c
i c G
. F c OPO
O cK , cK
A GL . G
M: F G c FHO (Taps)
E, FHJ N O cK ,
F cK . FH
. , c
6

1.2 i c

/ G PH M
0 cK A
. E FH ,
FH F, E,
cK P, c A A
P P (Instance Variables)
. FH A:
:
Tap_open, Qty_water,
Water_source

:
Start()
Stop()
1.3 FH

cK A G, FH c
MQA c (Water pipe)
. c FH A
(Building) . c
Q,
F FHO G
P . FE 1.4 -
A.
7

i FK c

t1, t2, t3 A
FH
p1, p2, p3 A
c

:water_pipe
:
Thickness, Length,
Valves

1.4 i c

:
Water_flow()
Valve_Status()
Time_Taken()

cK total_amount F,
t1.water_consumed()+t2.water_consumed+t3.water_consumed()

A. QQ FHJL cK
t1.water_consumed(), t2.water_consumed(),.... P.
t1.water_consumed() P1
N cK
P. t1 FH cK
P1 J bQA. G,
F M A
. O
A. G O A , GL
F M A. , k
H K A.
GF , , A
A
A. I J , GO
O M A.
O H M
GF PH C
.

JC M
I. G

EP GL
________ A.


H ____________________
A.

F O
F____________________________ A

M F
M ___________________
A.

II. W M M O:

1.

2.

F ?

3.

KFL
A?

III. W FE M

G
FK (Data Model) K MA. G
E K I .
EO M: Name, date of birth, language, nativity.
EO E J I
M:
) Stores - date of joining, dept, salary
) Scientist - area of specialisation, current project details, paper presentations
) Technician - height, weight, ailments, risk factor, department, wages

Ramanathan

Digitally signed by Ramanathan


DN: cn=Ramanathan, c=IN, o=Commercial Taxed Dept
Staff Training Institute,, ou=Computer Lecturer,,
email=ctdsti@gmail.com
Location: Commercial Taxes Staff Training Institute,
Computer Lecturer,Ph:9442282076
Date: 2008.03.13 11:56:42 +05'30'

2
C++ NJ
(Overview of C++)
2.1
C++ N 1980-O F & F
vv (Bjarne Stroustrup) .
C++ (C Hv Hv K )
K vC . ++ C NJ
I P .

2.2 C++ P F (C++ Character Set)


C- N C++ N P F
. FL M (Tokens- G P
O ) A.
P FJ W P A:
A........Z, a.......Z, 0........9
+, -, /, *, \, (, ),[, ], {, }
=, !=, <, >, ., , , ;, :
%, !, &, ?, -, #, <=, >=, @

O, A ,
F F H P.
P .
M (Tokens) A. M
GL eC Q . M
2.1- A.
M

P PL

2.1 MO

G
P

2.2.1 C
EP NJ GH (Language Compiler)
KA C A.

10

C .
P . C++ NJ
C O 2.1- .
auto

break

case

const

class

continue

default

delete

do

else

enum

for

friend

goto

if

inline

new

operator

private

protected

public

return

signed

sizeof

static

struct

switch

this

unsigned

virtual

while
2.1 C

2.2.2 P (Identifiers)
P, P (Variables) A.
P , F PL A G
PA. PJ F
WP(Underscore) A,
. _test; test; sum12 A
P. P H
P P .

2.2.3 PL (Constants)
PL F M
. PL ,
. PL
F . . F
O (decimal numbers) . (Integers)
I O (floating point) PL .

2.2.4 PL (Integer Constant)


PL
. HF .
+ - A P (prefix) .
0 - M (Octal) PL
. (-) 010 = 8 ([8]10 = [10]8).
0X F (hexadecimal)
. (-) 0XF=15 ([15]10 = [F]16).
11

2.2.5 I O PL (Floating Point Constant)


I O PL PJ P.
, F, F O, H F, F
A F . I O
PL PH F, H F A
MM.
. 58.64 I O
() . J
W PH:

5.864E1 => 5.864*101 => 58.64


5.864E-2 => 5864*10-2 => 58.64
0.5864E2 => 0.5864*102 => 58.64

I O J PH E
e .

2.2.6 P PL (Character Constant)


P PL P
P . C++ NJ P
FJ P (ASCII
FJ E/d/H C
P) . (tab), HQO (backspace)
KF (line feed), FHL (null), H (backslash) A C
P M K (escape sequence) PHA.
M K H
PH. 2.2- M K P
.

12

M K

\a

E L

\b

HQO

\n

F K/ KF

\t

\v

\\

\ () \

\o

\X

\0

FHL
2.2. M K P

2.2.6 G (String Literal)


G P
PO K . G
PO E (array of characters) A.
G (by default) \0 C P
P . F F PA.
(-) : testing

2.2.7 P (Operator)
P F M G
PA. P H
O (Operands) e A.
:
RESULT = NUM1+ NUM2

F, NUM1, NUM2 A H. +
M P .
=
FH(assignment) PJ M M F,
RESULT PJ IA. C++ NJ
PO 2.3- .

13

[]
()

*
+

%
<<

==
!=

=
*=

>=
&=

>>

/=

^=

->

<

+=

|=

++

!
size of

>
<=

&&
||

-=
%=

, -#

<<=

##

&

>=

?:

2.3 C++ NJ P

W P C++ N K:
:: .* ->*
# ## A P L (Preprocessor)
F .
P :

W (Arithmatic)

FH (Assignment)

K (Component Selection)

G (Conditional)

(Logical)

(Manipulator)

H P M (Member dereferencing)

G (Memory Management)

L (Preprocessor)

d (Relational)

(Scope Resolution)

(Shift)

(Type Cast)
HO E J
P (Unary) (Binary), (Ternary)
HK.
:
P H .
P H .
P H .

14

&

F
*

++

HG G


2.4()
P

P
+

*
/
%

<<
>>
HG
&
|
^

&&
||
FH =
/=
+=
*=
%=
-=
<<=
>>=
&=
|=
d

<
>
<=
>=
==
!=
.
->
::

M

N

eF

Q


FH
FH
FH
A FH
eF FH
N FH
FH
FH
HG FH
HG
FH
MCP
MK
MCP G
MK G
G
GK
K
K
/
P

.*
P M P
G
-> *
P M
?:
P
O
,
FH
2.4() P

15

2.2.7.1 W P (Arithmatic Operators)


W P E G
A. W PO :
P
+
*
/
%
+=, - =
*=, /=
%=

eF
A
FH

W P, PL/P,
W P
A A, W (arithmatic expressions)
A.
:

a = -5;
a = +b;
a/=5; (a=a/5)
a++; (H I P. a = a+1 G)
a- -; (H P. a = a-1 G)
++a; ( I P. a = a+1 G)
- -a; ( P. a = a-1 G)
a *= 2; (a = a*2 )
a %= 5; (a = a%5)
a = b + pow(x,y); (a = b + xy )

P KJ J GA.
H, P PH
FHA.
(association) A. F .
E PO
2.6 - .

16

P
K

( ), [ ]

H ++,

E-

++,
+, -

E-

*. /. %

E-

+, -

E-

2.6 E PO K

W , W FH
K MA.
5 + 6/3 = 7

5 * 6/3 = 10

17

(5 + 6) /3 =3

k int M 3 .
k float Q, M 3.66 A.
1 + pow (3, 2) = 10

18

1 + pow (3, 2) * [ (3 + 2) (8 - 7) + 2 ] = 55

I, P C NJ C++
NJ A. P
, FH 2.7- .

19

a++

a- F

FH 1 I

a = 5;
c = a++;

:
c = a;
a = a+1;

, c
PJ

F 5 .
++a

a- FH 1 I

a = 5;
c = ++a;

:
a = a+1;
c = a;

,
c-
PJ
.
F 6 .
a --

a- F
FH 1

a=5;
c = a--;

:
c = a;
a = a-1;

c-
F
?
--a

a- FH
F

a = 5;
c = --a;

:
a = a-1;
c = a;

c-
F
?
2.6 I, P

20

2.7- FO PO
F ?
1. a = 5;
b = 5;
a = a+b++;

2. x = 10;
f = 20;
c = x++ + ++f;

PJ

F_____

PJ

F
c-_______
x-_______
f-_______

3. fun=1;
sim=2;
final= - -fun + ++sim-fun- -

PJ
F
fun-_______
sim-______
final-______

2.7 O M

2.2.7.2 d P (Relational Operators)


F H d P A.
d PO :
== G
> MK
< MCP
>= MK G
<= MCP G
!= GK
d P F H A.
H d P
d A. ,
W G d P
.
10 > 20
500.45 <= 1005
99 != 99.5
9 == 9

21

d , K (true) (false) A
M . PL 0 (N) (false) F
PA. N F K (true)
P. O Of :

0 (10>20 )
1 (500.45<1005 K. NI F)
1 (99!=99.5 K. NI F)
1 (9==9 K. NI F)

W J F ?
num1=99, num2=20, num3=10 Q
(num1+num2-num3)/5*2<(num1%10) ?

2.8 - d FH:
P

==

5==6

!=

a!=a

>

5>6

M
0

a>A
<

5<6
a<A

>=

a>=z
5>=5

<=

a<=z
5<=6
2.8 d FH

22

d P, W PM
K . , x+y*z< e/f
FH:

2.2.7.3 P (L P)
GO M
P PA. && (AND), || (OR) ! (NOT)
A P .
:
c=5. d=6. choice=y, term=2' (K 1 ,
0 F ).
Result_1 = (c == d) && (choice != term)
Result_2 = (y == Y) || (term != 0')
Result_3 = (c==d) &&(y == Y) || (choice != term)
Result_4 = (c==d) || (y == Y) && (choice != term)
Result_1, Result_2 AP F ?
Result_1- F 0 (); Result_2- F 1 (K)
Result_3- F 1 (K); Result_4- F 0 ()

23

Result_1 = (c == d) && (choice != term) F Fh J

Result_2 = (y == Y)&& (term != 0) F Fh J

24

Result_3 = (c == d) && (y = = Y) || (choice != term)

F Fh J,

1 K

25

Result_4 = (c == d) || (y = = Y) && (choice != term)

F Fh J,

26

P, d P W
PM K . H
J F P ?

5<4 && 8+9

0 -

2.2.7.4 G P (?:) (Conditional Operator)


G P,
(num1>num2) ? K: ;
. ? : P. (num1>num2), K,
- A H. P G P
A. PJ ,
E1? E2: E3;

. E1, E2, E3, A H . E1


G . E2, E3 A F,
. , O K
PJ ,
max=(num1>num2) ? num1: num2;

. num1 F num2-
K Q max- num1 . , max- num2
.

27

W FJ, x PJ
F ?
a = 10;
b = 10;
x = (a<b)? a*a : b%a;

2.2.7.5 FH P (Assignment Operators)


= FH P . J
(PJ ), M PJ (P
J ) A.
FH P M N
FH P . FH P
W 2.9- .

M
A=5

A PJ 5 P 5
F A.
F A

A += 2

A += 2
A = A +2 .

A- J

A=A*4

A- J

A *= 4

F 7.
F 20.

A /= 2

A=A/2

A- J
F 2.

A-=2

A=A2

A- J

F 3.
A %= 2

A- J

A= A%2

F 1.
a=5, b=6, c=7 Q W O F :
a += b*c
c *= a + a / b
b += a % 2 * c
2.9. FH P

28

C++ NJ PO
K G F
2.10- .
P K

I
I

()
[]

H ++, ,
++,
!
+, -

E
E

E

I
I
I
I

* / %

+-

< <= > >=

= = !=

&& (AND)

|| (OR)

?:

= *= /= %= +=
-= <<= >>=
&= ^= |=

FH

2.10 P K
(P: C++ N K C P O M
)

29

2.2.8 G P (Punctuators)
G P PH E P
. G P P
2.11- .
G P

C++ A

;
//

P (Comments)
A.

/* */

P A F
P .

{ }

C++ F P
A. P
F P .

[ ]

EJ PH A
(Index) P
.

P P
.

P .
2.11 G P P

2.3 (Data Types)


G NJ P
PH F . M
(Data Types) A. C++ NJ
HK F, C I
GO E OA.
J HK
:
M GH (Compiler) K G
A F .

30

G G M
P F .
M J HK:

M (User defined type)


O M (Built-in type)
M M (Derived type)

HK 2.2-
:

C++Data
M
C++
Types

User Defined
type
(Structure)
Structure
(Union)
Union

(Class)
Class

(Enumeration)
Enumeration

Integral
type

int

O
Built
- in type

(M)
fundamental)

M
Derived
type
E
Array(Array)

Function (Function)

(Pointer)
Pointer
P
(Reference)
Reference

I
Floating
type

void

float

char

double

2.2 C++ M

2.3.1 M (User defined data type)


G M, A
F .
GL O (readability) A.

31

, W M :
MF
student
P

2.3 M

student M (class)
. P,
O M K K
. K M , M
FJ .
, class student M
F
M M H GL
O F.
G M F
. (Type definition) F
, M P A FA.
:
typedef <M> < M P>;
:
typedef int marks;
typedef char grade;

32

marks, grade A M P
. int, char P :
marks eng_marks, math_marks;
grade eng_grade, math_grade;

F M P A
typedef A. GL O A.

M M.
PO M
M A. int Pf PL
.
:
enum <M P> (F1, F2...... F n );

:
enum working_days (Monday, Tuesday, Wednesday, Thursday, Friday);
enum holidays (Sunday, Saturday);
working_days, holidays A P
M . (Monday, Tuesday,...) PL
PL F
.
MF W P
PM:
enum <P> P1, P2,.......Pn;

, working_days MF first_workingday,
last_workingday P W PM :
working_days first_workingday, last_workingday;

P working_days F
FO .
first_workingday = Monday;
last_workingday = Friday;

P PL Monday, Tuesday, Wednesday....... A


GJ GH Q 0-M A 1, 2, 3,...........
K int PL A. ,
FH .

33

first_workingday = 0;
last_workingday = 4;

, P PL O
F PH int PL
.
enum working_days (Monday=1, Tuesday, Wednesday, Thursday, Friday);

F Monday 1 F .
PL 2, 3, 4.... int PL F .

2.3.2 I (Storage Class)


PJ PM I F
A (qualifier) . auto, static, extern,
register I . static,
register A PO PM 0
F MA. auto PO
MF J F F
F. auto P, (Garbage)
F A. I
, 2.12-
:

34

auto

static

extern

register

P (local variables) .
PM
F P
. Q,
P
auto .
auto I

PHF.
P
F
P
F
N A. PP
P PM
F
H EP
GF AJ static
F(Modifier)
A. P C
F
A.
P,
GL
O
MP(global variables)
. P
GL .
register F,
PM P
CdM FO (registers)
F GH
A. I
.

void main()
{
auto float ratio;
auto int count;
}
ratio, count A P
main()

.
autoI

void fun()
{
static int x;
x++;
}

extern int filemode;


extern void factorial();

void fun()
{
register int i;
}

2.12 I

35

2.3.4 O M (Built-in Data Types)


O M M
M A. GHJ
. M

(integral), I(float), FHL(void) HK.


, int, char A M A. int
M. 1, 2, 3...... F . H F
. char P M. Q P
F, F A . ,
ch PJ PM, F FH
:
char ch = A;

,
char ch = 65;

PM. (65 A F vA F
). E G
A. ch PJ A A.
I M float, double A M
A. I M H F F
F F . (I O PL
P G ).
FHL M (void type) J A:
F FH
P
(generic pointer) PM
, W void.cpp, fun.cpp A C++ GO
:

36

void.cpp GL void cpp(void) PM


(prototype) M F FH ,
O (parameters) M M F
A. main( ) P,
fun() . fun.cpp GL PM
int fun(int a, int b) fun( ) int

F FH GH A.
main() P sum=fun(a,b)
. sum P return a+b
int FH A.

void M, F FH
GH A. L
A, void M F
A.
M F (modifiers)
A. F GJ AF
M A. signed. unsigned. long. short
A C F . M,
GF , P
F A 2.13- .

37

Type Byte

Range

char

-128 to 127

unsigned char

0 to 255

signed char

-128 to 127

int

-32768 to 32767

0 to 65535

signed short int

-32768 to 32767

long int,signed long int

-2147483648 to
2147483647

unsigned long int

0 to 4294967295

float

3.4e-38to3.4e+38

double

1.7e-308 to
1.7e+308

long double

10

3.4e-4932 to
1.1e+4932

unsigned int,
unsigned short int
signed int,short int,

2.13 M P F

2.3.4 M M (Derived Data Types)


M M int, float O
M MOL
. , E (Array)
MF PM/FH :
int num_array [5] = {1,2,3,4,5};
char dayname [7] [4] = { Sun, Mon, Tue, Wed, Thu, Fri, Sat};
num_array 5 F . EJ
H N A. H
0 ML A. num_array[0] H 1
. num_array [4] H 5 .
dayname[0], dayname[5], dayname[3][2] A O
F ?

38

(Pointers)
G K P .
PJ G H
A. EP GF
K . K .
i . K NULL F
A 1, 2, 3 ......... K .
, 640Kb G, NULL A
6,55,356 K . 2.4 .
640KB G
K

Null
1
2

655358
655359

Last Address

C K

2.4 640KB G

G F N , GH,
PK G H W A. P
M GF
A.
, W PM :
char c; int i; float f;

char c - 1
int i - 2
float f - 4

P K
PHA.
, c, i, f A PO G
H 1, 2, 4
. P K
F J PA.

39

M , K (&), F
(*) A P P P .
& P:
int num1=10;

, C++ GH (C++ Compiler) W


A:
1. GF int F F W

A
2. G num1 A.
3. GF F 10 F A.
PJ (HF )

num1

(HF F)

10

0X8f90f2

num1 PJ G K

// Program 2.1
// to demonstrate use of & operator
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i = 10;
cout << \n Address of the variable... <<&i;
cout << \nValue stored in the variable .. << i;
getch();
}

40

W F :

P: F P (*)
1. P PM .
2. GF F FJ
(F P).
3. P (Unary Operator) .

2.4 P (Variables)
PH FH A
F P A. , W
:
int num;
num = 5;
int num ; n int P

A.
int num;
num = 5;

FH , 5 F num PJ
A A.
G H
P .

41

PJ , , W (underscore)
A . F WP
. Q, WP G
P (internal system variables)
P. (Case Sensitive)
. . CP , K F
F PA. 2.14 - G .
P

K/
H?

A_b

1a_b

H

PJ
WP
.

_Test
Balance$
#one
Include
2.14 P O

2.4.1 PO PM
F F P G
A. P MF J GH
G A. , P
M PH PM .

int q;
float f1, f2;
char name [10], choice;

PM P :
M P

42

PJ

int side, float hypotenuse, area;

PM . PM.
, GH P K:
side, float, hypotenuse, area A int P
. , float H O
H F .
side int P, hypotenuse, area float
P PM .
PM P :


P PM P PH .
O HK .

M PM char, int, double, float, void, short, signed,


long, unsigned A A.
long, short, signed, unsigned A, O
O P F
(modifiers) FA (qualifiers) A.
void .
GJ 15 int F 00000000 00001111
A. int F J 16 H H
F A. A H
15 H F
A. int PJ
F +32767. C F -32768. (215 = 32768. F 0 +
32767 . F F -1 -32768 ) 16- H,
F H (Most Sifgnificant Bit ) P H (Sign Bit) A.
, (Positive) F (Negative)
PA. 16- H 1 Q F , 0 Q
.

43

int PJ PMH unsigned F


F PJ F .
P H F F. ,
FH 216 . 0
65535 F .


F (modifier) MF
H P F M A.

FO M:
unsigned F, /F
P P H F F
P FH
PA.
long F PH MF
FK, FH cA.
MF P PM,
F . ,
unsigned int registration_number;
unsigned long int index;
signed char c;

 P PM, F
M .
const FA (qualifier) GL F
PJ F A. const
PJ F GH H
F . const FA, H F
P PM M
A.
:
const float pi = 3.14;

44

FA F H
E FH P
M 2.15- :
Data Types
Type
unsigned char
char
enum
unsigned int
short int
int
unsigned long
long
float
double
long double

Length
8 bits
8 bits
16 bits
16 bits
16 bits
16 bits
32 bits
32 bits
32 bits
64 bits
80 bits

Range
0 to 255
-128 to 127
-32,768 to 32,767
0 to 65,535
-32,768 to 32,767
-32,768 to 32,767
0 to 4,294,967,295
-2,147,483,648 to 2,147,483,647
3.4 x 10-38to 3.4 x 1038
1.7 x 10-308to 1.7 x 10308
3.4 x 10-4932 to 1.1 x 104932

2.15 F M

P PM
int * iptr;

PJ

P, GH
A (P K )
int A
PA.
int * iptr PM , iptr int
P (pointer variable) .
int F F K iptr
P .

45

PJ PM :
char * cptr
float * fptr

char PM
float

void * vptr

M A
. G
PMA.

const int *ptr

PL int F ptr.
(ptr G KJ
int F P )

char * const cp

cp PL . cp-

K P .
2.16 P

2.4.2 P A (Initialization of Variables)


P PM F PH F
FH A. PO
FH A (Initialization) A.
int num = 10;
int fun(5);

P num PJ 10
FH. P A (constructor)
5 F fun PJ .

(Implicit Conversions)
J O GH
A. ,
W F :
float f = 7.6;
int x = f;

46

, x PJ 7 F . float
int M. GH
A.

MF:
P H
P .
F W G:
1.

H long double Q
HJ F long double .

2.

H double Q F double
.

3.

H float Q F float

4.

H unsigned long int Q F


unsigned long int .

5.

H long int Q F long


int .

6.

H unsigned int Q F
unsigned int .

// demonstrating implicit type conversions


// Program 2.2
# include <iostream.h>
# include <conio.h>
# include <iomanip.h>
void main()
{
clrscr();
int i;
float f;
double d;
long double ld;
unsigned int ui;

47

unsigned long int uli;


i = -5;
f = 2;
d = 3;
ld = 3;
ui = 6;
uli = 4;
cout <<\nSizeof long double..<<sizeof(ld*d)<<\t<<ld*d;
cout << \nSizeof double... << sizeof(d*f)<<\t<<d*f;
cout << \nSizeof float... << sizeof(f * i)<<\t << f*i;
cout << \nSizeof unsigned long int ...
<< sizeof(uli* f)<<\t<< uli * f;
cout << \nSizeof unsigned int... << sizeof(ui * i)
<<\t<< ui * i;
getch();
}

P: sizeof P.
MF M (M F GF
) O M .
Output
Sizeof
Sizeof
Sizeof
Sizeof
Sizeof

displayed by the above program:


long double
...10 9
double
...8
6
float
...4
-10
unsigned long int ...4
8
unsigned int
...2
65506

Program 2.2 - J 2.17 - G .


J F
M M .

48

1.

MJ

10

ld*d

ld P long
double

JL
F
long double
.
2.

d*f

------

JL
F
double
.
3.
4.

4
4

f*l
uli*f

5.

ui*i

2.17 Program 2.2- J JC

P A
P H PO K FA. ,
P M ,
PJ K . ,
int *iptr, num1;
num1 = 10;
iptr = &num1; // PJ

FH
W FH H:
int * iptr;
float num1 = 10.5;
iptr = &num1; // int iptr PJ
float PJ K

H F A.
P PJ M F
.
49

(Type Cast):
PJ F FH M P
PA (float)7
7 F float A.
P FH M
PH A. :
(< >) <P/F>

,
< > (P/F)
, O M
K.
x = 8% 7.7;

GH (compilation)
H F . , % eF P
int M FO . H
,
x = 8% (int) 7.7;

FF . float PL 7.7 F int


PL . 2.18 - G .
int x;
x =7 / 3;

x- F F ?

float x;
x = 7 / 3;

x- F F ?

float x;
x = 7.0 / 3.0;

x- F F ?

float x;
x = (float) 7 / 3;

x- F F ?

float x;
int a = 7 , b = 3;
x = a/b;

x- F F ?

float x;
int a = 7 , b = 3;
x = a/ (float) b;
x- F F ?
2.18 x- F P

50

JC M
1. W O FH K bQ:
i.
ii.
iii.
iv.
v.
vi.

a + pow(b, c) * 2
a || b && c
a<b && c || d > a
(c>=50) || (!flag) && (b+5 == 70)
(a+b) / (a-b)
(b*b) 4 * a * c

2. W GO H :

# include <iostream.h>
void main()

b.
# include <iostream.h>
void main()

a.

float f = 10.0;

float f = 10.0;

x = 50;
cout << x << f;

x = 50;
cout << x << f;
}

c.
# include <iostream.h>
void main()
{
int x,y,k,I;
x = y + kI;
cout << x;
}

51

3. GO Of ?

4. W C++ N FH:
a=5, b=3, d=1.5, int c, float f .
a.
f = a+b/a;
b.

c = d * a+b;

c.

x = a++ * d + a;

d.

y = a b++ * b;

e.

(x >= y) || (!(z==y) && (z < x)) Q,

52

x = 10, y = 5, z = 11 ( )

x = 10, y = 10, z = 10

x = 9, y = 10, z = 2

5. W G, G P F
C++ F :




x = 30 Q f = 0.5 f = 5
x> = 60 Q fn = 0.9 .7

6. P ?
7. P M PJ K F name
P PM P F .

53

Ramanathan

Digitally signed by Ramanathan


DN: cn=Ramanathan, c=IN, o=Commercial Taxed Dept
Staff Training Institute,, ou=Computer Lecturer,,
email=ctdsti@gmail.com
Location: Commercial Taxes Staff Training Institute,
Computer Lecturer,Ph:9442282076
Date: 2008.03.13 11:57:14 +05'30'

3

(Basic Statements)
C++ NJ (Basic Statements) M
(tokens) F A. C++ N
HK:
k/ Of (Input / Output)
PM (Declaration)
FH (Assignment)
(Control Structures)
(Function Call)
O F (Object Message)
FH (Return)

3.1 k / Of
F
OJ EP GL P
. PO F N
. , FH
. F .
N, GL F k
. G A , M N
k cin (C- K
) A. cin C++ NJ
(Object). k
(Standard Input Stream) AA. k ,
k M JL
PA. cin k OL
. P H . cin P
PM istream.h H (Header file)
. k/Of
istream.h, ostream.h A O
C PMO GA. istream, ostream A
iostream.h
.

54

, O
PM
.
-L P F,
H GL .
-L P # P .
, E GH
A.

L P #include<iostream.h>
. , iostream.h
H GL GH
A. cin/cout A
F Q, iostream.h
H GL .
C : iomanip.h, stdio.h, ctype.h,
math.h, fstream.h H.
>> P (extraction)
(get from) P . P,
OL (stream object) F ,

PJ F. , W
:
float temperature;
cin >> temperature;

P (>>), k cin-L
M , temperature PJ F
A. P ,
F k JL , K
PO F . , temperature, humidity
A P k ,
cin >> temperature >> humidity;

.
cout (C- K ) O
f (Object) .
Of F C A.

55

Of FF (redirect) .
<< P M (insertion) (put to)
P . P PJ
F O F . ,
W :
int marks = 85;
cout << marks;
cout << \n Marks obtained is : << marks;
marks PJ I F cout
H . marks- F FJ .
cout << \n Marks obtained is : <<marks;

, F PJ F
F H. , Of P
, M J
OJ .
C :
cout << \n The sum of the variables a,b .. << a+b;
cout << a+b << \t << a-b << \t << a/b;
cout << \n The difference of numbers . << a-b
<< \n The sum of two numbers . << a+b;

3.2 C++ G - C++ GL


// My first program Program 3.1
# include <iostream.h>//preprocessor directive
# include <conio.h>
float fact = 1; // declaration of variables
int term;
int main() // function header
{
clrscr(); // predefined function
cout << \n This program computes factorial of a
number;
cout << \n << Enter a number ...;
cin >> term;
// looping statement
for(int x = 2; x <= term;fact *= x,x++);
cout << \nThe factorail of the given number ..
<< term << is .. << fact;
return 0;
}

56

G . C++ G A HK
:

P, O PM

main() .
HIP G H, G
main() . P,
GL H , H A
.
3.3

(Declaration Statements)

GL P P
M .
int *iptr;

// int PMA.

iptr = new int; // int F G

A- P
M.
*iptr = 5; // 5 F A. G
H GA.
PJ PM, PJ
PA. , int *iptr;
PM . iptr int
P P K .
P, F FK G
A.
M, P,
, P PM
PM A. HK 2.3-
M P G .
P PM, G A Q,
PM (definition) .
,
int num;

PM P . ,
57

A. , M F G
MA. W F :
int num; // int PJ PM .
num = 5; // F 5 A.
G W O K
Qb? char, int, float
MO P PM G
MA.

 PM PJ PA.
PH M A.
 P GF K
A.
 C P PM G
MA.
 P PM
F. G W
A. new P G
W .
3.4 FH (Assignment Statements)
FH , FH P
J F PJ A. =
FH P . , PO
FH :
num = 5;
total = english+maths;
sum += class_marks;

FH , C++ GH,
J M, PJ M P
A. 2.4.2. - HKM M / O
G .

58

3.5 (Control Structures)


GL H
K G A .
GL C F GJ K G
. GJ, GL
FJL F A.
G
A. W :

-1 :

1.

2.

-2:

k , Q
1 , Q
OJ G 1- .
H K GA.
k , A
F, J
F G .
F eF A Q M=ODD
59

, eF M Q M=EVEN M
A HKA.

 , GL FJL
F G
A.
A
J, bQ (Decision Making Statements),
(Looping Statements) . bQ
(Selection Statements) . C++ NJ

W F
A.

Control Structures

Selection

if .. else

3.5.1

Loop

Exit Control
Do
d .. while () loop

switch

Entry Control
For(;;)
f
While
() loops
w

(Selection Statements)

GL G J bQ (Decision),
GL F A.
C++ NJ bQ O
A. I A, N
A if... else... . bQ
switch, PJ F J, A

60

HK A HKM F G
NA.
if : bQ O I O.
O A:

if (N)
if..else (N)
if (G/ )
{

if (G/ )

// F-1

}
else
{

// F
}

// F-2
}

W G (Program - 3.2) if MA:


// Program - 3.2
# include <iostream.h>
# include <conio.h>
// Demonstrates the use and syntax of if statement
void main()
{
int a;
clrscr();
cout << \nEnter a number ;
cin >> a;
if ( a%2 == 0)
cout << \nThe given number << a << is even;
getch();
}

61

GL, G K
The given.... F, . GL ,
cout<<\n The given... G
getch() MM.
W G (Program 3.3) if.. else... MA:
// Program 3.3
// Demonstrates the use and syntax of if else statement

# include <iostream.h>
# include <conio.h>
void main()
{
int a;
clrscr();
cout << \nEnter a number ;
cin >> a;
if ( a%2 == 0)
cout << \nThe given number << a << is even;
else
cout << \nThe given number << a << is odd;
getch();
}

GL, K
The given number 10 is even F .
else- .

62

if H G /

EJ :
branch P G .
int branch = 10 > 20;
if (branch)
{

// F-1;
}
else
{

// F-2;
}

N (Zero) K
, if(1) G .
if (1)
{

// F-1;
}
else
{

// F-2;
}

G . J F
>0 - FHJ, F-1 G.

F-2 G.
if ( a % 2 )
{

// F-1;
}
else
{

// F-2;
}

63

W G A F
P ?
// Program - 3.4
# include <iostream.h>
# include <conio.h>
void main()
{
int count = 1;
if (count > 0)
{
cout << \nNegating count ....;
count *= -1;
}
else
{
cout << \nResetting count ...;
count *= 1;
}
getch();
}

G A Negating count... F
. count F -1- H else-
H F M
?
F: if...else H if- W
F GJ, else F G
. if F G else
F G.

 N if..else...
H G J
PH F G.

64

1.

?
G H
P
. K :

if a> b
cout << True;

if (a>b)
cout << True;
2.

if ( a > b)
a- -;
cout<<\nVariable is decremented;
else
a++;
cout << Variable is incremented ..

G H, Misplaced
else H
F .
F

H O
P
.
K :
If ( a> b)
{
a--;
cout<<\nVariable is
decremented;
}
else
{
a++;
cout << Variable is
incremented .. }

3.

G H
O, if
P
M. GH,
Misplaced else H
. K :

if (a > b);
cout << Greater..;
else
cout << Lesser ..;

if (a > b)
cout << Greater..;
else
cout <<Lesser ..;
3.1. if

65

3.2.2- E G
if F :

F 90-
H A G

2.

F 90- H A
G . B .

3.

E 30 A.e- Q

if

Accelerate - traffic to flow

E 31-L 40 A.e. Q
Moderate - accelerate by 10 kmph ,
Good- be careful..

F FJ .
3.2. if

H if : if..else H if
. W if..else
H .
(-1)
(-2)
-1;

-2;

-3;

H if..else H,
else- , else- if

66

,
if (grade = = A)
if (basic > 5500)
incentive = basic * 10/100;
else
incentive = basic * 5/100;
else
cout << Try to attain Grade A;

F :

grade== A, basic ==5501 Q incentive- F 550 .


grade == A, basic ==5000 Q incentive- F 250 .
grade != A Q, if G. O else
G. Try to attain Grade A
F A.
if , W if
G? F J .
if (grade = = A && basic > 5500)
incentive = basic * 10/100;
else if (grade = = A && basic <5501)
incentive = basic * 5/100;
else
cout << Try to attain Grade A;

switch : AHK . GJ

J, O
. A:

67

switch ()
switch (remainder)

{
case 1 : F-1;

{
case 1 : cout << remanider 1;

break;

break;

case 2 : F-2;

case 2 : cout << remanider 2;

break;

break;

case 3 : F-3;
break;

default :cout << Divisible by 3;

default :F-4;
}

W G, switch P MA:
// Program - 3.5
// to demonstrate the use of switch statement
# include <iostream.h>
# include <conio.h>
void main()
{
int a, remainder;
cout << \nEnter a number ...;
cin >> a;
remainder = a % 3;
switch (remainder)
{
case 1 : cout << \nRemainder is one;
break;
case 2 : cout << \nRemainder is two;
break;
default: cout << \nThe given number is divisible by 3;
break;
}
getch();
}

68

G,

a = 4 eF
Remainder is one

1 Q,

a = 5 eF
Remainder is two .

2 Q,

a = 3 3- Q,
The given number is divisable by 3 .

Q,
F FJ .
W GL Of A
A?
// Program - 3.6
// to demonstrate the use of switch statement
# include <iostream.h>
# include <conio.h>
void main()
{
int rank = 1;
char name[] = Shiv;
switch (rank)
{
case 1: cout<<\n<<name<<secured 1st rank;
case 2: cout<<\n<<name<<secured 2nd rank;
}
getch();
}

GL Of :
Shiv secured 1st rank
Shiv secured 2nd rank
case 1, case 2 A F
KA? Program-3.5 Program- 3.6 A

GO F H .
Program- 3.6- M KA? .
break M. FL P ?

69

FJ FJ break
. , GJ J
M G case- A
case-O F K G.
(Program - 3.6) case 1-
A. F G
A. F break case 2-
F K GA.

 PH case- F
G switch M
O, FJ FJ break
.
W swich H.
:
1.

2.

char name[ ] = Shiv;


switch (name)
{
case Shiv : cout << \n
<< name << secured
1st rank;
case Rama : cout << \n
<< name << secured
2nd rank;
}

switch selection expression must


be of integral type G

float value;
switch (value)
{
case 1.5 : cout << \n
<< value 0.5;
case 2.9 : cout << \n
<< value + 0.1;
}

switch() P
float

H H.
,switch-
J F,
(char, enum, int)
PL
.

HA.

70

3.

case 1 to 2
. case-

switch (rank)
{
case 1 to 2 : cout << \n
<< Best rank;
break;
case 3 to 4 : cout << \n
<< Good rank;
}

F
.
PH
F
F
Q, G
F
.
switch (rank)
{case 1 :
case 2 : cout << Best rank;
break;
case 3 :
case 4 :
cout << Good rank;
break;
}

3.5.2. (Loops)
, F PH F
F GA. , W Program - 3.7-
:
// Program - 3.7
# include <iostream.h>
# include <conio.h>
F
G KF
G

void main()
{
int i = 1;
loop_start:
if (i < 6)
{
cout << i <<\t;
i = i + 1;
goto loop_start;
}
}

F F G
F
GL , F
F G

FJ
.

71

G A, if P W
F 5 G, 1 5 J
FJ .
Program- 3.7 A:

1. i P PM FHA.
2. i < 6 d KFA.
3. G K Q F (cout<<i;i=1;)
GA. GL e loop_start
A (goto loop_start).
d J G J
F F F A. i P,
P PHA. ,
F F F G i- F
A.
, F, F,
F . G
KA. J, GL , A
F e e FHM, F F G
A. W G :
//Program - 3.7 A
void main()
{
int i = 6;
loop_start:
if (i < 6)
{
cout << i << \t;
i = i + 1;
goto loop_start;
}
cout << i;
}

//Program - 3.7 B
void main()
{
int i = 6;
loop_start:
{
cout << i << \t;
i = i + 1;
if (i < 6)
goto loop_start;
}
cout << i;
}

72

GO Of G
Al?
Program-3.7A, 6 FJ .Program-3.7B, 7 .
GL, (cout<<i; i=i+1;) A H G
. G
M. H G KA. i- F
7 GL loop_start I
M. FL P ?

 A FJ FJ G
J, F
GM.
 G ,
, -F (Program3.7A), O-F
(Program - 3.7B) HK.
A , A:
1. G PJ F A.
2.

A F GA.

3.

G PJ F IA.

4.

G P, d J KF
A. d J F J
, FJ ,
M O.

C++ NJ, :
1) for()
2) while()
3) do..while()

73

do...while() :

:
do
{

F;
} while <(G)>
W G :
// Program - 3.8
# include <iostream.h>
# include <conio.h>
// to print the square of numbers
// between 2 to 5
void main()
{
clrscr();
int num = 2;
do
{
cout << num * num << \t;
num += 1;
}while (num < 6);
getch();
}

Program -3.8 GL J W M M
:
) :
1. P
2. A FJ
3. KF
) G?
) GL Of ?
) ?

74

M:
) 1. P num
2. A F :
cout<<num*num<<\t;
num += 1;

3. (num<6) KF
)
) 4 9 16 25
) O-F
A G :
1. A A.
2. num- F FJ A.
3. PJ F 1 IA.
4. G FA. M
J G-2 A.
5. A.
do..while(G) O - F
. , G , A F
F . G
J . :
int ctr = 5, sum = 0;
do
{

int ctr = 1, sum = 0, check = 1;


do
{
cout << ctr;

cout << ctr;


sum = sum + ctr;
ctr = ctr - 2;

sum = sum + ctr;


ctr = ctr + 2;
check = (ctr < 11);
}while(check);

}while(ctr);

75

int ctr = 5,sum = 0,c=1;


do
{
cout << ctr;
sum = sum + ctr;
ctr = ctr - 2;
}while(ctr >= 1);

W GO Of ?

Snippet A-J i- F -32768 G.


Snippet A, Snippet B A GO H

. O G
K . Snippet A-J i- F
10- M . Snippet B-J choice- F
1 G K A MA.
F G K G
I A. A F
PJ F I/H
G .

76

while() :
-F . :
while <(condition)>
{

F;
}
while() P F K M

A FJ G
. F J F
. do..while() A M G while()
F e . (Program-3.9).
// Program - 3.9
# include <iostream.h>
# include <conio.h>
// to print the square of numbers
// between 2 to 5
void main()
{
clrscr();
int num = 2;
while (num < 6)
{
cout << num * num << \t;
num += 1;
}
getch();
}

A FJ
F F
G
.

:
1. num PJ 2 F
A.
2. num<2 F FH, M K
G-3 A.
3. num PJ FH ,
FJ A.
4. num PJ F IA.
5. GL G-2 A.
6. A.

77

G Program- 3.10- J W M
M O.
//Program-3.10
# include <iostream.h>
# include <conio.h>
void main()
{
int x = 3, y = 4, ctr = 2,res = x;
while(ctr <= y)
{
res *= x;
ctr += 1;
}
cout << x to the power of y is : << res;
getch();
}

) 1. GL
P ?
2. A F ?
3. KF ?
) GA?
) GL O ?
) F
?
M:
) 1. P ctr
2. res *= x; ct +=1;
3. ctr <=y

)
) 81
) - F

78

W G Program-3.11- Of ?
G k y, y, y, y, n :
// Program - 3.11
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
int counter = 0;
char choice = y;
while (choice == y)
{
cout << Continue <y/n> ...;
cin >> choice;
counter = counter + 1;
}
cout << \The loop is executed ..
<< counter << times;
getch();
}

W G H. P H
P. G K H F:
//Program - 12 A

//Program - 12 A
# include <iostream.h>
# include <conio.h>
//to print numbers between 5&10
void main()
{
int start = 5, end = 10;
while (start <= end)
cout << start++;
getch();
}

# include <iostream.h>
# include <conio.h>
//to print numbers between
5&10
void main()
{
int start = 5, end = 10;
while (start >= end)
cout << start++;
getch();
}

79

//Program 13 A
# include <iostream.h>
# include <conio.h>
// to print numbers between
10&5
void main()
{
clrscr();
int start = 5,end = 10;
while (start <= end)
cout << start--;
getch();

//Program

13 B
# include <iostream.h>
# include <conio.h>
// to print numbers between 10&5
void main()
{
clrscr();
int start = 5,end = 10;
while (start <= end)
cout << end--;
getch();
}

}
//Program 14 A
# include <iostream.h>
# include <conio.h>
// to print numbers
between 1 & 5
void main()
{
clrscr();
int start = 1;
while (Start <=5)
cout << start++;
getch();
}

//Program 14 B
# include <iostream.h>
# include <conio.h>
// to print numbers
between 1 & 5
void main()
{
clrscr();
int start = 1;
while (1)
cout << start++;
getch();
}

for() :

. PH
G A. :
for ( F; F G; I)
{

F;
}
for(;;) :

1.
2.

GL L AQ ,
PJ F .
G KF. G K Q A
F .
A.

80

3. A F e G
PJ F I, e G
KF.
4. G .
W G for(;;) A M:
//Program - 3.15
# include <iostream.h>
# include <conio.h>
void main()
{
int i, fact = 1;
for(i = 1; i < 6; i++)
fact *= i;
cout << \nThe factorial of the number is .. <<fact;
}

for(i = 1; i < 6; i++)


I ( F)
KF G (2- F)
PJ
FH (3- F)




PJ FH,
A F L
,
.
A F
G
KF.
G KF
PJ F I.

81

W G . P Of
? F .
// Program 3.16
# include <iostream.h>
# include <conio.h>

Of:
10

void main()
{
int ctr = 10;
for(; ctr >= 6; ctr)
cout << ctr << \n;
}

9
8
7
6

// Program 3.17
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
for(int i=2,fact =1;i<6;fact*=i,i++);
cout <<\nThe factorial .. << fact;
getch();
}

Of:
The factorial.. 120

for() P FH I FJ
, Qb?
H J K. for( )
A F P .
O HK . W A
FJ Of ?
void main()
{
for (int i = 1, j = 0 ; i < 8,j<3;i++,j++)
cout << I << \t;
for (int i = 1, j = 0 ;j < 3,i < 8;i++,j++)
cout << i << \t;
}

82

Of :
1 2 3 // j < 3 .
1 2 3 4 5 6 7 // j< 8
. O P
F G
. G (
) K
.
G && ||
P .

// Program 3.18
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
int sum =0, ctr = 1;
for(;ctr <= 5;)
{
sum += ctr;
ctr = ctr + 1;
}
cout << \nSum : << sum;
getch();
}
// Program 3.19
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
int sum =0, ctr = 1;
char ch =y;
for(;ch == y;)
{
sum += ctr;
ctr++;
cout <<\nContinue <y/n> ? ;
cin >> ch;
}
cout << \nSum : << sum;
cout << \nChoice : << ch;
getch();
}

Of
:
sum : 15

FH
I
for()

M
Qb?

Continue
Continue
Continue
Continue
sum:10
Choice :

<y/n>
<y/n>
<y/n>
<y/n>

?
?
?
?

y
y
y
n

F for()
G

F bQ
Qb?

W G FJ H ?
int ctr, sum = 0;
for (ctr =1; ctr <5; ctr++);
sum += ctr;
cout << sum;

Of 5 .
KA? for() O .
, sum + = ctr , A F
M.

83

W GFO Of ?

for(it ctr =1; ; ctr++)


cout << ctr;

FJ PJ
F e e 1 A
MA. Of
1 . ,
M AM.

G
int F
-32768, +32767
A
F
. M
AM.

3.5.3. continue
A FJ continue , A
C . A FJ continue
H G.
//Program 3. 20
# include <iostream.h>
# include <conio.h>

for() A I
F .

void main()
{
int i = 1,sum = 0;
for(;i<10;i++)
{
if( i % 2 == 0)
{
sum += i;
continue;
}
cout <<i;
}
cout << \nSum of evennos..<<sum;
getch();
}

84

O continue M W :

3.5.4. break
KF G , A
GA. C GO, KF G
PH F A
. , Program - 3.21- :
//Program - 3.21
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
int a[] = {1,2,3,4,5,6,7,8,9};
int search_item = 7;
for(int x=0; x<9;x++)
{
if (a[x] == search_item)
{
cout << \nItem found at position .. << x;
break;
}
}
cout << \n << value of index position is .. << x;
getch();
}

85

Of :
Item found at position .. 6
Value of index position is..6

 break , GL

O A.
x - F 6 -
, break

A PA.

 break AM
O .
 break AM
.
H (Nested Loops)
A FJ H
. W G Program- 3.22- .
// nesting of loops Program- 3.22
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
for(int i = 1; i <= 3; i++)
{
int j = 1;
while(j <= i)
{
cout << * ;
j++;
}
cout << \n;
}
getch();
}

86

Of:

*
* *
* * *

O A:

H O A:

for ..loop

while loop

i = 1

G 1 ( j<= I)

i = 2

G ( j = 1 .. 2)

i = 3

G ( j = 1.. 3)

W GL Of ?
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
int i = 1, j = 1;
while(i <= 3)
{
cout << \n;
for(i=1;i<=j;i++)
cout << *;
i++;
j++;
}
getch();
}

Of?

87

H MF:
1. O-, - P
. H
.
2. - O-A F
F .

3.6 G
EP NJ F, G
NJ G A. G NJ
G P (Source Code) A. P
F K M N . F
N M G (Object file) A.
GH (Compilers) PJL
A. GH , P
JL F N N G
. GH, NJ ( )
KA. HJ PJL
A.
, G (executable file)
A. O K 3.1- .

3.1 G

88

JC M:
1. W PM K, H PH.
H Q, .
PM

K/H

int A; a;
char name(10);
float f, int;
double d, float f;
int 1choice, _2choice

2. W GL H FF, K G :

3. W PM
F :
) 8/3 W M F .
) Emp_Name PJ Kalam
FH.
) Y-yes, N-no PH KI k
.
4. W G FO H :
)

int a = 10; b = 5;
if a > b
cout << a;

89

) if (a<b) && (a<0)


cout << a is negative and ...

) char option = Y;
do while option == y
{
cout << * ;
.................
}

for (int i = 1; i < 10; i ++)


cout << i * 2;

) do
{
cout << * ;
}while (cout << \n Continue <y/n> ...; cin >> ch; ch == y);

5. W G/ GFO Of
?
// 5 a.
# include iostream.h>
# include <conio.h>

// 5 b.
# include <iostream.h>
# include <conio.h>

void main()
{
int feet;
const int inch_conversion = 12;
clrscr();
cout << \nEnter feet ;
cin >> feet;
cout << \nConverted
to inches
<< feet * inch_conversion;
}

void main()
{
int i = 1, sum = 0;
clrscr();
while(i++ <= 5)
{
cout << \n << i;
s += i;
}
cout << \nValue of the
variable i after
executing the
while loop ..
<< i << \nSum:
<< s;
}

input 7 for feet

90

// 5 d
# include <iostream.h>
# include <conio.h>
void main()
{
int i = 1, sum = 0;
clrscr();
for(i = 1; i <= 5; i++)
{
cout << \n << i;
sum += i;
}
cout << \n << i << \t << sum;
for(i = 1; i <= 5; ++i)
{
cout << \n << i;
sum += i;
}
cout << \n << i << \t << sum;
}

// 5 c
# include <iostream.h>
# include <conio.h>
void main()
{
int i = 1, sum = 0;
clrscr();
while(++i <= 5)
{
cout << \n << i;
sum += i;
}
cout << \n << i << \t << sum;
getch();
}

//5e
# include <iostream.h>
# include <conio.h>

// 5 f
# include <iostream.h>
# include <conio.h>

void main()
{
int i = 1, j = 1;
clrscr();
do
{
while (j<=i)
{
cout << #;
j++;
}
cout << \n;
i++;
j = 1;
} while(i<= 5);
getch();
}

void main()
{
int num = 1784, s= 0, d = 0, x;
x = num;
clrscr();
for(;num > 0;)
{
d = num % 10;
s += d;
num = num / 10;
}
cout << \nThe sum of digits of
<< x << is :
<< s;
getch();
}

//5 g
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
for(int i = 1,s = 0; ; i++)
{
if (i%2 == 0)
continue;
s += i;
if ( i > 9)
break;
}
cout << \nThe sum is .... <<
s;
getch();

// 5 h
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
for(int i = 1,x = 0;i <= 5; i++)
x = x + i%2==0 ? i*1 : i * -1;
cout << x;
getch();
}

91

//5 j
# include <iostream.h>
# include <conio.h>

//5 k
# include <iostream.h>
# include <conio.h>

void main()
{
clrscr();
do
{
cout << \ndo loop ...;
} while (0);
getch();
}

void main()
{
clrscr();
int i = 0;
for(i = -5; i >= 5; i)
cout << Bjarne Stroustrup;
cout << \nReturning to Edit Window..;
getch();
}

//5 l
# include <iostream.h>
# include <conio.h>

// 5 m
# include <iostream.h>
# include <conio.h>
void main()
{ int day = 3;
switch (day)
{
case 0 : cout << \nSunday ..;
case 1 : cout << \nMonday ..;
case 2 : cout << \nTuesday ..;
case 3 : cout << \nWednesday .. ;
case 4 : cout << \nThursday ..;
case 5 : cout << \nFriday ..;break;
case 6 : cout << \nSaturday ..;
}

void main()
{
clrscr();
int month = 5;
if (month++ == 6)
cout << \nMay ...;
else if (month == 6)
cout << \nJune ...;
else if (month == 5)
cout << \nMay again ..;
}

// 5 n
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
int bool = 2,b =4;
while(bool)
{
cout << bool << \t << ++b << \n;
bool;
b;
}
getch();
}

92

6. G :
) a , b . ab P. (while()
).
) E (factorial)
P. (for() ).
) n C P.
C : 0, 1, 1, 2, 3, 5, 8, 12, 18, 20, 32, ...
)
W E Of . (Q
Q G )
4

1
1
1
1
1

2
2
2
2

3
3
3

4
4

A
B

7. F F k , W
F OJ. switch() .
) day 1 Q, 1st day in the month OJ.
) day 2/22 Q, 2nd / 22nd day in the month OJ
) day 3/23 Q 3rd / 23rd day in the month OJ.
)

day 4,5,......14, 15,16........ Q 4th/5th....... day in the month

OJ.

93

Ramanathan

Digitally signed by Ramanathan


DN: cn=Ramanathan, c=IN, o=Commercial Taxed
Dept Staff Training Institute,, ou=Computer Lecturer,,
email=ctdsti@gmail.com
Location: Commercial Taxes Staff Training Institute,
Computer Lecturer,Ph:9442282076
Date: 2008.03.13 11:57:45 +05'30'

(Functions)
4.1
C++ GO F

(Functions) . GL F M
A. G O main()

. ,

 GL c A.
 PJ A.

// To print X!/F! Program - 4.1


# include < iostream.h >
# include <conio.h >
void main ( )
{ int x, f,xfact=1, ffact = 1;
clrscr ( );
cout << \nEnter values ...;
cin >> x >> f; for (int a =1; a < = x; a
++)
xfact * = a;
for (a = 1; a< = f; a++)
ffact * = a;
cout << xfact/ffact;
getch();
}

94

// To print X!/F! using functions


// Program - 4.2
# include <iostream.h >
# include <conio.h >
int fact (int num)
{
int factorial = 1;
for (int a=1; a<= num; a++)
factorial *= a;
return factorial;
}
void main ( )
{ int x, f;
clrscr ( );
cout<<\nEnter values;
cin >> x >> f;
cout << fact (x)/fact(f);
}

Program - 4.1 GL A
F F . Program-4.2 GL
fact (int num)
. , W J A:

G PJ (fact( )
)

P Q G (Compiling)
G
A .

P F
:

// To print X!/F! using functions


// Program - 4.3
# include <iostream.h >
# include <conio.h >
int fact (int num)
{
int factorial = 1;
for (int a=1; a<= num; a++)
factorial *= a;
return factorial;
}
void main ( )
{
int x, f;
clrscr ( );
cout<<\nEnter values;
cin >> x >> f;
cout << fact (x)/fact(f);
}

95

,
P
A. GL



A.
return

4.2 P (Function Prototype)


, GL PM
. M PM
A. , Program - 4.4 G .

//Program -4.4
# include <iostream.h>
void fun (char name [ ]) ;


(P PM)

void main ( )
{
char n [ ] = { C++ programming.};
fun (n);
}
P

void fun (char name[] )


{ cout << name; }

GH W
A:
1. O (arguments) E P
M-(char name[]) ).
2. FH FH M.(
fun() P M void
, FH F
. Program-4.2 - e . fact()
FH M int .

96

M :
<M> <P > <>;

:
void fun(char);
int max(int, int);
int max(int a, int b);

M , P
K GH .
P PM ,
(Template) A.
, K
, FH F K
F GH F
A. FH M
M Q, GH, H
GH .
int max (int, int) K?

PMH, O FK P.
PH I. M
PH P F (place holders)
PHA.
P J O (P)
A. , P
A.
void fun(char name[ ])
{
cout<<name;
}
int fact(int num)
{
int f =1;
for(int a = 1;a <= num; a++)
f *= a;
return f;
}

name, num
A P
.

97

4.3 P
P PH, P
L . , . P
O HK H
P . :
// Program - 4.5
# include <conio.h>
# include <iostream.h>

(Formal Parameters)

int add (int a, int b)


{ return a + b;}
void main ( )
{ int x1, x2, sum = 0
cin >> x1 >> x2;


(Actual Parameters)

sum = add (x1, x2);


cout << sum;
}

P :
int add(int, int)
{
...............
return a+b;
}


A.

void main()
{
......................
sum =add(x1,x2);
cout<<sum;
}

98

4.4 P H
P , (Arguments)
(Parameters) K
A.
PL P PL
P O
N A.
C++ NJ , J
.

F (Call by value)

P (Call by reference)

4.4.1 F (Call by Value)


H ,
H O F F , F
P A. O ( P
PH ) F, O
(P H PH ) .
, O A
F A. G Program -4.5- G .
// Program - 4.5
# include <iostream.h>
# include <conio.h>
int add (int a, int b)
{ return a + b;}
void main ( )
{ int x1, x2, sum;
cin >> x1 >> x2;
sum = add (x, x2);
cout << sum;
}
Assume x 1 = 5, x2 = 7

main()

add()

x1 = 5
a=5
x2 = 7
b=7
sum =
Assume address of the variables :
x1 = Oxf1
x2 = Oxf3
a = Oxf7
b = Oxf9
sum = Oxf6

99

address
Oxf1
Oxf2
Oxf3
Oxf4
Oxf5
Oxf6
Oxf7
Oxf8
Oxf9

data
5
7

12
5
7

x1, x2 A,
a, b A G HO
Qb? , F
J, , PL,
P .
// Program - 4.6
// To exchange values
#include <iostream.h>
#include <conio.h>
# include <iomanip.h>
void swap (int n1, int n2)
{
int temp;
temp = n1;
n1 = n2;
n2 = temp;
cout << \n<<n1<<\t<<n2<<\n;
}

void main ( )
{
int m1 = 10, m2 = 20;
clrscr ( );
cout <<\n Values before invoking swap
<< m1 << \t << m2;
cout << \n Calling swap..;
swap (m1, m2);
cout << \n Back to main.. Values are
<< m1 << \t << m2;
getch ( );
}

Of:
Values before invoking swap
Calling
20

10

20

10

20

swap ..
10

Back to main... values are

100

m1, m2 A PO F P, main()
P HFLM. E b?
F H,
, M
MO F P A. O
F F PO A.
, PO
, O HFLF.

F J, O
O HF
LF.

4.4.2 P (Call by Reference)


J, P -
- P O
(alias) A.
O e J, O
e A . Program - 4.6 - G
. P F, swap () P
FF .
//Program - 4.7
// To exchange values
# include <iostream.h>
#include <conio.h>
void swap (int &n1, int &n2)
{
int temp;
temp = n1;
n1 = n2;
n2 = temp;
cout<<\n<< n1
<<\t<<n2<<\n;
}

101

void main ( )
{
int m1 = 10, m2 = 20;
clrscr();
cout<<\nValues before swap call
<< \t << m1 << \t << m2;
swap(m1,m2);
cout<<\n Calling swap..;
cout<<\n Back to main.Values are
<< \t << m1 << \t<< m2;
getch ( );
}

Of:
Values before invoking swap
Calling
20

10

20

20

10

swap ..
10

Back to main... values are

O
O HFL. , P
O G
A.
W M :
main()

swap()

m1=10
m2=20

n1=10
n2=20
temp

m1, m2 A PO G K
Oxf4 .
m1 = Oxf1 = 10
m2 = Oxf4 = 20

102

Oxf1,

O P (reference)
M:
m1 = 10;
n1 , m1- P ,
int &n1 = m1;

PH. , m1- n1 .
, m1, n1 G H PA.
, O ,
n1 = m1 = Oxf1 = 10
n2 = m2 = Oxf4 = 20

.
K

Oxf1 (n1, m1)

10

20

Oxf4 (n2, m2)

20

10

, O
O FL.
P J, O
O HF
L.
W G A :
// Reference variables
// Program -4.8
# include <iostream.h>
# include <conio.h>
void main ( )
{
int num1 = 10, & num2 = num1;
num2 ++;
cout << num1;
}

103

GL Of 11 A.
PH M num1, num2 A P
G H A.
, num1- num2- HFLA.

MF
1. F J PL, P
J M
H .
,
int add (int n1, int n2)


P :
x = add (5, 10);
x = add (a1, a2) [a1, a2 A int P]

2. P J ,
PO M H .
,

P ,

int add( int &n1, int &n2)

x= add (a1, b1) (a1,b1 A int P)

x = add (a1+b1, a1);


x = add (5,10) ;

H.

104

// Program - 4.9
//To print 5 stars per row and 5 such rows
# include <iostream.h>
# include <conio.h>
void fun_starts(int &i)
{
int j = 5;
for (i= 1; i <= j; i++)
cout << <<*;
}
void main()
{
int mi = 1;
clrscr( );
for (; mi<= 5; mi++)
{
cout << \n;
fun_starts (mi);
}
getch();
}

G, PJ PH Of
KA? G,
*****

K M . K M .
: i P mi PJ P (reference)
. G i- F 6
AMA. mi- F 6 AM. , main() -
for( ) A.

4.3.4 Q (Default Parameters)


C++ NJ P M
O Q F .

105

:
// Program - 4.10
// formal parameters with default values
# include <iostream.h>
# include <conio.h>
float power (float n, int p = 1)
{
float prd = 1;
for (int i = 1; i<= p; i++)
prd *= n;
return prd;
}
void main ( )
{
clrscr ( );
int x = 4, b = 2;
cout << \n Call statement is power(b, x)...
<< power (b, x);
cout << \n Call statement is power(b)..
<< power (b);
getch ( );
}

Of:
Call statement is power (b,x)
Call statement is power(b)
power(b, x)
:
n = b,

...
...

16
2

P FH

p=x

power (b) P, n PJ
2 F .
p PJ Q F 1 .

106

P:

 PJ FH M Q F
.
 P P C M
M
Q N A.
 Q F M
L F . L
F, M .
W G A :
//Program - 4.11
# include <iostream h>
# include <conio.h>
int area (int side1 = 10, int side2=20)
{
return (side1 * side2);
}
void main( )
{
int s1 = 4, s2 = 6;
clrscr( );
cout << area (s1, s2) << \n;
cout << area (s1) << \n;
cout << area (s2) << \n;
getch( );
}

Of:
24
80
120

PJ FH:
H:
side1 = s1, side2 = s2

2 H:
side1 = s1, side2 = 20

3- H:
side1 = s2, side2 = 20

107

W GL Of ?
// Program - 4.12
// arguments with default values
# include <iostream.h>
# include <conio.h>
void print (int times, char ch = * )
{
cout << \n;
for (int i = 1, i < = timers; i ++)
cout << ch;
}
void main ( )
{
clrscr ( );
print (50);
print (A, 97);
print ( );
}

M:
print (50)

times

50 F
. , 50 * OJ.

print (A, 97)

times PJ A F
. (char F int F
). times- 65

F .

108

97 F ch- . ,
int F char F .
ch- a .
,

J .
a 65 .
-

print()


Q (default)
F . ,
* P, 50 .

4.5 FH FH
F FH void PM
A. P PM M O
PHMQ, P int
. ,
int add (int, int);
add (int, int);

O FH F int .
C++ NJ Q FH M
int .
W :
K

FH

float power (float, int)

float

char choice ( )

char

char * success ( )

pointer to character

double fact (int)

double

109

4.5.1 P FH
P P:

// Program - 4.13
# include <iostream.h>
# include <conio.h>
void main ( )
{

int i = 5;
int &count = i ;
cout << \nCount: << count;
count ++;
cout << \ni: << i;
getch ( );

Of:
Count : 5
i:6
count P, 5 F ?
count PJ F I i- F F
K? count i A P GF M

. , PJ
P PJ FL.

J, W GL Of
P:

110

// Program - 4.14
# include <iostream h>
# include <conio.h>
int &maxref (int &a, int &b)
{
if (a>b)
return a;
else
return b;
}
void main ( )
{ int x = 20, y = 30, max = 0;
max = maxref (x,y);
cout << /n Maximum is:<< max;
}

Of:
Maximum is : 30

GL, maxref () int PK


PH (reference) FHA. maxref(x,y)
, a, b AP K P FH
. , max P y- F A.
W GL Of ?
// Program 4.15
# include <iostream h>
# include <conio.h>
int & maxref (int &a, int &b)
{
if (a > b),
return a;
else
return b;
}
void main ( )
{
int x = 20, y = 30, max = 0;
maxref (x,y) = -1;
cout << \n Value of x is : << x;
cout << \n Value of y is: <<y;
getch ( );
}

111

Of:
Value of x is : 20
Value of y is : -1

P:
1.

PH FH P
FH P .

2.

, y- -1 . ,
maxref() , b P
P FH main() P b-
P y . , FH P
b . -1 .

3.

P P
P
. ,
int & maxref (int a, int b);

H, GH H A.
a, b A PO maxref ()
P .

4.6 inline
O
L:

G PJ , G
C ; GL c
.

I, P
, GH
, H F
A. G (stacks)
A GH .
, FH, A
QC C I
A. GL
A. C GO, P CP
( EJ ) ,

112

GH IF,
P GH Fh M. F,
P FL A. inline
A.
inline GL
O. GH (compiling time)

P
F GL M. inline
, F G
.
W :
// Program - 4.16
// inline functions
# include <iostream.h>
# include <conio.h>

// working of Program - 4.16


// inline functions

inline float convert_feet(int x)


{
return x * 12;
}

# include <iostream.h>
# include <conio.h>

void main()
{
clrscr();
int inches = 45;
cout << convert_feet(inches);
getch();
}

void main()
{
clrscr();
int inches = 45;
cout << inches * 12;
getch();
}

, convert_feet (inches)
, return P (inches * 12)
Fh .
P inline PM Q,
Program-4.16- P H inline
C .
P: inline C, GH
K . C O GH, K
EM F.

113

4.7 PO

(scope) MF

PJ A (accessibility)
PA. C++ NJ .
:
1. (Local scope)
2. (Function scope)
3. (File scope)
4. (Class scope)

4.7.1 (Local Scope)


// Program - 4.17
// to demonstrate local variable
# include < iostream.h
# include <conio.h>
void main( )
{
int a, b;
a = 10;
b = 20;
if (a > b)
{
int temp; // local to this if block
temp = a;
a = b;
b = temp;
}
cout << \n Descending order;
cout << \n <<a << \n <<b;
getch( );
}

P, F (Block) A.

PJ
F .

P PM F
OJL .

114

Program -4.18 PJ MA.


//Program - 4.18
# include <iostream.h>
# include <conio.h>
void main ( )
{
int a, b;
a = 10
b = 20;
if (a > b)
{
int temp;
temp = a;
a= b;
b = temp;
}
cout << a << b << temp;
getch ( );
}

G G
, GH H
F .
Error in line no. 13
The variable temp is not accessible

PO

F
. P F

P NA.

P PM
F O PF.
F O P
F.

P PM F
G.

GL F ,
P A.
O NA.

Program- 4.19 GL P .
P PH.

115

// Program - 4.19
# include <iostream.h>
void main ( )
{
int flag = 1; a = 100;
while (flag)
{
int x = 200;
if (a > x)
{ int j;
------}
else
{ int h;
-----}
}
}

x: while( ) F

1. x

2. j

j : if ( a > x ) { }

F
3. k

k :else { } F

4.7.2 (Function Scope)


P PM PO ,
P F F
-F MKA.
Program-4.19- flag P main()
. while(), if() A -F
.
PJ ,
FJ . P
O
.

4.7.3 (File Scope)


F
(P main() P ) PM P,
. PJ
GL MKA.
G c.

116

// Program - 4.20
// To demonstrate the scope of a variable
// declared at file level
# include <iostream.h>
# include <conio.h>
int i = 10;
void fun()
{
cout << i;
}
void main()
{
cout << i;
while (i)
{
......
......
}
}

4.7.4 P (Scope Operator)


P PJ A

O . W G .
num P main()-

, O

PM
Qb? :: num
int num = 15;
PH A
m ? ::
void main()
bQ
{
P A.
clrscr();
F PM
int num = 5;
P PH
num = num + ::num;
P A.
cout << num << \t <<++::num;
getch();
P K
}
G GO
P .
// Program - 4.21
# include <iostream.h>
# include <conio.h>

117

4.7.5
6- P M.

JC

1. MO J W
.
()

Procedural -fuction()

- , F FH
.
M:
void Procedural-function (void);
, void Procedural-function( );

()

Manupulative-function()

- double , int F
FH .
M
(i) int Manipulative-function(double) ;
(ii) int Manipulative-function(double d);
(iii) Manipulative-function (double)

()

fun-default()

- . Q
int F . float F.
F FH.
M:
void fun-default (float, int num = 10);

118

()

return-reference-fun()

- int , int P
FH.
M:
int & char-reference-fun(int&, int&);

()

multi-arguments()

- float .
pi-J F.
P . F FH.
M:
void multi-arguments (float const pi, float & a);

2. W O H :
. float average (a, b);
. float prd (int a, b);
. int default -arg (int a=2, int b);
. int fun (int, int, double = 3.14);
. void strings(char[ ]);
3. W P
main() P .
void line (int times, char ch)
{
cout << \n;
for (int i =1; i <=times; i++)
cout << ch;
cout << \n;
}

119

4. GL PO
PH:
M:

# include <iostream.h>
float a, b ; void f1 (char);

a,b -

int main ( )
{

ch -
-main()

char ch;
............
{

- F

int i = 0;
...........
}
}
void f1(char g)

x, y, g -

- f1()
short x, y ;
...............

5. W GO H :
) include <iostream.h>
xyz (int m, int n)
{

M:
P F
m P
PM .

int m = 10;
n = m * n;
return n;
}
void main( )
{
cout << xyz(9, 27);
}

120

# include <iostream.h>
void xyz ( );
void main ( )
{ int x = xyz ( ) ; }
void xyz ( )
{ return \0 ; }

# include <iostream.h>
void counter (int & a)
{ ++ a;}
void main ( )
{counter (50); }

6.

M:
void PM
P return
. ,
,
J F
.

M:
, P

F
M H
.

W GO Of ?
. # include <iostream.h>

M : 101

int val = 10;


divide (int);
void main( )
{
int val = 5;
val = divide (::val/val);
cout << :: val<<val;
}
divide (int v)
{
return v/2;
}

121

. # include <iostream.h>
divide (int v)
{ return v / 10;}
void main( )
{ int val = -1;
val = divide (400) = = 40;
cout << \In Val. << val;

M : 1

(i) divide(400) 40-


M
(ii) divide(400)==40 40==40
, G
K val- F
1 .

# include <iostream.h>
int incre (int a)

M : 10

{ return a++; }
void main( )
{

int x = 10;
x = incre (x);
cout << x;

# include <iostream.h>
# include <iostream.h>
void line( )
{
static int v = 5;
int x = v - -;
while (x)
{cout << * ; x --;
}
cout << \n;
}
void main( )
{ clrscr( );
for (int i = 1; i < = 5; i ++)
line( );
getch( );
}

M :
*****
****
***
**

122

. # include <iostream.h>

M :

Val : 50

first (int i)
{ return i++; }
second (int x)
{ return x ; }
void main ( )
{
int val = 50;
val = val * val/val
val = second (val);
val = first (val);
cout << \n Val: << val;
}

7. G :
()

float cube (int, int, int) P .


cube() - F main() P

.
()

unsigned long int factorial(int) P


. E F P :
5- =1x2x3x4x5. factorial (n)- F P
main( ) P .

()

char odd-even-check(int) P .
, Q E ,
O M . P
A, K F OJ main() P

F .

()

int prime(int) P .

, Q 1,
-1 M . P
A, K F OJ main() P
F .

123

Ramanathan

Digitally signed by Ramanathan


DN: cn=Ramanathan, c=IN, o=Commercial Taxed Dept
Staff Training Institute,, ou=Computer Lecturer,,
email=ctdsti@gmail.com
Location: Commercial Taxes Staff Training Institute,
Computer Lecturer,Ph:9442282076
Date: 2008.03.13 11:58:14 +05'30'

5
M - E
(Structured Data Type - Arrays)
5.1.
C++ NJ E M M .
M F .
F , FJ
A E .
, W G :
1. O FJ K
P:
)

FJ H, d
:
if (a > b)
max = a;
else
max = b;

FJ H, d
:
if (a > b) && (a > c)
max = a;
else if (b > c)
max = b;
else
max = c;

124

FJ H, d
:
if (a > b && a > c && a > d)
max = a;
else if (b > c && b > d)
max = b;
else if (c > d)
max = c;
else
max = d;

E F d F
Qb? N
HP, F O
F I.
, W F :
int a [4] = { 10, 40, 30, 20}; max = 0 ; i = 0;
for (; i < 4; i ++)
if a [i] > max
max = a [i] ;
cout << \n The largest number is << max;

F, O L
K bQA. , 10, 40, 30, 20 A O
40 OJA. if Am? F
O , M O
F E PM.
, E M PO F
. EJ PH .

125

EJ :
1)

K: PH EJ
.

2)

K: PH EJ, K
E .

5.2. K E (Single Dimensional Array)


O K E
. K E PMA:
int num _array [5];

EJ

5.1 K E

EJ E (positive)
.
int num_array[5];

PMH , num_array F
K E . EJ
EJ , EJ H (position)P . ,
num_array[3] = 99;

, EJ H 99- A.
num_array EJ , [3] W (Subscript)
(position) .
num_array- G 10 .
F int . ( int F 2
. 5 x 2 = 10 ).

126

G W :
num_array - EJ
0

W
F
( )

3
99

EJ W 0- M . num_array
E PJ W 0 ML 4 .
num_array[5] H, W
0 4 . E PMH K H :
i) int array [100];
ii) float exponents [10];
iii) char name [30];
iv) const i = 10;
double val [i];
v) int days [ ] = { 1, 2, 3, 4, 5, 7};
iv)- , val EJ E
i PL PH. (v)- ,
days EJ E 7
. A A? , {1, 2, 3, 4, 5,
6, 7} FH P bQA. 7
EJ bQA. , Program - 5.1

. EJ G
MA.
E :
cin >> number[4]
EJ
F A.
cout<< number[subscript] subscript- PH
H FJ .
number[3] = number[2]
H
EJ H .
number[3] ++
H
F I.
number [++a] = 10
++a - PH E H
10 F .

127

W G A Of :

// Program - 5.1
// arrays and basic manipulations on
// arrays
# include <iostream.h>
# include <conio.h>
int a[5],ctr = 0, sum;
void main()
{
for(;ctr<5;ctr++)
{
cout << \nEnter value ..;
cin >> a[ctr];
}
// generating sum of all the elements
// stored in array
for(ctr = 0, sum = 0; ctr<5;ctr++)
sum+= a[ctr];
clrscr();
// display values stored in array
for(ctr = 0; ctr < 5; ctr++)
cout <<\t << a[ctr];
cout << \nSum of the elements ... << sum;
getch();
}

128

// try out
// Program 5.2

Of:
abcdeffedcba

# include <iostream.h>
# include <conio.h>
char ch [ ] = {a, b, c, d, e, f};
void main ( )
{
for (int i = 0; i < 5; i++)
cout << ch[ i];
for (j=4; j>=0; j)
cout << ch [j];
getch();
}
// try out
// Program - 5.3
# include <iostream.h>
# include <conio.h>

void main ( )
{
int even [3] = {0, 2, 4}; int reverse [3];
for (int i=0, int j = 2; i<3; i++, j )
reverse [j] = even [i];
clrscr ( );
for (i=0; i<3, i++)
cout \t << even [i] << \t << reverse [i] << \n;
getch ( );
}

Of:
0

129

// try out
//Program - 5.4
# include <iostream.h>
# include <conio.h>
void main ( )
{
int x[5] = {1,2,3,4,5}, y [5] = {5,4,3,2,1},
result [5] = { 0,0,0,0,0 };
int i= 0;
while (i++ < 5)
result [i] = x [i] - y [i];
clrscr ( );
cout << \n The contents of the array are: \n;
i = 0;
do
{
cout << \t << x [i]
<< \t << y [i]
<< \t << result [i]<<\n;
i++;
} while (i<5);
getch ( );
}

Of:
The contents of the array are:
1

-1

-2

130

//Try out
//Program - 5.5
# include <iostream.h>
# include <conio.h>
void main()
{
int vector[] = {2, 4, 8, 10, 0};
for(int i=4; i>2; i)
vector [i]= vector[i-1];
clrscr();
cout << \n Elements of array before insertion \n;
for (i= 0; i<5; i++)
cout << vector[i];
vector [2] = 6;
cout << \n Elements after insertion \n;
for (i= 0; i<5; i++)
cout << vector[i];
getch();
}

Of:
Elements of array before insertion
248810
Elements after insertion
246810

EJ
K . K (sorting) A.

5.3 (Strings)
, F (literals) A.
K char E A. E
PM PM . ,
(i)

char name [10];

(ii)

char vowels [ ] = { a, e, i, o, u};

(iii)

char rainbow[ ] = VIBGYOR;

131

char E (ii), (iii) AP


FHF PM. (ii)- char E
Q F \0(NULL)
P PH .
// Program - 5.6
// Reading values into an array of characters
# include <iostream.h>
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main()
{
clrscr();
char name [30], address [30], pincode[7];
cout << \n Enter name ...;
cin >> name;
cout << \n Enter address...;
gets (address);
cout << \n Enter pincode ...;
cin.getlin e (pincode, 7,#);
clrscr();
cout << \n Hello << name;
cout << \n Your address is ... << address;
cout << \n Pincode ....;
cout.write(pincode, sizeof(pincode));
getch();
}

(Program - 5.6), name, address, pincode A


P F cin, gets(), getline() AP
A. cin, O F F (Carriage
Return - Enter Key) F .
,
cin >> name;

132

k K V N Perumal , name- K
. K- O
M F .
name- F K.V.N.Perumal k , name-
)
K.V.N.Perumal .
FH O
Q, stdio.h H gets()
P . k
istream- getline()- .
:
gets()

: gets(char EJ ) gets (char *)

getline()

: cin.getline(char *, O E, P);

F FJ N
:
1. cout <<name; - H PO F OJ
2. cout.write (pincode, 7); cout.write (pincode, sizeof(pincode));
write() Of ostream-
. M
, M O
(object/instance) . write() P
F ,
O E. :
//Program - 5.7
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
char name[ ] = Tendulkar;
int i=1;
while (i<10)
{
cout.write (name, i);
cout << \n;
i++;
}
getch();
}

133

Of:
T
Te
Ten
Tend
Tendu
Tendul
Tendulk
Tendulka
Tendulkar
string.h H ,
O M 5.1- :

strlen()
strlen(char*)
1

FH
F
F O
E . (.):
char name[ ] = Lasya;
strlen(name),5 M .

strcpy()

strcpy(char *, F
char*)

(.)
strcpy(name, petname)

strcmp()

strcmp(char*, H,
char*)
G Q,

0 M .
K
H1
M . H H 1 M A. (-)
strcmp (Abc, Abc) 0
M . strcmp (Abc, abc)
-1 M.
strcmp ( abc, Abc) 1
M .
5.1.

134

K char E
. :
// Program - 5.8
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
char name[] = Pascal, reverse[7];
int i= 0;
while (name[i] != \0)
i++;
reverse[i] = \0;
i;
int j = 0;
while (i>= 0)
reverse [i] = name [j++];
cout << \n The contents of the string are: << name;
cout << \n The contents of the reversed string ...
<< reverse;
getch();
}

W GL Of ?
//Program - 5.9
# include <iostream.h>
# include <conio.h>
# include <string.h>
main()
{
char word[] = test;
int i=0, k = 4, j = 0;
clrscr();
while (i < 4)
{
j = 0;
while (j<=i)
cout << word [j++];
cout << \n;
i++;
}
return 0;
}

135

5.4. K E (Two-Dimensional Array)


K E , K E
E. , marks[3] [4] E, 3
A, 4 P.
0,0

0,1

0,2

0,3

1,0

1,1

1,2

1,3

2,0

2,1

2,2

2,3

int marks[3] [4] = {90, 70, 80, 0, 75, 95, 65, 0, 80, 90, 90, 0};

K E,
0

90

70

80

75

95

65

80

90

90

.
P:

K EJ E, A/
EO .
marks E 12 (3x4)
.
E P W (subscripts)
0-M A. A P W 0
2 , 0 3 .
K EJ marks[ A] [ ]
PH. ,
marks [0] [3] = marks[0] [0] + marks[0] [1] + marks[0] [2];

, AJ F,
90, 70, 80 AP A.

136

K E PMA:
< M > < E> [< A>] [< >];

:
1. int a[3] [2] - a E 3 A, 2
.
2. cons int i = 5;
float num [i] [3]; - num K 5
A, 3 .

3. short fine [A] [E] - fine K 65


A, 69 .
P:
EJ K (A/)
:
1. int PL
2. K PL P
3. char PL
4. enum P

5.4.1. K EO G H
K EJ GF C
HO FA. O F
W P A.
1. A K: A - (row - major)
2. K: - (column - major)

137

:
int sales[2][4] EJ A:

A- K
0,0
0,1

1- A

0,2
0,3
1,0
1,1

2- A

1,2
1,3

- K
0,0

1-

1,0
0,1

2-

1,1
0,2

3-

1,2
0,3

4-

1,3

sales[1][0] H A-
KJ, - KJ Qb?

138

K EJ G
AA:
E x G .
, int sales[2][4] EJ
AA:
E = A x = 2 x 4 = 8
8 x 2 ( int F 2 )
= 16
float num [4][6] EJ ?

M :
4 x 6 x 4 = 96

W E :
int num[4][3] = {78, 7, 6, 4, 5, 8, 9, 7, 6, 1, 2, 3};
num

2
3

9
1

7
2

6
3

num J I FO F

139

M:
num[0][1] - (7)
num[1][1] - (5)
num[2][0] - (9)
num[3][1] - (2)

W PM EO, O E
bQ:
) int array[10] [12];

M : 120

) int x[ ] [2] = {0, 1, 1, 2, 2, 3}

M : 6
(A-3, -2)

F E PM,
K PH I.

, E H
. EJ PH
. K PH J.
E P
A. , EJ , GF E
FJ K PA. H P
O P. EJ
PH, K H
A (P ).

140

// Program - 5.10
#include <iostream.h>
# include <conio.h>
void accept (int s[3][4], int &total)
{
int r = 0, c = 0;
for (; r < 3; r++)
{
cout << \n Month: << r+1;
for (c = 0; c < 4; c++)
{
cout << \n << c+1 << Quarter..;
cin >> s[r][c];
total += s[r][c];
}
}
}
void display (int d[3][4], int total)
{
int r, c;
clrscr ( );
cout << \nSales figures for 3 months & their
respective quarters..;
for (r = 0; r < 3; r++)
{
cout << \n Month ... << r+1;
for (c = 0; c < 4; c ++)
cout << \t << d[r][c];
}
cout << \n Total sales .. << total;
}
void main()
{
clrscr();
int sales[3][4], t = 0;
accept(sales,t);
display(sales,t);
getch();
}

141

W G :
// Program - 5.11
# include <iostream.h>
# include <conio.h>
void accept(int a)
{
cout << \n Enter a number ..;
cin >> a;
}
void display(int a)
{
cout << \n << a;
}
void main()
{
int num [2][2] ={{0,0},{0,0}}, r = 0, c = 0;
clrscr ( );
for (; r < 2; r++)
for (; c < 2; c++)
accept(num[r][c]);
clrscr();
for (r = 0; r < 2; r++ )
for (c = 0; c < 2; c++)
display (num[r][c]);
getch();
}
accept() P k 1, 2, 3, 4 A F
. M:
0
0
0
0

A. num EJ O 1, 2, 3, 4 A F
M E b?
, void accept() P
QQ H A. , F
. P
F .

142

P:
EJ EJ K PA.
, GL accept() P P
. 1, 2, 3, 4 k , Of,
1
2
3
4

5.4.2. E (Matrix)
E m x n m AO n
O E M . E
K E .
Program-5.12 G E F
k E O G (equality)
KF M A.

5.5. O E
O E K char E . E
H (A E) O
E P.
(
E) O c P. :
char day-names [7] [10]

{Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday};

EJ O F 5.1-
.

143

//Program - 5.12
# include <iostream.h>
# include <conio.h>
void accept(int mat[3][3])
{
clrscr();
int r = 0, c = 0;
for (; r < 3; r++)
{
cout << \n Enter elements for row.. << r;
for (c=0; c < 3; c++)
cin >> mat[r][c];
}
}
void main()
{
int m1[3][3], m2[3][3];
accept (m1);
accept (m2);
int i=0, j = 0, flag = 1;
for (; i < 3; i++)
{
for (; j < 3; j ++)
if (m1[i][j] != m2[i][j])
{
flag = 0;
break;
}
if (flag == 0)
break;
}
if (flag)
cout << \n The matrices are equal ...;
else
cout << \n The matrices are not equal..;
getch();
}

144

0
1
2
3
4
5
6

0
S
M
T
W
T
F
S

1
u
o
u
e
h
r
a

2
n
n
e
d
u
i
t

3
d
d
s
n
r
d
u

4
a
a
d
e
s
a
r

5
y
y
a
s
d
y
d

6
\0
\0
y
d
a
\0
a

\0
a
y

y
\0

\0

\0

day-names [0]
day-names [1]
day-names [2]
day-names [3]
day-names [4]
day-names [5]
day-names [6]

5.1 GF EJ

EJ QQ day-names[0]
(A ) PH . F
PH EJ Q
day-names[0][5] (A/
) PH .
F FJ P (null
character - \0) I (optional). M
C++ GH .

JC M
1. W H ?
)

int a[5.5];
- EJ K .

float f[3] = {1.0, 2.0};


- H . EJ PH

EM F
. Q, eF
O 0 F M.
)

float num[A];

145

- EJ K O PM
. F, A P F
A. ,
float num[A] ,
const A=10; float num[A];

P .
)

char a[3] [ ] = { one, two, three};


- F PM K

EJ K MM.
K .
,
char a[ ] [6] = {one, two, three};

P .
)

char ch[1] = s;
-char E PM Q,
char ch[2] = s;

PM. char E PM
Q,
char ch[1] = {s};

PM .
)

char test [4];


test = {abc};
- EJ FH .
PM FHF,
char test[4] = abc;

PH. ,
char test [4];
strcpy(test, abc);

F, PJ .

146

int num[ ] = {1,2,3}, num2[3];


num2 = num;

-E PM FH NJ.
int num1[5], num2[10], num3[15];

PM.
FH JH QQ
.
)

int num[3];
cout << num;
cin >> num;
- E k/ Of

FJ.
.
cout << num [1];
cin >> num[2];

.
)

int roaster = { 1,2,3,4};


- roaster P F .

,
int roaster = 10; ,
int roaster [ ] = {1,2,3,4} ;

.
2. W FHH EJ
?
)

int rate[ ] = {30, 40, 50};

char ch[b] = {bbbbb\0}; -b O PA.


ch[0] = C; ch[4] = T; ch[3] = A;

char product-list [ ] [5] = { Nuts, Bolts, Screw};

M:
)

rate [0] = 30, rate[1] =40, rate[2] = 50

147

ch[0] = C, ch[1] = , ch[2]= , ch[3] = A,


ch[4] = T, ch[5] = \0

product-list[0] = Nuts\0, product-list[1] = Bolts\0,


product-list[2] = Screw\0.

3. W GO Of ?
)

# include <iostream.h>
void main ( )

M: END

{
char ch [ ] = END;
cout << ch;
}

# include <iostream.h>
void main ( )
{
int a [ ] = {1,2,3,4,5};
for (int i = 0, i < 4, i++)
a[i+1] = a[i];
for (i= 0; i<5; i++)
cout << \n << a[i];
}

M:
EJ :
a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4, a[4] = 5

for() ,
i = 0 : a[1] = a[0]
i = 1 : a[2] = a[1]
i = 2 : a[3] = a[2]
i = 3 : a[4] = a[3]

148

, Of,
1
1
1
1
1
.
)

# include <iostream.h>
# include <conio.h>
void main( )
{
char name[ ] = Jerry; int k=5;
for (int i = 0; i < 3; i ++, k )
name [k] = name[i];
cout << name;
getch( );
}

M:
for() ,
i = 0 : k = 5, name[5] = name[0];
i = 1 : k = 4, name[4] = name[1];
i = 2 : k = 3, name[3] = name[2];

, Of, JerreJ A.
4. G :
1)

int - array int E 10, 20, 30, 40, 50 A

F . E
O F M .

149

2)

10 F F int E
PM. EJ KI
F . F H KJ
FJ.

3)

word PJ (char E) KI
. while() switch()
A F K J
(vowels) A M OJ. ,
char word[ ] = The Vowel count AEIOU aeiou;
Vowel count is 14

4)

MATRIX[3][3] E .

M O F P
OJ.
5)

matrixA [4][4], matrixB [4][4] A E

F k . E
sum-matrix[4][4] EJ
F, OJ.
:
matrixA

matrixB

sum-matrix

1234
5678

9876
5432

10 10 10 10
10 10 10 10

9123
4567

1987
6543

10 10 10 10
10 10 10 10

150

Ramanathan

Digitally signed by Ramanathan


DN: cn=Ramanathan, c=IN, o=Commercial Taxed Dept
Staff Training Institute,, ou=Computer Lecturer,,
email=ctdsti@gmail.com
Location: Commercial Taxes Staff Training Institute,
Computer Lecturer,Ph:9442282076
Date: 2008.03.13 11:58:51 +05'30'

6

(Classes and Objects)
6.1 -
C++ NJ I A .
C++ N A vv N L
C (C with Classes)
A , M A FA.
M , F
M F N
FA.
F N A.
class student
{
char name[30];
int rollno, marks1, marks2, total_marks;
}

M PM rollno, marks1, marks2,


total_marks A P student F

OA.
, student MK .
N (methods) . ,
O e G ( F
k , W )
A.
P,

A.

6.2
M F :
1) PM
2) M

151

// Program - 6.1
# include <iostream.h>
# include <conio.h>
class student
{
private :
char name[30];
int rollno, marks1, marks2 ,total_marks;
protected:
void accept()
{
cout<<\n Enter data name, roll no, marks 1 and
marks 2.. ;
cin>>name>>rollno>>marks1>>marks2;
}
void compute()
{
total_marks = marks1+ marks2;
}
void display()
{
cout<<\n Name <<name;
cout<<\n Roll no <<rollno;
cout<<\n Marks 1.. <<marks1;
cout<<\n Marks 2.. <<marks2;
cout<<\n Total Marks.. << total_marks;
}
public:
student()
{
name[0]=\0';
rollno=marks1=marks2=total_marks=0;
cout<<\n Constructor executed ... ;
}
void execute()
{
accept();
compute();
display();
}
};
void main()
{
clrscr();
student stud;
stud.execute();
}

152

PMH :

class class-name
{
private:
variable declaration
function declaration



class student
{ private;
char name [10];
int rollno, marks1, marks2, total_marks;

protected:
variable decl.
function decl.

protected:
void accept( );
void compute( );
void display( );

public:
variable decl.
function decl.
};

public:
student( );
void execute( );
};

class C ,
class A.

M F O P
O A.
M F P O
PM .
M F A H
(O C) : private, public,




protected.




private A PH I.

M A PHM Q
M private
F.
private PM
.
protected PM
, M J
M .
public PM
O .

153

6.3 (Data Abstraction)



F (Encapsulation)
A.
private
OJL .
(Data Hiding) A. H O (objects),
(members) J
MF (Data Abstraction) A.
FA
.
GF (Object Oriented
Programming-OOP) .
private

M
(friend functions) C C
.

protected

M
O .

public

M
IP O O
.

6.4
.

(Data Members). (member functions).

M
P PA.
M P E GA
PA. N (methods) ,
(attributes) .
, Program-6.1- student
6.1- . A (constructors), NH
(destructors) Q C
. A/NH P 8- MK
.
154

student

M P student
(tag) .

name, rollno,
marks1,marks2, total_marks

accept()
compute()
display()
execute()
student()

stud

student M (instance)/
(object) / P (variable)
6.1 Program- 6.1 student

6.5
student stud;

PM . P stud
student M (instance) (object)
.
H, MF P
PM . stud student P. student
class M. C++ NJ P
(objects) PA. MO
P PM M PM
A. A O P
, PO PH .

6.1

155

6.6
M O P
. , student M execute() P
:


O P
O

M private M
, C C
.
student M name, marks1,
marks2, total_marks A , accept(), display(), compute()
A .
O PM , private protected
,
.
public PM
M .
stud.execute();

. , execute()
public PM . ,
O PM stud
. , stud.accept(), stud.compute(), stud.display(),
stud.marks1- , G H
not accessible H F .
E M Program- 6.2
. F , P,
A P .
sum P public A , s
A.

156

//Program - 6.2
# include <iostream.h>
# include <conio.h>
class add
{
private:
int a,b;
public:
int sum;
void getdata()
{
a=5;
b=10;
sum = a+b;
}
};
void main()
{
add s;
s.getdata();
cout<<s.sum;
}

6.7 M
class add
{
int a,b;
public:
add()
{
a = 0;
b = 0;
}
void display();
};
void add::display()
{
int sum;
sum = a+b;
cout<<sum;
}

N-1

N-2

157

N 1-, add() , add


PM, .
N 2-, display() , add
PM, O
.
M
. inline
A.
O ,
type class_name: : function_name();

.
:

P
/
P

function_name()- class_name:: (add::)


P , ,
class_name- K A. P H
PH A
PA.
QC H
. G F F
A.


F . P
P.

158

 M private
. H .
 P
. O P J.
( O H
A).
 , C++
. H .
 FH F, M
.
 static .

6.8 G W
H F
GF
A.
P F ,

QQ G F. P
G QQ
A.
. , O P
F .
W PM :
class product
{
int code, quantity;
float price;
public:
void assign_data();
void display();
};
void main()
{
product p1, p2;
}

159

F assign_data(), display() A
GF M FJ A. p1, p2
A M FJ
.
p1, p2 A O G W :

G W

p1

code, quantity, price

p2

code, quantity, price

6.2 G W

M , Program- 6.3- M
, H
.

6.9 static
M static F O
. static 

M ,
0 F . H
FHF.

GF, P
( F F). M
H GF
AA.

A
. , G
.

static P G
(Program-6.4):

160

// Program - 6.3
#include<iostream.h>
#include<conio.h>
class product
{
int code, quantity;
float price;
public:
void assign_data( int c, int q, float p)
{
code = c;
quantity = q;
price = p;
}
void display()
{
cout<<\n Code : <<code;
cout<<\n Quantity :<<quantity;
cout<<\n Price : << price;
}
};
void main()
{
product p;
p.assign_data( 101, 200, 12.5);
p.display();
}

161

// Program - 6.4
// To demonstrate the use of static member variables
#include<iostream.h>
#include<conio.h>
class simple_static
{
int a,b,sum;
static int count;
public:
void accept()
{
cout<<\n Enter values.. ;
cin>>a>>b;
sum = a+b;
count++;
}
void display()
{
cout<<\n The sum of two numbers <<sum;
cout<<\n This is addition <<count;
}
};
static_simple.count = 0;
void main()
{
simple_static p1,p2,p3;
p1.accept();
p1.display();
p2.accept();
p2.display();
p3.accept();
p3.display();
}

162

Of:
Enter values 10 20
The sum of two numbers 30
This is addition 1
Enter values 5 7
The sum of two numbers12
This is addition 2
Enter values.. 9 8
The sum of two numbers 17
This is addition 3
static P count 0
FHA. O
A count- F IA.
accept() count- F
I 3 F A.
count- A,
count- F 3 A. , 6.2- M.

6.2 static P - count


static PJ F O
A.

163

6.10 O E
W , G
W :
class product
{
int code,quantity;
float price;
public :
void assign_Data();
void display();
} p[3];
void main()
{
p[0].assign_Data();
p[0].display();
}

code
quantity
price

p[0]

code
quantity
price

p[1]

code
quantity
price

p[2]

JC M
I. H P F:
class x
{
public:
int a,b;
void init()
{
a =b = 0;
}
int sum();
int square();
};
int sum()
{
return a+b;
}

164

int square()
{
return sum() * sum()
}

M:

int x : : sum() int x : : square()


II.
#include<iostream.h>
class simple
{
int num1, num2 , sum = 0;
protected:
accept()
{
cin>>num1>>num2;
}
public:
display()
{
sum = num1 + num2;
}
};
void main()
{

simple s;
s.num1=s.num2=0;
s.accept();
display();

165

M:
1) sum PJ PMH F
.
2) num1, num2 A P private
main()- L .
3) s.accept() . protected
.
4) display() P O
.
III.
#include<iostream.h>
#include<conio.h>
class item
{
private:
int code,quantity;
float price;
void getdata()
{
cout<<\n Enter code, quantity, price ;
cin>>code>>quantity>>price;
}
public:
float tax = 0;
void putdata()
{
cout<<\n Code : <<code;
cout<<\n Quantity : <<quantity;
cout<<\n Price : <<price;
if( quantity >100 )
tax = 2500;
else
tax =1000;

166

cout<< \n Tax :<<tax;


}
};
void main()
{ item i; }

GL J W G
:
i-
G
W
IV.
1)

private

public

W employee
:
employee M private :
empno -
ename - 20
basic - float
netpay, hra, da - float
calculate()- basic+hra+da A float

F FH .
employee M public :
havedata() - empno, ename, basic, hra, da - A F
k , calculate() P
netpay- A .
dispdata() - FJ
.
2)

W math
:
private :
num1, num2, result - float
init() - num1, num2, result AP 0

FH.

167

protected :
add() - num1, num2 ,
result- .
prod() - num1, num2 A,
result- .
public :
getdata() - num1, num2 A F
menu() 1. Add...
2. Prod...
(menu) .

1 add() P, 2-
prod() P F, result- FJ
.

168

Ramanathan

Digitally signed by Ramanathan


DN: cn=Ramanathan, c=IN, o=Commercial Taxed Dept
Staff Training Institute,, ou=Computer Lecturer,,
email=ctdsti@gmail.com
Location: Commercial Taxes Staff Training Institute,
Computer Lecturer,Ph:9442282076
Date: 2008.03.13 11:59:19 +05'30'

(Polymorphism)
7.1
polymorphism , (poly-many, morph-shapes)
A. IN A. C++
NJ, , EI (function overloading),
P EI (operator overloading) AP G
A. J EI (overloading) ,
PA.
, EI Q
P PA. G
K EI C++ NJ O
.

7.2 EI (Function Overloading)


F M O
P F E
I A.
G W MA
:
area_circle()
area_triangle()
area_rectangle()

// F E
// F E
// F E

O P
, K ( )
.
float area(float radius);
float area(float half, float base, float height);
float area(float length, float breadth);

169

Program-7.1- . O
F A area() P
F :
// Program - 7.1
// to demonstrate the polymorphism - function overloading
#include<iostream.h>
#include<conio.h>
float area ( float radius )
{
cout << \nCircle ;
return ( 22/7 * radius * radius );
}
float area (float half, float base, float height)
{
cout << \nTriangle ..;
return (half* base*height);
}
float area ( float length, float breadth )
{
cout << \nRectangle ;
return ( length *breadth );
}
void main()
{
clrscr();
float r,b,h;
int choice = 0;
do
{
clrscr();
cout << \n Area Menu ;
cout << \n 1. Circle ... ;
cout << \n 2. Traingle ...;
cout << \n 3. Rectangle ... ;
cout <<\n 4. Exit ... ;
cin>> choice;
switch(choice)
{
case 1 :
cout << \n Enter radius ... ;
cin>>r;
cout<<\n The area of circle is ...
<< area(r);
getch();
break;
case 2:
cout<< \n Enter base, height ... ;
cin>>b>>h;
cout<<\n The area of a triangle is...
<< area (0.5, b, h);
getch();
break;
case 3:
cout<< \n Enter length, breadth.. ;
cin>>h>>b;
cout<<\n The area of a rectangle is ...
<< area(h,b);
getch();
break;
}
}while (choice <=3);
}

170

O Qb? :
float area(float radius);
float area(float half, float base, flaot height);
float area(float length, float breadth);

area() O
GAl? , P
O EJ A.
M , F ,
F .
float
. F.
M .
O E . O
E H P M P
.
W O EI
O H P ?
?
:

H :

void fun(int x);


void fun(char ch);
void fun(int y);
void fun(double d);

void fun(int x);


void fun(int y);

E
J
.
H.

EIH A?
GH, IC (Best Match)
HA. F G
H

I K
.

171

I K Q,
P .
,
,
.

, GL (Program-7.1) float area (float


radius) . ,
area(r) F r float
. r int Q,
F, int PL/P char float double
F . F
area(r) area(float radius)
.
GH
. GH J
. :


char M int/float/double .

int M char/float/double .

float M int/double/char .

double M int float .

Program-7.1 GL area() P

W Q M ?

area (5.0)
area (0.5, 4.0, 6.0)
area(3.0, 4.5)

172

Of

EIH MF
1)
2)
3)

4)

EI O E
J, P O .
EI FH M
, .
EI O Q ,
O L F C++ GH
F .
H
.

PM, W HQ
.
void fun ( char a, int times)
{
for (int i=1; i<=times;i++)
cout<<a;
}
void fun( char a= *, int times )
{
for(int i=1;i<=times;i++)
cout<<a;
}
void fun( int times)
{
for(int i=1; i<=times ;i++)
cout<<@;
}
void main()
{
fun ( +, 60);
fun(60);
}

173

G G H
:
 conflict between fun(char a, int times) and fun(char a = *, int times)
 conflict between fun(char a = *, int times) and fun (int times)
fun (+, 60) fun(char a, int times)
fun(char a = *, int times) A .

O
P EI . ,
, O E M
J .

7.3 P EI (Operator Overloading)


P EI , +, ++, -, --, +=, -=, *, <, >
C++ P
PA.
sum = num1 + num 2;

, (int/float/double) , M sum
PJ E F .
, W :
name = first_name + last_name;

F name, first_name, last_name A (char


E) . C++ NJ + P
P ?
+ P GH H .
, strcat() P
.
+ P I O
? P EI + PJ
c .

174

W :
// Program -7.2 OPERATOR OVERLOADING
# include <iostream.h>
# include <conio.h>
# include <string.h>
class strings
{
char s[10];
public :
strings()
{
s[0] = \0;
}
strings(char *c)
{
strcpy(s,c);
}

void main()
{
clrscr();
strings s1(test),s2( run\0);
char *concatstr ;
concatstr = s1 + s2;
cout <<\nConcatenated string ...
<< concatstr;
getch();
}

char * operator+(strings x1)


{
char *temp;
strcpy(temp,s);
strcat(temp,x1.s);
return temp;
}

};

concatstr = s1 + s2;

P.
char * operator + (strings x1)

, + P E
. x1
M A. M:
x1
strings

char * operator+(strings x1)


{
char *temp;
strcpy(temp,s);
strcat(temp,x1.s);
return temp;
}

M .

s1, s2 A
strings

. M
+
P .

concatstr = s1 + s2;

175

7.1, P P F
H MA.

7.1 P F

P EI A:


+, *, -, ++, --, >, <, += C++ NJ

M EI
P .

P
. (friend
functions) . (
L ).

P F
PJ . ,
GL + P
P . GL +
P
E . GH,
PJ
A. ,

P F
A.

cout << 5+10;

15 FJ . (+-
).
176

concatstr = s1 + s2;

, char * operator+(strings x1)


P . , F +
PJ H
s1, s2 A strings M
.

P EIH :


F M
A M EI
PJ F .

operator <symbol>() P M public

FJ PM .

GP
J P P
.
M PO O
W MA.
M O F F (negative value)
P - P Program - 7.3
MA.
// Program - 7.3
# include <iostream.h>
# include <conio.h>
class negative
{
int i;
public :
void accept()
{
cout << \nEnter a number ...;
cin >> i;
}
void display()
{
cout << \nNumber ...<<i;
}

177

void operator-()
{
i = -i;
}
};
void main()
{
clrscr();
negative n1,n2;
n2.accept();
-n2;
n2.display();
getch();
}

sum = - num1;

F, P - P ,
M void operator-() F
(Negative) A.
W G . M M :
// Program 7.4
# include <iostream.h>
# include <conio.h>
class distance
{
int feet,inches;
public :
void distance_assign(int f, int i)
{
feet = f;
inches = i;
}
void display()
{
cout << \nFeet
: << feet
<< \tInches : << inches;
}
distance operator+(distance d2)
{
distance d3;
d3.feet = feet + d2.feet;
d3.inches = (inches + d2.inches) % 12;
d3.feet += (inches + d2.inches)/12;
return d3;
}
};
void main()
{
clrscr();
distance dist_1,dist_2;
dist_1.distance_assign(12,11)
dist_2.distance_assign(24,1);
distance dist_3 = dist_1 + dist_2;
dist_1.display();
dist_2.display();
dist_3.display();
getch();
}

178

1.

EI P .

2.

EI P F
.

3.

EI PJ
HO ?

4.

EI P P
F .

EI +=, -=
MA.

A Program-7.5

//Program-7.5
// operator overloading
# include <iostream.h>
# include <conio.h>
# include <string.h>
class library_book
{
char name[25];
int code,stock;
public :
void book_assign(char n[15],int c,int s)
{
strcpy(name,n);
code = c;
stock = s;
}
void display()
{
cout <<
cout <<
cout <<
cout <<
}

\n
\n
\n
\n

Book details ....;


1. Name
..... << name;
2. Code
..... << code;
3. Stock
..... << stock;

void operator +=(int x)


{
stock += x;
}
void operator -=(int x)
{
stock -= x;
}
};

179

class library_cdrom
{
char name[25];
int code,stock;
public :
void cdrom_assign(char n[15],int c,int s)
{
strcpy(name,n);
code = c;
stock = s;
}
void display()
{
cout <<
cout <<
cout <<
cout <<
}

\n
\n
\n
\n

CD
1.
2.
3.

ROM details ....;


Name
..... << name;
Code
..... << code;
Stock
..... << stock;

void operator +=(int x)


{
stock += x;
}
void operator -=(int x)
{
stock -= x;
}
};

void main()
{
library_book book;
library_cdrom cdrom;
book.book_assign(Half Blood Prince,101,55);
cdrom.cdrom_assign(Know your Basics,201,50);
char choice,borrow;
do
{
cout << \nBook,cdrom,exit<b/c/e> ...;
cin >> choice;
if (choice != e)
{
cout << \nBorrow/Return <b/r> ...;
cin >> borrow;
}

180

switch (choice)
{
case b:
switch (borrow)
{
case b : book += 1;break;
case r : book -= 1;break;
}
book.display();
break;
case c :
switch (borrow)
{
case b : cdrom += 1;break;
case r : cdrom -= 1;break;
}
cdrom.display();
break;
case e : cout << \nTerminating ..;
break;
}
} while (choice != e);
getch();
}

F , +=, -= A P
F EJ I O I,
Qb?
book += 1;
book -= 1;
cdrom += 1;
cdrom -= 1;

P F
P EI A.

181

P E I MF:
PO EI C
. :


P EI
. F P .

EI P A H
M
.

PJ P
. , PJ
. P
E .

EI P P H
P
A.

PO (Binary Operators) EI,


PJ ,
M
.

P
EI, O
.

JC M
I.

W E G EI
G :
) O K P
) O K P
M: max(int, int), max(int, int, int)

182

II.

W E EI F
:
) float PJ F I
) char PJ F I
M: - float incr(float), char incr(char)

III. EI W E G

C++ NJ G :

) x, y A int Q, xy F P.
) x, y A float Q, xy F P.
M: int power(int, int), float power (float, float)
IV. P EIH ?
V. EI P F H

G L.

VI. EI P L.
VII. complex_numbers M

G . F,
F
. W G , complex_numbers
F c1, c2
main() P F .
class complex_numbers
{
float x;
float y;
public :
void assign_data(float real, float imaginary);
void display_Data();
complex_numbers operator +(complex_numbers n1);
}

183

Ramanathan

Digitally signed by Ramanathan


DN: cn=Ramanathan, c=IN, o=Commercial Taxed
Dept Staff Training Institute,, ou=Computer
Lecturer,, email=ctdsti@gmail.com
Location: Commercial Taxes Staff Training Institute,
Computer Lecturer,Ph:9442282076
Date: 2008.03.13 11:59:48 +05'30'

8
A NH
(Constructors and Destructors)

8.1
M (instance) ,
A (constructor) C A. A
(class object) AA , O F A.
O NH (destructor)
C A. A, NH A
M .
F FH F.
M .
8.2 A (Constructors)
// Program - 8.1
// to determine constructors and destructors
#include<iostream.h>
#include<conio.h>
class simple
{
private:
int a,b;
public:
simple()
{
a= 0 ;
b= 0;
cout<< \n Constructor of class-simple ;
}
~simple()
{
cout<<\n Destructor of class simple .. ;
}
void getdata()
{
cout<<\n Enter values for a and b... ;
cin>>a>>b;
}
void putdata()
{
cout<<\nThe two integers .. <<a<<\t<< b;
cout<<\n The sum of the variables .. << a+b;
}
};
void main()
{
simple s;
s.getdata();
s.putdata();
}

184

G J s
simple() A, A.
s- , GL
~simple() NH A.
GL Of :
Constructor of class - simple ..
Enter values for a & b 5 6
The two integers.. 5 6
The sum of the variables.. 11
Destructor of class - simple

8.3 AJ
1) O O F
A.
2) GF A.

8.4 AJ EI (Constructor Overloading)


A M C ,
EI (Function Overloading) A .
Program- 8.2 G A EI MA.
add() A A (non-parameterized constructor) . A (default constructor)

A. , A GH
A (compiler generated constructors) PH.
, A E
P A A A.
PM,
A A.

185

// Program - 8.2
// To demonstrate constructor overloading
# include<iostream.h>
#include<conio.h>
class add
{
int num1, num2, sum;
public:
add()
{
cout<<\n Constructor without parameters.. ;
num1= 0;
num2= 0;
sum = 0;
}
add ( int s1, int s2 )
{
cout<<\n Parameterized constructor... ;
num1= s1;
num2=s2;
sum=NULL;
}
add (add &a)
{
cout<<\n Copy Constructor ... ;
num1 = a.num1;
num2 = a.num2;
sum = NULL;
}
void getdata()
{
cout<<Enter data ... ;
cin>>num1>>num2;
}
void addition()
{
sum=num1+num2;
}
void putdata()
{
cout<<\n The numbers are..;
cout<<num1<<\t<<num2;
cout<<\n The sum of the numbers are.. << sum;
}
};
void main()
{
add a, b (10, 20) , c(b);
a.getdata();
a.addition();
b.addition();
c.addition();
cout<<\n Object a : ;
a.putdata();
cout<<\n Object b : ;
b.putdata();
cout<<\n Object c : ;
c.putdata();
}

186

Of:
Constructor without parameters.
Parameterized Constructor...
Copy Constructors
Enter data .. 5 6
Object a:
The numbers are 5 6
The sum of the numbers are .. 11
Object b:
The numbers are 10 20
The sum of the numbers are 30
Object c:
The numbers are 10 20
The sum of the numbers are .. 30

A A
A. A , int PL
P PM .
P: char, float, double .
F int M
.
add(int s1, int s2)

: add a(10, 60) / add a(ivar, ivar)


add (add &a) A, A (copy constructor) A.

A W O :
1)

P
H.
: void add : : outdata (add x);

2)

FH
.
: add getdata();

3)

P A H
.
: add a; b(a);

W G (Program - 8.3) A
A MA:

187

// Program 8.3
// To demonstrate constructor overloading
# include<iostream.h>
#include<conio.h>
class add
{
int num1, num2, sum;
public:
add()
{
cout<<\n Constructor without parameters.. ;
num 1 = 0;
num 2 = 0;
sum
= 0;
}
add ( int s1, int s2 )
{
cout<<\n Parameterized constructor... ;
num1= s1;
num2=s2;
sum=NULL;
}
add (add &a)
{
cout<<\n Copy Constructor ... ;
num1= a.num1;
num2=a.num2;
sum = NULL;
}
void getdata()
{
cout<<Enter data ... ;
cin>>num1>>num2;
}
void addition(add b)
{
sum=num1+ num2 +b.num1 + b.num2;
}
add addition()
{
add a(5,6);
sum = num1 + num2 +a.num1 +a.num2;
}
void putdata()
{
cout<<\n The numbers are..;
cout<<num1<<\t<<num2;
cout<<\n The sum of the numbers are.. << sum;
}
};

188

void main()
{
clrscr();
add a, b (10, 20) , c(b);
a.getdata();
a.addition(b);
b = c.addition();
c.addition();
cout<<\n Object a : ;
a.putdata();
cout<<\n Object b : ;
b.putdata();
cout<<\n Object c.. ;
c.putdata();
}

ouput of the above program


Constructor without parameters..
Parameterized constructor...
Copy Constructor ... Enter data ... 2 3
Copy Constructor ...
Parameterized constructor...
Parameterized constructor...
Object a :
The numbers are..2
3
The sum of the numbers are.. 35
Object b :
The numbers are..0
1494
The sum of the numbers are.. 0
Object c..
The numbers are..10
20
The sum of the numbers are.. 41

A A
Qb?
Program - 8.3 GL W A
A:

189

add c(b); // b H

A.

addition()
P HA.
a.addition(b); // b

b = c.addition(); // addition()
FHA.

addition() P E
I (overloaded). GL F PM
EI P.

8.5

A MF:
1) AJ M .
2) A, O
3) A , EI .
4) A , GH
A A .
5) , A
. F.

8.6 NH (Destructors)
NH , , A
G F MM
. M .
, ~ P K .

190

:
class simple
{
...............
...............
public :
~simple()
{
................
}
}

8.7

NH MF:
1) NHJ , ~
P M
.
2) NH, .
3) NH, F FH .
4) NHJ EI . ,
M NH .
5) , NH ,
GH NH A
.
6) GL O
, NH
. F.

191

JC M:

W G :
M

NH

1.
(scope) PM ?
2. EI F?
3. A?
....
4. ?
II W GF H ?
class simple
{
private:
int x;
simple()
{
x = 5;
}
};

class simple
{
private:
int x;
public:
simple(int y)
{
x = y;
}
};
void main()
{
simple s;
}

class simple
{
private:
int x;
public:
simple(int y)
{
x = y;
}
simple(int z=5)
{
x = z;
}
};
void main()
{
simple s(6);
}

192

Ramanathan

Digitally signed by Ramanathan


DN: cn=Ramanathan, c=IN, o=Commercial Taxed Dept
Staff Training Institute,, ou=Computer Lecturer,,
email=ctdsti@gmail.com
Location: Commercial Taxes Staff Training Institute,
Computer Lecturer,Ph:9442282076
Date: 2008.03.13 12:00:18 +05'30'

9
K
(Inheritance)
9.1
G NJ F
K (Interitance) . K
O J F
. , (base classes)
, M (derived classes)
A. M , M
K MA. IP, M
M N
FK . K
J I P .
K H K N
A. , (Parent)
MI K A.

9.1 K

193

9.2 KF
K W OA:
1) G PJ (Reusability of Code);
GF
A. P
F, A
F . G
A.
2) G P A (Code Sharing);
M N(methods) M
A .
3) F (Consistency of Interface);
K N
N FK
A. 1- , M
, N, M, , A A
M
. M
.
9.3 M
ML H MJ
A. M
M K A.
M , W
MF H :
) class C .
) class , M
M .
) O (:) .
)
private, public protected A
A MA PH
.
) M PH .
) M K
, N .

194

// Program - 9.1
#include< iostream.h>
#include<conio.h>
class add
{
int sum;
protected:
int num1, num2;
public:
add()
{
num1 = num2 = sum = 0;
cout<<\n Add constructor .. ;
}
accept()
{
cout<<\n Enter two numbers .. ;
cin>>num1>>num2;
}
plus()
{
sum = num1 + num2;
cout<<\n The sum of two numbers is .. << sum;
}
};
class subtract :public add
{
int sub;
public:
subtract()
{
sub = 0;
cout<<\n Subtract constructor .. ;
}
minus()
{
add::accept();
sub= num1-num2;
cout<<\n The difference of two numbers are ...
<< sub;
}
};

195

void main()
{
subtract s;
int choice = 0;
cout<<\n Enter your choice ;
cout<<\n1. Add..\n2. Subtract ..;
cin>>choice;
switch(choice)
{
case 1:
s.accept();
s.plus();
break;
case 2:
s.minus();
break;
}
}
Program -9.1- add . subtract

M . M
:
class < > : < A> < M >
{

M M ;
M M ;
}
Program- 9.1- substract M
:

G/ A
M
M
M
M
P

196

9.4 G / A H
(Visibility Mode / Accessibility Specifier)

KF PH Q, M
M M
, F .
, A (accessibility) A. private, public, protected
A H . A
H G (visibility mode) .
Q G private . M
M, O
A 9.1 MA.

private

protected

M
private

protected

public

K
F.

G.
private A

N
A.

protected

protected

N
A.

A.

public

private

protected

public

N
A.

N
A.

A.

9.1 M M O
A

197

private A M
, M public protected ,
M M private GA.
protected A M
, M protected public ,
M M protected GA.
public A M
, M protected , M
M protected , public public
GA.
public, protected, private
M M private
M M K
F. M M
G. .
Program -9.1- add, subtract A PM

class add
{
private:
int sum;
protected :
int num1, num2;
public:
add();
accept();
plus();
};
class subtract : private add
{
int sub;
public:
subtract();
minus();
};

198

subtract N ,
:
int num1 & num2 - subtract- private
accept(), plus() - subtract- private

O A 9.2-
.
M A (constructors), M
M N F. , M
M (instance) L
A.

199

class subtract

II

III

private:

private:

private:

sub
num1
num2
accept()
plus()
public:
subtract()
minus();
private mode
of inheritance

sub
protected:
num1. num2
accept();
plus();
public:
subtract()
minus()
protected mode
of inheritance

sub
protected:
num1. num2
public:
accept();
plus();
subtract()
minus();
public mode
of inheritance

9.2 O A
Program - 91- PM .

GL J W G :
M

N/

add
subtract
9.3 O /

A:--------------------, ----------------------.

200

9.5 KF
M F
M . KF : N
K, N K, G K, K,
K.
1) N K (Single Inheritance)
M
N K .

Base
class - Employee
- E

M
Derived
Class -
Manager
-

2) N K (Multiple Inheritance)
OL
M N K
.

Base
Class:

K
Address

Base
class:
Office

M
Derived
Class - :
Manager

201

3) G K (Multilevel Inheritance)
M
M G
K .

M :

M :

Program - 9.2 GL Of ?

202

// Program - 9.2
#include<iostream.h>
#include<conio.h>
class base
{
public:
base()
{
cout<<\nConstructor of base class... ;
}
~base()
{
cout<<\nDestructor of base class.... ;
}
};
class derived:public base
{
public :
derived()
{
cout << \nConstructor of derived ...;
}
~derived()
{
cout << \nDestructor of derived ...;
}
};
class derived2:public base
{
public :
derived()
{
cout << \nConstructor of derived2 ...;
}
~derived()
{
cout << \nDestructor of derived2 ...;
}
};
void main()
{
derived2 x;
}

203

Of:
Constructor of base class
Constructor of derived .
Constructor of derived2
Destructor of derived2
Destructor of derived
Destructor of base class ..

 A, K O KJ
A. , M
A F M J.
NH H KJ .
H M
F
(Abstract Classes) A. , F O
.

JC M
1)

W ,
-4,5,6 A G :
class node
{
int x;
void init();
public:
void read();
protected:
void get();
};
class type : public node
{
int a;
public:
void func1();
protected:
int b;
void getb();
}

204

class statement :private type


{
int p;
public:
void func2();
protected:
void func3();
};
type M

O A
private

protected

public

type M
N

type M

4 type
statement
M

O A
private

protected

public

statement
M N

statement
M

5 statement

type
statement
-6

205

2)

W GL H , :
#include<iostream.h>
class A
{
private :
int a1;
public:
int a2;
protected:
int a3;
};
class B : public A
{
public:
void func()
{
int b1, b2 , b3;
b1 = a1;
b2 = a2;
b3 = a3;
}
};
void main()
{
B der;
der.a3 = 0;
der.func();
}

3)

W PM . W M
M :
class vehicle
{
int wheels;
public:
void inputdata( int, int);
void outputdata();
protected :
int passenger;
};

206

class heavy_vehicle : protected vehicle


{
int diesel_petrol;
protected:
int load;
public:
void readdata( int, int);
void writedata();
};
class bus: private _heavy_vehicle
{
char marks[20];
public:
void fetchdata( char );
void displaydata();
};

heavy.vehicle M
M PH.
. displaydata() A
PH.
. bus M , A
PH.
outputdata() P, heavy_vehicle M
.
?

4.

W GL Of ?
#include<iostream.h>
#include<conio.h>
class student
{
int m1, m2, total;
public:
student ( int a, int b)
{
m1 = a;
m2 = b;
cout<<\n Non parameterized constructors..;
};
}

207

También podría gustarte