Vous êtes sur la page 1sur 110

kAC1ICAL NC 01

WA Lo lnpuL name of Lhe employee code of Lhe employee baslc salary


CalculaLe
A 13 of 8S
1A 23 of 8S
CLher 10 of 8S
CalculaLe gross salary 8S+A+1A+oLher
Also calculaLe l as 10 of 8S and l1 deducLlon ls 2 of gross salary
CalculaLe Lhe nL1 SALA8?CS l l1

Source code

#include<iostream.h
#include<conio.h
class emp
,
char ename10,,ecode10,;
float bs,pf,pa,ta,other,ns,gs,it;
public:
void getdata(void);
void calsal();
void disp(void);
,;
void emp::getdata()
,
clrscr();
cout<<\nEnter Employee Name : <<endl;
cinename;
cout<<\nEnter Employee Code : <<endl;
cinecode;
cout<<\nEnter Basic salary : <<endl;
cinbs;
cout<<endl<<endl;
,

void emp::calsal()
,
// float sal;
pa=(0.15)bs;
ta=(0.25)bs;
other=(0.10)bs;
pf=(0.10)bs;
it=(0.02)bs;
gs=pa+ta+other+bs;
ns=gs-(pf+it);
,

void emp::disp()
,
cout<<\n--------------------------;
cout<<\nEmployee Name\t : <<ename<<endl;
cout<<\nEmployee Code\t : <<ecode<<endl;
cout<<\nBasic Salary\t : <<bs<<endl;
cout<<\Npa\t\t : <<pa<<endl;
cout<<\Nta\t\t : <<ta<<endl;
cout<<\Nother\t\t : <<other<<endl;
cout<<\Npf\t\t : <<pf<<endl;
cout<<\Nit\t\t : <<it<<endl;
cout<<\nGross Salary\t : <<gs<<endl;
cout<<\nNet Salary\t : <<ns<<endl;
,

void main()
,
clrscr();
emp e;
e.getdata();
e.calsal();
e.disp();
getch();
,
















CU1U1

Enter Name Jf Employee :
chetan
Enter Employee Code :
023
Enter Basic Salary :
10000
Employee Name : chetan
Employee Code : 023
Basic Salry : 10000
PA : 1500
TA : 2500
JTHER : 1000
PF : 1000
IT : 200
Gross Salary(BS+PA+TA+JTHER) :15000
Net Salary(GS-PF-IT) :13800



WA to generate customer b||| for purchas|ng S |tems
ca|cu|ate the d|scount as per the fo||ow|ng cr|ter|a
If |tem name |s IAN D|sc |s 40 of the amt
If |tem name |s AC d|sc |s S of the amt
If |tem name |s neater D|sc |s 1S of the amt
Ior any other Item the d|sc |s 2
Ca|u|ate the customer b||| for purchas|ng any S |tems


#include<iostream.h
#include<conio.h
#include<string.h
struct item
,
char itemname10,;
float quant,price;
,;

void main()
,
int j;
float total=0,discnt,gt;
item i3,;
clrscr();

for(j=0;j<3;j++)
,
cout<<"Enter the item name :";
cinij,.itemname;
cout<<"Enter the item quantity:";
cinij,.quant;
cout<<"Enter the price :";
cin ij,.price;

if(strcmp("FAN",ij,.itemname)==0)
,
total=ij,.priceij,.quant;
discnt=0.4total;
total=total-discnt;
gt=gt+total;
,
else if(strcmp("AC",ij,.itemname)==0)
,
total=ij,.priceij,.quant;
discnt=0.05total;
total=total-discnt;
gt=gt+total;
,
else if(strcmp("HEATER",ij,.itemname)==0)
,
total=ij,.priceij,.quant;
discnt=0.15total;
total=total-discnt;
gt=gt+total;
,
else
,
total=ij,.priceij,.quant;
discnt=0.02total;
total=total-discnt;
gt=gt+total;
,
,

for(j=0;j<3;j++)
,
cout<<"\nItem name :"<<ij,.itemname;
cout<<"\nQuantity & Price of "<<ij,.itemname<<"
resp.:"<<ij,.quant<<" "<<ij,.price;
,
cout<<"\n\nGrand total paid by customer is :"<<gt;
getch();
,









CU1U1

Lnter the |tem name IAN
Lnter the |tem quant|ty4
Lnter the pr|ce 1000
Lnter the |tem name AC
Lnter the |tem quant|ty2
Lnter the pr|ce 2000
Lnter the |tem name nI11Lk
Lnter the |tem quant|ty1
Lnter the pr|ce 1000

Item name IAN
uant|ty r|ce of IAN resp4 1000
Item name AC
uant|ty r|ce of AC resp2 2000
Item name nI11Lk
uant|ty r|ce of nI11Lk resp1 1000

Grand tota| pa|d by customer |s 70S0
kAC1ICAL NC 06

WA to |nput the va|ues |n the 2 d|mens|ona| array and
* f|nd out d|agona| sum of the e|ements
* f|nd sum of |ower tr|ang|e and upper tr|ang|e


Cu1u1

LnLer Lhe values ln 3*3 maLrlx
1 2 3

0 3 6

4 7 9


LnLered MaLrlx
1 2 3
0 3 6
4 7 9

Sum of upper elemenLs of dlagonal elemenLs ls 11

Sum of lower elemenLs of dlagonal elemenLs ls 11






kAC1ICAL NC 07

Wap to |nput the word f|nd out
* |ength of the word
* to pr|nt |n reverse
* to copy one str|ng to another
* to f|nd out pa||ndrome or not


#include<iostream.h
#include<conio.h
#include<string.h

void main()
,
char name20,,n20,;
int i=0,count=0;

clrscr();

cout<<"Enter the word : ";
cinname;

/1) length of word /

while(namei,!='\0')
,
count=count+1;
i++;
,
cout<<"\n1) Length of word is :"<<count;

/2) Reverse of word /

cout<<"\n\n2) Reverse of word is :";
for(int j=count-1;j=0;j--)
,
cout<<namej,;
,

/3) copy in another /

i=0;
while(namei,!='\0')
,
ni,=namei,;
i++;
,
ni,='\0';
cout<<"\n\n3) Word in another variable is :"<<n;

/4) Palindrom or not /

for(i=count;i=0;i--)
,
ncount-i,=namei-1,;
,
ncount,='\0';
if(strcmp(name,n)==0)
cout<<"\n\n4) Word is Palindrome.";
else
cout<<"\n\n4) Word is not Palindrome.";

getch();
,

CU1U1

Lnter the word chetan
1Length of word |s 6

2keverse of word |s natehc

3Word |n another var|ab|e |s chetan

4Word |s not a||ndrome


kAC1ICAL NC 08


WA to enter two words concatenate two words

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

void main()
,
char word120,,word220,;
int i=0,j=0,count=0;
clrscr();
cout<<"Enter the first word : ";
cinword1;
cout<<"Enter the second word : ";
cinword2;
while(word1i++,!='\0')
count=count+1;

i=0;
while(word2i,!='\0')
word1count++,=word2i++,;
word1count,='\0';

cout<<"\nConcatenation of two words are : "<<word1;
getch();
,
CU1U1

Lnter the f|rst name or word chetan
Lnter the second word or neme mahesh

Concatnat|on of two words arechetanmahesh


kAC1ICAL NC 09
WA to ca|cu|ate
A |f user se|ects 1 then area of tr|ang|e
8 |f user se|ects 2 then area of c|rc|e
C |f user se|ects 3 then area of rectang|e
D |f user se|ects 4 then area of square


#lncludelosLreamh
#lncludeconloh
#lncludesLdllbh
vold Lrlang(vold)
vold clr(floaL)
floaL recL(vold)
floaL sqr(floaL)

vold maln()

floaL rsdarea
lnL n
clrscr()
whlle(n!3)

couLnnMLnunL1Area of LrlaglenL2Area of ClrclenL3Area of
8ecLanglenL4Area of SquarenL3LxlLn
couLnLnLer ?our CpLlon
clnn
swlLch(n)

case 1 Lrlang()
break
case 2 couLLnLer Lhe radlus of clrcle
clnr
clr(r)
break
case 3 arearecL()
couLArea of 8ecLangle ls area
break
case 4 couLLnLer Lhe slde of Square
clnsd
areasqr(sd)
couLArea of Square ls area
defaulL exlL

//geLch()

geLch()


vold Lrlang(vold)

floaL hbarea
couLLnLer Lhe hleghL breadLh of Lrlangle
clnhb
area(03)*h*b
couLArea of 1rlangle ls area


vold clr(floaL rad)

floaL area
area314*rad*rad
couLArea of Clrcle ls area


floaL recL(vold)

floaL lbarea
couLLnLer Lhe lengLh breadLh of 8ecLangle
clnlb
areal*b
reLurn(area)


floaL sqr(floaL slde)

floaL area
areaslde*slde
reLurn(area)

CU1U1

MLNU
1Area of tr|ag|e
2Area of C|rc|e
3Area of kectang|e
4Area of Square
SLx|t

Lnter our Cpt|on 1
Lnter the h|eght breadth of tr|ang|e 24 48
Area of 1r|ang|e |s S76

MLNU
1Area of tr|ag|e
2Area of C|rc|e
3Area of kectang|e
4Area of Square
SLx|t

Lnter our Cpt|on 2
Lnter the rad|us of c|rc|e 24
Area of C|rc|e |s 180864001S

MLNU
1Area of tr|ag|e
2Area of C|rc|e
3Area of kectang|e
4Area of Square
SLx|t

Lnter our Cpt|on 3
Lnter the |ength breadth of kectang|e 24 34
Area of kectang|e |s 816

MLNU
1Area of tr|ag|e
2Area of C|rc|e
3Area of kectang|e
4Area of Square
SLx|t

Lnter our Cpt|on 4
Lnter the s|de of Square 23
Area of Square |s S29
kAC1ICAL NC 11

WA a funct|ons
* to |nsert a va|ue |n an array
* de|ete the va|ue from array

#lncludelosLreamh
#lncludeconloh

class updaLe

lnL arr10cnL
publlc
vold lnpuL(vold)
vold lnsL(vold)
vold del(vold)
vold dlsp(lnL n)


vold updaLelnpuL(vold)

lnL l
cnL10
couLnLnLer Lhe 10 numbers for array nnn
for(l0l10l++)
clnarrl
couLnA88A?
for(l0l10l++)
couLarrl

vold updaLelnsL(vold)

lnL lLLemp
couLAL whaL poslLlon you wanL Lo lnserL
clnL
couLLnLer Lhe number for lnserLlon
clnLemp
for(l9l(L1)l)
arrlarrl1
arrL1Lemp


vold updaLedel(vold)

lnL lLLemp
couLAL whaL poslLlon you wanL Lo deleLe
clnL
for(lLl10l++)
arrl1arrl
arr9nuLL

vold updaLedlsp(lnL n)

lnL l
swlLch(n)

case 1 couLnn****** Al1L8 Auul1lCn *****nn
break
case 2 couLnn****** Al1L8 uLLL1lCn *****nn
break

couLnA88A?
for(l0arrl!nuLLl10l++)
couLarrl

vold maln()

updaLe s
lnL nch0
clrscr()
sclear()
slnpuL()
whlle(ch0)

a
couLnLnLer your opLlon nL1lnserLnL2ueleLe
clnn
swlLch(n)

case 1 slnsL()
break
case 2 sdel()
break
defaulLcouLlease LnLer correcL opLlon
goLo a

sdlsp(n)
couLn LnLer 0 Lo conLlnue
clnch

geLch()




Cu1u1

Lnter the 10 Numbers for array

23 31 1 4S 64 22 89 2 3 4 100

AkkA 23 31 1 4S 64 22 89 2 3 4
Lnter your opt|on
1Insert
2De|ete |ease Lnter correct opt|on
Lnter your opt|on
1Insert
2De|ete 1
At what pos|t|on you want to |nsert 3
Lnter the number for |nsert|on S

****** AI1Lk ADDI1ICN *****

AkkA 23 31 S 1 4S 64 22 89 2 3
Lnter 0 to cont|nue

De|ete |ease Lnter correct opt|on
Lnter your opt|on
1Insert
2De|ete 1
At what pos|t|on you want to |nsert 3
Lnter the number for |nsert|on S

****** AI1Lk ADDI1ICN *****

AkkA 23 31 S 1 4S 64 22 89 2 3
Lnter 0 to cont|nue 0
Lnter your opt|on
1Insert
2De|ete 2
At what pos|t|on you want to de|ete 3


****** AI1Lk DLLL1ICN *****


AkkA 23 31 1 4S 64 22 89 2 3
Lnter 0 to cont|nue


WA to enter a word from user encrypt that word as the second word encrypted |n ASCII va|ue
Decrypt |t aga|n

#lncludeconloh
#lncludesLdloh
#lncludelosLreamh
class enc

char a80
publlc
vold encd(vold)
vold decd(vold)



vold encencd(vold)

lnL l
couLnLnLer Lhe sLrlng for encrypLlon
clna
couLnn8efore encrypLlon sLrlng ls a
l0
whlle(al!0)

alal+3
l++

couLn?our encrypLed word ls a

vold encdecd(vold)

lnL l0
couLnLnLer Lhe sLrlng for decrypLlon
clna
couLnn8efore decrypLlon sLrlng ls a
l0
whlle(al!0)

alal3
l++

couLn?our decrypLed word ls a


vold maln()


enc e
lnL n

clrscr()
whlle(n!3)

couLnnL1Lncodlng nL2uecodlng nL 3LxlLnnLnLer your opLlon
clnn
swlLch(n)

case 1 eencd()
break
case 2 edecd()
break
case 3
break
defaulL couLnWrong opLlon


geLch()


CU1U1

1Lncod|ng
2Decod|ng
3Lx|t

Lnter your opt|on 1

Lnter the str|ng for encrypt|on chetan
8efore encrypt|on str|ng |s chetan
our encrypted word |s fkhwdq

1Lncod|ng
2Decod|ng
3Lx|t

Lnter your opt|on 2

Lnter the str|ng for decrypt|on fkhwdq
8efore decrypt|on str|ng |s fkhwdq
our decrypted word |s chetan







v|rtua| funct|onv|rtua| c|ass

#lncludelosLreamh
#lncludeconloh

class publlcaLlon

char LlLle13
floaL prlce
publlc
vold lnpuL(vold)

couLnLnLer Lhe LlLle prlce resp
clnLlLle
clnprlce

vold dlsp(vold)

couLnn1he LlLle prlce of publlcaLlon ls LlLleLprlce



class bookvlrLual publlc publlcaLlon

prlvaLe
lnL pcnL
publlc
vold lnpuL1(vold)

couLnLnLer Lhe no of page counL
clnpcnL

vold dlsp1(vold)

couLnn1he age counL of 8ook ls pcnL



class LapevlrLual publlc publlcaLlon

floaL Llme
publlc
vold lnpuL2(vold)couLnLnLer Lhe Llme ln floaL clnLlme
vold dlsp2(vold)couLnn1he Llme of Lape ls Llme


class modpubllc bookpubllc Lape


vold maln()

mod m
clrscr()
mlnpuL()
mlnpuL1()
mlnpuL2()
mdlsp()
mdlsp1()
mdlsp2()
geLch()



CU1U1

Lnter the t|t|e pr|ce resp c++
20000

Lnter the no of page count 4S9

Lnter the t|me |n f|oat 11S6


1he t|t|e pr|ce of pub||cat|on |s c++ 200

1he age count of 8ook |s 4S9

1he t|me of tape |s 11S6


8AC1lCAL nC 20

wrlLe a program Lo Lake one lnLeger value
ln Lwo dlfferenL classes Add Lhe values uslng frlend funcLlon

#lncludelosLreamh
#lncludeconloh
#lncludelomanlph

class second
class flrsL

lnL one
publlc
flrsL()
frlend vold add(flrsL fsecond s)


class second

lnL Lwo
publlc
second()
frlend vold add(flrsL fsecond s)


flrsLflrsL()

couLnLnLer 1he llrsL value
clnone

secondsecond()

couLnLnLer 1he Second value
clnLwo

vold add(flrsL fsecond s)

couLnnSum fone+sLwoendl

vold maln()

clrscr()
flrsL f
second s
add(fs)
geLch()

CU1U1


Lnter 1he I|rst va|ue 23

Lnter 1he Second Va|ue 31


Sum S4







]* Constant Defau|t Argument *]'


#lncludelosLreamh
#lncludeconloh
vold maln()

floaL pl314rareaarea1
floaL aclrcle(consL floaL *floaL r936)
clrscr()
areaaclrcle(pl)
couLuefaulL ArgumenL Area of clrcle areaendl
area1aclrcle(pl10)
couLArea of clrcle area1endl
geLch()

floaL aclrcle(consL floaL *p1floaL r1)
floaL k
k*p1
reLurn(k*r1*r1)


CuLpuL

uefaulL ArgumenL Area of clrcle 273094147
Area of clrcle 314













Dynam|c constructor w|th memory a||ocat|on us|ng new command


#lncludelosLreamh
#lncludesLrlngh
#lncludeconloh
class sLrlng

char *name
lnL lengLh
publlc
sLrlng()

lengLh 0
namenew charlengLh+1 //one exLra for 0

sLrlng (char *s)

lengLh sLrlen(s)
name new charlengLh+1//one exLra for 0
sLrcpy (names)

vold dlsplay()

couLnameendl

vold [oln (sLrlng asLrlng b)

vold sLrlng [oln(sLrlng a sLrlng b)

lengLh alengLh + blengLh
deleLe name
name new charlengLh+1 //dynamlc allocaLlon
sLrcpy(nameaname)
sLrcaL(namebname)

vold maln ()

clrscr()
char *flrsL !oseph
sLrlng name1(flrsL)name2(Louls )name3(Lagrange)s1s2
s1[oln(name1name2)
s2[oln(s1name3)
name1dlsplay()
name2dlsplay()
name3dlsplay()
s1dlsplay()
s2dlsplay()
geLch()


Cutput

!oseph
Louls
Lagrange
!oseph Louls
!oseph Louls Lagrange










Call by Reference using Friend Function


#includeiostream.h~
#includeconio.h~
class PQR;
class ABC;

class ABC

int x;
public:
ABC() : x(10)}

void display()

cout"\nValue oI x : "xendl;
}
Iriend void swap(ABC &,PQR&);
};

class PQR

int y;
public:
PQR() : y(20)}
void display()

cout"\nValue oI y : "yendl;
}
Iriend void swap(ABC &,PQR &);
};
void swap(ABC &A,PQR &P)

int temp;
tempA.x;
A.xP.y;
P.ytemp;
}

void main()

clrscr();
ABC obj1;
PQR obj2;

cout"\n\nBeIore swapping :- \n";
obj1.display();
obj2.display();
swap(obj1,obj2);

cout"\n\nAIter swapping :- \n";
obj1.display();
obj2.display();
coutendl;
getch();
}

utput:

BeIore swapping :-

Value oI x : 10

Value oI y : 20


AIter swapping :-

Value oI x : 20

Value oI y : 10



&nary perator ;erload


#includestdio.h~
#includeiostream.h~
#includeconio.h~
class counter

protected :
unsigned int count;
public :
counter()//constructor,no args

count0;
}
counter(int c) //constructor one arg

countc;
}
unsigned int getcount() const //return count

return count;
}
counter operator () //incr count(preIix)

return counter(count);
}
};
class countdn : public counter

public :
countdn() : counter() //constructor,no args
}
countdn(int c) : counter(c) //constructor one arg
}
countdn operator --() //decr count(preIix)

return countdn(--count);
}
};
void main()

countdn c1; //class countdn
countdn c2(100);
clrscr();

cout"\n\nc1 "c1.getcount();//display
cout"\n\nc2 "c2.getcount();//display
c1;//increment c1
c1;
c1;
cout"\n\nc1 "c1.getcount(); //display c1
--c2; //decrement
--c2;
cout"\n\nc2 "c2.getcount();//display c2
countdn c3--c2; //creates c3 Irom c2
cout"\n\nc3 "c3.getcount();//display c3
coutendl;
getch();
}

utput:
c1 0

c2 100

c1 3

c2 98

c3 97




















ke|at|ona| Cperator Cver|oad


#includeiostream.h~
#includeconio.h~

class circle

int r;
public :
circle () : r(0)
circle(int r)

this-~rr;
}
int operator (circle T)

iI(r ~ T.r)
return 1;
else
return 0;
}
void area()

cout"Area "3.14*r*r;
}
};
void main()

circle c1(5),c2(10);
clrscr();

iI(c1 c2)
c1.area();
else
c2.area();
getch();
}

utput:
Area 314

]* program to demonstrate over|oad|ng of Ass|gnment operator *]
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
class Alpha

lnL daLa
publlc
Alpha()
Alpha(lnL d)

daLad

vold show()

couLn value daLa

Alpha operaLor (Alpha a)

daLaadaLa
couLnlnslde overloaded asslgnmenL operaLor
reLurn Alpha(daLa)


vold maln()

clrscr()
Alpha a1(30)
Alpha a2
a2a1
Alpha a3a2
a2show()
a3show()
geLch()















]* rogram for Lng||sh d|stances *]
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
sLrucL dlsLance

lnL feeL
lnL lnches

dlsLance add(dlsLance dd1dlsLance dd2)

dlsLance dd3
dd3lnchesdd1lnches+dd2lnches
dd3feeL0
lf(dd3lnches120)

dd3lnches 120
dd3feeL++

dd3feeL +dd1feeL+dd2feeL
reLurn dd3


vold dlsp(dlsLance dd)

couLddfeeL ddlnches

vold maln()

dlsLance d1d2d3
clrscr()
couLLnLer feeLendl
clnd1feeL
couLLnLer lnchesendl
clnd1lnches
couLLnLer feeLendl
clnd2feeL
couLLnLer lnchesendl
clnd2lnches
d3add(d1d2)
dlsp(d3)
geLch()




ata con;ersion

#includestdio.h~
#includeiostream.h~
#includeconio.h~




class distance
,
int feet;
float inch;


public:

distance(float p) //costructor to concert basic type to
class type
,

feet= (int) p;
inch=(p-feet)12;
,


void show()
,
cout<<"feet="<<feet;
cout<<"inch="<<inch;
,
,;

void main()
,
distance d1=1.75;

d1.show();
,

out put

feet=1;
inch=9;



8AC1lCAL nC 17

v|rtua| c|ass

#lncludelosLreamh
#lncludeconloh

class publlcaLlon

char LlLle13
floaL prlce
publlc
vold lnpuL(vold)

couLnLnLer Lhe LlLle prlce resp
clnLlLle
clnprlce

vold dlsp(vold)

couLnn1he LlLle prlce of publlcaLlon ls LlLleLprlce



class bookvlrLual publlc publlcaLlon

prlvaLe
lnL pcnL
publlc
vold lnpuL1(vold)

couLnLnLer Lhe no of page counL
clnpcnL

vold dlsp1(vold)

couLnn1he age counL of 8ook ls pcnL



class LapevlrLual publlc publlcaLlon

floaL Llme
publlc
vold lnpuL2(vold)couLnLnLer Lhe Llme ln floaL clnLlme
vold dlsp2(vold)couLnn1he Llme of Lape ls Llme


class modpubllc bookpubllc Lape


vold maln()

mod m
clrscr()
mlnpuL()
mlnpuL1()
mlnpuL2()
mdlsp()
mdlsp1()
mdlsp2()
geLch()




CU1U1

Lnter the t|t|e pr|ce resp c++
20000

Lnter the no of page count 4S9

Lnter the t|me |n f|oat 11S6


1he t|t|e pr|ce of pub||cat|on |s c++ 200

1he age count of 8ook |s 4S9

1he t|me of tape |s 11S6












ultile;el and Hybrid (Virtual)


#includeiostream.h~
#includeconio.h~
#includestring.h~
const int maxlen25;

class person

private :
char name|maxlen|;
char sex;
int age;
public :
void readperson()

cout"\nEnter Name : ";
cin~~name;
cout"\nEnter sex : ";
cin~~sex;
cout"\nEnter age : ";
cin~~age;
}
void displayperson()

cout"Name : "nameendl;
cout"Sex : "sexendl;
cout"age : "ageendl;
}
};
class sports : public virtual person //virtual class

private :
char name|maxlen|;//name oI game
int score; //score awarded Ior result declaration
protected :
void readdata()

cout"\nEnter game played : ";
cin~~name;
cout"\nEnter game score : ";
cin~~score;
}
void displaydata()

cout"\nSports played : "nameendl;
cout"Game score : "scoreendl;
}
int sportsscore()

return score;
}
};
class student : public virtual person //virtual class

private :
int rollno;
char branch|30|;
public :
void readstudentdata()

cout"\nEnter rollno : ";
cin~~rollno;
cout"\nEnter branch in studying : ";
cin~~branch;
}
void putstudentdata()

cout"\nRollno : "rollno;
cout"\nBranch : "branch;
}
};
class exam : public student

protected :
int sub1marks,sub2marks;
public :
void readdata()

cout"\nEnter scored in subject1 : ";
cin~~sub1marks;
cout"\nEnter scored in subject2 : ";
cin~~sub2marks;
}
void displaydata()

cout"\nInternal marks scored in subject1 : "sub1marks;
cout"\nInternal marks scored in subject2 : "sub2marks;
cout"\nTotal marks scored : "totalmarks()endl;
}
int totalmarks()

return(sub1markssub2marks);
}
};
class result : public exam ,public sports

private :
int total;
public :
void readdata()

readperson(); //access person class member
student : : readstudentdata();
exam : : readdata();//uses readdata oI exam class
sports : : readdata();
}
void displaydata()

displayperson();
student : : putstudentdata();
exam : : displaydata();
sports : : displaydata();
cout"\nOverall perIormance,(examsports) : "
percentage()"";
}
int percentage()

return(exam : : totalmarks()sportsscore())/3;
}
};

void main()

clrscr();
result student;
cout"\nEnter data Ior student..."endl;
student.readdata();
cout"\n------------------------";
cout"\nStudent details...\n"endl;
student.displaydata();

getch();
}

utput:

Enter data Ior student...

Enter Name : Raj

Enter sex : M

Enter age : 22

Enter rollno : 4

Enter branch in studying : Computer-science

Enter scored in subject1 : 92

Enter scored in subject2 : 88

Enter gamed played : cricket

Enter game score : 85
------------------------
Student details...

Name : Raj
Sex : M
age : 22

ROllno : 4
Branch : Computer-science
Internal marks scored in subject1 : 92
Internal marks scored in subject2 : 88
Total marks scored : 180

Sports played : cricket
Game score : 85
Overall perIormance,(examsports) : 88

o|nters to Cb[ect

#lncludelosLreamh
#lncludeconloh
class lLem

lnL code
floaL prlce
publlc
vold geLdaLa(lnL afloaL b)

codea
prlceb


vold show(vold)

couLCode coden
couLrlce prlcen


consL lnL slze2

lnL maln()

lLem *pnew lLem slze
lLem *dp
lnL xl
floaL y
clrscr()
for(l0lslzel++)

couLlnpuL code and prlce for lLeml+1
clnxy
pgeLdaLa(xy)
p++


for(l0lslzel++)

couLlLeml+1n
dshow()
d++

geLch()
reLurn 0



Cutput

lnpuL code and prlce for lLem1 101
330
lnpuL code and prlce for lLem2 102
630

lLem1
Code 101
rlce 330
lLem2
Code 102
rlce 630
































v|rtua| funct|on


#lncludelosLreamh
#lncludeconloh
class Shape

proLecLed
lnL wldLh helghL
publlc
Shape( lnL a0 lnL b0)

wldLh a
helghL b

vlrLual lnL area()

couL arenL class area endl
reLurn 0


class 8ecLangle publlc Shape
publlc
8ecLangle( lnL a0 lnL b0)

Shape(a b)

lnL area ()

couL 8ecLangle class area endl
reLurn (wldLh * helghL)


class 1rlangle publlc Shape
publlc
1rlangle( lnL a0 lnL b0)

Shape(a b)

lnL area ()

couL Lrlangle class area endl
reLurn (wldLh * helghL / 2)


// Maln funcLlon for Lhe program
lnL maln( )

Shape *shape
8ecLangle rec(107)
1rlangle Lrl(103)
// sLore Lhe address of 8ecLangle
shape rec
// call recLangle area
shapearea()

// sLore Lhe address of 1rlangle
shape Lrl
// call Lrlangle area
shapearea()

reLurn 0



CuLpuL
8ecLangle class area
1rlangle class area


















]* rogram to d|sp|ay f||e contents *]
#lncludelosLreamh
#lncludeconloh
#lncludesLdloh
#lncludefsLreamh
//#lncludelsLreamh
vold maln()

char ch
clrscr()
lfsLream lff(f1LxL)
lf(!lff)

couLLrror! llle can noL be opened n

else

whlle(lff)

lffch
couLch


geLch()














ract|ca| No 12

Streams and files in text modes

#includeiostream.h~
#includeconio.h~
#includeIstream.h~
#includestring.h~
void main()

int j;
char ch; //character to read
char str||"Sies college oI management studies";
clrscr();

oIstream outIile;
outIile.open("test.txt"); //create Iile Ior output
Ior(j0;jstrlen(str);j)
outIile.put(str|j|); //each character write it to Iile

cout"\nFile written";
outIile.close();

iIstream inIile;
inIile.open("test.txt"); //create Iile Ior input
coutendl;
while(inIile) //read until EOF or error

inIile.get(ch); //read character
coutch; //display character
}
getch();
}

utput:

Contents of test.txt

Sies college oI management studies






Streams and files in binary modes

#includeiostream.h~
#includeconio.h~
#includeIstream.h~

class person

protected:
char name|80|;
int age;
public:
void getdata() //get person's data Irom user

cout"\nEnter name : ";
cin~~name;
cout"\nEnter age : ";
cin~~age;
}
void showdata() //display person's data

cout"\nNAME : "name;
cout"\nAGE : "age;
}
};

void main()

char ch;
clrscr();
person p1;
Istream Iile; //create input/output Iile

Iile.open("sample.txt",ios::app , ios::out , ios::in , ios::binary);

//data Irom user to Iile
do

cout"\nEnter person's data :";
p1.getdata();
Iile.write((char *)&p1,sizeoI(p1)); //write to Iile
cout"\nWant to enter more details (y/n) ?";
cin~~ch;
}while(ch'y');

Iile.seekg(0); //reset to start oI Iile
Iile.read((char *)&p1,sizeoI(p1)); //read Iirst person data
while(!Iile.eoI())

cout"\n";
p1.showdata();
Iile.read((char *)&p1,sizeoI(p1));
}
coutendl;
getch();
}


utput:

Enter person's data :
Enter name : Nayab
Enter age : 24

Want to enter more details (y/n) ?y

Enter person's data :
Enter name : Prachi
Enter age : 23

Want to enter more details (y/n) ?n

NAME : Nayab
AGE : 24

NAME : Prachi
AGE : 23













Insert|on Lxtract|on Cperator Cver|oad


#includeiostream.h~
#includeconio.h~
#includeIstream.h~
#includeiostream.h~
class distance

private :
int Ieet;
Iloat inches;
public :
distance() : Ieet(0),inches(0.0) //constructor with no args
}
distance(int It,Iloat in) : Ieet(It),inches(in) //constructor with two args
}
Iriend istream& operator ~~(istream& s,distance& d);
Iriend ostream& operator (ostream& s,distance& d);
};

istream& operator ~~ (istream& s,distance& d) //get data Irom Iile or keyboard

char ch;
s~~d.Ieet~~ch~~ch~~d.inches~~ch; //overload ~~ operator
return s;
}

ostream& operator (ostream& s,distance& d) //send data to Iile or screen

sd.Ieet"\'-"d.inches'\"'; //overload operator
return s;
}

void main()

char c;
distance d1;
oIstream oIile; //create and open output stream
clrscr();
oIile.open("dist.txt");

do

cout"\nEnter distance : ";
cin~~d1; //get distance Irom user
oIiled1; //write it to output stream
cout"\nWant to enter more details (y/n)?";
cin~~c;
}while(c!'n');
oIile.close(); //close output stream

iIstream iIile; //create and open input stream
iIile.open("dist.txt");
cout"\nContents oI Iile are : \n";

while(1)

iIile~~d1; //read distance Irom stream
iI(iIile.eoI()) //quit on EOI
break;
cout"Distance : "d1endl; //display distance
}
getch();
}

utput:
LnLer dlsLance 1 2 3 4 3

WanL Lo enLer more deLalls (y/n)?y

LnLer dlsLance 9 8 7 6 3

WanL Lo enLer more deLalls (y/n)?n

ConLenLs of flle are
ulsLance 14
ulsLance 96

DIS11k1

1496




* program for error sLaLus blLs */
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
vold maln()

clrscr()
lnL m
couL lnslde maln() meLhod n
clnm
lf(clnbad())
couL 8adn endl
else
couL noL 8adn endl
lf(clngood())
couL Cood n
else
couL noL Cood n
lf(clnfall())
couLlall n
else
couL noL lalln
geLch()



/* program for Pybrld lnherlLance */
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class SLudenL

publlc
lnL rno
char name20
vold geL()

couLLnLer 8oll no
clnrno
couLLnLer name
clnname

vold prlnL()

couL name name
couL 8oll no rno


class Lxampubllc SLudenL

publlc
lnL m1m2m3m4m3m6
vold geLmrks()

couLLnLer Marks of 6 sub[ecLs
clnm1m2m3m4m3m6



class SporLs

publlc
char sp
vold geLsp()

couLLnLer WheLher you are sporLsman?(y/n)
clnsp


class 8esulLpubllc Lxampubllc SporLs


publlc
floaL Lp
vold calprlnL()

lf(spy)

Lm1+m2+m3+m4+m3+m6+23

else

Lm1+m2+m3+m4+m3+m6

pL/6
couL Marks CbLaln L
couL ercenLage p



vold maln()


8esulL r
clrscr()
rgeL()
rgeLsp()
rgeLmrks()
rcalprlnL()
geLch()


/* program for lnherlLance */
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class SLudenL

prlvaLe
lnL rno
char name20
publlc
vold geL()

couLnLnLer 8oll no and name endl
clnrnoname

vold show()

couL n8oll no rno
couL nname name


class sLudpubllc SLudenL

prlvaLe
lnL m1m2m3
publlc
vold marks()

couL nLnLer Marks of 3 sub[ecL
clnm1m2m3

vold dlsp()

couL nMarks ln 1sL sub[ecL m1
couL nMarks ln 2nd sub[ecL m2
couL nMarks ln 3rd sub[ecL m3


vold maln()

clrscr()
sLud s
sgeL()
smarks()
sshow()
sdlsp()
geLch()

/* rogram for overloadlng lnserLlon LxLracLlon operaLor*/
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
class Complex

lnL real
floaL lmg
publlc
Complex()
Complex(lnL rfloaL l)

realr
lmgl

frlend osLream operaLor(osLream Complex c)
frlend lsLream operaLor(lsLream Complex c)

osLream operaLor(osLream sComplex c)

screal + clmgln
reLurn s

lsLream operaLor(lsLream sComplex c)

screalclmg
reLurn s

vold maln()

clrscr()
Complex c1(1323)c2(3343)c3
couLnc1 c1
couLnc2 c2
couLnc3 c3
geLch()








]*program for |os funct|on *]
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
#lncludelomanlph
//#lncludelosh
consL lnL dlsL4
consL lnL monLhs3
vold maln()
clrscr()
lnL dm
lnL salesdlsLmonLhs
for(d0ddlsLd++)

for(m0mmonLhsm++)


couLn LnLer sales for for dlsLrlcL d+1
couL MonLh m+1 !
clnsalesdm


couLnn
couL MonLhs n
couL 1 seLw(10) 2 seLw(10) 3 seLw(10)
for(d0ddlsLd++)
couLn ulsLrlcL d+1
for(m0mmonLhsm++)

couLseLlosflags(losflxed)
couLseLlosflags(losshowpolnL)
couLseLpreclslon(2)
couLseLw(10)
couLsalesdm


couLnn

geLch()


]* program for Mu|t||eve| |nher|tance *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class SLudenL

publlc
lnL rno
char name20
vold geL()

couLLnLer 8oll no
clnrno
couLLnLer name
clnname


class Markspubllc SLudenL

publlc
lnL m1m2m3
vold geLmrks()

couLLnLer Marks of 3 sub[ecLs
clnm1m2m3

vold show()

couL8oll no rno
couL name name



class 8esulLpubllc Marks


publlc
floaL Lp
char *grade
vold cal()

Lm1+m2+m3
pL/3
lf(p73)
//couL nCrade ulsLlncLon
grade ulsLlncLon
else lf ((p60)(p73))
grade llrsL Class
else lf ((p30)(p60))
grade Second Class
else lf((p43)(p30))
grade ass class
else
grade lall

vold dlsp()

couL

couLnMarks ln sub[ecL1 m1
couLnMarks ln sub[ecL2 m2
couLnMarks ln sub[ecL3 m3
couLn1oLal L
couLnercenLage p
couLnCrade grade


vold maln()


8esulL r
clrscr()
rgeL()
rgeLmrks()

couLn 8esulL n
rcal()
rshow()
rdlsp()
geLch()



]* program for Mu|t|p|e |nher|tance *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class SLudenL

publlc
lnL rno
char name20
vold geL()

couLLnLer 8oll no
clnrno
couLLnLer name
clnname


class Marks

publlc
lnL m1m2m3
vold geLmrks()

couLLnLer Marks of 3 sub[ecLs
clnm1m2m3


class 8esulLpubllc SLudenLpubllc Marks

floaL Lp
char *grade
publlc
vold cal()

Lm1+m2+m3
pL/3
lf(p73)
//couL nCrade ulsLlncLon
grade ulsLlncLon
else lf ((p60)(p73))
grade llrsL Class
else lf ((p30)(p60))
grade Second Class
else lf((p43)(p30))
grade ass class
else
grade lall

vold dlsp()

couL
couLn 8esulL
couLnname of SLudenL name
couLn8oll no rno
couLnMarks ln sub[ecL1 m1
couLnMarks ln sub[ecL2 m2
couLnMarks ln sub[ecL3 m3
couLn1oLal L
couLn ercenLage p
couLn Crade grade


vold maln()

8esulL r
clrscr()
rgeL()
rgeLmrks()
rcal()
rdlsp()
geLch()


]* program for over|oad defau|t copy constructor *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class Clrcle

prlvaLe
lnL radlus
floaL xy
publlc
Clrcle() radlus0
x00
y00
// couLxxyyradlus

Clrcle(lnL rfloaL xfloaL y)

radlusr
xx
yy
couLxxyyrradlus

Clrcle(Clrcle ob[)

radlusob[radlus
xob[x
yob[y

vold show()

couL8adlus radlusL (xy) (x y)n


vold maln()
clrscr()
//Clrcle r1(102323)
Clrcle r2r3
Clrcle r1(102323)
// clrscr()
/* r2r3r1
Clrcle r4r1
Clrcle r3(r1)
r1show()
r2show()
r3show()
r4show()
r3show() */
geLch()



]* program to ca|cu|ate percentage of student *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class SLudenL

prlvaLe
lnL rno
char name20
lnL s1s2s3s4s3
publlc
vold show()
vold geL()

vold SLudenLgeL()

couL LnLer 8oll no and name
clnrnoname
couLLnLer marks of 3 sub[ecLs
clns1s2s3s4s3

vold SLudenLshow()

floaL Lp
Ls1+s2+s3+s4+s3
pL/6
couL SLudenL ueLalls endl
couLendl
couL8oll no rnoendl
couLname nameendl
couL1oLal Marks LendlercenLage pendl

vold maln()

SLudenL s1
clrscr()
s1geL()
s1show()
geLch()


]* rogram to over|oad postf|x operator us|ng fr|end funct|on*]
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
class lndex

lnL c
publlc
lndex()c(0)
lndex(lnL l)c(l)
frlend vold operaLor ++(lndex lnL)
vold show()

couL value c


vold operaLor ++(lndex alnL)

(ac)++

vold maln()

clrscr()
lndex d
dshow()
d++
dshow()
d++
dshow()
geLch()




]* Iunct|on Cver|oad|ng for Area *]
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
vold area(floaL)
vold area(floaLfloaL)
lnL area(lnL)
double area(doubledouble)
vold maln()

floaL radluslb
lnL sval
double anshbs
clrscr()
couLArea of Clrcleendl
couLendl
couLLnLer 8adlusendl
clnradlus
area(radlus)
couLendl
couLArea of 8ecLangleendl
couLLnLer LengLh and 8readLhendl
clnlb
area(lb)
couLendl
couLArea of Squareendl
couLendl
couLLnLer sllde of Squareendl
clns
valarea(s)
couLArea of square valendl
couLendl
couLArea of 1rlangleendl
couLendl
couLLnLer helghL and base helghL of Lrlangleendl
clnhbs
ansarea(hbs)
couLArea of Lrlangle ansendl
geLch()

vold area(floaL a)

double b
b314*a*a
couLArea of clrcle bendl

vold area(floaL afloaL b)

floaL c
ca*b
couLArea of 8ecLangle cendl

lnL area(lnL a)

lnL b
ba*a
reLurn b

double area(double adouble b)

double c
c03*a*b
reLurn (c)


]* rogram for s|mp|e |nher|tance *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
#lncludesLrlngh
class 1lme

publlc
lnL hrmlnsec
vold seL(lnL hlnL mlnL s)
vold show()

class Clockpubllc 1lme

publlc
char sLr3
vold seLAmm()

vold 1lmeseL(lnL hhlnL mmlnL ss)

hrhh
mlnmm
secss

vold 1lmeshow()

couLhr mln sec

vold ClockseLAmm()

lf(hr12)

sLrcpy(sLrpm)
couLsLr

else

sLrcpy(sLram)
couLsLr


vold maln()
clrscr()
Clock L
LseL(32330)
Lshow()
LseLAmm()
geLch()


/* program Lo readcharacLer from keyboard sLore ln flle */
#lncludesLdloh
#lncludeconloh
#lncludefsLreamh
vold maln()

char ch
couL LnLer characLer (z)
ofsLream oflle(f2LxL)
whlle(cln)

clngeL(ch)
ofllech

oflleclose()
geLch()

* 28eLurn SLrucLure */
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
sLrucL employee

lnL empld
//char *name
lnL salary

employee ee2
employee dlsp(lnLlnL)
vold maln()

lnL nosal
clrscr()
couLenLer Lmployee ldendl
clnno

couLLnLer salary
clnsal

e2dlsp(nosal)

couLLmployeelu e2empldn Salary e2salaryendl
geLch()

employee dlsp(lnL nolnL bs)

employee e1
e1empldno
//e1namenm
e1salarybs
reLurn(e1)

* program to over|oad Unary Cperator w|th fr|end funct|on *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class lndex
lnL c
publlc
lndex()c(3)
lndex(lnL l)c(l)
frlend vold operaLor ++(lndex)

vold showdaLa()

couLn value c



vold operaLor ++(lndex a)

++(ac)

vold maln()

clrscr()
lndex d
dshowdaLa()
++d
dshowdaLa()
++d
dshowdaLa()
geLch()


]* rogram for v|rtua| Iunct|on *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class Shapes

publlc
vlrLual vold draw()0


class Llnepubllc Shapes

publlc
vold draw()

couLnlnslde llne


class Clrclepubllc Shapes

publlc
vold draw()

couLnlnslde Clrcle


class 8ecLpubllc Shapes

publlc
vold draw()

couLnlnslde 8ecL


vold maln()

Shapes *pL
Llne l1
pLl1
pLdraw()
Clrcle c1
pLc1
pLdraw()
8ecL r
pLr
pLdraw()
geLch()





]]prototype

#lncludelosLreamh
vold odd(lnL a)
vold even(lnL a)
lnL maln()

lnL l
do

couL1ype a number(0 Lo exlL)
clnl
odd(l)

whlle(l!0)
reLurn 0


vold odd(lnL a)

lf((a2!0))
couLnumber ls Cddn
else
even(a)

vold even(lnL a)

lf(a20)
couLnumber ls Lvenendl
else
odd(a)



#lncludelosLreamh
vold odd(lnL a)
vold even(lnL a)
lnL maln()

lnL l
do

couL1ype a number(0 Lo exlL)
clnl
odd(l)

whlle(l!0)
reLurn 0


vold odd(lnL a)

lf((a2!0))
couLnumber ls Cddn
else
even(a)

vold even(lnL a)

lf(a20)
couLnumber ls Lvenendl
else
odd(a)




#lncludelosLreamh
#lncludeconloh
#lncludesLrlngh

vold maln()

char name20n20
lnL l0counL0

clrscr()
couLLnLer Lhe Word
clnname

/* 1) lengLh of Lhe Word */
whlle(namel!0)

counLcounL+1
l++

couLn 1) LengLh of Lhe Word lscounL

/* 8everse of Word */
couLnn 2) 8everse of Lhe Word ls
for(lnL [counL1[0[)

couLname[


/* 3) Copy ln CLher sLrlng */
l0
whlle(namel!0)

nlnamel
l++

nl0
couLnn 3) Word ln oLher varlable lsn
/*4) alllndrome or noL*/
for(lcounLl0l)

ncounLlnamel1

ncounL0
lf(sLrcmp(namen)0)

couLnn 4)Word ls palllndrome

else

couLnn 4) Word ls noL palllndrome


/* 3) ConcaLenaLe Lwo word */
couLnLnLer Lhe 2nd word
clnn
whlle(namel++!0)
counLcounL+1
l0

whlle(nl!0)
namecounL++nl++
namecounL0
couLn ConcaLenaLe of Lwo words arename
geLch()


Insert|on Lxtract|on Cperator Cver|oad

!rogram Code
#includeiostream.h~
#includeconio.h~
#includeIstream.h~
#includeiostream.h~
class distance

private :
int Ieet;
Iloat inches;
public :
distance() : Ieet(0),inches(0.0) //constructor with no args
}
distance(int It,Iloat in) : Ieet(It),inches(in) //constructor with two args
}
Iriend istream& operator ~~(istream& s,distance& d);
Iriend ostream& operator (ostream& s,distance& d);
};

istream& operator ~~ (istream& s,distance& d) //get data Irom Iile or keyboard

char ch;
s~~d.Ieet~~ch~~ch~~d.inches~~ch; //overload ~~ operator
return s;
}

ostream& operator (ostream& s,distance& d) //send data to Iile or screen

sd.Ieet"\'-"d.inches'\"'; //overload operator
return s;
}

void main()

char c;
distance d1;
oIstream oIile; //create and open output stream
clrscr();
oIile.open("dist.txt");

do

cout"\nEnter distance : ";
cin~~d1; //get distance Irom user
oIiled1; //write it to output stream
cout"\nWant to enter more details (y/n)?";
cin~~c;
}while(c!'n');
oIile.close(); //close output stream

iIstream iIile; //create and open input stream
iIile.open("dist.txt");
cout"\nContents oI Iile are : \n";

while(1)

iIile~~d1; //read distance Irom stream
iI(iIile.eoI()) //quit on EOI
break;
cout"Distance : "d1endl; //display distance
}
getch();
}

utput:
LnLer dlsLance 1 2 3 4 3

WanL Lo enLer more deLalls (y/n)?y

LnLer dlsLance 9 8 7 6 3

WanL Lo enLer more deLalls (y/n)?n

ConLenLs of flle are
ulsLance 14
ulsLance 96
DIS11k1

1496
ultile;el and Hybrid (Without &sing Virtual)

!rogram Code
#includeiostream.h~
#includeconio.h~
#includestring.h~
const int maxlen25;

class person

private :
char name|maxlen|;
char sex;
int age;
public :
void readperson()

cout"\nEnter Name : ";
cin~~name;
cout"\nEnter sex : ";
cin~~sex;
cout"\nEnter age : ";
cin~~age;
}
void displayperson()

cout"Name : "nameendl;
cout"Sex : "sexendl;
cout"age : "ageendl;
}
};
class sports : public virtual person //virtual class

private :
char name|maxlen|;//name oI game
int score; //score awarded Ior result declaration
protected :
void readdata()

cout"\nEnter game played : ";
cin~~name;
cout"\nEnter game score : ";
cin~~score;
}
void displaydata()

cout"\nSports played : "nameendl;
cout"Game score : "scoreendl;
}
int sportsscore()

return score;
}
};
class student : public virtual person //virtual class

private :
int rollno;
char branch|30|;
public :
void readstudentdata()

cout"\nEnter rollno : ";
cin~~rollno;
cout"\nEnter branch in studying : ";
cin~~branch;
}
void putstudentdata()

cout"\nRollno : "rollno;
cout"\nBranch : "branch;
}
};
class exam : public student

protected :
int sub1marks,sub2marks;
public :
void readdata()

cout"\nEnter scored in subject1 : ";
cin~~sub1marks;
cout"\nEnter scored in subject2 : ";
cin~~sub2marks;
}
void displaydata()

cout"\nInternal marks scored in subject1 : "sub1marks;
cout"\nInternal marks scored in subject2 : "sub2marks;
cout"\nTotal marks scored : "totalmarks()endl;
}
int totalmarks()

return(sub1markssub2marks);
}
};
class result : public exam ,public sports

private :
int total;
public :
void readdata()

readperson(); //access person class member
student : : readstudentdata();
exam : : readdata();//uses readdata oI exam class
sports : : readdata();
}
void displaydata()

displayperson();
student : : putstudentdata();
exam : : displaydata();
sports : : displaydata();
cout"\nOverall perIormance,(examsports) : "
percentage()"";
}
int percentage()

return(exam : : totalmarks()sportsscore())/3;
}
};

void main()

clrscr();
result student;
cout"\nEnter data Ior student..."endl;
student.readdata();
cout"\n------------------------";
cout"\nStudent details...\n"endl;
student.displaydata();

getch();
}

utput:

Enter data Ior student...

Enter Name : Raj

Enter sex : M

Enter age : 22

Enter rollno : 4

Enter branch in studying : Computer-science

Enter scored in subject1 : 92

Enter scored in subject2 : 88

Enter gamed played : cricket

Enter game score : 85
------------------------
Student details...

Name : Raj
Sex : M
age : 22

ROllno : 4
Branch : Computer-science
Internal marks scored in subject1 : 92
Internal marks scored in subject2 : 88
Total marks scored : 180

Sports played : cricket
Game score : 85
Overall perIormance,(examsports) : 88

Static ata embers and Static ember Functions

!rogram Code
#include iostream.h~
#includeconio.h~
class employee

char name |30|;
Iloat bs;
static Iloat S;

public:
void read()

cout "\n\n Enter name : ";
cin~~name;
cout"\n Enter basic sal : ";
cin ~~bs;

S Sbs;
}
void show()

coutname"\t"bs"\n";
}

static void avgsal()

Iloat avg S/3;
cout "\n Avg Sal : "avg;
}
};

Iloat employee : : S;

void main ()

clrscr();
employee A,B,C;
A.read ();
B.read();
C.read();

cout "\n\nEmployee list \n";
cout "\n---------------------\n";
cout "\nNAME \tBASICSAL\n";
cout "---------------------\n";

A.show();
B.show();
C.show ();

employee :: avgsal();
getch();
}

utput:

Enter name : AAA
Enter basic sal : 2000

Enter name : BBB
Enter basic sal : 3000

Enter name : CCC
Enter basic sal : 4000

Employee list
---------------------
NAME BASICSAL
---------------------
AAA 2000
BBB 3000
CCC 4000

Avg Sal : 3000
program to demonstrate over|oad|ng of Ass|gnment operator *]
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
class Alpha

lnL daLa
publlc
Alpha()
Alpha(lnL d)

daLad

vold show()

couLn value daLa

Alpha operaLor (Alpha a)

daLaadaLa
couLnlnslde overloaded asslgnmenL operaLor
reLurn Alpha(daLa)


vold maln()

clrscr()
Alpha a1(30)
Alpha a2
a2a1
Alpha a3a2
a2show()
a3show()
geLch()

/* rogram for overloadlng lnserLlon LxLracLlon operaLor*/
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
class Complex

lnL real
floaL lmg
publlc
Complex()
Complex(lnL rfloaL l)

realr
lmgl

frlend osLream operaLor(osLream Complex c)
frlend lsLream operaLor(lsLream Complex c)

osLream operaLor(osLream sComplex c)

screal + clmgln
reLurn s

lsLream operaLor(lsLream sComplex c)

screalclmg
reLurn s

vold maln()

clrscr()
Complex c1(1323)c2(3343)c3
couLnc1 c1
couLnc2 c2
couLnc3 c3
geLch()

/*program for los funcLlon */
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
#lncludelomanlph
//#lncludelosh
consL lnL dlsL4
consL lnL monLhs3
vold maln()
clrscr()
lnL dm
lnL salesdlsLmonLhs
for(d0ddlsLd++)

for(m0mmonLhsm++)


couLn LnLer sales for for dlsLrlcL d+1
couL MonLh m+1 !
clnsalesdm


couLnn
couL MonLhs n
couL 1 seLw(10) 2 seLw(10) 3 seLw(10)
for(d0ddlsLd++)
couLn ulsLrlcL d+1
for(m0mmonLhsm++)

couLseLlosflags(losflxed)
couLseLlosflags(losshowpolnL)
couLseLpreclslon(2)
couLseLw(10)
couLsalesdm


couLnn

geLch()

]* program for Mu|t||eve| |nher|tance *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class SLudenL

publlc
lnL rno
char name20
vold geL()

couLLnLer 8oll no
clnrno
couLLnLer name
clnname


class Markspubllc SLudenL

publlc
lnL m1m2m3
vold geLmrks()

couLLnLer Marks of 3 sub[ecLs
clnm1m2m3

vold show()

couL8oll no rno
couL name name



class 8esulLpubllc Marks


publlc
floaL Lp
char *grade
vold cal()

Lm1+m2+m3
pL/3
lf(p73)
//couL nCrade ulsLlncLon
grade ulsLlncLon
else lf ((p60)(p73))
grade llrsL Class
else lf ((p30)(p60))
grade Second Class
else lf((p43)(p30))
grade ass class
else
grade lall

vold dlsp()

couL

couLnMarks ln sub[ecL1 m1
couLnMarks ln sub[ecL2 m2
couLnMarks ln sub[ecL3 m3
couLn1oLal L
couLnercenLage p
couLnCrade grade


vold maln()


8esulL r
clrscr()
rgeL()
rgeLmrks()

couLn 8esulL n
rcal()
rshow()
rdlsp()
geLch()



]* program for Mu|t|p|e |nher|tance *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class SLudenL

publlc
lnL rno
char name20
vold geL()

couLLnLer 8oll no
clnrno
couLLnLer name
clnname


class Marks

publlc
lnL m1m2m3
vold geLmrks()

couLLnLer Marks of 3 sub[ecLs
clnm1m2m3


class 8esulLpubllc SLudenLpubllc Marks

floaL Lp
char *grade
publlc
vold cal()

Lm1+m2+m3
pL/3
lf(p73)
//couL nCrade ulsLlncLon
grade ulsLlncLon
else lf ((p60)(p73))
grade llrsL Class
else lf ((p30)(p60))
grade Second Class
else lf((p43)(p30))
grade ass class
else
grade lall

vold dlsp()

couL
couLn 8esulL
couLnname of SLudenL name
couLn8oll no rno
couLnMarks ln sub[ecL1 m1
couLnMarks ln sub[ecL2 m2
couLnMarks ln sub[ecL3 m3
couLn1oLal L
couLn ercenLage p
couLn Crade grade


vold maln()

8esulL r
clrscr()
rgeL()
rgeLmrks()
rcal()
rdlsp()
geLch()

]* program to ca|cu|ate percentage of student *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class SLudenL

prlvaLe
lnL rno
char name20
lnL s1s2s3s4s3
publlc
vold show()
vold geL()

vold SLudenLgeL()

couL LnLer 8oll no and name
clnrnoname
couLLnLer marks of 3 sub[ecLs
clns1s2s3s4s3

vold SLudenLshow()

floaL Lp
Ls1+s2+s3+s4+s3
pL/6
couL SLudenL ueLalls endl
couLendl
couL8oll no rnoendl
couLname nameendl
couL1oLal Marks LendlercenLage pendl

vold maln()

SLudenL s1
clrscr()
s1geL()
s1show()
geLch()


]* program for stat|c member funct|on *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class Sample

prlvaLe
sLaLlc lnL c

publlc
Sample()

c++

sLaLlc vold show()

couLc cendl


lnL Samplec0//lnlLlallzlng sLaLlc varlable
vold maln()

Sample s1s2
clrscr()
s1show()
s2show()
//Sampleshow()
//Sampleshow()

geLch()


]* program for stat|c member member *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class Sample

prlvaLe
sLaLlc lnL l
lnL counL
publlc
Sample()

l++
counL++

vold show()

couLl lCounL counLendl


lnL Samplel0//lnlLlallzlng global varlable
vold maln()

Sample s1s2
s1show()
s2show()
clrscr()
geLch()


keturn Structure *]
#lncludesLdloh
#lncludeconloh
#lncludelosLreamh
sLrucL employee

lnL empld
//char *name
lnL salary

employee ee2
employee dlsp(lnLlnL)
vold maln()

lnL nosal
clrscr()
couLenLer Lmployee ldendl
clnno

couLLnLer salary
clnsal

e2dlsp(nosal)

couLLmployeelu e2empldn Salary e2salaryendl
geLch()

employee dlsp(lnL nolnL bs)

employee e1
e1empldno
//e1namenm
e1salarybs
reLurn(e1)


]* rogram for v|rtua| Iunct|on *]
#lncludelosLreamh
#lncludesLdloh
#lncludeconloh
class Shapes

publlc
vlrLual vold draw()0


class Llnepubllc Shapes

publlc
vold draw()

couLnlnslde llne


class Clrclepubllc Shapes

publlc
vold draw()

couLnlnslde Clrcle


class 8ecLpubllc Shapes

publlc
vold draw()

couLnlnslde 8ecL


vold maln()

Shapes *pL
Llne l1
pLl1
pLdraw()
Clrcle c1
pLc1
pLdraw()
8ecL r
pLr
pLdraw()
geLch()
















#include
class distance
{
private:
float inch;
int feet;
public:
distance()
{
feet = inch = 0;
}
distance(int ft, float in)
feet = inch = 0;
}
distance(int ft, float in)
{
feet = ft;
inch = in;
}
distance(float mtr) //converts float into distance
{
feet = int(mtr * 3.280833);
inch = 12 * ((mtr * 3.280833)-feet);
}
void getdist()
{
cout<<"Enter Feet : ";
cin>>feet;
cout<<"Enter Inches : ";
cin>>inch;
}
void dispdist()
{
cout<<"\nDistance = "<<METER<<" meters?;
cout<<"Feet : "<<FEET<<"\NINCH ?<<inch;


}

main
{
Distance d1;
d1=meter ; // calls conversion constructor
}

class distance
,
int feet;
float inch;


public:

distance(float p) //costructor to concert basic type to
class type
,

feet= (int) p;
inch=(p-feet)12;
,


void show()
,
cout<<"feet="<<feet;
cout<<"inch="<<inch;
,
,;

void main()
,
distance d1=1.75;

d1.show();
,

out put

feet=1;
inch=9;

Vous aimerez peut-être aussi