Sei sulla pagina 1di 41

Q1define a class to represent a book

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class Library

{ int BookNo;

char BName[25];

char Author[25];

char Publisher[25];

float Price;

int No_of_Copies;

int No_of_Copies_Issued;

public:

void initial()

cout<<endl<<"Enter Book Number: ";


cin>>BookNo;

cout<<endl<<"Enter Book Name: ";

gets(BName);

cout<<endl<<"Enter Author Name: ";

gets(Author);

cout<<endl<<"Enter Publisher Name: ";

gets(Publisher);

cout<<endl<<"Enter Price: ";

cin>>Price;

cout<<endl<<"Enter Number of copies: ";


cin>>No_of_Copies;

void issue_book()

{cout<<"Enter book details......."<<endl;

initial();

if(No_of_Copies>0)

cout<<"enter How many book you want to issue:";

cin>>No_of_Copies_Issued;

if(No_of_Copies>=No_of_Copies_Issued)

No_of_Copies=No_of_Copies-No_of_Copies_Issued;

cout<<endl<<" "<<No_of_Copies_Issued<<" book is issued..";

display();

}
else

cout<<”Copies_Issued<<" books is not available in stock..";

else

cout<<"Book is not available";

void return_book()

cout<<"enter book detail you want to return...";


cout<<endl<<"Enter Book Number: ";

cin>>BookNo;

cout<<endl<<"Enter Book Name: ";

gets(BName);

No_of_Copies=No_of_Copies+No_of_Copies_Issued;

cout<<endl<<BookNo<<":"<<BName<<"Book is returned......";

void display()

cout<<"Book Number: "<<BookNo<<endl;


cout<<"Book Name: "<<BName<<endl;

cout<<"Author Name: "<<Author<<endl;

cout<<"publisher Name: "<<Publisher<<endl;

cout<<"Price: "<<Price<<endl;

};
Q2 define a class outfit

class Outfit

char OCode[15];

char OType[15];

int OSize;

char OFabric[15];

float OPrice;
void InitPrice()

if(strcmp(OFabric,"DENIM")==0)

if(strcmp(OType,"TROUSER")==0)

OPrice=1500;

else if(strcmp(OType,"JACKET")==0)

OPrice=2500;

else

{ if(strcmp(OType,"TROUSER")==0)
Price=1500-1500*0.25;

else if(strcmp(OType,"JACKET")==0)

Price=2500-2500*0.25;

public:

Outfit()

strcpy(OCode,"NOT ASSIGNED");

strcpy(OType,"NOT ASSIGNED");
strcpy(OFabric,"NOT ASSIGNED");

OSize=0;

OPrice=0;

void Input()

cout<"Enter code";

gets(OCode);

cout<<"\nEnter type:";

gets(OType);
cout<<"\nEnter Size:";

cin>>OSize;

cout<<"\nEnter Material";

gets(OFabric);

cout<<"\nEnter Price:";

cin>>OPrice;

InitPrice();

void Display()

{
cout<<"\nCode:"<<OCode<<endl;

cout<<"\nType:"<<OType<<endl;

cout<<"\nSize:"<<OSize<<endl;

cout<<"\nMaterial:"<<OFabric<<endl;

cout<<"\nPrice:"<<OPrice<<endl;

};
Q3

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<process.h>

int const min=500;


class Account

{ char name[20];

long ano;

public:

void getdata()

{ cout<<"Enter customer name: ";

gets(name);

cout<<"Enter account no.: ";

cin>>ano;

cout<<"Enter opening balace: ";


cin>>balance;

void display()

{ cout<<"Customer name: "<<name<<endl;

cout<<"Account no: "<<ano<<endl;

cout<<"Balance :"<<balance;

};

class Current:public Account

{ float depo,with,pen;
public:

void deposit()

{ cout<<endl<<"Enter money to deposit: ";

cin>>depo;

display();

balance=balance+depo;

cout<<endl<<"After deposit main balance is: "<<balance<<endl;

void withdraw()

{ cout<<endl<<"Enter money to withdraw: ";


cin>>with;

if(with<balance)

{ display();

balance=balance-with;

cout<<endl<<"After withdraw main balance is: "<<balance<<endl;

else

{ cout<<endl<<"You cannot withdraw money....."<<endl;

}
void check_bal()

{ if(balance<min)

{ cout<<"Opening balance should not be less than 500...."<<endl;

balance=balance-150;

cout<<endl<<"After penalty main balance is: "<<balance<<endl;

};

class Savings:public Account

{ float depo,with,intr;
public:

void deposit()

{ cout<<endl<<"Enter money to deposit: ";

cin>>depo;

display();

balance=balance+depo;

cout<<endl<<"After deposit main balance is: "<<balance<<endl;

void withdraw()

{ cout<<endl<<"Enter money to withdraw: ";


cin>>with;

if(with<balance)

{ display();

balance=balance-with;

cout<<endl<<"After withdraw main balance is: "<<balance<<endl;

else

{ cout<<"You cannot withdraw money....."<<endl;

}
void cal_intr()

{ intr=(balance*2)/100;

balance=balance+intr;

cout<<endl<<"After calculating interest balance is: "<<balance;

};

void main()

{ clrscr();

Current c;

Savings s;
char ch;

int choice,ch2;

cout<<"Enter 'S' for saving and 'C' for current: ";

cin>>ch;

if(ch=='C'||ch=='c')

{ c.getdata();

c.check_bal();

l2:cout<<"\n 1. Display \n 2.Deposit \n 3.Withdraw \n 4. Exit \n";

cout<<"Enter your choice: ";

cin>>choice;

switch(choice)
{

case 1: c.display();

goto l2;

break;

case 2: c.deposit();

goto l2;

break;

case 3: c.withdraw();

goto l2;

break;
case 4: exit(0);

else if(ch=='S'||ch=='s')

{ s.getdata();

l1:cout<<"\n 1. Display \n 2.Deposit \n 3.Withdraw \n 4.Calculate

iterest \n 5. Exit \n";

cout<<"Enter your choice: ";

cin>>ch2;

switch(ch2)
{

case 1: s.display();

goto l1;

break;

case 2: s.deposit();

goto l1;

break;

case 3: s.withdraw();

goto l1;

break;
case 4: s.cal_intr();

goto l1;

break;

case 5: exit(0);

else

cout<<"Wrong choice..........."<<endl;

getch();

}
Q4

class Person

char name[20];

long phone;

public:

void set()

{ strcpy(name,"NULL");
phone=7878963522;

void get()

{ cout<<"Enter name: ";

gets(name);

cout<<"Enter phone: ";

cin>>phone;

void display()

{ cout<<"Name: "<<name<<endl;
cout<<"Phone: "<<phone<<endl;

Person()

{ strcpy(name,"Rahul");

phone=9965869922;

Person(char na[20],long ph)

{ name=na;

phone=ph;

}
void getName()

cout<<"Enter name:";

gets(name);

};

class Spouse:public Person

char spouseName[20];

public:

void getName()
{

cout<<"Enter name:";

gets(spousename);

void display()

{ cout<<"Name: "<<name<<endl;

cout<<"Phone: "<<phone<<endl;

cout<<"spouse name: "<<spousename<<endl;

Spouse()
{ strcpy(spouseName,"NULL");

Spouse(char sn[20])

{ spouseName=sn;

};
Q4 merging of two files

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<stdio.h>

#include<stdlib.h>

void main()

clrscr();

ifstream ifiles1, ifiles2;


ofstream ifilet;

char ch, fname1[20], fname2[20], fname3[30];

cout<<"Enter first file name (with extension like file1.txt) : ";

gets(fname1);

cout<<"Enter second file name (with extension like file2.txt) : ";

gets(fname2);

cout<<"Enter name of file (with extension like file3.txt) which will


store the contents of the two files (fname1 and fname1) : ";

gets(fname3);

ifiles1.open(fname1);

ifiles2.open(fname2);

if(ifiles1==NULL || ifiles2==NULL)

perror("Error Message ");

cout<<"Press any key to exit...\n";

getch();

exit(EXIT_FAILURE);

ifilet.open(fname3);

if(!ifilet)
{

perror("Error Message ");

cout<<"Press any key to exit...\n";

getch();

exit(EXIT_FAILURE);

while(ifiles1.eof()==0)

ifiles1>>ch;

ifilet<<ch;

while(ifiles2.eof()==0)

ifiles2>>ch;

ifilet<<ch;

cout<<"The two files were merged into "<<fname3<<" file


successfully..!!";

ifiles1.close();

ifiles2.close();
ifilet.close();

getch();}

Q5 count the no. of lines starting with 'A'

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<string.h>

void main()

clrscr();

char a[80];

int count=0;

ifstream ifile("out.txt");

while(!ifile.eof())

ifile.getline(a,80);

if(a[0]=='A')

count++;
}

cout<<endl<<"Number of lines starting with 'A' in the file out.txt:


"<<count;

getch();

Q6

#include<iostream.h>

#include<fstream.h>

#include<string.h>

int main()

{
ifstream fin("my_data.txt"); //opening text file

int count=0;

char ch[20];

while(!fin.eof())

fin>>ch;

if(strcmp(ch,you)==0)

count++;

cout<<"Occurrence="<<count<<"n";

fin.close(); //closing file

return 0;

#include<fstream.h>
#include<conio.h>

void main()

clrscr();

ifstream fin(“STORY.TXT”);

char ch;

int d=0,i;

while(!fin.eof())

fin.get(ch);

i=ch;

if(i>47&&i<58)

d++;

cout<<“nNo. Of Digits:”<<d;

getch();

}
Q

void COPYAREA(){

ifstream fin("CONSUMER.DAT",ios::in|ios::binary);

ofstream fout("BACKUP.DAT",ios::out|ios::binary);

Consumer c;

while(!fin.eof())

{ fin.read((char*)&c,sizeof(c));

if(c.checkcode("SOUTH")==0)

fout.write((char*)&c,sizeof(c));

fin.close();
fout.close();