Anda di halaman 1dari 129

Title:Largestandsmallestnumber

Aim:Programtofindthelargestandsmallestnumber

Algorithm
1.Start
2.Printthenumberofterms
3.Readn
4.printtheelements
5.fori=0ton
6.reada[i]
7.fori=0ton
8.ifa[i]>l
l=a[i]
9.ifa[i]<s
s=a[i]
10.Endfor
11.Endfor
12.PrintthelargestelementI
13.Printthesmallestelements
14.Stop

Program

#include<iostream.h>
#include<conio.h>
voidmain()
{
inta[50],i,n,l=0,s=100;
clrscr();
cout<<"Enterthelimit:";
cin>>n;
cout<<"Entertheelements:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
if(a[i]>l)
{
l=a[i];
}
if(a[i]<s)
{
s=a[i];
}
}
cout<<"\nThelargestelementis:"<<l;
cout<<"\nThesmallestelemetis:"<<s;
getch();
}

Result:
Theprogramistestedandverified


Output
Enterthelimit:10
Entertheelements:20
35
12
4
0
85
77
6
28
100

Thelargestelementis:100
Thesmallestelementis:0


Title:Squareofanumber
Aim:Writeaprogramtofindsquareofanumber

Algorithm
1:start
2:readn
3:calculatesquare
4:printsquare
5:end

Program
#include<iostream.h>
#include<conio.h>
Voidmain()
{
intn,s;
clrscr();
cout<<Enterthenumber:;
cin>>n;
s=n*n;
cout<<square=:<<s;
getch();
}
Result:
Theprogramistestedandverified


Output
Enterthenumber:5
Square:25

Title:Factorialofanumber
Aim:Writeaprogramtofindfactorialofanumber
Algorithm
1.start
2.declarevariablesn,i,f
3.initializef=1
4.readn
5.initializei=1
6.ifi<=ngotostep6otherwisegotostep8
7.calculatef=f*i
8.i=i+1andgotostep5
9.displayf
10.stop

Program
#include<iostream.h>
#include<conio.h>
voidmain()
{
intn,i,f;
f=1;
clrscr();
cout<<"Enterthenumber:\n";
cin>>n;
for(i=1;i<=n;i++)
{
f=f*i;
}
cout<<"Thefactorial="<<f;
getch();
}
Result:
Theprogramistestedandverified

Output:
Enterthenumber:
5
Thefactorial=120


Title:Sumofdigitsofanumber
Aim:Programtofindsumofdigitsofanumber
Algorithm
1.
2.
3.
4.
5.
6.
7.

Start
Readn
InitializeI,s=0
if(n>0)gotonextstepelsegotostep7
d=n%10,s=s+r,n=n/10
prints
stop

Program
#include<stdio.h>
#include<conio.h>
voidmain()
{

intn,s=0,d;
clrscr();
printf("Enteranumber:");
scanf("%d",&n);
while(n>0)
{
d=n%10;
s=s+d;
n=n/10;
}
printf("Sum=%d",s);
getch();
}

Result:Theprogramistestedandverified

Output
Enteranumber:562
Sum=13

Title:Reverseofanumber
Aim:Programtofindreverseofanumber
Algorithm
1:start
2:initializer=0
3:readn
4:ifn>0gotostep5otherwisegotostep8
5.d=n%10
6.r=(r*10)+d
7.n=n/10Gotostep4
8:displayr
9:stop

Program
#include<stdio.h>
#include<conio.h>
voidmain()
{
intn,r=0,d;
clrscr();
printf("Enteranumber:");
scanf("%d",&n);
while(n>0)
{
d=n%10;
r=(r*10)+d;
n=n/10;
}
printf("Reverse=%d",r);
getch();
}
Result:Theprogramistestedandverified

Output
Enterthenumber:
254
Reverse=452

Title:Fibonacciseries
Aim:ProgramtogenerateFibonacciseries
Algorithm
1:start
2:initializes=0,f0=0,f1=1
3:readn
4:displayf0andf1
5:initializei=0
6:ifi<ngotostep7otherwisegotostep11
7:calculates=f0+f1
8:displays
9:setf0=f1andf1=s
10:=i+gotostep6
11:stop

Program
#include<stdio.h>
#include<conio.h>
voidmain()
{
intf0,f1,s=0,n,i;
clrscr();
f0=0;
f1=1;
printf("Enterthelimit:");
scanf("%d",&n);
printf("%d\n%d",f0,f1);
for(i=0;i<n;i++)
{
s=f0+f1;
printf("\n%d",s);
f0=f1;
f1=s;

getch();
}
Result:Theprogramistestedandverified

Output
Enterthelimit:7
0
1
1
2
3
5
8
13
21

Title:Primeornot
Aim:Programtoinputanumberandcheckitisprimeornot.
Algorithm
1:start
2:declarevariablesn,i,z
3:readn
4:initializei=2
5:ifi<ngotostep6
6:ifn%i=0gotostep7
7:setz=1
8:seti=i+1gotostep5
9:ifz=1gotostep10,otherwisegotostep11
10:Printthenumberisprime.Gotostep12
11:printNotprimenumber.gotostep12
12:stop

Program
#include<stdio.h>
#include<conio.h>
#include<process.h>
voidmain()
{
intn,i,z;
clrscr();
printf("Enterthenumber:");
scanf("%d",&n);
if(n==1)
exit(0);
i=2;
while(i<n)
{
if(n%i==0)
{
z=1;
}
i=i+1;
}
printf("Thenumberis..");
if(z==1)
{

printf("Notaprimenumber");
}
else
{
printf("....aprimenumber");
}
getch();
}

Result:
Theprogramistestedandverified

Output
Enterthenumber:5
Thenumberis......aprimenumber

Title:Factorialofanumber
Aim:Programtofindfactorialofanumberusingclassconcept.
Algorithm
1.Classdefenition
1.1:declaref,i,n
1.2:publicmemberfunctionvoidgetdata()
1.3:publicmemberfunctionvoidshow()
1.4:endofclassdefinition
2:voidgetdata()function
2.1:acceptn
2.2:initializef=1andcalculatef=f*i
3:voidshow()function
3.1:displayf
4:main()function
4.1:createtheobjectofclass
4.2:callvoidgetdata()function
4.3:callvoidshow()function
4.4:end

Program
#include<iostream.h>
#include<conio.h>
classfact
{
public:
intf,i,n;
voidgetdata();
voidshow();
};
voidfact::getdata()
{
cout<<"Enterthenumber:";
cin>>n;
f=1;
for(i=1;i<=n;i++)

f=f*i;

}
voidfact::show()
{
cout<<"Factorial="<<f;
}

voidmain()
{
factd;
clrscr();
d.getdata();
d.show();
getch();
}

Result:Theprogramistestedandverified

Output
Enterthenumber:4
Factorial=24

Title:Primenumbers
Aim:Classprogramtogenerateprimenumbers.
Algorithm
1:classdefinition
1.1:declaren,p,I,j,f,c;
1.2:publicmemberfunctionvoidgenerate()
1.3:endofclassdefinition
2:voidgetdata()function
2.1:initializec=0
2.2:acceptnandinitializei=2
2.3::ifi<=ngotostep2.5
2.4initializef=0
2.5:initializej=2
2.6:ifj<igotostep2.7
2.7:ifi%j=0gotostep2.8
2.8:f=f+1gotostep2.9
2.9:iff=0,c=c+1andprintc
3:main()function
3.1:createtheobjectoftheclass
3.2:callgenerate()function
3.3:end

Program
#include<iostream.h>
#include<conio.h>
classprime
{
public:
intn,p,i,j,f,c;
voidgenerate();
};
voidprime::generate()
{
c=0;
cout<<"Enterthelimitofnumbers:";
cin>>n;
clrscr();
cout<<"\t\tPrimeNumbers\n";
cout<<"\t\t........................\n";
cout<<"\n\tSl.No\tPrimeNumber\t\t\n";
cout<<"\n\t.......\t...............\n";
for(i=2;i<=n;i++)
{
f=0;
for(j=2;j<i;j++)
{

if(i%j==0)
{f++;
}
}
if(f==0)
{
c++;
cout<<"\n\t"<<c<<"\t\t"<<i;
}
}
}
voidmain()
{
primec;
clrscr();
c.generate();
getch();
}

Result:Theprogramistestedandverified

Output
Enterthelimitofnumbers:10
PrimeNumbers
........................
Sl.No:PrimeNumber
........................
12
23
35
47


Title:Banktransactions
Aim:Writeaprogramforbanktransactions.
Algorithm
1:classdefinition
1.1:declarevariablesdpt,wdrw,bal,temp,name[20],type[10]
1.2:publicmemberfunctionvoidinitial()
1.3:publicmemberfunctionvoiddeposit()
1.4:publicmemberfunctionvoidwithdraw()
1.5:publicmemberfunctionvoiddisplay()
1.6:endofclassdefinition
2:voidinitial()function
2.1:acceptname
2.2:acceptaccounttype
2.3:setbalance=fxd
3:voiddeposit()function
3.1:acceptdepositamount
3.2:setbal=fxd+dpt
3.3:displaycurrentbalance
4:voidwithdraw()function
4.1:settemp=0
4.2:acceptwithdrawamount
4.3:if((wdrw<bal)&&(wdrw>500))gotostep4.4
Otherwisegotostep4.5

4.4:settemp=balwdrw;
setbal=temp;
4.5:displaycurrentbalance
5:voiddisplay()function
5.1:displaynameandbalance
6:main()function
6.1:ifchoice=1,acceptaccountnumberandcallinitial()function
6.2:ifchoice=1,acceptaccountnumberandcalldeposit()function
6.3:ifchoice=1,acceptaccountnumberandcallwithdraw()function
6.4:ifchoice=1,acceptaccountnumberandcalldisplay()function
6.5:ifchoice=5,end

Program
//Banking
#include<iostream.h>

#include<conio.h>
intfxd=500;
classbank
{
private:
charname[20],type[10];
intdpt,wdrw,bal,temp;
public:
voidinitial();
voiddeposit();
voidwithdraw();
voiddisplay();
}m[10];

voidbank::initial()
{
cout<<"\nEnteryourname:";
cin>>name;
cout<<"\nEnterthetypeofaccound:";
cin>>type;
bal=fxd;
}

voidbank::deposit()

{
cout<<"\nEntertheamountfordeposit:";
cin>>dpt;
bal=fxd+dpt;
cout<<"\nYourcurrentbalanceisRs:"<<bal<<"/";
}

voidbank::withdraw()
{
temp=0;
cout<<"\nEntertheamoundforwithdraw:";
cin>>wdrw;

if((wdrw<bal)&&(wdrw>500))
{
temp=balwdrw;
bal=temp;
}
else
cout<<"\nSorry...!Youraccoundbalanceis"<<bal<<"only";
}

voidbank::display()
{

cout<<"\nName:"<<name;
cout<<"\nCurrentbalance:"<<bal;
}

voidmain()
{
inti,n;
clrscr();
do
{
cout<<"\n1:Createanaccount";
cout<<"\n2:Deposit";
cout<<"\n3:Withdraw";
cout<<"\n4:Balance";
cout<<"\n5:Exit";
cout<<"\n\n\n\t\t\tEnteryourchoice";
cout<<"\n\n\n";
cin>>n;
switch(n)
{
case1:
cout<<"Entertheaccountnumber(b/w1&10):";
cin>>i;
m[i].initial();

break;
case2:
cout<<"Enteryouraccountnumber:";
cin>>i;
m[i].deposit();
break;
case3:
cout<<"Enteruouraccountnumber:";
cin>>i;
m[i].withdraw();
break;
case4:
cout<<"Enteruouraccountnumber:";
cin>>i;
m[i].display();
break;
case5:
voidexit();
break;
default:
cout<<"wrongcase";
break;
}
}while(n<5);

getch();
}

Result:
Theprogramistestedandverified

Output

1:Createanaccount
2:Deposit
3:Withdraw
4:Balance
5:Exit

Enteryourchoice

Entertheaccountnumber(b/w1&10):1

Enteryourname:ARUN

Enterthetypeofaccound:Simple

1:Createanaccount
2:Deposit
3:Withdraw
4:Balance
5:Exit

Enteryourchoice

2
Enteryouraccountnumber:1

Entertheamountfordeposit:500

YourcurrentbalanceisRs:1000/

Title:Marklist
Aim:Writeaprogramtodisplaymarklistofstudents
Algorithm
1:classdefinition
1.1:privatedatamembersname,grade,regno
m1,m2,m3,m4,m5,tot,per
1.2:publicmemberfunctionvoidgetdata()
1.3:publicmemberfunctionvoidcalc()
1.4:publicmemberfunctionvoiddisplay()
2:voidgetdata()function
2.1:acceptname
2.2:acceptregno
2.3:acceptm1,m2,m3,m4,m5
3:voidcalc()function
3.1:calculatetotal
3.2:calculatepercentage

3.3:ifpercentage>=80,otherwisegotostep3.4
Setgrade=A
3.4:ifper>=60andper<80,otherwisegotostep3.5
Setgrade=B
3.5:ifper>=50andper<60,otherwisegotostep3.6
Setgrade=C
3.6:ifper>=40andper<50,otherwisegotostep3.7
Setgrade=D
3.7:Setgrade=E
4:voiddisplay()function
4.1:displayname,registernumber
4.2:displaym1,m2,m3,m4,m5
4.3:displaytotal,percentage,grade
5:main()function
5.1:initializei=0
5.2:ifi<ngotostep5.3
5.3:calltogetdata()function
5.4:i=i+1,gotostep5.2
5.5:initializei=0
5.6:ifi<ngotostep5.7
5.7:calltocalc()function
Calltodisplay()function
5.8:i=i+1,gotostep5.6
5.9:end

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

private:

charname[20],grade;

intregno,m1,m2,m3,m4,m5,tot;

floatper;

public:

voidgetdata();

voidcalc();

voiddisplay();

}s[40];

voidstudents::getdata()

cout<<"\n\nEnterthenameofstudent:";

cin>>name;

cout<<"\nEntertheregisternumber:";

cin>>regno;

cout<<"\nEnterthemarkof5subjects:";

cin>>m1>>m2>>m3>>m4>>m5;

voidstudents::calc()

tot=m1+m2+m3+m4+m5;

per=tot/5;

if(per>=80)

grade='A';

elseif((per>=60)&&(per<80))

grade='B';

elseif((per>=50)&&(per<60))

grade='C';

elseif((per>=40)&&(per<50))

grade='D';

else

grade='E';

voidstudents::display()

cout<<"\n\n"<<name<<"\t"<<regno<<"\t"<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<m4
<<"\t"<<m5<<"\t"<<tot<<"\t"<<per<<"\t"<<grade<<"\n";

voidmain()

intn,i,j;

clrscr();

cout<<"\t\t................DATAINPUT.................\n";

cout<<"\n\nEnterthenumberofstudents:";

cin>>n;

for(i=0;i<n;i++)

s[i].getdata();

clrscr();

cout<<"\t\t..........STUDENTSDETAILS...........\n\n";

cout<<"\n\nREG.NO:\tNAME\tSUB1\tSUB2\tSUB3\tSUB4\tSUB5\tTOTAL\tPER:(%)\
tGRADE\n";

cout<<".................................................................................\n";

for(i=0;i<n;i++)

s[i].calc();

s[i].display();

getch();

Result:
Theprogramistestedandverified

Output
................DATAINPUT.................
Enterthenumberofstudents:2
Enterthenameofstudent:ANJU
Entertheregisternumber:17524
Enterthemarkof5subjects:98

75
88
92
88
Enterthenameofstudent:ANU
Entertheregisternumber:17379
Enterthemarkof5subjects:80
52
63
54
90
..........STUDENTSDETAILS...........
REG.NO:NAMESUB1SUB2SUB3SUB4SUB5TOTALPER:(%)GRADE
............................................................................................................................
17524ANJU987588928844188A
17379ANU805263549033967B
Title:Copyconstructor
Aim:Writeaprogramtoimplementcopyconstructor

Algorithm
1:classdefinition
1.1:privatedatamemberid
1.2:constructormemberfunctionsdeclarationanddefinition
1.3:copyconstructormemberfunctiondefinition

1.4:publicmemberfunctionvoiddisplay(void)
1.5:endofclassdefinition
2:mainfunction()
2.1:objectAiscreatedandinitialized
2.2:copyconstructorcalled
2.3:copyconstructorcalledagain
2.4:Discreated,notinitialized
2.5:D=A
2.6:callthefunctions
A.display()
B.display()
C.display()
D.display()

Program
#include<iostream.h>
#include<conio.h>
classcode
{
intid;
public:
code(){}//constructor
code(inta){id=a;}//constructoragain
code(code&x)//copyconstructor

{
id=x.id;//copyinthevalue
}
voiddisplay(void)
{
cout<<id;
}
};
intmain()
{
codeA(100);
codeB(A);
codeC=A;
codeD;
clrscr();
D=A;
cout<<"\nidofA:";A.display();
cout<<"\nidofB:";B.display();
cout<<"\nidofC:";C.display();
cout<<"\nidofD:";D.display();
getch();
return0;
}

Result:
Theprogramistestedandverified

Output

idofA:100
idofB:100
idofC:100
idofD:100

Title:Functionoverloading
Aim:Writeaprogramtoimplementfunctionoverloading.
Algorithm
1:start
main()function
2:passvolume(10)tointvolume(ints)functionanddisplayvolume
3:passvolume(2.5,8)todoublevolume(doubler,inth)functionanddisplay
volume
4:passvolume(100l,75,15)tolongvolume(longl,intb,inth)functionand
displayvolume
5:intvolume(ints)function
return(s*s*s)
6:doublevolume(doubler,inth)
return(3.14519*r*r*h)
7:longvolume(longl,intb,inth)

return(l*b*h)
8:stop

Program
#include<iostream.h>
#include<conio.h>
classarea
{
public:
intl,b,a,r,m,n;
floatpi,result;
voidfindarea(float,int);
voidfindarea(int,int);
voidfindarea(int);
};
voidarea::findarea(floatp,intx)
{
x=p;
r=x;
a=x*r*r;
cout<<"Areaofcircle="<<a;

}
voidarea::findarea(intl,intb)
{
m=l;
n=b;
a=m*n;
cout<<"Areaofrectangle="<<a;
}
voidarea::findarea(ints)
{
a=s;
a=a*a;
cout<<"Areaofsquare="<<a;
}
voidmain()
{
areag;
clrscr();
g.findarea(3.14,5);
g.findarea(5,4);
g.findarea(6);
getch();
}

Result:
Theprogramistestedandverified

Output
Areaofcube:1000
Areaofcylinder:157.2595
Areaofrectangularbox:112500


Title:Inlinefunction
Aim:Writeaprogramtoimplementinlinefunctions.

Algorithm

1:classdefinition
1.1:privatedatamemberr
1.2:publicmemberfunctionvoidgetdata(inta)
1.3:publicmemberfunctionfloatarea()
1.4:publicmemberfunctionfloatvol()
1.5:publicmemberfunctionvoiddisp()
1.6:endofclassdefinition
2:definitionofgetdata(inta)functionasinline
3:definitionofarea()functionasinline
4:definitionofvol()functionasinline
5:definitionofdisp()functionasinline
6:main()function
6.1:createobjectoftheclass
6.2:callthefunctiongetdata(a)
6.3:callthefunctiondisp()
6.4:end
Program
#include<iostream.h>

#include<conio.h>
classcircle
{
intr;
public:
voidgetdata(inta);
floatarea();
floatvol();
voiddisp();
};
inlinevoidcircle::getdata(inta)
{
r=a;
}
inlinefloatcircle::area()
{
return3.14*r*r;
}
inlinefloatcircle::vol()
{
returnarea()*r;
}
inlinevoidcircle::disp()
{

cout<<"\n\n\tTheareais............\n\n\t";
cout<<area();
cout<<"\n\n\tThevolumeis............\n\n\t";
cout<<vol();
}
voidmain()
{
clrscr();
inta;
circled;
cout<<"\n\n\tTheradiusofcircleis.........\n\n\t";
cin>>a;
d.getdata(a);
d.disp();
getch();
}

Result:Theprogramistestedandverified
Output
Theradiusofcircleis.........

5
Theareais............

78.5


Thevolumeis............

392.5

Title:Defaultconstructor
Aim:Writeprogramusingdefaultconstructor

Algorithmforclass

1.Start
2.declaretheclasssum
3.Definingconstructorsum()ass=0
4.Definingthefunctionfindsum()
5.displayvalueofaandb
6.Displaythesumc
7.Stop

Algorithmformain

1.Start
2.Createtheobjectofclasssum
3.Callthefunctionfindsum()withobjecta
4.Stop

Program

//Programofdefaultconstructor

#include<iostream.h>
#include<conio.h>
classsum
{
public:

inta,b,s;
sum()
{
s=0;
}
voidfindsum()
{
cout<<"Entertwonumber\n";
cin>>a>>b;
s=a+b;
cout<<"Sum="<<s;
}
};
voidmain()
{
suma;
clrscr();
a.findsum();
getch();
}

Result:
Theprogramistestedandverified

OUTPUT
Entertwonumber
2
3
Sum=5

Title:Parameterizedconstructor
Aim:Writeprogramusingparamerterizedconstructor

Algorithmforclass

1.Start
2.declaretheclassfactorialwithmembern,I,f
andfunctionfindfact()
3.Definingconstructorwith1argumentandassignit
tox
4.Defingfunctionfindfact()andfinthefactorial
withforloop
5.Stop

Algorithmformain

1.Start
2.Callingconstructorwith1argument
3.displaythevalue
4.Stop

Program

#include<iostream.h>
#include<conio.h>
classfactorial
{
public:
intn,i,f;
factorial(int);

voidfindfact();
};
factorial::factorial(intx)
{
n=x;
}
voidfactorial::findfact()
{
f=1;
for(i=n;i>0;i)
{
f=f*i;
}
cout<<"Factorial="<<f;
}
voidmain()
{
factorialf(5);
clrscr();
f.findfact();
getch();
}

Result:
Theprogramistestedandverified

OUTPUT

Factorial=120

Title:Singleinheritance
Aim:Writeprogramusingsingleinheritance

Algorithmforclass

1.Start
2.Createclassstaffwithdatamembersas
no,bp,ta,da,hra,pf,name,designandfunctioncalculation()
3.Createclassemployeasderivedclassofstaffwithfunction
getdata()forreadinginformationanddisplay()forprintingdata
4.stop

Algorithmformainfunction

1.Start
2.Createobjectofclassemploye
3.Callfunctiongetdata()withobjecte
4.Callfunctiondisplay()withobjecte
5.stop

Program

/*Singleinheritance*/

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

classstaff
{
public:
intbp,ta,da,hra,pf,no;
charname[20],desig[20];
voidgetdata()
{
cout<<"Entertherollnumberofemploye:";
cin>>no;
cout<<"Enterthenameoftheemploye:";
cin>>name;
cout<<"Enterthedesignationoftheemploye:";
cin>>desig;
cout<<"Enterbasicpay:";
cin>>bp;
cout<<"Entertheta:";
cin>>ta;
cout<<"Entertheda:";
cin>>da;
cout<<"Enterthehra:";
cin>>hra;
cout<<"Enterthepf:";
cin>>pf;
}
};
classemploye:publicstaff
{
public:
inttotal,net;
voidcalculation()
{
total=bp+ta+da+hra;
net=totalpf;
}
voiddispaly()
{


cout<<no<<"\t"<<name<<"\t"<<desig<<"\t"<<bp<<"\t"<<ta<<"\t"<<da<<"\t"<<hra
<<"\t"<<pf<<"\t"<<total<<"\t"<<net<<"\n";
}
};
voidmain()
{
employee;
clrscr();
e.getdata();
e.calculation();
cout<<"no\tname\tdesi\tbp\tta\tda\thra\tpf\total\tent\n";
cout<<"\n";
e.dispaly();
getch();
}

Result:
Theprogramistestedandverified

OUTPUT
Entertherollnumberofemploye:1
Enterthenameoftheemploye:Suresh
Enterthedesignationoftheemploye:Kollam
Enterbasicpay:5000
Entertheta:0
Entertheda:510
Enterthehra:310
Enterthepf:1500
nonamedesibptadahrapftotalnet

1SureshKola500005103101500
totalnet

58204320

Title:Multilevelinheritance
Aim:Writeprogramusingmultilevelinheritance

Algorithmforclass

1.Start
2.createclassfirstwithdatamemberid,nameand
functiongetstudent()
3.Createclasssecondwithdatamembers,cnameand
functiongetcourse()
4.Createclassthirdwithdatamembertms1,s2,s3
andfunctiongetresult()andgetdetails()
5.Stop

Algorithmformainfunction

1.Start
2.createobjectrforclassthird
3.Callfunctiongetstudent()withobjectr
4.Callfunctiongetcourse()withobjectr
5.callfunctiongetresultandgetdetailswithobject
r
6.Stop

Program

#include<iostream.h>
#include<conio.h>
classstudent
{
public:
intid,total;
charname[10];
voidgetstudent()
{
cout<<"Enterthestudentid:";
cin>>id;
cout<<"Enterthenameofthestudent:";
cin>>name;
}
};
classcourse:publicstudent
{
public:
ints;
charcname[20];
voidgetcourse()
{
cout<<"Enterthesemester:";
cin>>s;
cout<<"Enterthenameofthecourse:";
cin>>cname;
}
};
classresult:publiccourse
{
public:
inttm,s1,s2,s3;
voidgetresult()
{

cout<<"Enterthemarkofs1:";
cin>>s1;
cout<<"Enterthemarkofs2:";
cin>>s2;
cout<<"Enterthemarkofs3:";
cin>>s3;
tm=s1+s2+s3;
}
voidgetdetails()
{
cout<<id<<"\t"<<name<<"\t"<<s<<"\t"<<cname<<"\t"<<tm<<"\n";
}

};
voidmain()
{
resultr;
clrscr();
r.getstudent();
r.getcourse();
r.getresult();
cout<<"\t\tSTUDENTDETAILS\n";
cout<<"\t\t\n";
cout<<"ID\tname\tsemester\tcoursename\ttotalmark\n";
cout<<"\n";
r.getdetails();
getch();
}

Result:
Theprogramistestedandverified

Output
Enterthestudentid:1
Enterthenameofthestudent:Asha
Enterthesemester:1
Enterthenameofthecourse:Bsccomputerscience
Enterthemarkofs1:85
Enterthemarkofs2:95
Enterthemarkofs3:99
STUDENTDETAILS

IDnamesemestercoursenametotal

1Asha1Bsc279


Title:Multipleinheritance
Aim:Writeprogramusingmultipleinheritance

Algorithmforclass

1.start
2.CreateclassAwithdatamemberaandfunction
geta()
3.CreateclassBwithdatamemberbandfunction
getb()
4.CreateclassCasderivedclassofAandBwith
membercandfunctiongetresult()
5.Stop

Algorithmformain

1.Start
2.CreateobjectofCclass
3.Callfunctiongeta()andgetb()withobjectd
4.Stop

Program

#include<iostream.h>
#include<conio.h>
classA
{
public:
inta;
voidgeta()
{
cout<<"Enterthevalueofa:";
cin>>a;
}
};
classB
{
public:
intb;
voidgetb()
{
cout<<"Enterthevalueofb:";
cin>>b;
}
};
classC:publicA,publicB
{
public:
intc;
voidgetresult()
{
geta();

getb();
c=a+b;
cout<<"Thevalueofc="<<c;
}
};
voidmain()
{
Cd;
clrscr();
d.getresult();
getch();
}

Result:
Theprogramistestedandverified

Output
Enterthevalueofa:2
Enterthevalueofb:5
Thevalueofc=7

Title:Heirarchialinheritance
Aim:WriteprogramusingHerirarchialinheritance

Algorithmforclass

1.Start
2.createclassAwithmemberaandfunction
geta()
3.CreateclassBasderivedclassofAwith
memberbandfunctiongetb()
4.CreateclassCasderivedclassofAwith
membercandfunctiongetc()
5.Stop

Algorithmformain

1.Start
2.CreateobjectforclassA
3.CreateobjectforclassB
4.Callfunctiongeta(),getb()andgetsum()withobject
p
5.Callfunctiongeta(),getb()andgetmul()with
objectq
6.Stop

Program

#include<iostream.h>
#include<conio.h>
classA
{
public:
inta;
voidgeta()
{
cout<<"Enterthevalueofa:";
cin>>a;
}
};
classB:publicA
{
public:
intb,c;
voidgetb()
{
cout<<"Enterthevalueofb:";
cin>>b;
}
voidgetsum()
{
c=a+b;
cout<<"Sum="<<c;
}

};
classC:publicA
{
public:
intd,b,c;
voidgetd()
{
cout<<"\nEnterthevalueofd:";
cin>>d;
}
voidgetmul()
{
c=a*d;
cout<<"Product="<<c;
}
};
voidmain()
{
Bp;
Cq;
clrscr();
p.geta();
p.getb();
p.getsum();
q.geta();
q.getd();
q.getmul();
getch();
}

Result:
Theprogramistestedandverified

Output
Enterthevalueofa:2
Enterthevalueofb:5
Sum=7Enterthevalueofa:6

Enterthevalueofd:7
Product=42

Title:hybridinheritance
Aim:Writeprogramusinghybridinheritance

Algorithmforclass

1.Start
2.Createclassplayerwithmembername,gender,age
3.Createclasstestasderivedclassofplayer
withmemberheight,weight
4.Createclasssportswithmembersnameandfunction
getitem()
5.Createclassresultasderivedclassoftestand
playerwithfunctiondisplay()
6.Stop

Algorithmformain

1.Start
2.Createobjectofclassresult
3.Callfunctiongetitem()andgetitem()and
display()withobjectr
4.stop

Program

#include<iostream.h>
#include<conio.h>
classstudent
{
public:
intno;
charname[10],desig[10];
voidgetstudent()
{
cout<<"Entertherollnumberofthestudent:";
cin>>no;
cout<<"Enterthenameofthestudent:";
cin>>name;
cout<<"Enterthedesignationofthestudent:";
cin>>desig;
}
};
classtest:publicstudent
{
public:
intm1,m2,m3,total;
voidgettest()

{
cout<<"Enterthemarkofenglish:";
cin>>m1;
cout<<"Enterthemarkofmathematics:";
cin>>m2;
cout<<"Enterthemarkofscience:";
cin>>m3;
total=m1+m2+m3;
}
};
classsports
{
public:
charsname[20];
voidgetitem()
{
cout<<"Enteryourfavouraitesportsitem:";
cin>>sname;
}
};
classresult:publictest,publicsports
{
public:
voiddisplay()
{
cout<<"\t\tstudentdetails\n";
cout<<"\t\t\n";
cout<<"\t\tNumber:"<<no<<"\n";
cout<<"\t\tName:"<<name<<"\n";
cout<<"\t\tDesignation:"<<desig<<"\n";
cout<<"\t\tMarkofEnglish:"<<m1<<"\n";
cout<<"\t\tMarkofMathematics:"<<m2<<"\n";
cout<<"\t\tMarkofscience:"<<m3<<"\n";
cout<<"\t\total="<<total<<"\n";
cout<<"\t\tfavouraitesportsitem:"<<sname<<"\n";
}

};
voidmain()
{
resultr;
clrscr();
r.getstudent();
r.gettest();
r.getitem();
r.display();
getch();
}

Result:
Theprogramistestedandverified

Output

Entertherollnumberofthestudent:1
Enterthenameofthestudent:Manu
Enterthedesignationofthestudent:Ayoor
Enterthemarkofenglish:85
Enterthemarkofmathematics:60
Enterthemarkofscience:73
Enteryourfavouraitesportsitem:Cricket
studentdetails

Number:1
Name:Manu
Designation:Ayoor
MarkofEnglish:85
MarkofMathematics:60
Markofscience:73
total=218
favouraitesportsitem:Cricket

Title:Operatoroverloading
Aim:Writeprogramusingoperatoroverloading

Algorithmforclass

1.start
2.Createclassdemowithmemberx,y,z
3.Definefunctiongetdata()with3argument
4.definefunctionoperator()anddisplay()
5.stop


Algorithmformain

1.Start
2.Createobjectforclassdemo
3.CallFunctionwithobjectd
4.Stop

Program

#include<iostream.h>
#include<conio.h>
classdemo
{
public:
intx,y,z;
voidgetdata(inta,intb,intc);
voidoperator();
voiddisplay();
};
voiddemo::getdata(inta,intb,intc)
{
x=a;
y=b;
z=c;
}
voiddemo::operator()
{

x=x;
y=y;
z=z;
}
voiddemo::display()
{
cout<<"x="<<x<<"\n";
cout<<"y="<<y<<"\n";
cout<<"z="<<z<<"\n";
}
voidmain()
{
demod;
clrscr();
d.getdata(10,20,30);
d;
d.display();
getch();
}

Result:
Theprogramistestedandverified

Output
x=10
y=20
z=30

Title:Friendfunction
Aim:Writeprogramusingfriendfunction

AlgorithmforClass

1.Start

2.Createclassmultiplywithdatamembersa,band
functiongetvalue()
3.Declareafriendfunctionproduct
4.definefriendfunctionandprinttheproduct
5.Stop

Algorithmformain

1.Start
2.Createobjectd
3.Callfunctiongetvalue()withobjectd
4.Callfriendfunctionproduct
5.Stop

Program

#include<iostream.h>
#include<conio.h>
classmultiply
{
inta,b;
public:
voidgetvalues()
{
cout<<"Entertwonumber:";
cin>>a>>b;
}
friendintproduct(multiplym)

};
intproduct(multiplym)
{
intc;
c=m.a*m.b;
return(c);
}
voidmain()
{
multiplyd;
clrscr();
d.getvalues();
cout<<"Productoftwonumbers="<<product(d);
getch();
}

Result:
Theprogramistestedandverified

Output
Entertwonumber:52
Productoftwonumbers=10

Title:Purevirtualfunction
Aim:Writeprogramusingpurevirtualfunction

Algorithmforclass


1.Start
2.Createclassfirstwithmemberb
3.Definethefunctionfirst()asb=10
4.Definetheclassdisplayasvirtualandprintb
5.Createclasssecondasderivedclassoffirst
withdatamemberdandfunctionsecond()
5.Definethefunctiondisplay()printthevalueof
d
6.Stop

Algorithmformain

1.Start
2.Createobjectsofsecond
3.createobject*poffirst
4.callfunctiondisplay()withp
5.Stop

Program

#include<iostream.h>
#include<conio.h>
classfirst
{
public:
intb;
first()
{
b=10;
}
virtualvoiddispaly()
{

cout<<"Valueofb="<<b;
}
};
classsecond:publicfirst
{
public:
intd;
second()
{
d=20;
}
voiddisplay()
{
cout<<"Valueofd="<<d;
}
};
voidmain()
{
first*p,f;
p=&f;
p>display();
seconds;
p=&s;
s>display();
getch();
}

Result:
Theprogramistestedandverified

Output
Valueofb=10
Valueofd=20

Title:Functiontemplate
Aim:Writeprogramusingfunctiontemplate

Algorithmfortemplatefunction

1.Start
2.Definefunctiontemplatesumtoaddtwoelements
3.Stop


Algorithmformainfunction

1.Start
2.setx,yandhx,fy
3.z=callfunctionsumwithargumentasxandy
4.fz=callfunctionsumwithargumentasfxandfy
5.printzandfz
6.Stop

Program

#include<iostream.h>
#include<conio.h>
template<classT>
Taddition(Ta,Tb)

{
Ts;
s=a+b;
return(s);
}
voidmain()
{
intx,y,z;
floatfx,fy,fz;
clrscr();
cout<<"Entertwointegernumber:";
cin>>x>>y;
z=addition(x,y);
cout<<"Sum="<<z;
cout<<"Entertworealnumber:";
cin>>fx>>fy;
fz=addition(fx,fy);
cout<<"Sum="<<fz;
getch();
}

Result:
Theprogramistestedandverified

Output

Entertwointegernumber:23
Sum=5
Entertworealnumber:3.52.5
Sum=6

Title:Classtemplate
Aim:Writeprogramusingclasstemplate

Algorithmforclass

1.Start

2.Createclasstemplatewithdatamemberspointervariablev
3.Createconstructorfunctionwithnoarguments
4.Createconstructorfunctionwith*aasargument
5.Oerloadoperator*
6.Stop

Algorithmformainfunction

1.Start
2.Createtwoarraysxandywiththreeelement
3.Createtemplatevariables
4.v1=xandv2=y
5.Dooperationbetweenv1andv2withoverloadoperator*and
assigntheresulttor
6.Printr
7.Stop

Program

#include<iostream.h>
#include<conio.h>
constsize=3;

template<classT>
classvector
{
T*v;
public:
vector()
{
v=newT[size];
for(inti=0;i<size;i++)
{

v[i]=0;
}
}
vector(T*a)
{
for(inti=0;i<size;i++)
{

v[i]=a[i];
}
}
Toperator*(vector&y)
{
Tsum=0;
for(inti=0;i<size;i++)
{

sum+=this>v[i]*y.v[i];
}
return(sum);
}
};
intmain()
{
intx[3]={1,2,3};
inty[3]={1,2,3};
vector<int>v1;
vector<int>v2;

v1=x;
v2=y;
intr=v1*v2;
cout<<"\nR="<<r;
return0;
}

Result:
Theprogramistestedandvarified

Output
R=32

Title:Exceptionhandling
Aim:WriteprogramusingExceptionhanling

Algorithmforfunction

1.Start
2.Definefunctiontest()
3.Tryblock
ifx==1
Throwx
ifx=0
Throwx
ifx=1

Throw1.0
4.Catchblock

ifxisinteger

printCaughtinteger

Ifxischaracter

printCaughtcharacter

Ifxisdouble

printCaughtdouble
5.Stop

Algorithmformainfunction

1.Start

2.callfunctiontest()withargumentas1
3.Callfunctiontest()withargumentas0
4.Callfunctiontest()withargumentas1
5.Callfunctiontest()withargumentas2
6.Stop

Program

#include<iostream.h>
#include<conio.h>
voidtest(intx)
{
try
{
if(x==1)
throwx;
elseif(x==0)
throw'x';
elseif(x==1)
throw1.0;
}
catch(charc)
{
cout<<"\nCoughtacharactor";
}
catch(intm)
{
cout<<"\nCoughtaninteger";

}
catch(doubled)
{
cout<<"\nCoughtadouble";
}
}
intmain()
{
clrscr();
cout<<"\nm=1;
test(10;
cout<<"\nm=0";
test(0);
cout<<"\nm=1;
test(2);
cout<<"\nm=2;
test(2);
return0;
}

Result:
Theprogramistestedandvarified

Output

m=1
caughtaninteger
m=0
caughtachracter
m=1
caughtadouble
m=2

Title:File
Aim:Writeprogramusingfile

ALGORITHMFORCLASS:

1.Start

2.Createclasstelewithdatamembersfnm[23],dno[20],stm[10],city[20]and
pnoandwithmemberfunctionget()forreadingandshowwall()forprinting
data
3.Stop
Algorithmformainfunction:

1.Start
2.Createobjecttforclasstele
3.Createobjectfforfstream
4.Assigncharacterch==y
5.openfileaddress.datusingobjectfininputmode
6.Ifch==y
Callfunctionget()usingt

Writefetcheddatatofile
Readvalueofch
Repeatstep6
7.Seekfilepointertobegningoffile
8.callfunctionshowwall()usingt
9.Closefileaddress.dat
10.Stop

Program

/*Writeandreadoperationusingfile*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<string.h>
classtele

{
charfnm[20];
chardno[10],stnm[20];
charroad[20],city[10];
longpno;
public:
voidget();
voidshowwall();
};
voidtele::get()
{
cout<<"\nEnterfullname:";
gets(fnm);
cout<<"\nEnterdoorNo:";
cin>>dno;
cout<<"\nEnterstreetname:";
gets(stnm);
cout<<"\nEnterroadname:";
gets(road);
cout<<"\nEntercity:";
gets(city);
cout<<"EnterphoneNo:";
cin>>pno;
}
voidtele::showwall()
{
cout<<"\nFullName:"<<fnm;
cout<<"\nDorrNo:"<<dno;
cout<<"\nStreetName:"<<stnm;
cout<<"\nRoadName:"<<road;
cout<<"\nCity:"<<city;
cout<<"\nPhoneNo:"<<pno;
}
voidmain()
{
clrscr();

telet;
fstreamf;
charch='y';
intc=0;
f.open("address,dat",ios::in);
while(ch=='y')
{
t.get();
f.write((char*)&t,sizeof(t));
cout<<"\nDoyouwanttocontinue?(y/n)";
cin>>ch;
}
f.seekg(0);
while(f.read((char*)&t,sizeof(t)));
{
cout<<"\n\n"<<++c<<"Information";
t.showwall();
}
f.close();
getch();
}

Result:
Theprogramistestedandvarified

Output

Enterfullname:Ashasuresh

EnterdoorNo:20

Enterstreetname:SPGcomplex

Enterroadname:Dwarka

Entercity:Delhi
EnterphoneNo:065897231

Doyouwanttocontinue?(y/n)n

Information
FullName:Ashasuresh
DorrNo:20
StreetName:SPGcomplex
RoadName:Dwarka
City:Delhi
PhoneNo:53


Title:FileAccesing
Aim:Writeprogramusingfile

Algorithmforclass

1.Start
2.Createclassbookwithmemberbnm[20],author[30],peges,priceand
memberfunctionasget()forreadingdataandlist()forprintingdata
3.Stop

Algorithmformain

1.Start
2.Createarrayofobjectb[3]ofbooks
3.Createobjectkofbook
4.Createobjectf1forfstream
5.Openfilebook.dataininputmode
6.fori=0to2
7.Callfunctionget()withb[i]
8.Writwdatareadbyb[i]tofile
9.Endfori
10.Seekfilepointerf1tobeginningoffile
11.fori=0to2
12.Readdatafromfile
13.Callfunctionlist()withb[i]
14.Endfori
15.Stop

Program
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
classbook
{
charbnm[20],author[30];
intpages,price;
public:
voidget();
voidlist();
};
voidbook::get()
{
cout<<"\nEnterbookname:";
gets(bnm);
cout<<"\nEnterauthorname:";
gets(author);
cout<<"\nEnterpages:";
cin>>pages;
price=pages*float(0.40);
}
voidbook::list()
{
cout<<"\nBookname:"<<bnm;
cout<<"\nAuthorname:"<<author;
cout<<"\nPages:"<<pages;
cout<<"\nPrice:"<<price;
}
voidmain()
{
inti;

bookb[3],k;
fstreamf1;
clrscr();
f1.open("book.dat.",ios::in);
for(i=0;i<3;i++)
{
b[i].get();
f1.write((char*)&b[i],sizeof(b[i]));
}
f1.seekg(0);
for(i=0;i<3;i++)
{

f1.read((char*)&b[i],sizeof(b[i]));

b[i].list();
}
getch();
}

Result:
Theprogramistestedandvarified

Output

Enterbookname:Harrypotter

Enterauthorname:JKRowling

Enterpages:1000

Enterbookname:Computerorganization

Enterauthorname:Williamstalling

Enterpages:826

Enterbookname:Earthmagnetism


Enterauthorname:WallaceHall

Enterpages:100

Bookname:Harrypotter
Authorname:JKRowling
Pages:1000
Price:400
Bookname:ComputerorganizationWilliamstalling
Authorname:Williamstalling
Pages:826
Price:330
Bookname:Earthmagnetism
Authorname:WallaceHall
Pages:100
Price:40

Anda mungkin juga menyukai