Anda di halaman 1dari 34

Computer Science(C++)

Presented By:
Manisha Keim XII B Kendriya Vidyalya Janak Puri [IInd Shift]

2012

Inheritance
Single Inheritance #include<iostream.h> Class A { int a,b; public : void getdata() { cout<<"\n Enter the value of a and b"; cin>>a>>b; } void putdata() { cout<<"\n The value of a is :"<<a<<"and b :"<<b; } }; class B : public A //class is publicly derived from A { int c,d; public : void intdata() { cout<<"\n Enter the value of c and d :"; cin>>c>>d; } void outdata() { cout<<"\n The value of c ;"<<c<<"and d :"<<d; } }; void main() { B obj; obj.getdata(); //base class member function obj.indata(); //derived class member function obj.putdata(); obj.outdata(); } OUTPUT Enter the value of a and b 2 3 Enter the value of c and d 4 5 The value of a is 2 and b is 3 The value of c is 4 and d is 5

#include<iostream.h> #include<conio.h> class B { int a; public: int b; void get_ab(); int get_a(); void show_a(); }; class D: private B { int c; public: void mul(); void display(); }; void B::get_ab() { cout<<"Enter Values for a and b"; cin>>a>>b; } int B::get_a() { return a; } void B::show_a(){ cout<<"a= "<<a<<"\n"; } void D::mul() { get_ab(); c=b*get_a(); } void D:: display() { show_a(); cout<<"b= "<<b<<"\n"; cout<<"c= "<<c<<"\n\n"; } void main() { clrscr(); D d; d.mul(); d.display(); d.mul(); d.display(); getch(); } OUTPUT

A=5 A=5 B=10 C=50 A=5 B=20 C=100

Multiple Inheritance #include <iostream.h> class Square { protected: int l; public: void set_values (int x) { l=x;} }; class CShow { public: void show(int i); }; void CShow::show (int i) { cout << "The area of the square is::" << i << endl; } class Area: public Square, public CShow { public: int area() { return (l *l); } }; int main () { Area r; r.set_values (5); r.show(r.area()); return 0; } OUTPUT: The area of the square is:: 25

#include <iostream.h>

// Base class Shape class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // Base class PaintCost class PaintCost { public: int getCost(int area) { return area * 70; } }; // Derived class class Rectangle: public Shape, public PaintCost { public: int getArea() { return (width * height); } }; int main(void) { Rectangle Rect; int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; // Print the total cost of painting cout << "Total paint cost: $" << Rect.getCost(area) << endl; return 0; } OUTPUT: Total area: 35

Total paint cost: $2450 MULTILEVEL INHERITANCE /********* IMPLEMENTATION OF MULTILEVEL INHERITANCE 1 *********/ #include< iostream.h> #include< conio.h> class student // Base Class { protected: int rollno; char *name; public: void getdata(int b,char *n) { rollno = b; name = n; } void putdata(void) { cout< < " The Name Of Student \t: "< < name< < endl; cout< < " The Roll No. Is \t: "< < rollno< < endl; } }; class test:public student // Derieved Class 1 { protected: float m1,m2; public: void gettest(float b,float c) { m1 = b; m2 = c; } void puttest(void) { cout< < " Marks In CP Is \t: "< < m1< < endl; cout< < " Marks In Drawing Is \t: "< < m2< < endl; } }; class result:public test // Derieved Class 2 { protected: float total; public: void displayresult(void) { total = m1 + m2; putdata(); puttest(); cout< < " Total Of The Two \t: "< < total< < endl;

} }; void main() { clrscr(); int x; float y,z; char n[20]; cout< < "Enter Your Name:"; cin>>n; cout< < "Enter The Roll Number:"; cin>>x; result r1; r1.getdata(x,n); cout< < "ENTER COMPUTER PROGRAMMING MARKS:"; cin>>y; cout< < "ENTER DRAWING MARKS:"; cin>>z; r1.gettest(y,z); cout< < endl< < endl< < "************ RESULT **************"< < endl; r1.displayresult(); cout< < "**********************************"< < endl; getch(); }

OUTPUT: Enter Your Name:Lionel Enter The Roll Number:44 ENTER COMPUTER PROGRAMMING MARKS:95 ENTER DRAWING MARKS:90 ************ RESULT ************** The Name Of Student : Lionel The Roll No. Is : 44 Marks In CP Is : 95 Marks In Drawing Is : 90 Total Of The Two : 185 ********************************** /********* IMPLEMENTATION OF MULTILEVEL INHERITANCE 2 *********/ #include <iostream.h> class mm { protected: int rollno; public: void get_num(int a) { rollno = a; } void put_num() { cout << "Roll Number Is:\n"<< rollno << "\n"; }

}; class marks : public mm { protected: int sub1; int sub2; public: void get_marks(int x,int y) { sub1 = x; sub2 = y; } void put_marks(void) { cout << "Subject 1:" << sub1 << "\n"; cout << "Subject 2:" << sub2 << "\n"; } }; class res : public marks { protected: float tot; public: void disp(void) { tot = sub1+sub2; put_num(); put_marks(); cout << "Total:"<< tot; } }; int main() { res std1; std1.get_num(5); std1.get_marks(10,20); std1.disp(); return 0; } OUTPUT: Roll Number Is: 5 Subject 1: 10 Subject 2: 20 Total: 30 HIERARCHICAL INHERITANCE #include <iostream.h> class Side { protected:

int l; public: void set_values (int x) { l=x;} }; class Square: public Side { public: int sq() { return (l *l); } }; class Cube:public Side { public: int cub() { return (l *l*l); } }; int main () { Square s; s.set_values (10); cout << "The square value is::" << s.sq() << endl; Cube c; c.set_values (20); cout << "The cube value is::" << c.cub() << endl; return 0; } OUTPUT: The square value is:: 100 The cube value is::8000 #include<iostream.h> Class A { int a,b; public : void getdata() { cout<<"\n Enter the value of a and b"; cin>>a>>b; } void putdata() { cout<<"\n The value of a is :"<<a "and b is "<<b; } }; class B : public A { int c,d; public : void intdata() {

cout<<"\n Enter the value of c and d "; cin>>c>>d; } void outdata() { cout<<"\n The value of c"<<c"and d is"<<d; } }; class C: public A { int e,f; public : void input() { cout<<"\n Enter the value of e and f"; cin>>e;>>f } void output() { cout<<"\nthe value of e is"<<e"and f is" <<f; } void main() { B obj1 C obj2; obj1.getdata(); //member function of class A obj1.indata(); //member function of class B obj2.getdata(); //member function of class A obj2.input(); //member function of class C obj1.putdata(); //member function of class A obj1.outdata(); //member function of class B obj2.output(); //member function of class A obj2.outdata(); //member function of class C } OUTPUT : Enter the value of a and b 34 Enter the value of c and d 68 Enter the value of a and b 9 4 Enter the value of e and f 18 the value of a is 3 and b is 4 the value of c is 6 and d is 8 the value of a is 9 and b is 4 the value of e is 1 and f is 8 HYBRID INHERITANCE #include <iostream.h> class mm { protected:

10

int rollno; public: void get_num(int a) { rollno = a; } void put_num() { cout << "Roll Number Is:"<< rollno << "\n"; } }; class marks : public mm { protected: int sub1; int sub2; public: void get_marks(int x,int y) { sub1 = x; sub2 = y; } void put_marks(void) { cout << "Subject 1:" << sub1 << "\n"; cout << "Subject 2:" << sub2 << "\n"; } }; class extra { protected: float e; public: void get_extra(float s) {e=s;} void put_extra(void) { cout << "Extra Score::" << e << "\n";} }; class res : public marks, public extra{ protected: float tot; public: void disp(void) { tot = sub1+sub2+e; put_num(); put_marks(); put_extra(); cout << "Total:"<< tot; } }; int main() { res std1; std1.get_num(10); std1.get_marks(10,20); std1.get_extra(33.12);

11

std1.disp(); return 0;

OUTPUT: Roll Number Is: 10 Subject 1: 10 Subject 2: 20 Extra score:33.12 Total: 63.12 /******** IMPLEMENTATION OF HYBRID INHERITANCE ********/ #include< iostream.h> #include< conio.h> class student { private : int rn; char na[20]; public: void getdata() { cout< < "Enter Name And Roll No : "; cin>>na>>rn; } void putdata() { cout< < endl< < na< < "\t"< < rn< < "\t"; } }; class test : public student { protected: float m1,m2; public: void gettest() { cout< < endl< < "Enter your marks In CP 1 And Cp 2 :"; cin>>m1>>m2; } void puttest() { cout< < m1< < "\t"< < m2< < "\t"; } }; class sports { protected: float score; public: void getscore()

12

{ cout< < endl< < "Enter your score :"; cin>>score; } void putscore() { cout< < score< < "\t"; } }; class results : public test , public sports { private : float total; public : void putresult() { total = m1+m2+score; cout< < total; } }; void main() { results s[5]; clrscr(); for(int i=0;i< 5;i++) { s[i].getdata(); s[i].gettest(); s[i].getscore(); } cout< < "______________________________________________"< < endl; cout< < endl< < "Name\tRollno\tCP 1\tCP 2\tScore\tTotal"< < endl; cout< < "----------------------------------------------"< < endl; for(i=0;i< 5;i++) { s[i].putdata(); s[i].puttest(); s[i].putscore(); s[i].putresult(); } cout< < endl< < "----------------------------------------------"; getch(); } OUTPUT: Enter Name And Roll No : Lionel 1 Enter your marks In CP 1 And Cp 2 :45 Enter your score :45 Enter Name And Roll No : Cyril 65 Enter your marks In CP 1 And Cp 2 :49

13

42 Enter Enter 40 Enter 43 Enter Enter 78 Enter 24

your score :45 Name And Roll No : Mayank your marks In CP 1 And Cp 2 :41 your score :46 Name And Roll No : Dylan your marks In CP 1 And Cp 2 :23

Enter your score :25 Enter Name And Roll No : Ansha 39 Enter your marks In CP 1 And Cp 2 :45 45 Enter your score :45 ______________________________________________ Name Rollno CP 1 CP 2 Score Total ---------------------------------------------Lionel 1 45 23 45 113 Cyril 65 49 42 45 136 Mayank 40 41 43 46 130 Dylan 78 23 24 25 72 Ansha 39 45 45 45 135 ----------------------------------------------

14

Text File
First Program //Program to count number of characters. #include<fstream.h> #include<conio.h> int main() { ifstream fin; fin.open("out.txt"); clrscr(); char ch; int count=0; while(!fin.eof()) { fin.get(ch); count++; } cout<<"Number of characters in file is "<<count; fin.close(); getch(); return 0; } Second Program //Program to copy contents of file to another file. #include<fstream.h> int main() { ifstream fin; fin.open("out.txt"); ofstream fout; fout.open("sample.txt"); char ch; while(!fin.eof()) { fin.get(ch); fout<<ch; } fin.close(); return 0; } Third Program #include<iostream.h> #include<conio.h> #include<stdlib.h> fstream fs; void count() { int count=0, count2=0;

15

char a[80]; fs.open("file.txt"); if(!fs) { cout<<"File doesn't exist"; } while(!fs.get(a)) { fs>>a; if(strcmp(a,"do")==0) count++; else if(strcmp(a,"does")==0) count2++; cout<<count; cout<<count2; } void main) { clrscr(); cout<<endl; count(); getch(); } Fourth Program //Program to count number of lines #include<fstream.h> #include<conio.h> int main() { ifstream fin; fin.open("out.txt"); char str[80]; int count=0; while(!fin.eof()) { fin.getline(str,80); count++; } cout<<"Number of lines in file is "<<count; fin.close(); getch(); return 0; } Fifth Program //C++ program to write number 1 to 100 in a data file NOTES.TXT #include<fstream.h> int main() { ofstream fout; fout.open("NOTES.TXT");

16

for(int i=1;i<=100;i++) fout<<i<<endl; fout.close(); return 0; } Sixth Program Program to read from text file and display it #include<fstream.h> #include<conio.h> int main() { ifstream fin; fin.open("out.txt"); char ch; while(!fin.eof()) { fin.get(ch); cout<<ch; } fin.close(); getch(); return 0; } Seventh Program #include<fstream.h> #include<conio.h> #include<stdlib.h> void func() { char ch; ifstream ifile; ifile.open("space.txt") if (!ifile) { cout<<"File doesn't exist"; exit(0); } int count=0; while(ifile) { ifile.get(ch) if(ch==' ') count++; } cout<<"Total spaces"<<count; ifile.close() } void main() {

17

clrscr(); func(); getch(); } Eighth Program #include<fstream.h> #include<conio.h> void main() { clrscr(); ifstream fin("STORY.TXT"); char ch; int i,a=0,s=0,d=0; while(fin) { fin.get(ch); i=ch; if(i>63&&i<91||i>96&&i<123) a++; else if(ch==' ') s++; else if(i>47&&i<58) d++; } cout<<"No. OF Alphabates:"<<a; cout<<"\nNo. Of Digits:"<<d; cout<<"\nNo. Of Spaces:"<<s; getch(); } Ninth Program #include<fstream.h> #include<conio.h> #include<stdlib.h> void calculate() { stream tfile; clrscr(); tfile.open("PARA.TXT", ios::in); char arr[80]; char ch; int i=0, sum=0, n=0; while(tfile) { tfile.get(ch);

18

arr[i] = ch; i++; if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U' ) { i--; sum = sum + i; i = 0; n++; } } cout << "Total no. of vowels : " << n; } void main() { clrscr(); calculate(); getch(); } Tenth Program #include<fstream.h> #include<conio.h> #include<stdlib.h> void func() { char ch[10]; ifstream ifile; ifile.open("text.txt"); if(ifile==NULL) { cout<<"File doesn't exist"; exit(0); } int count=0; while(ifile) { ifile>>ch; count++; } cout<<"Total words"<<count; ifile.close(); } void main() { clrscr(); func(); getch(); }

19

20

Binary File
First Program # include <fstream.h> # include <conio.h> # include <stdio.h> # include <string.h> struct game { char gname[20]; char participant[10][30]; }; game a; void make() { fstream ifile; ifile.open("game.dat", ios::out); int n; cout<< "Enter how many records ??? "; cin >> n; for(int i=0;i<n;i++) { cout << "Enter the Name of Game "; gets(a.gname); for(int j=0;j<10;j++) { cout << "Enter the Participants "; gets(a.participant[j]); } ifile.write((char *) &a, sizeof(game)); } ifile.close(); } void copy() { fstream file1, file2; file1.open("game",ios::in); file2.open("basketball.dat",ios ::out); while(file1) { file1.read((char *)&a, sizeof(game)); if (strcmp(a.gname,"Basket Ball")==0) { cout << " Record found "; file2.write((char *)&a,sizeof(game)); } } file1.close(); file2.close(); } void display() { fstream ifile; ifile.open("BASKET.DAT",ios::in); while(ifile) {

21

ifile.read((char *)&a, sizeof(game)); for(int j=0;j<10;j++) cout << a.Participant[j]; } ifile.close(); } void main() { clrscr(); make(); copy(); cout << "Summary of the copied file \n"; display(); getch(); } Second Program #include <iostream.h> #include <fstream.h> #include <conio.h> #include <ctype.h> #include <stdio.h> #include <string.h> class TRAIN { int Tno; //Train Number char From[20]; //Train Starting Point char To[20]; //Train Destination public: char* GetFrom() {return From;} char* GetTo() {return To;} void Input() {cin>>Tno;gets(From);gets(To);} void Show() {cout<<Tno<< ":"<<From<< ":"<<To<<endl;} }; // The function is as: void Read() { TRAIN T; ifstream fin; fin.open("TRAIN.DAT", ios::binary); while(fin.read((char*)&T, sizeof(T))) { if (strcmp(T.GetTo(), "Delhi") == 0) T.Show(); } fin.close(); // Ignore } void main() { clrscr(); Read();

22

} Third Program #include<iostream.h> #include<conio.h> #include<fstream.h> #include <ctype.h> #include <stdio.h> #include <string.h> struct PRODUCT { char Product_Code[10]; char Product_Description[10]; int Stock; }; // Function to update PRODUCT data void update() { fstream fs; PRODUCT pr; char P_Code[10]; cout << "\nEnter the product code : "; gets(P_Code); fs.open("PRODUCT.DAT", ios::binary|ios::in|ios::out); while(fs.read((char *)&pr, sizeof(PRODUCT))) { if(strcmp(pr.Product_Code, P_Code) == 0) { cout << "\nEnter the new stock : "; cin >> pr.Stock; fs.seekp(fs.tellg() - sizeof(pr)); // OR fs.seekp(fs.teelg() - sizeof(pr), ios::beg); // OR fs.seekp(- sizeof(pr), ios::curr); fs.write((char *)&pr, sizeof(PRODUCT)); } } fs.close(); } // Function to add new colony data void append() { fstream fs; PRODUCT pr; char opt = 'y'; clrscr(); fs.open("PRODUCT.DAT", ios::binary|ios::in|ios::app); cout << "\nProduct data entry\n"; do { cout << "Enter product code => "; gets(pr.Product_Code); cout << "Enter product description => "; gets(pr.Product_Description); cout << "Enter the stock => ";

23

cin >> pr.Stock; fs.write((char *)&pr, sizeof(PRODUCT)); cout << "Do you want to continue... "; cin >> opt; } while(opt == 'y'|| opt=='Y'); fs.close(); } // Function to display product data void display() { clrscr(); fstream fs; PRODUCT pr; cout << "The output of PRODUCT.dat data file :\n"; fs.open("PRODUCT.dat", ios::binary|ios::nocreate|ios::in); fs.read((char *) &pr, sizeof(PRODUCT)); while (fs) { cout << pr.Product_Code << '\t' << pr.Product_Description << '\t' << pr.Stock << endl; fs.read((char *) &pr, sizeof(PRODUCT)); } fs.close(); getch(); } void main() { clrscr(); int ch; char opt; do { cout << "\n Main Menu "; cout << "\n1. Add Record "; cout << "\n2. Display Record "; cout << "\n3. Update Record "; cout << "\n4. Exit"; cout <<"\nEnter your choice : "; cin >> ch; switch(ch) { case 1 : append(); break; case 2: display(); break; case 3: update(); break; case 4: break; }

24

} while ((ch >= 1) && (ch <= 3)); } Fourth Program #include <iostream.h> #include <fstream.h> #include <conio.h> #include <string.h> class Directory { char Name[20]; char Address[30]; char AreaCode[5]; char Phone_No[15]; public: void Register(); void Show(); int CheckCode(char AC[]) { return strcmp(AreaCode, AC); } }; void COPYABC() { fstream afile, bfile; Directory DR; int ctr; afile.open("TELEPHON.DAT",ios::in); bfile.open("TELEBACK.DAT",ios ::out); while(afile) { afile.read((char *)&DR, sizeof(DR)); ctr = DR.CheckCode("123"); if (ctr == 0) { cout << "Record found "; bfile.write((char *)&DR,sizeof(DR)); } } afile.close(); bfile.close(); } void main() { clrscr(); COPYABC(); } Fifth Program #include <iostream.h> #include <fstream.h> #include <conio.h> #include <ctype.h> class USER {

25

int Uid; //User Id char Uname[20]; //User Name char Status; //User Type : A Active I Inactive public : void Register( ); //Function to enter the content //Function to display all data members void show ( ); char Getstatus( ) {return Status;} }; void USER::show() { cout << Uid << " " << Uname << " " << Status; } // The function is as: void show_Detail() { fstream afile; USER U; char S; afile.open("USER.DAT",ios::in); while(afile) { afile.read((char *)&U,sizeof(USER)); S = U.Getstatus(); if (S == 'A') U.show(); } afile.close(); } void main() { clrscr(); show_Detail(); } Sixth Program #include #include void append(); void list(); void search(); void modify(); void del(); struct employee { int no, sal; char gen, name[20]; }; void main() { int a; char ch; do{ printf(\nEMPLOYEE DATABASE\n\n);

26

printf(1.Append Employee Record\n2.List Employee Record\n3.Modify Employee Record\n4.Delete Employee Record\n5.Search Employee Record\n Enter Choice : ); scanf(%d,&amp;a); switch(a) { case 1: append(); break; case 2: list(); break; case 3: modify(); break; case 4: del(); break; case 5: search(); break; default : printf(Invalid Choice!); } printf(\n More Actions ? (Y/N) :); fflush(stdin); scanf(%c, &amp;ch); }while(ch==y'|| ch==Y'); } void append() { int i,n; struct employee e; FILE *fp; fp=fopen(Employee.dat, a); if(fp==NULL) { printf(File Creation Failed!); exit(0); } printf(Enter the nos. of employees : ); scanf(%d, &amp;n); for(i=0;i { printf(Enter the Employee Number : ); scanf(%d, &amp;e.no); printf(Enter the Employee Salary : ); scanf(%d, &amp;e.sal); printf(Enter the Employee gender: ); fflush(stdin); scanf(%c, &amp;e.gen); printf(Enter the Employee Name : ); fflush(stdin); gets(e.name);

27

printf(\n\n); fwrite((char *)&amp;e, sizeof(e), 1, fp); } fclose(fp); } void list() { int nofrec=0; struct employee e; FILE *fp; fp=fopen(Employee.dat, rb); if(fp==NULL) { printf(\n\tFile doesnt exist!!!\TRY AGAIN); exit(0); } while((fread((char *)&amp;e, sizeof(e), 1, fp))==1) { nofrec++; printf(\nEmployee Number : %d, e.no); printf(\nEmployee Salary : %d, e.sal); printf(\nEmployee gender : %c,e.gen); printf(\nEmployee Name : %s,e.name); printf(\n\n); } printf(Total number of records present are : %d, nofrec); fclose(fp); } void modify() { int recno, nofrec=0; char ch; struct employee e; FILE *fp; fp=fopen(Employee.dat, rb+); printf(Enter the Employee Number to modify : ); scanf(%d, &amp;recno); while((fread((char *)&amp;e, sizeof(e), 1, fp))==1) { nofrec++; if(e.no==recno) { printf(\nEmployee Number : %d, e.no); printf(\nEmployee Salary : %d, e.sal); printf(\nEmployee gender : %c,e.gen); printf(\nEmployee Name : %s,e.name); printf(\n); printf(Do you want to modify this record : ? (Y/N));

28

fflush(stdin); scanf(%c, &amp;ch); fseek(fp, ((nofrec-1)*sizeof(e)), 0); if(ch==Y'|| ch==y') { printf(Enter the Employee Salary : ); scanf(%d, &amp;e.sal); printf(Enter the Employee gender: ); fflush(stdin); scanf(%c, &amp;e.gen); printf(Enter the Employee Name : ); fflush(stdin); gets(e.name); fwrite((char *)&amp;e, sizeof(e), 1, fp); printf(Record Modified); } else printf(No modifications were made); fclose(fp); } } } void del() { int recno; char ch; struct employee e; FILE *fp, *ft; fp=fopen(Employee.dat, rb); ft=fopen(Temp.dat, wb); printf(Enter the Employee Number to delete : ); scanf(%d, &amp;recno); while((fread((char *)&amp;e, sizeof(e), 1, fp))==1) { if(e.no==recno) { printf(\nEmployee Number : %d, e.no); printf(\nEmployee Salary : %d, e.sal); printf(\nEmployee gender : %c,e.gen); printf(\nEmployee Name : %s,e.name); printf(\n); printf(Do you want to delete this record : ? (Y/N)); fflush(stdin); scanf(%c, &amp;ch); }

29

} if(ch==y'||ch==Y') { rewind(fp); while((fread((char *)&amp;e, sizeof(e), 1, fp))==1) { if(recno!=e.no) { fwrite((char *)&amp;e, sizeof(e), 1, ft); } } } else printf(No Record was deleted); fclose(fp); fclose(ft); remove(Employee.dat); rename(Temp.dat, Employee.dat); } void search() { int s,recno; char sname[20]; struct employee e; FILE *fp; fp=fopen(Employee.dat, rb); printf(\n1.Search by Name\n2.Search by Employee No.\n Enter choice : ); scanf(%d, &amp;s); switch(s) { case 1: printf(Enter the Employee Name to Search : ); fflush(stdin); gets(sname); while((fread((char *)&amp;e, sizeof(e), 1, fp))==1) { if(strcmp(sname,e.name)==0) { printf(\nEmployee Number : %d, e.no); printf(\nEmployee Salary : %d, e.sal); printf(\nEmployee gender : %c,e.gen); printf(\nEmployee Name : %s,e.name); printf(\n); } } break; case 2:

30

printf(Enter the Employee Number to Search : ); scanf(%d, &amp;recno); while((fread((char *)&amp;e, sizeof(e), 1, fp))==1) { if(e.no==recno) { printf(\nEmployee Number : %d, e.no); printf(\nEmployee Salary : %d, e.sal); printf(\nEmployee gender : %c,e.gen); printf(\nEmployee Name : %s,e.name); printf(\n); } } break; } } Seventh Program #include <fstream.h> #include <iostream.h> #include <stdio.h> #include <conio.h> class vehicle { char vehicletype[10]; int no_of_wheels; public: void getdetails() { gets(vehicletype); cin >> no_of_wheels; } void showdetails() { cout << "Vehicle Type" << vehicletype; cout << "Number of wheels = " << no_of_wheels; } }; void showfile() { vehicle VH; int reccount = 0; fstream vfile; vfile.open("SPEED.DAT", ios::in|ios::binary); while (vfile) { vfile.read((char *)&VH, sizeof(VH)); reccount++; VH.showdetails(); } cout << "Total number of records are : " << reccount; vfile.close();

31

} void main() { clrscr(); showfile(); } Eighth Program #include <fstream.h> #include <iostream.h> #include <stdio.h> #include <conio.h> class LAUGHTER { int Idno; // Identification number char Type[5]; // LAUGHTER Type char Desc[255]; // Description public : void Newentry() { cin >> Idno; gets(Type); gets(Desc); } void Shownscreen() { cout << Idno << " " << Type << endl << Desc << endl; } }; void append() { fstream afile; afile.open("FUN.DAT", ios::binary|ios::app); LAUGHTER LAUG; int n, i; cout << "How many objects you want to add : "; cin >> n; for(i = 0; i<n; i++) { LAUG.Newentry(); afile.write((char *)&LAUG, sizeof(LAUGHTER)); } afile.close(); } void main() { clrscr(); append(); } Ninth Program #include <iostream.h> #include <fstream.h>

32

#include <conio.h> #include <ctype.h> #include <stdio.h> class LAPTOP { long ModelNo; float RAM, HDD; char Details[120]; public: void StockEnter() {cin>>ModelNo>>RAM>>HDD; gets(Details);} void StockDisplay() {cout<<ModelNo<<RAM<<HDD<<Details<<endl;} long ReturnModelNo() {return ModelNo;} }; void Search() { LAPTOP L; long modelnum; cin>>modelnum; ifstream fin; fin.open("LAPTOP.DAT", ios::binary|ios::in); while(fin.read((char*)&L, sizeof(L))) { if (L.ReturnModelNo() == modelnum) L.StockDisplay(); } fin.close(); } void main() { clrscr(); Search(); } Tenth Program #include <iostream.h> #include <fstream.h> #include <conio.h> #include <ctype.h> #include <stdio.h> #include <string.h> class FLIGHT { int Fno; char From[20]; char To[20]; public: char* GetFrom() {return From;} char* GetTo() {return To;} void Enter() {cin>>Fno;gets(From);gets(To);} void Display() {cout<<Fno<< ":"<<From<< ":"<<To<<endl;} }; // The function is as: void Read() { FLIGHT F;

33

ifstream fin; fin.open("FLIGHT.DAT", ios::binary); while(fin.read((char*)&F, sizeof(F))) { if (strcmp(F.GetTo(), "Mumbai") == 0) F.Display(); } fin.close(); // Ignore

} void main() { clrscr(); Read(); }

34

Anda mungkin juga menyukai