Vous êtes sur la page 1sur 10

Q3. Assuming a binary file joke.dat is containing objects belonging to a class joke as defined below.

Write a user defined function in C++ to add more objects belonging to class joke at the bottom of it. Write a user-defined function to display all the objects from the file joke .dat Write a user-defined function to search and display a particular type of joke from the file joke.dat Write a user-defined function to copy all the objects from the file joke.dat to back.dat Write a user-defined function to count all the objects from the file joke.dat
#include<fstream.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> #include<string.h> class Joke { int jokeid; char type[30]; char jokedesc[225]; public: void newjokeentry() {cout<<" enter joke id, type and description \n"; cin>>jokeid; gets(type); gets(jokedesc);

} void showjoke() { cout<<jokeid<<"\t"<<type<<"\t"<<jokedesc<<endl; } char*rettype() { return type; } }; void addfile() { Joke j; char ch; fstream fout("Joke.dat",ios::app|ios::binary); do {j.newjokeentry(); fout.write((char*)&j,sizeof(j)); cout<<"\n add more jokes Y/N \n"; cin>>ch; }while(ch=='Y'||ch=='y'); fout.close(); } void display() {

Joke j; fstream fin("Joke.dat",ios::in|ios::binary); while(fin.read((char*)&j,sizeof(j))) { j.showjoke(); } fin.close(); } void countfile() { Joke j; int c=0; fstream fin("Joke.dat",ios::in|ios::binary); while(fin.read((char*)&j,sizeof(j))) { c++; } fin.close(); cout<<"\n number of objects ="<<c; } void search() { Joke j; int found=0; char ty[30];

fstream fin("Joke.dat",ios::in|ios::binary); cout<<"\n enter type of joke to search \n"; gets(ty); while(fin.read((char*)&j,sizeof(j))) { if(strcmpi(j.rettype(),ty)==0) {j.showjoke(); found=1; }} fin.close(); if(!found) cout<<"\n joke not found \n"; } void copy() { Joke j; fstream fin("Joke.dat",ios::in|ios::binary); fstream fout("Backup.dat",ios::out|ios::binary); while(fin.read((char*)&j,sizeof(j))) { fout.write((char*)&j,sizeof(j)); } fin.close(); fout.close();

} void main() { int ch; do {cout<<"\n Joke Menu \n"; cout<<" 1) Add Joke \n 2) Display Joke \n 3) Search Joke \n 4) Copy Joke \n 5) Count Jokes \n 6) Exit"; cout<<"\n enter choice \n"; cin>>ch; switch (ch) { case 1: addfile();break; case 2: display();break; case 3: search();break; case 4: copy();break; case 5: countfile();break; case 6: exit(0); } }while(ch>=1&&ch<=5); getch(); }

Question 4: define a class student with the following specifications: Private members: Admno- 4 digit admission number Name- 20 characters Mark- array of 5 floating point values AvgGetavg()- to compute the average marks obtained in 5 subjects Public members: Readinfo()- function to accept values for admno, name, marks,and invoke getavg() Display()- display all data members on the screen
#include<iostream.h> #include<stdio.h> #include<conio.h> class student { int admno; char name[20]; float mark[5]; float avg; void getavg() {avg=0 ; for(int i=0;i<5;i++) avg+=mark[i]; avg=avg/5;

} public: void readinfo() { cout<<"enter the admission number, name and marks of the student\n"; cin>>admno; gets(name); for(int i=0;i<5;i++) cin>>mark[i]; getavg(); } void displayinfo() { cout<<"\nthe details are as follows\n"; cout<<"admision no:"<<admno<<"\nname:"<<name<<"\nthe marks you entered arerespectively:\n"; for(int i=0;i<5;i++) {cout<<mark[i]; cout<<endl; } cout<<"the average of the marks is: "<<avg; } }; void main() {student m; m.readinfo();

m.displayinfo(); getch(); }

Vous aimerez peut-être aussi