Sei sulla pagina 1di 42

C++ Lab Manual

Department of MCA

WORKING OF CLASSES, ARRAYS AND FUNCTIONS ( Processing Shopping List) 1. Write a program to perform shopping list operations such as addition, modification and deletion of an item and show the bill value using classes, arrays and functions. CODING: // DEPARTMENTAL STORE PROCESSING #include<iostream.h> #include<conio.h> #include<iomanip.h> class item { int itemcode[50]; int itemqty[50]; float itemprice[50]; char itemname[50][10]; int count; public: void counte() { count = 0; } void getitem(); void modi(); void remove(); void displayitems(); }; // MEMBER FUNCTION DECLARATION void item :: getitem() { clrscr(); cout<<"\n\t\t\t SHOPPING LIST ENTRIES"; cout<<"\n\t\t\t ---------------------"; cout<<"\n\t\t\t Enter the number of items: "; int n; cin>>n; for(int i=0;i<n;i++) { cout<<"\n\t\t Enter item code: "; cin>>itemcode[count]; cout<<"\n\t\t Enter item name: "; cin>>itemname[count]; cout<<"\n\t\t Enter no. of quantities: "; cin>>itemqty[count]; cout<<"\n\t\t Enter item price: "; cin>>itemprice[count]; cout<<"\n"; count++; } Lecturer: S. Jayasankari 1

C++ Lab Manual }

Department of MCA

void item :: modi() { int b,qy; char c; cout<<"\n\n\t\t\t Enter item code for modification: "; cin>>b; for(int i=0;i<count;i++) { if(itemcode[i]==b) { cout<<"\n\t\t Enter number of quantities: "; cin>>qy; cout<<"\n\t\t Increase of decrease quantity (I/D): "; cin>>c; if(c=='I' || c=='i') { itemqty[i]=itemqty[i]+qy; } else if (c=='D' || c=='d') { if(itemqty[i]<qy) { cout<<"\n\t\tInvalid no. of quantity\n\t\t\t Try again"; } else itemqty[i]=itemqty[i]-qy; } cout<<"\n\t\t Any changes in price (Y/N): "; char cp; cin>>cp; if(cp=='y' || cp=='Y') { cout<<"\n\t\t Enter item price: "; float pri; cin>>pri; itemprice[i]=pri; } } }} void item::remove() { int a,flag=0; cout<<"\n Enter item code: "; cin>>a; for(int i=0;i<count;i++) { if(itemcode[i]==a) { itemcode[i]=0; flag=1; } Lecturer: S. Jayasankari 2

C++ Lab Manual } if(flag==0) { cout<<"\n\t\t Item no is not available\n\t\t Enter valid no.";} else { cout<<"\n\t\t Item is deleted";} flag=0; }

Department of MCA

void item :: displayitems() { float value[50]; float total=0; cout<<"\n\n\t\t ABC DEPARTMENTAL STORES"; cout<<"\n\t\t 05, MAIN ROAD, CHENNAI"; cout<<"\n\t--------------------------------------------------------"; cout<<"\n\t CODE NAME QUANTITY PRICE VALUE"; cout<<"\n\t--------------------------------------------------------"; for(int i=0;i<count;i++) { if(itemcode[i]==0) continue; else { value[i]=itemqty[i]*itemprice[i]; total=total+value[i]; cout<<"\n\t "<<setw(5)<<itemcode[i]; cout<<" "<<setw(10)<<itemname[i]; cout<<" "<<setw(4)<<itemqty[i]<<" "; cout<<setw(6)<<setprecision(2); cout.setf(ios::showpoint); cout<<itemprice[i]<<" "; cout<<setw(10)<<setprecision(2); cout.setf(ios::showpoint); cout<<value[i]; } } cout<<"\n\t-------------------------------------------------------"; cout<<"\n\t\t Total Value :" << total; cout<<"\n\t-------------------------------------------------------"; cout<<"\n"; } void main() { item order; order.counte(); Lecturer: S. Jayasankari 3

C++ Lab Manual int x; clrscr(); do { cout<<"\n\t\t\t Processing Shopping List"; cout<<"\n\t\t\t ------------------------"; cout<<"\n\t\t 1. Addition of an item"; cout<<"\n\t\t 2. Deletion of an item"; cout<<"\n\t\t 3. Modification"; cout<<"\n\t\t 4. Billing"; cout<<"\n\t\t 5. Exit"; cout<<"\n\n\t\t Enter your choice: "; cin>>x; switch(x) { case 1: order.getitem(); getch(); clrscr(); break; case 2: order.remove(); getch(); clrscr(); break; case 3: order.modi(); getch(); clrscr(); break; case 4: order.displayitems(); getch(); clrscr(); break; case 5: break; dafault: cout<<"\nInvalid input!! Try again!!"; } }while(x!=5); }

Department of MCA

Lecturer: S. Jayasankari

C++ Lab Manual OUTPUT: PROCESSING SHOPPING LIST 1. Addition of an item 2. Deletion of an item 3. Modification 4. Billing 5. Exit Enter your choice: 1 SHOPPING LIST ENTRIES Enter the number of items: 3 Enter item code: 101 Enter item name: PONDS Enter no. of quantities: 8 Enter item price: 30.20 Enter item code: 102 Enter item name: LUX Enter no. of quantities: 5 Enter item price: 25.50 Enter item code: 103 Enter item name: HAMAM Enter no. of quantities: 2 Enter item price: 12.25

Department of MCA

Lecturer: S. Jayasankari

C++ Lab Manual Processing Shopping List 1. Addition of an item 2. Deletion of an item 3. Modification 4. Billing 5. Exit Enter your choice: 4 ABC DEPARTMENTAL STORES 05, MAIN ROAD, CHENNAI

Department of MCA

------------------------------------------------------------------------CODE 101 102 103 NAME PONDS LUX HAMAM Total Value QUANTITY 8 5 2 : 393.60 PRICE VALUE 30.20 25.50 12.25 241.60 127.50 24.50 -------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------------Processing Shopping List 1. Addition of an item 2. Deletion of an item 3. Modification 4. Billing 5. Exit Enter your choice: 3 Enter item code for modification: 101 Enter number of quantities: 2

Lecturer: S. Jayasankari

C++ Lab Manual Increase of decrease quantity (I/D): D Any changes in price (Y/N): Y Enter item price: 28.50 Processing Shopping List 1. Addition of an item 2. Deletion of an item 3. Modification 4. Billing 5. Exit Enter your choice: 4 ABC DEPARTMENTAL STORES 05, MAIN ROAD, CHENNAI

Department of MCA

---------------------------------------------------------------------CODE 101 102 103 NAME PONDS LUX HAMAM Total Value QUANTITY 6 5 2 : 323.00 PRICE VALUE 28.50 25.50 12.25 171.00 127.50 24.50 ---------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------Processing Shopping List 1. Addition of an item 2. Deletion of an item 3. Modification 4. Billing 5. Exit Lecturer: S. Jayasankari 7

C++ Lab Manual Enter your choice: 2 Enter item code: 102 Item is deleted Processing Shopping List 1. Addition of an item 2. Deletion of an item 3. Modification 4. Billing 5. Exit Enter your choice: 4 ABC DEPARTMENTAL STORES 05, MAIN ROAD, CHENNAI

Department of MCA

---------------------------------------------------------------------CODE 101 103 NAME PONDS HAMAM Total Value QUANTITY 6 2 : 195.50 PRICE VALUE 28.50 12.25 171.00 24.50 ----------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------

Lecturer: S. Jayasankari

C++ Lab Manual

Department of MCA

STATIC MEMBER FUNCTION 2. Write a c++ program to implement the static member variable and static member function. CODING: //Static member function #include<iostream.h> #include<conio.h> class test { int code; static int count; public: void setcode() { code=++count;} void showcode() { cout<<"\n\t\t\tObject number: "<<code<<"\n";} static void showcount() { cout<<"\n\t\t\tCount: "<<count<<"\n"; } }; int test::count; void main() { clrscr(); cout<<"\n\n\t\t\t Testing of static member function\n\n"; test t1,t2; t1.setcode(); t2.setcode(); test::showcount(); test t3; t3.setcode(); test::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); getch(); }

Lecturer: S. Jayasankari

C++ Lab Manual OUTPUT: Testing of static member function Count: 2 Count: 3 Object number: 1 Object number: 2 Object number: 3

Department of MCA

Lecturer: S. Jayasankari

10

C++ Lab Manual OVERLOADED CONSTRUCTORS (Complex Numbers Addition)

Department of MCA

3. Write a c++ program to add two complex numbers using friend function and implementing the overloading of constructors. CODING: //Overloaded constructors #include<iostream.h> #include<iomanip.h> #include<conio.h> class complex { float x,y; public: complex() {} complex(float a) { x=a; y=a; } complex(float real, float imag) { x=real; y=imag; }

friend complex sum(complex, complex); friend void show(complex); }; complex sum(complex c1, complex c2) { complex c3; c3.x=c1.x+c2.x; c3.y=c1.y+c2.y; return(c3); } void show(complex c) { cout.setf(ios::showpoint); cout<<setw(6)<<setprecision(2); cout<<c.x; cout<<" +i"; cout<<setw(6)<<setprecision(2); cout<<c.y<<"\n"; Lecturer: S. Jayasankari 11

C++ Lab Manual

Department of MCA

} void main() { float r1,r2,i1; char ch; do { clrscr(); cout<<"\n\t\tComplex numbers addition using friend function"; cout<<"\n\n\t\tInput"; cout<<"\nEnter 1st complex number: "; cin>>r1>>i1; cout<<"\nEnter 2nd complex number: "; cin>>r2; complex A(r1,i1); complex B(r2); complex C; C=sum(A,B); cout<<"\n\t\tOutput"; cout<<"\n\tA= "; show(A); cout<<"\n\tB= "; show(B); cout<<"\n\tC= "; show(C); getch(); cout<<"\nDo you want to continue: "; cin>>ch; getch(); }while(ch=='y'||ch=='Y'); } OUTPUT: Complex numbers addition using friend function Input Enter 1st complex number: 12.45 25.15 Enter 2nd complex number: 14.32

Lecturer: S. Jayasankari

12

C++ Lab Manual Output A= 12.45 +i 25.15 B= 14.32 +i 14.32 C= 26.77 +i 39.47 Do you want to continue: y

Department of MCA

Complex numbers addition using friend function Input Enter 1st complex number: 5.23 6.24 Enter 2nd complex number: 2.24 3.25 Output A= 5.23 +i 6.24 B= 2.24 +i 2.24 C= 7.47 +i 8.48 Do you want to continue: n

Lecturer: S. Jayasankari

13

C++ Lab Manual

Department of MCA CONSTRUCTORS AND DESTRUCTORS

4. Write a c++ program to implement the constructor and destructor concept. CODING: //Implementation of destructors #include<iostream.h> #include<conio.h> int count=0; class alpha { public: alpha() { count++; cout<<"\n\t Number of objects created: "<<count; } ~alpha() { cout<<"\n\t "<<count<<"th object is destroyed"; count--; }}; void main() { clrscr(); cout<<"\n\nEntering main function\n"; alpha a1,a2,a3,a4; { cout<<"\n\nEntering Block1 \n"; alpha a5; } { cout<<"\n\nEnter Block2 \n"; alpha a6; } cout<<"\nRe-entering main\n"; getch(); }

Lecturer: S. Jayasankari

14

C++ Lab Manual

Department of MCA

OUTPUT: Entering main function Number of objects created: 1 Number of objects created: 2 Number of objects created: 3 Number of objects created: 4 Entering Block1 Number of objects created: 5 5th object is destroyed Enter Block2 Number of objects created: 5 5th object is destroyed Re-entering main 4th object is destroyed 3th object is destroyed 2th object is destroyed 1th object is destroyed

Lecturer: S. Jayasankari

15

C++ Lab Manual DYNAMIC CONSTRUCTORS (String Manipulation)

Department of MCA

5. Write a c++ program that shows the usage of dynamic constructors. CODING: //Dynamic constructors #include<iostream.h> #include<string.h> #include<conio.h> class string { char *name; int length; public: string() { length=0; name=new char[length+1]; } string(char *s) { length=strlen(s); name=new char[length+1]; strcpy(name,s); } void display() { cout<<"\n\t\t\t\t"<<name<<"\n"; } void join(string &a,string &b); }; void string::join(string &a, string &b) { length=a.length+b.length; delete name; name=new char[length+1]; strcpy(name,a.name); strcat(name,b.name); } void main() { char *first = "Object "; clrscr(); cout<<"\n\t\t\tDynamic Constructors"; Lecturer: S. Jayasankari 16

C++ Lab Manual string name1(first),name2("Oriented "); string name3("Language "); string s1,s2; s1.join(name1,name2); s2.join(s1,name3); name1.display(); name2.display(); name3.display(); s1.display(); s2.display(); getch(); } OUTPUT: Dynamic Constructors Object Oriented Language Object Oriented Object Oriented Language

Department of MCA

Lecturer: S. Jayasankari

17

C++ Lab Manual OVERLOADING OPERATORS (Vector Manipulations)

Department of MCA

6. Develop a c++ program to implement overloading of operators using friend function. CODING: //Overloading operators using friends #include<iostream.h> #include<conio.h> const size=3; class vector { int v[size]; public: vector(); vector(int * x); friend vector operator * (int a, vector b); friend vector operator * (vector a, int b); friend istream & operator >> (istream &,vector &); friend ostream & operator << (ostream &,vector &); }; vector :: vector() { for(int i =0;i<size;i++) v[i]=0; } vector :: vector(int * x) { for(int i=0;i<size;i++) v[i]=x[i]; } vector operator *(int a, vector b) { vector c; for(int i=0;i<size;i++) c.v[i]=a*b.v[i]; return c; } vector operator *(vector b, int a) { vector c; for(int i=0;i<size;i++) c.v[i]=b.v[i]*a; Lecturer: S. Jayasankari 18

C++ Lab Manual return c; } istream & operator >> (istream & din, vector & b) { for(int i=0;i<size;i++) din>>b.v[i]; return(din); } ostream & operator << (ostream & dout,vector & b) { dout<<"("<<b.v[0]; for(int i=1;i<size;i++) dout<<","<<b.v[i]; dout<<")"; return (dout); } int x[size]={2,4,6}; void main() { vector m; vector n=x; char rep; do { clrscr(); cout<<"\n\n\t\t\tOverloading operator with friends"; cout<<"\n\tINPUT"; cout<<"\n\t\tEnter elements of vector m: "; cin>>m; cout<<"\n\tOUTPUT"; cout<<"\n\t\t\tm= "<<m; vector p,q; p=2*m; q=n*2; cout<<"\n\n\t\t\tp= "<<p; cout<<"\n\n\t\t\tq= "<<q; cout<<"\n\t\t Do u want to continue (y/n) : "; rep=getche(); getch(); }while(rep=='y' || rep=='Y'); }

Department of MCA

Lecturer: S. Jayasankari

19

C++ Lab Manual OUTPUT Overloading operator with friends INPUT Enter elements of vector m: 10 20 30 OUTPUT m= (10,20,30) p= (20,40,60) q= (4,8,12) Do u want to continue (y/n) : n

Department of MCA

Lecturer: S. Jayasankari

20

C++ Lab Manual HYBRID INHERITANCE (Student details)

Department of MCA

7. Develop a c++ program to implement a hybrid inheritance concept. CODING: #include<iostream.h> #include<conio.h> #include<iomanip.h> class student { protected: int rollno; char *name; public: void getval(int a, char *s) { rollno=a; name=s; } void putval() { cout<<"\n\t\tRoll No.: "<<rollno <<"\tName: "<<name; } }; class test : public student { protected: float part1, part2; public: void getmarks(float x, float y) { part1=x; part2=y; } void putmarks() { cout<<"\n\t\tMarks Obtained"; cout.setf(ios::showpoint); cout<<"\n\t\t\tMaths : "<<setw(6) <<setprecision(2)<<part1<<"\n"; cout<<"\t\t\tEnglish : "<<setw(6) <<setprecision(2)<<part2<<"\n"; }}; class sports { Lecturer: S. Jayasankari 21

C++ Lab Manual protected: float score; public: void getscore(float s) { score = s; } void putscore() { cout<<"\n\t\t Sports weight"; cout.setf(ios::showpoint); cout<<"\t\t\tScore : "<<setw(6) <<setprecision(2)<<score<<"\n"; } }; class result : public test, public sports { float total; public: void display(); }; void result :: display() { total=part1+part2+score; cout<<"\n\tOUTPUT"; cout<<"\n\t\t--------------------------------------"; cout<<"\n\t\t Student Details"; cout<<"\n\t\t--------------------------------------"; putval(); cout<<"\n\t\t--------------------------------------"; putmarks(); putscore(); cout<<"\n\t\t--------------------------------------"; cout.setf(ios::showpoint); cout<<"\n\t\t Total: "<<setw(6) <<setprecision(2)<<total; cout<<"\n\t\t--------------------------------------"; }

Department of MCA

Lecturer: S. Jayasankari

22

C++ Lab Manual void main() { result stu1; int stdno; char stuname[20],rep; float m1,m2,swei; do { clrscr(); cout<<"\n\n\n\t\t\t Hybrid Inheritance"; cout<<"\n\tINPUT"; cout<<"\n\n\t\t Enter the student number : "; cin>>stdno; cout<<"\n\t\t Enter the student name : "; cin>>stuname; cout<<"\n\t\t Enter the marks for maths : "; cin>>m1; cout<<"\n\t\t Enter the marks for english : "; cin>>m2; cout<<"\n\t\t Enter the sports weightage : "; cin>>swei; getch(); clrscr(); stu1.getval(stdno, stuname); stu1.getmarks(m1,m2); stu1.getscore(swei); stu1.display(); cout<<"\n\n\n\t\t Do u wnat to continue (y/n): "; cin>>rep; }while(rep=='y' || rep=='Y'); }

Department of MCA

Lecturer: S. Jayasankari

23

C++ Lab Manual

Department of MCA

OUTPUT Hybrid Inheritance INPUT Enter the student number : 1001 Enter the student name Enter the marks for maths : G.Saicharan : 100

Enter the marks for english : 90 Enter the sports weightage : 98

OUTPUT ---------------------------------------------------Student Details ---------------------------------------------------Roll No.: 1001 Name: G.Saicharan ---------------------------------------------------Marks Obtained Maths : 100.00 English : 90.00 Sports weight Score : 98.00 ---------------------------------------------------Total : 288.00 ---------------------------------------------------Do u want to continue (y/n): n

Lecturer: S. Jayasankari

24

C++ Lab Manual MULTIPLE INHERITANCE ( Book Details)

Department of MCA

8. To write a C++ program for getting and displaying book details using multiple inheritance. CODING: //Multiple Inheritance #include<iostream.h> #include<conio.h> class b { protected: char bname[25], author[25]; float price; public: void get1() { cout<<"\nEnter the book name : "; cin>>bname; cout<<"\nEnter the author name: "; cin>>author; cout<<"\nEnter the price : "; cin>>price; }}; class b2 { protected: char pname[25]; int year, edition; public: void get2() { cout<<"\nEnter the publisher name: "; cin>>pname; cout<<"\nEnter the year : "; cin>>year; cout<<"\nEnter the edition : "; cin>>edition; } };

Lecturer: S. Jayasankari

25

C++ Lab Manual class d : public b, public b2 { int ncopy; float total; public: void get3() { cout<<"\nEnter the no. of copies: "; cin>>ncopy; total = ncopy * price; } void put() { clrscr(); cout<<"\n\t\tBook Details"; cout<<"\n\tBook name : "<<bname; cout<<"\n\tAuthor : "<<author; cout<<"\n\tPublisher name : "<<pname; cout<<"\n\tEdition : "<<edition; cout<<"\n\tYear : "<<year; cout<<"\n\tNo. of copies : "<<ncopy; cout<<"\n\tPrice : "<<price; cout<<"\n\tTotal amount : "<<total; } }; void main() { char n; d z; do { clrscr(); cout<<"\n\t\t\tBook author details\n"; z.get1(); cout<<"\n\t\t\tBook publishers details\n"; z.get2(); cout<<"\n\t\t\tOrdering details\n"; z.get3(); cout<<"\n\t\t\tBook details output"; z.put(); cout<<"\n\n\tDo u want to continue: "; cin>>n; }while(n=='y'||n=='Y'); }

Department of MCA

Lecturer: S. Jayasankari

26

C++ Lab Manual OUTPUT: Book author details Enter the book name : Microprocessor Enter the author name: Mathur Enter the price : 400.50 Book publishers details Enter the publisher name: PCL Enter the year Enter the edition : 2005 :8 Ordering details Enter the no. of copies: 10 Book Details Book name : Microprocessor Author : Mathur Publisher name : PCL Edition :8 Year : 2005 No. of copies : 10 Price : 400.50 Total amount : 4005 Do u want to continue: n

Department of MCA

Lecturer: S. Jayasankari

27

C++ Lab Manual VIRTUAL FUNCTION (Area calculation of different shapes)

Department of MCA

9. To write a C++ program to calculate are of rectangle, triangle and circle using the virtual function concept. CODING: #include<iostream.h> #include<math.h> #include<stdlib.h> #include<conio.h> class tri { protected: float b,h; double d; public: void getdata(int x, int y); virtual void show(); }; void tri::getdata(int x, int y) { b=x; h=y;} void tri :: show() { cout<<"\nArea of triangle: "; d=b*h*0.5; cout<<d; } class rec:public tri { protected: double t; public: void show(); }; void rec :: show() { cout<<"\nArea of rectangle: "; t=b*h; cout<<t; } class cir:public tri { Lecturer: S. Jayasankari 28

C++ Lab Manual protected: double t; public: virtual void show(); }; void cir::show() { cout<<"\nArea of circle: "; t=b*b*3.14; cout<<t; } void main() { clrscr(); tri q; rec c; cir r; tri *ptr; int ch, x, y; clrscr(); do { cout<<"\n\n\n\tMENU"; cout<<"\n\n\t1. Area of a triangle"; cout<<"\n\t2. Area of rectangle"; cout<<"\n\t3. Area of circle"; cout<<"\n\t4. Exit"; cout<<"\n\tEnter ur choice: "; cin>>ch; switch(ch) { case 1: cout<<"\n\tArea of a triangle"; ptr=&q; cout<<"\nEnter the values of breath and height: "; cin>>x>>y; ptr->getdata(x,y); ptr->show(); break;

Department of MCA

Lecturer: S. Jayasankari

29

C++ Lab Manual case 2: cout<<"\n\tArea of a rectangle"; ptr=&c; cout<<"\nEnter the values of breath and height: "; cin>>x>>y; ptr->getdata(x,y); ptr->show(); break; case 3: cout<<"\n\tArea of a circle"; ptr=&r; cout<<"\nEnter the radius: "; cin>>x; ptr->getdata(x,x); ptr->show(); break; case 4: break; } }while(ch!=4); } OUTPUT: MENU 1. Area of a triangle 2. Area of rectangle 3. Area of circle 4. Exit Enter ur choice: 1 Area of a triangle Enter the values of breath and height: 2 3 Area of triangle: 3

Department of MCA

Lecturer: S. Jayasankari

30

C++ Lab Manual

Department of MCA

MENU 1. Area of a triangle 2. Area of rectangle 3. Area of circle 4. Exit Enter ur choice: 2 Area of a rectangle Enter the values of breath and height: 2 3 Area of rectangle: 6 MENU 1. Area of a triangle 2. Area of rectangle 3. Area of circle 4. Exit Enter ur choice: 3 Area of a circle Enter the radius: 3 Area of circle: 28.26

MENU 1. Area of a triangle 2. Area of rectangle 3. Area of circle 4. Exit Enter ur choice: 4

Lecturer: S. Jayasankari

31

C++ Lab Manual FILE UPDATION

Department of MCA

10. To develop a c++ program to implement random access in files. CODING: #include<fstream.h> #include<iostream.h> #include<iomanip.h> #include<conio.h> #include<stdlib.h> class INVENT { char name[10]; int code; float cost; public: void getdata(void); void putdata(void); }; void INVENT::getdata(void) { cout<<"\nEnter the name:"; cin>>name; cout<<"\nEnter the code:"; cin>>code; cout<<"\nEnter the cost:"; cin>>cost; } void INVENT::putdata(void) { cout<<"\n"; cout<<setiosflags(ios::left)<<setw(8)<<code<<setiosflags(ios::left)<<setw(8)<<name<<s etiosflags(ios::right)<<setw(8)<<setprecision(2)<<setw(10); cout.setf(ios::showpoint); cout<<cost<<endl<<"\t"; }

Lecturer: S. Jayasankari

32

C++ Lab Manual

Department of MCA

void main() { clrscr(); INVENT item; int ch; fstream inoutfile; inoutfile.open("ABI.DAT",ios::ate|ios::in|ios::out|ios::binary); do { clrscr(); cout<<"\nMain Menu"; cout<<"\n\t1.Add an item\n\t2.Update an item\n\t3.Display an item\n\t4.Exit"; cout<<"\nEnter your choice:"; cin>>ch; switch(ch) { case 1: clrscr(); cout<<"\n\tAdd an item:"; inoutfile.seekg(0,ios::end); item.getdata(); inoutfile.write((char *)&item,sizeof(item)); cout<<"\nItem stored in the file."; break; case 2: clrscr(); int itemno; cout<<"\n\tEnter item no. to be updated:"; cin>>itemno; inoutfile.seekg(0,ios::end); int totitem=inoutfile.tellg()/sizeof(item); if(itemno>totitem) { cout<<"\n\tCannot update"; break; } int loc=(itemno-1)*sizeof(item); if(inoutfile.eof())inoutfile.clear(); inoutfile.seekg(loc); cout<<"\n\tEnter the new value:"; item.getdata(); inoutfile.write((char *)&item,sizeof(item))<<flush; cout<<"\nStock Updated"; break; case 3: clrscr(); Lecturer: S. Jayasankari 33

C++ Lab Manual inoutfile.seekg(0,ios::beg); cout<<"\n\tCurrent Stock"; cout<<"\n\t-------------"; cout<<"\nCode\tName\tPrice\n"; cout<<"---------------------------------------------"; while(inoutfile.read((char *)&item,sizeof(item))) { item.putdata(); } int tot=inoutfile.tellg(); int noobj=tot/sizeof(item); inoutfile.clear(); cout<<"\n\tTotal no. of bytes:"<<tot; cout<<"\n\tTotal no. of items:"<<noobj; inoutfile.clear(); break; case 4: inoutfile.close(); exit(0); } getch(); } while(ch<=3); }

Department of MCA

OUTPUT: Main Menu 1.Add an item 2.Update an item 3.Display an item 4.Exit Enter your choice:1 Add an item: Enter the name:Pen Enter the code:101 Enter the cost:30 Item stored in the file. Lecturer: S. Jayasankari 34

C++ Lab Manual

Department of MCA

Main Menu 1.Add an item 2.Update an item 3.Display an item 4.Exit Enter your choice:1 Add an item: Enter the name:Pencil Enter the code:102 Enter the cost:10 Item stored in the file. Main Menu 1.Add an item 2.Update an item 3.Display an item 4.Exit Enter your choice:1 Add an item: Enter the name:Scale Enter the code:103 Enter the cost:10 Item stored in the file. Main Menu 1.Add an item 2.Update an item 3.Display an item 4.Exit Enter your choice:3

Lecturer: S. Jayasankari

35

C++ Lab Manual Current Stock Code Name Price --------------------------------------------101 Pen 30.00 102 103 Pencil Scale 10.00 10.00

Department of MCA

Total no. of bytes:48 Total no. of items:3 Main Menu 1.Add an item 2.Update an item 3.Display an item 4.Exit Enter your choice:2 Enter item no. to be updated:2 Enter the new value: Enter the name:Eraser Enter the code:105 Enter the cost:5 Stock Updated Main Menu 1.Add an item 2.Update an item 3.Display an item 4.Exit Enter your choice:3

Lecturer: S. Jayasankari

36

C++ Lab Manual Current Stock Code Name Price --------------------------------------------101 Pen 30.00 105 103 Eraser Scale 5.00 10.00

Department of MCA

Total no. of bytes:48 Total no. of items:3 Main Menu 1.Add an item 2.Update an item 3.Display an item 4.Exit Enter your choice:4 Exit

Lecturer: S. Jayasankari

37

C++ Lab Manual COMMAND LINE ARGUMENTS

Department of MCA

11. To develop a c++ program to implement command line argument . CODING: #include<fstream.h> #include<stdlib.h> #include<conio.h> main(int argc,char *argv[]) { int number[20],i,n; if(argc!=3) { cout<<"\n\t argc="<<argc<<"\n"; cout<<"\n\t Error in arguments"; exit(1); } cout<<"\n\t Enter the number of values:"; cin>>n; cout<<"\n\t Enter the values:"; for(i=0;i<n;i++) cin>>number[i]; ofstream fout1,fout2; fout1.open(argv[1]); if(fout1.fail()) { cout<<"\n\t could not open the file"<<argv[1]<<"\n"; exit(1); } fout2.open(argv[2]); if(fout2.fail()) { cout<<"\n\t could not open the file"<<argv[2]<<"\n"; exit(1); } for(i=1;i<n;i++) { if(number[i]%2==0) fout2<<number[i]<<" "; else fout1<<number[i]<<" "; } fout1.close(); fout2.close(); ifstream fin; Lecturer: S. Jayasankari 38

C++ Lab Manual char ch; for(i=1;i<argc;i++) { fin.open(argv[i]); cout<<"\n\t contents of "<<argv[i]<<"file:"; do { fin.get(ch); cout<<ch; }while(fin); cout<<"\n"; fin.close(); } getch(); } OUTPUT: C:\TC\BIN>command a b Enter the number of values:5 Enter the values:2 3 4 5 6 Contents of a file:3 5 Contents of b file: 2 4 6

Department of MCA

Lecturer: S. Jayasankari

39

C++ Lab Manual EXCEPTION HANDLING

Department of MCA

12. To develop a c++ program to implement Exception Handling using try, throw and catch - keywords. CODING: #include<iostream.h> int main() { int x,a,b; cout<<"\nEnter the values of a & b:"<<endl; cin>>a>>b; x=a-b; try { if(x!=0) cout<<"\n\tThe value of (a/x) is "<<(a/x)<<endl; else throw 0; } catch(int i) { cout<<"\n\tThe exception caught is "<<i<<"\n"; } return 0; } OUTPUT: Enter the values of a & b: 12 6 The value of (a/x) is 2 Press any key to continue

Enter the values of a & b: 5 5 The exception caught is 0 Press any key to continue

Lecturer: S. Jayasankari

40

C++ Lab Manual FUNCTION TEMPLATE 13. To develop a c++ program to implement function template. CODING: #include<iostream.h> #include<conio.h> template<class x> void swapargs(x &a, x &b) { x temp; temp=a; a=b; b=temp; } int main() { clrscr(); int i=10,j=20; double x=.1,y=23.3; char a='x',b='z'; cout<<"\n\tOriginal Arguments:"; cout<<"\n\t-------------------"; cout<<"\nOriginal i,j:"<<i<<" "<<j<<'\n'; cout<<"Original x,y:"<<x<<" "<<y<<'\n'; cout<<"Original a,b:"<<a<<" "<<b<<'\n'; swapargs(i,j); swapargs(x,y); swapargs(a,b); cout<<"\n\tSwapped Arguments:"; cout<<"\n\t------------------"; cout<<"\nSwapped i,j:"<<i<<" "<<j<<'\n'; cout<<"\nSwapped x,y:"<<x<<" "<<y<<'\n'; cout<<"\nSwapped a,b:"<<a<<" "<<b<<'\n'; getch(); return 0; }

Department of MCA

Lecturer: S. Jayasankari

41

C++ Lab Manual OUTPUT: Original Arguments: Original i , j : 10 20 Original x , y : 0.1 23.3 Original a , b : x z Swapped Arguments: Swapped i , j : 20 10 Swapped x , y : 23.3 0.1 Swapped a , b : z x

Department of MCA

Lecturer: S. Jayasankari

42

Potrebbero piacerti anche