Sunteți pe pagina 1din 12

PROGRAMS ON CLASS AND OBJECTS:

1.Write a c++ program using class to store list of items and manipulate those using
prices.
//program using a class to store price list of 50 items .
#include<iostream.h>
#include<conio.h>
class item{ private:
int itcode[3];
float itprice[3];
public:
void ini();
float largest();
float sum();
void displayit();
};
void item::ini(void)
{
for(int i=0;i<3;i++)
{
cout<<"\n"<<"item no"<<(i+1);
cout<<"\n"<<"enter item code";
cin>>itcode[i];
cout<<"\n"<<"enter item price";
cin>>itprice[i];
cout<<"\n";
}
}
float item::largest(void)
{
float large=itprice[0];
for(int i=0;i<3;i++)
{
if(itprice[i]>large)
large=itprice[i];
}
return large;
}
float item::sum(void)
{
float sum=0;
for(int i=0;i<3;i++)
sum=sum+itprice[i];
return sum;
}
void item::displayit(void)
{
cout<<"\ncode price\n";
for(int i=0;i<3;i++)

cout<<"\n"<<itcode[i]<<"\n"<<itprice[i];
}
void main()
{
item order;
order.ini();
float total,biggest;
int ch=0;
for(;;)
{
cout<<"\n"<<"1.display largest price"<<"\n"<<"2.display sum of
prices"<<"\n"<<"display item list"<<"\n";
cout<<"enter your choice"<<"\n";
cin>>ch;
if(ch==0)
break;
switch(ch)
{
case 1:
biggest=order.largest();
cout<<"the largest price is"<<biggest<<"\n";;
break;
case 2:
total=order.sum();
cout<<"the sum of prices"<<total<<"\n";
break;
case 3:
order.displayit();
break;
default:
cout<<"\nwrong choice!!\n";
break;
}
}
}

2. Write a c++ program using class to display of cricket overs.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class player{ private:
int over[6];
int score;
int wic;
public:
player(int A,int C)
{
score=A;
wic=C;
}
void ini();
int display(int);
int wickets();
};
int player::display(int c)
{
cout<<"over-"<<c<<"\n";
cout<<"This over:"<<"\n";
char bowler[20];
cout<<"enter bowler name";
gets(bowler);
cout<<"bowler:"<<bowler;
for(int i=0;i<6;i++)
{
if(over[i]<=6)
{
cout<<over[i]<<"\t";
score=score+over[i];
}
else
cout<<"W"<<"\t";
}
cout<<"total score-\n"<<score;
return score;
}
void player::ini()
{
cout<<"enter runs";
for(int i=0;i<6;i++)
cin>>over[i];
}
int player:: wickets()
{

for(int i=0;i<6;i++)
{
if(over[i]>6)
wic++;
}
cout<<"Wickets-"<<wic;
return wic;
}
void main()
{
cout<<"***************CRICKET LEAGUE******************\n";
int a,x=0,y=0,scor=0,wicc=0;
for(a=1;a<=3;a++)
{
x=scor;
y=wicc;
player s1(x,y);
s1.ini();
clrscr();
scor=s1.display(a);
wicc=s1.wickets();
cout<<"\n";
}
}

3.Write a c++ program using classes to check whether a file can be created in the
available space.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class myfolder{
private:

char file[2][10];
long availspace;
long usedspace;
int c;
public:
void fileent();
long retavailsp();
void showfile();
myfolder(long A,int X)
{
availspace=A ;
c=X;
}
};
void myfolder::fileent()
{
cout<<"enter name of files\n";
for(int i=0;i<2;i++)
{
cout<<"enter file size\n";
cin>>usedspace;
if(usedspace<=availspace)
{
cout<<"Document"<<(i+1)<<":";
gets(file[i]);
availspace=availspace-usedspace;
c=1;
usedspace=0;
}
else
{
cout<<"Not enough space available\n";
break;
}
}
}
void myfolder::showfile()
{
if(c!=0)
{
for(int i=0;i<2;i++)
cout<<"Directory contains:\n"<<file[i]<<"\n";
}
else
cout<<"No files in directory";
}
void main()
{
int s=0,x=0;
int ch;

long a;
cout<<"enter no of bytes";
cin>>s;
a=s*1024;
myfolder d(a,x);
cout<<"\nMAIN MENU\n";
cout<<"1.FILE ENTRY\n"<<"2.SHOW ALL FILES\n";
cout<<"enter your choice\n";
cin>>ch;
switch(ch)
{
case 1:
d.fileent();
break;
case 2:
d.showfile();
break;
default:
cout<<"\nwrong choice";
break;
}
}

4.Write a c++ program using class to check and fine the bank accounts.
#include<iostream.h>
#include<conio.h>
class account{
int accno[2];
long balance[2];
public:
void ini();
void fine();
};

void account::ini()
{
cout<<"\nEnter account details";
for(int i=0;i<2;i++)
{
cout<<"\nenter account no";
cin>>accno[i];
cout<<"\nenter balance";
cin>>balance[i];
}
}
void account::fine()
{
int a=0;
for(int i=0;i<2;i++)
{
if(balance[i]<=100)
{
balance[i]-=50;
a++;
}
}
for(i=0;i<2;i++)
{
cout<<accno[i]<<"\t"<<"\t";
cout<<balance[i]<<"\n";
}
cout<<a<<"accounts fined";
}
void main()
{
int b;
account s;
for(b=1;b<=2;b++)
{
s.ini();
cout<<"\nNEW ACCOUNT DETAILS\n";
cout<<"ACCOUNT NO"<<"\t"<<"BALANCE"<<"\n";
s.fine();
}
}

5. Write a c++ program using class to generate Fibonacci series.


#include<iostream.h>
#include<conio.h>
class fibo{
private:
int i,f,s,t;
public:
void genefibo();
fibo(int a,int b)
{
f=a;
s=b;
}

};
void fibo::genefibo()
{
cout<<f<<" "<<s;
for(i=0;i<10;i++)
{
t=f+s;
f=s;
s=t;
cout<<t<<" ";
}
}
void main()
{
clrscr();
int m,n;
cout<<"enter initial 2 terms of fibonacci series\n";
cin>>m;
cin>>n;
fibo s1(m,n);
s1.genefibo();
}