Sie sind auf Seite 1von 55

C++ Lab

Program Number Program Name Date implemented

: 1 : Implementation of Classes and Objects. : November22, 2012

The domain is Mobile Store Management System. This program aimed at accepting inputs like customer name, address, details of the mobile purchased such as brand, model, price and quantity with validation for all inputs. It generates bill by calculating the amount by multiplying price per piece and quantity.
/* Lab 1 Classes and Objects Rajesh G */ using namespace std; #include <iostream> #include<stdio.h> #include<stdlib.h> #include <time.h> #include<string.h> #include<conio.h> int stockquan=30; /* This Function Prints Current Date and Time */ int printtime() { time_t mytime; mytime = time(NULL); printf(" \t\t"); printf(ctime( &mytime)); } class billcal { int custid,availmob,reqmob,price; int quantiti; double amt; char name[15],add[20],mobname[10]; char mobno[11],model[10]; public: void customerdet(); void mobiledet(); void display(); void stockcount() { int temp; temp=stockquan-quantiti; cout<<"\n\n\a The Stock Count Now is: "<<temp; } }; /*To Get Customer Deatails */ void billcal :: customerdet() { cout<<"\n--- Customer DETAILS ---"; chknam: cout<<"\nEnter Customer Name: "; cin>>name; int i=0,j=0,flag=0; while(name[i]) { if(!isalpha(name[i])) { Department of Computer Science, Christ University

C++ Lab

cout<<"\n\a\nInvalid Entry!"; i++; goto chknam; } break; } cout<<"\nEnter Customer Address: "; cin>>add; chkmob: cout<<"\nEnter Customer Contact Number: "; cin>>mobno; int mlen; while(mobno[i]) { if (isdigit(mobno[i])) flag=1; else flag=0; i++; } mlen=strlen(mobno); if((mlen<10)||(mlen>10)||(flag==0)) { cout<<"\a\t\n\aInvalid CONTACT NUMBER!!!\nCheck.."; goto chkmob; } } /* To get purchase details */ void billcal :: mobiledet() { cout<<"\n--- Purchase DETAILS ---"; cout<<"\n\nEnter Brand Name: "; cin>>mobname; cout<<"\n\nEnter Model: "; cin>>model; cout<<"\nEnter the Mobile Price: "; cin>>price; cout<<"\nQuantity: "; cin>>quantiti; amt=price*quantiti; } /* Display Details */ void billcal :: display() { printtime(); cout<<"\n\n --- CUSTOMER DETAILS ---"; cout<<"\n Customer Name: "<<name; cout<<"\n Customer Address: "<<add; cout<<"\n Customer Contact Number: "<<mobno; cout<<"\n\n --- MOBILE DETAILS ---"; cout<<"\n Brand Name: "<<mobname; cout<<"\n Price: "<<price; cout<<"\n - - - - - - - - - - - - "; cout<<"\n Bill Amount: "<<amt; cout<<"\n - - - - - - - - - - - - "; }

int main() Department of Computer Science, Christ University

C++ Lab

{ int i,todo,n1; char ch='y',choice,n[1]; float billamt; billcal bil[10]; //object creation cout<<"\t\t--- MOBILE SHOP MANAGEMENT SYSTEM ---"; cout<<"\n\t ---------------------------------------------"; do { cout<<"\nEnter Your Choice: \n\t1.Bill. \n\t2.Exit."; cin>>todo; switch(todo) { case 1: chknum: cout<<"\n\nEnter Number Of Customer Details U Want to Bill: "; fflush(stdin); cin>>n; for(int j=0;j<1;j++) { if(!isdigit(n[j])) { cout<<"\n\a\nInvalid Entry!"; goto chknum; break; } } n1=atoi(n); for(i=0;i<n1;i++) { bil[i].customerdet(); bil[i].mobiledet(); system("cls"); bil[i].display(); } break; case 2: cout<<"\a\a\n\n * * * U HAVE CHOSEN TO EXIT * * *"; break; default: cout<<"\a\a\n\n * * * ERROR * * *"; } cout<<"\n\n Want To Continue.........? "; cin>>ch; }while(ch=='y'); cout<<"\a\a\n- - - END OF PROGRAM - - -"; getch(); return 0; }

Department of Computer Science, Christ University

C++ Lab

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

Program Number Program Name Date implemented

: 2 : Implement Constructors and Destructors with array of Objects. : November 26, 2012

This program is to enter the details of the arrived mobile. The mobile is bought by the shop from the dealer. The program is aimed at accepting the details of the dealer and mobile such as the name, id, and price with proper validations. It is then displayed in a proper format with the total worth.
/* Lab 2.Implement Constructors and Destructors with array of Objects. Rajesh G */ #include<iostream> using namespace std; class mobiles { int dealer_id; string mname; string mtype; string mcompany; float price; double worth; int quan; public: mobiles() //default constructor { dealer_id=1234; mname = "CORBY"; mtype="PRO"; mcompany="SAMSUNG"; quan=25; } ~mobiles() { //destructor cout<<"\nObject of class mobiles destroyed!"; cin.get(); } mobiles(int quan,float price) //parametrized { worth=price*quan; cout<<"\n WORTH IS :"<<worth; } mobiles(mobiles &m2) //Copy { price=m2.price; quan=m2.quan; dealer_id=1234; mname = "CORBY"; mtype="PRO"; mcompany="SAMSUNG"; } void read(); void write(); };

Department of Computer Science, Christ University

C++ Lab

// read data void mobiles::read() { cout<<"\nEnter the cost per Model: "; cin>>price; worth=price*quan; } // display data void mobiles::write() { worth=price*quan; cout<<"\n"<<"Dealer ID :"<<dealer_id<<"\n"<<"Mobile Name:"<<mname<<"\n"<<"Mobile Type: "<<mtype<<"\n"<<"Manufacturer: "<<mcompany; cout<<"\nPrice Per Peice: "<<price; cout<<"\nNET WORTH: "<<worth; cin.get(); } /* Print Header */ void print() { cout<<"\n-------------MOBILE Store Management---------------\n"; } int main() { system("cls"); char choice; mobiles m2; int bill,ch,quantity; float price ; char bill1[3]; print(); cout<<"- - - - - - - - - - - - - - - - - - - - - - - - - - \n"; do { cout<<"\n 1.Normal Quantity \n 2.Define Quantity \n Enter Choice: "; cin>>ch; switch(ch) { case 1: cout<<"\n * * * * Default Constructor Usage * * * * *\n"; m2.read(); m2.write(); break; case 2: cout<<"\n * * * * Parametrized Constructor Usage * * * * *\n"; cout<<"Enter Number of Mobiles Being Bought:"; cin>>quantity; cout<<"Enter Price Per Mobile: "; cin>>price; mobiles m1(quantity,price); break; } cout<<"\n * * * * Copy Constructor Usage * * * * *\n"; mobiles m3(m2); m3.write(); cout<<"\n Do You Wish to Continue: "; cin>>choice; }while(ch=='y'); cout<<"- - - - - END OF PGM - - - - - :("; } Department of Computer Science, Christ University

C++ Lab

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

Program Number Program Name Date implemented

: 3 : Implement a program to demonstrate passing and returning by reference. : November 29, 2012

This program is aimed at comparing of mobile. The mobiles are classified into various categories and the comparison is done on the basis of type. It is then displayed in a proper format with the total worth.
/* Lab 3. Program to demonstrate passing and returning by reference Rajesh G */ #include<iostream> #include<string.h> #include<stdio.h> #include<conio.h> using namespace std; char val[50]; //input validation int isstring(char s[]) { int i=0; while(s[i]) { if(!((s[i]>=65 && s[i]<92) || (s[i]>=97 && s[i]<123) || s[i]==32)) { return 0; } i++; } return 1; } //input validation int isint(char s[]) { int i=0; while(s[i]) { if(!((s[i]>=48 && s[i]<=57))) { return 0; } i++; } return 1; } //input validation int isfloat(char s[]) { int i=0,f=0; while(s[i]) { if(!((s[i]>=48 && s[i]<=57))) { if(f==0&&s[i]=='.') { f=1; i++; Department of Computer Science, Christ University

C++ Lab

continue; } return 0; } i++; } return 1; } class Mobile { int id, type, model_no; float max_dis, base_price; char name[20],ts[10]; double price; public: void read(); void disp(); void selectprice(); Mobile& cmp(Mobile&,int&); }; // Get input data void Mobile::read() { idl: cout<<"\n\n Enter the model number :"; cin>>val; if(!isint(val)) { printf("\nINVALID ID \n"); goto idl; } id=atoi(val); namel: cout<<" Mobile Name :"; fflush(stdin); gets(name); if(!isstring(name)) { printf("\nINVALID NAME \n"); goto namel; } tidl: cout<<"Enter Type ID :\n1.LogiSense\n2.Blackberry\n3.Windows Phone\n4.Android\n"; cin>>val; if(!isint(val)) { printf("\nINVALID TYPE ID \n"); goto tidl; } type=atoi(val); hl: cout<<"Max_dis :"; cin>>val; if(!isfloat(val)) { printf("\nINVALID INPUT \n"); goto hl; } max_dis=atof(val); wl: cout<<"Base_price :"; Department of Computer Science, Christ University

C++ Lab

10

cin>>val; if(!isfloat(val)) { printf("\nINVALID INPUT \n"); goto wl; } base_price=atof(val); selectprice(); } // display information void Mobile::disp() { cout<<"\n\nMobiles details"; cout<<"\nModel number : "<<id; cout<<"\nMobile Name : "<<name; cout<<"\nType : "<<ts; cout<<"\nPrice : "<<price; cout<<"\nMax_dis : "<<max_dis; cout<<"\nBase_price : "<<base_price; } //select the price function void Mobile::selectprice() { switch(type) { case 1: price=15000; strcpy(ts,"LogiSense"); break; case 2: price=20000; strcpy(ts,"BlackBerry"); break; case 3: price=25000; strcpy(ts,"Windows"); break; case 4: price=30000; strcpy(ts,"Android"); break; } } // Comparing mobiles function Mobile& Mobile::cmp(Mobile &a,int &s) { if(type == a.type) s=1; if(type > a.type) return *this; else return a; } // Print Domain Header void head() { cout<<"\n***************************************************"; cout<<"\n Mobile Store Management"; cout<<"\n***************************************************\n\n"; Department of Computer Science, Christ University

C++ Lab

11

} int main() { Mobile x,y,z; int s=0; head(); cout<<"\n\nEnter Two Details to Compare the Mobiles "; cout<<"\n\n First :"; x.read(); cout<<"\n\n Second :"; y.read(); z=x.cmp(y,s); system("cls"); head(); if(s==0) { cout<<"Details of Mobiles with More Features"; z.disp(); } else { cout<<"Both have same features \nDetails are "; x.disp(); y.disp(); } getch(); }

Department of Computer Science, Christ University

C++ Lab

12

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

13

Program Number Program Name Date implemented

: 4 : Demonstrate Function Overloading. : December 06, 2012

This program is aimed at accepting inputs like transaction id, month of purchase, name of the mobile, brand name, number of pieces, cost. It calculates the bill amount and displays it as result.

/* Lab 4. Demonstrate Function Overloading.Rajesh G */ #include <iostream> #include <string.h> using namespace std; /*this prints current date and time */ int printtime() { time_t mytime; mytime = time(NULL); printf(" \n\n\t\t"); printf(ctime( &mytime)); } class mobile { int tran_id; string mobname; string company; public: float price; string month; int quan; mobile() //default constructor { tran_id=0; mobname = ""; company=""; price=0.0; quan=0; } void read(); void write(); long double bill(int, float, float); long double bill(int, float); }; //To display details void mobile::read() { cout<<"\nEnter the Transaction Id: "; cin>>tran_id; cout<<"\n Enter the Month of Purchase: "; cin>>month; s: cout<<"\nEnter the Mobile Name: "; cin>>mobname; for(int i=0;i<mobname.length();i++) { if(!isalpha(mobname[i])) { cout<<"\nYou have entered an invalid character!Please re-enter!"; goto s; Department of Computer Science, Christ University

C++ Lab

14

} } r: cout<<"\nEnter Brand Name: "; cin>>company; for(int i=0;i<company.length();i++) { if(!isalpha(company[i])) { cout<<"\nYou have entered an invalid character!Please re-enter!"; goto r; } } cout<<"\nEnter the cost of the mobile: "; cin>>price; cout<<"\nEnter the number of pieces purchased: "; cin>>quan; } /* displays info */ void mobile::write(){ printtime(); cout<<"\n TRANSACTION ID: "<<tran_id; cout<<"\n Mobile name: "<<mobname; cout<<"\n Manufacturing company: "<<company; cout<<"\n Number of pieces recieved: "<<quan; cout<<"\n Mobile price: "<<price; cin.get(); } /* calculates the total amount with vat */ long double mobile::bill(int quan,float price,float rt) { long double amt,temp; temp=quan*price; amt=temp-(temp*(rt/100)); return amt; } /* calculates the total amount without vat */ long double mobile::bill(int quan,float price) { long double amt; amt=quan*price; return amt; } /* prints header */ void print() { cout<<"\n-------------Mobile Store Management---------------\n"; } int main() { system("cls"); print(); mobile l; long double total; l.read(); system("cls"); print(); string tempmonth="dec"; Department of Computer Science, Christ University

C++ Lab

15

bool tme=tempmonth.compare(l.month); if(tme) { total=l.bill(l.quan,l.price); l.write(); cout<<"\n- - - - - - - - - - - - - - - - - - - - -"; cout<<"\n The bill amount is: " <<total; cout<<"\n- - - - - - - - - - - - - - - - - - - - -"; } else { cout<<"\a\a\n Congrats!!! You Have Availed Your Xmas Discount!!!!!\n"; total=l.bill(l.quan,l.price,12.00); l.write(); cout<<"\n- - - - - - - - - - - - - - - - - - - - -"; cout<<"\n The bill amount is: " <<total; cout<<"\n- - - - - - - - - - - - - - - - - - - - -"; } cin.get(); }

Department of Computer Science, Christ University

C++ Lab

16

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

17

Program Number Program Name Date implemented

: 5 : Implement pointer sort operation. : December 06, 2012

This program is aimed at accepting inputs like number of mobiles, names of the mobiles. The names are sorted and printed in alphabetical order as result. The time of sorting is also printed along with the stock details.
/* Lab 5. Implementation of pointer sort Rajesh G */ #include <iostream> #include <string.h> #include <conio.h> #include <iomanip> using namespace std; /* print current date &time */ int printtime() { time_t mytime; mytime = time(NULL); printf("\t"); printf(ctime( &mytime)); } class mobile { private: char name[20]; float rate; public: void getdata() { cin>>name; cin>>rate; } void putdata() { cout<<"\n"<<name; cout<<" "<<rate; } char *getname() { return name; } }; int main() { int n,i; void sort(mobile **,int); mobile *mob[10]; cout<<"Enter total no of mobiles: "; cin>>n; for(i=0;i<n;i++) { cout<<"Enter the name of the mobile "<<i<<": "; mob[i]=new mobile ; mob[i]->getdata(); } cout<<"\n Before Sorting Stock: \n"; Department of Computer Science, Christ University

C++ Lab

18

for(int i=0;i<n;i++) { mob[i]->putdata(); } sort(mob,n); cout<<"\n After Sorting Stock is: \n"; cout<<"\n STOCK SORTED ON: "; printtime(); for(i=0;i<n;i++) { mob[i]->putdata(); } getch(); return 0; } /* Sort Operation */ void sort(mobile **s,int n) { void swap(mobile **,mobile **); for(int i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) swap(s+i,s+j); } } /* Swap Operation */ void swap(mobile **m1,mobile **m2) { if(strcmp((*m1)->getname(),(*m2)->getname())>0) { mobile *temp=*m1; *m1=*m2; *m2=temp; } }

Department of Computer Science, Christ University

C++ Lab

19

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

20

Program Number Program Name Date implemented

: 6 : Implement operator overloading. : December 11, 2012

This program is aimed to input name of the mobile, model name, company name, and price. The program gives the user an option to add price of two mobiles, find difference in price of two mobiles, add a mobile to list or remove a mobile from list. The details of the mobile are displayed as output.
/* Lab 6. Implementation Operator Overloading Rajesh G */ #include <iostream> #include <conio.h> #include <string.h> #include <iomanip.h> using namespace std; int n=0; class mobile { string mobilename; int mobile_id; float price; public: int neew; mobile() { mobilename=""; mobile_id=0; neew=0; price=0.0; } //To read input void read() { cout<<"\nEnter the mobile name: "; cin.ignore(); getline(cin,mobilename); o: cout<<"\nEnter model number: "; cin>>mobile_id; if(mobile_id < 0 ) { cout<<"\nInvalid number! Re-Enter!"; goto o; } cout<<"\nEnter the mobile price: "; cin>>price; } //To display the data void echo() { cout<<setw(20)<<mobilename; cout<<setw(20)<<mobile_id; cout<<setw(15)<<price; } //pre increment void operator++() { cout<<"\npre increment Value : "<<++neew; } Department of Computer Science, Christ University

C++ Lab

21

//post increment void operator++(int) { cout<<"\npost increment Value : "<<n++; } //pre decrement void operator--() { cout<<"\npre Decrement Value : "<<--n; } //post decrement void operator--(int x) { cout<<"\npost decrement Value : "<<neew--; } //Overload equal to operator mobile operator=(mobile st) { mobilename=st.mobilename; mobile_id=st.mobile_id; return *this; } //Overload plus operator float operator+(mobile s1) { float pric = s1.price+price; return pric; } //Overload minus operator float operator-(mobile s1) { float pric = price-s1.price; return pric; } //Overload [] operator funcion void operator [](int increment) { price=1000; float pric=price+increment; cout<<"Final price"<<pric; } //Overload () operator funcion void operator()() { cout<<"\n-------------Mobile Management System---------------\n"; } }; int main() { mobile s[5], new1; int ch; char c='y'; while((c=='y')||(c=='Y')) Department of Computer Science, Christ University

C++ Lab

22

{ system("cls"); s[0](); cout<<"\n\nEnter 1 to enter a new mobile details"; cout<<"\nEnter 2 to remove a mobile"; cout<<"\nEnter 3 to find the total price or difference of two mobile"; cout<<"\nEnter 4 to add increment\n"; cin>>ch; if(((ch==2)||(ch==3))&&(n==0)) { cout<<"\n Sorry there are no mobile!"; exit(0); } switch(ch) { case 1: in: s[n].read(); cout<<"\nNew mobileadded!"; s[n]++; cout<<"\nNumber of mobile: " <<n; cout<<"\nAdd more mobile? (y/n): "; char in; cin>>in; if((in=='y')||(in=='Y')) goto in; else { cout<<"\nPreincrement object\n"; new1.read(); ++new1; cout<<"\nValue after increment: "<<new1.neew; } break; case 2: pp: cout<<"\nNumber of mobile available: " <<n; cout<<"\n\nMobile Available"; cout<<"\n----------------"; cout<<"\n\nMobile number"<<setw(15)<<"Mobile name"<<setw(15)<<"Model number"<<setw(15)<<"Mobile price"; for(int i=0;i<n;i++) { cout<<"\n"<<setw(5)<<i+1; s[i].echo(); } cout<<"\n"; cout<<"\nEnter the mobile number to be removed from the list: "; int pn; cin>>pn; if((pn<0)||(pn>n)) { cout<<"\nWrong mobile number!"; goto pp; } s[pn-1]=(s[n-1]); --s[n-1]; cout<<"\nRemove more mobile? (y/n): "; char out; cin>>out; if((out=='y')||(out=='Y')) goto pp; else { cout<<"\nPreincrement object\n"; new1.echo(); new1--; Department of Computer Science, Christ University

C++ Lab

23

cout<<"\nValue after decrement: "<<new1.neew; } break; case 3: cout<<"\n\nMobile Available"; cout<<"\n\nMobile number"<<setw(15)<<"Mobile name"<<setw(15)<<"Model number "<<setw(15)<<"Mobile price"; for(int i=0;i<n;i++) { cout<<"\n"<<i+1; s[i].echo(); } cout<<"\n"; cout<<"\n1.Calculate the price of 2 mobiles \n2.Calculate the difference between 2 pricearies\n Enter your choice: "; int ck, p1,p2; cin>>ck; cout<<"\n\nEnter the two mobile numbers: "; float price; cin>>p1>>p2; p1--; p2--; switch(ck) { case 1:price=s[p1]+s[p2]; cout<<"\n\nThe total of the price of these 2 mobiles is: "<<price; break; case 2:price=s[p1]-s[p2]; cout<<"\n\nThe difference in the prices of these 2 mobiles is: "<<price; break; default:cout<<"\nWrong choice!"; break; } break; case 4:cout<<"\nEnter increment in price for 5 mobiles"; int incr; for(int i=0;i<5;i++) { cout<<"\nEnter increment amount for "<<i+1<<"mobile"; cin>>incr; s[i][incr]; } break; default: cout<<"\nWrong choice!"; break; } cout<<"\nDo you wish to add or remove more mobiles? (y/n): "; cin>>c; } getch(); return 0; }

Department of Computer Science, Christ University

C++ Lab

24

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

25

Program Number Program Name Date implemented

: 7 : Implement a program to demonstrate friend functions and friend classes. : December 11, 2012

This program is to enter the details of the different category mobiles. The mobiles are bought by the shop from the dealer. The program is aimed at accepting the details of the mobile such as the name, id, and price with proper validations. Stock worth is calculated and it is then displayed in a proper format with the total worth.
/* Lab 7. Implement program to demonstrate friend functions and friend classes. Rajesh G */ #include<iostream> using namespace std; class android; //forward declaration class windows { int pno; float price; public: void in() { cout<<"\nEnter the number of windows phone received: "; cin>>pno; cout<<"\nEnter the total price of stock: "; cin>>price; } friend class finalamt; friend int compared(windows l1, android l2); }; class android { int pno; float price; public: void inp() { cout<<"\nEnter the number of andoird phones received: "; cin>>pno; cout<<"\nEnter the total price of stock: "; cin>>price; } friend int compared(windows l1, android l2); }; class finalamt { int quantity; float amt; public: void cal(windows l1) { cout<<"\n\n Enter the stock in store for the product "<<l1.pno<<" : "; cin>>quantity; amt=quantity * l1.price; } void output(windows l1) { cout<<"\n\n Stocks received: "<<l1.pno; Department of Computer Science, Christ University

C++ Lab

26

cout<<"\n Price: "<<l1.price; cout<<"\nQuantity: "<<quantity; cout<<"\n\nTotal stock amount: "<<amt; cin.get(); } }; /* function to compare the price of two products*/ int compared(windows l1,android l2) { return l1.price > l2.price?l1.pno:l2.pno; } // function to print domain header inline void print() { cout<<"\n-------------Mobile management system---------------\n"; } int main() { system("cls"); print(); windows l1; l1.in(); android l2; l2.inp(); int model = compared(l1,l2); cout<<"\n\nThe stock with greater price: "<<model; finalamt fa; fa.cal(l1); system("cls"); print(); fa.output(l1); cin.get(); int p; cin>>p; return 0; cin.get(); }

Department of Computer Science, Christ University

C++ Lab

27

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

28

Program Number Program Name Date implemented

: 8 : Implement Inheritance. : December18, 2012

This program is aimed at accepting inputs like model number, mobile name, company name, date of release, colors available, maximum discount that can be allowed and user ratings. The entered details are displayed in neat format as result.

/* Lab 8. Implementation of inheritance Rajesh G */ #include<iostream> #include<conio.h> using namespace std; class basic { protected: int model_no; char name[40]; char company[40]; char dor[12]; public: void get_input(); void print_data(); }; /* This function is used to input basic basic information */ void basic::get_input() { cout<<"\nEnter model-no:"; cin>>model_no; cout<<"\nEnter mobile name:"; cin.ignore(); cin.getline(name,100); cout<<"\nEnter Brand Name:"; cin.getline(company,20); cout<<"\nEnter date of release of this model :"; cin.getline(dor,12); } /* This function is used to display basic basic information */ void basic::print_data() { cout<<"\n\nModel:"<<model_no; cout<<"\n\nName:"<<name; cout<<"\n\nDate of release:"<<dor; cout<<"\n\nCompany:"<<company; } //Creating an class with name mo_info class mo_info:virtual protected basic { protected: string colors[15]; int n; public: void get_colors(); void print_colors(); };

Department of Computer Science, Christ University

C++ Lab

29

/* This function is used to input colors of six subjects */ void mo_info::get_colors() { cout<<"\nEnter number of colors: "; cin>>n; cout<<"\nEnter colors: \n-------------------\n"; for(int i=0;i<n;i++) { cout<<"\nColor "<<i+1<<":"; cin>>colors[i]; } } /* This function is used to display colors of six subjects */ void mo_info::print_colors() { for(int i=0;i<n;i++) cout<<"\n\nColors"<<i+1<<": "<<colors[i]<<"\n"; cin.get(); } //Creating a class with name rating class rating:virtual public basic { protected: int p_g; float bp; float dis; public: void get_grade(); void print_grade(); }; void rating::get_grade() { cout<<"\nEnter the basic price for this model: "; cin>>bp; cout<<"\nEnter the max discount to be allowed: "; cin>>dis; while(true) { cout<<"\nEnter rating for this model(1/2/3/4):"; cin>>p_g; if(p_g==1||p_g==2||p_g==3||p_g==4) break; cout<<"\nInvalid grade entered!!!"; } } /* This function is used to display performance grade of a basic */ void rating::print_grade() { cout<<"\nRating:"<<p_g; cin.get(); } class result:public mo_info,public rating { private: float grand_total; public: void get_info(); void display(); Department of Computer Science, Christ University

C++ Lab

30

}; /* to get all the information(basic, performance in mo_info and rating) of a basic */ void result::get_info() { get_input(); get_colors(); get_grade(); } /* This function is used to display all the information of a basic along with total */ void result::display() { grand_total=bp-dis; print_data(); print_colors(); print_grade(); cout<<"\n\nPrice of the model is: "<<grand_total<<"\n"; cin.get(); } int main() { int num,i; cout<<"\n ***************Mobile Management System***************"; cout<<"\n"; while(true) { cout<<"\n\nEnter number of mobiles:"; cin>>num; if(num>0)//Checking validity of number of basics break; cout<<"Invalid number given!!\n"; } result *mob=new result[num];//Dynamically allocating number of instances for(i=0;i<num;i++) { cout<<"\n\nEnter record for mobile no:"<<i+1<<"\n\n----------------------------"; mob[i].get_info(); } system("cls"); cout<<"\n\t\tDisplaying details \n--------------------------------------------------------\n"; for(i=0;i<num;i++) { cout<<"\n model no:"<<i+1<<"\n\n------------------------"; mob[i].display(); } cout<<"\n"; delete[]mob; return 0; getch(); }

Department of Computer Science, Christ University

C++ Lab

31

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

32

Department of Computer Science, Christ University

C++ Lab

33

Program Number Program Name Date implemented

: 9 : Implement Abstract and Virtual Class. : January 3, 2013

This program is aimed at accepting the inputs like privileged and new customer. If new customer then details such as name, email id and phone number are taken as input. If privileged customer then details like model number, mobile name, company name, basic price, tax are taken as input. It calculates bill. It displays the details with bill amount as result.
/* Lab 9. Implementation of Abstract and virtual class Rajesh G */ #include<iostream> #include <string.h> #include<conio.h> using namespace std; class store { public: virtual long double bill() = 0; }; class mobile:public store { protected: int model_no; string mname; string company; float price; int quan; public: void read(); void write(); /* to calcutate total amount with default rate */ long double bill() { long double amt; amt=quan*price; return amt; } /* to calcutate total amount with new rate*/ long double bill(float rate) { long double amt,tot; amt=price*(rate/100); amt=price+amt; tot=amt*quan; return tot; } }; /* to read the mobil details */ void mobile::read() { o: cout<<"\nEnter the model number: "; cin>>model_no; if(model_no < 0 ) { Department of Computer Science, Christ University

C++ Lab

34

cout<<"\nInvalid ID! Re-Enter!"; goto o; } k: cout<<"\nEnter the mobile name: "; cin.ignore(); getline(cin,mname); for(int i=0;i<mname.length();i++) { if((isalpha(mname[i]))||(mname[i]==' ')) { } else { goto k; } } cout<<"\nEnter manufacturing company name: "; getline(cin,company); p:cout<<"\nEnter the cost of the mobile: "; cin>>price; if(price < 0 ) { cout<<"\nInvalid price! Re-Enter!"; goto p; } q: cout<<"\nEnter the number of pieces purchased: "; cin>>quan; if(quan < 0 ) { cout<<"\nInvalid pieces! Re-Enter!"; goto q; } } // display result void mobile::write() { cout<<"\n\nmobile name: "<<mname; cout<<"\nmodel number: "<<model_no; cout<<"\nManufacturing company: "<<company; cout<<"\nNumber of pieces recieved: "<<quan; cout<<"\nmobile price: "<<price; cin.get(); } class customer { char name[20]; int ph; char id[30]; public: void read_data() { cout<<"\n Enter the customer's name: "; cin>>name; cout<<"\n Enter the phone number: "; cin>>ph; cout<<"\n Enter the email-id: "; cin>>id; } void write_data() { cout<<"\n Name: "<<name; Department of Computer Science, Christ University

C++ Lab

35

} }; //To print header inline void print() { cout<<"\n-------------Mobile Store Management---------------\n"; } int main() { long double total; system("cls"); print(); cout<<"\n Enter 1 if billing is for privilaged customer \n Enter 2 for new customer: "; int ch1; cin>>ch1; if(ch1==1) { a: mobile v; v.read(); cout<<"\n1.Enter 1 to Prepare the bill with VAT"<<"\n2. Enter 2 to Prepare the bill with base value of the bill\n"; int ch; cin>>ch; if(ch==1) { r: cout<<"\n Enter the VAT rate (Ex: if VAT rate is 15.25% enter 15.25):"; float rate; cin>>rate; if(rate < 0 ) { cout<<"\nInvalid ID! Re-Enter!"; goto r; } total=v.bill(rate); v.write(); cout<<"\nThe bill amount with the VAT is: " <<total; } else if(ch==2) { total=v.bill(); v.write(); cout<<"\nThe bill amount with the VAT is: " <<total; } else { cout<<"You Entered a wrong choice!"; } } else { customer c; c.read_data(); //c.write_data(); goto a; } cin.get(); return 0; }

Department of Computer Science, Christ University

C++ Lab

36

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

37

Program Number Program Name Date implemented

: 10 : Implement Templates : January 28, 2013

This program is aimed at accepting inputs like model number, mobile name, company name, base price. The bill amount is calculated and details are printed in neat format as result.
/* Author Rajesh.G*/ #include <iostream> #include <string> #include <iomanip> #include<conio.h> using namespace std; template <class x,class y,class z> //To display amount with formatting void display(x a,y b,z c,x d) { cout<<"\n"<<setw(10)<<a<<setw(20)<<b<<setw(20)<<c<<setw(20)<<d; } template<class x> //To display amount with formatting void display(x a,x b) { cout<<setw(10)<<a<<setw(10)<<b<<"\n"; } template<class x> //To display amount with formatting void display(x a) { cout<<"\n Amount "; cout<<a; } //To display header inline void print() { cout<<"\n-------------Mobile Store Management---------------\n"; } class mobile { public: int model; string mobname; float price; int quan; void read(); long double bill(float,mobile l[],int); };

Department of Computer Science, Christ University

C++ Lab

38

// read mobile info void mobile::read() { cout<<"\nEnter the model number: "; cin>>model; cout<<"\nEnter the mobile name: "; cin.ignore(); getline(cin,mobname); cout<<"\nEnter the cost of the mobile: "; cin>>price; cout<<"\nEnter the number of pieces purchased: "; cin>>quan; } /* function calculates the bill amount*/ long double mobile::bill(float tr,mobile l[],int n) { long double amt=0,x=0; for(int i=0;i<n;i++) { x=(l[i].quan)*(l[i].price); amt=amt+x; } tr=(tr/100)*amt; amt=amt+tr; return amt; } int main() { system("cls"); print(); string fname,sname; int n; long double amt; float rt; cout<<"\nEnter the customer's first name: "; getline(cin,fname); cout<<"\nEnter customer's second: "; getline(cin,sname); cout<<"\nEnter the number of mobiles purchased: "; cin>>n; mobile d[n]; for(int i=0;i<n;i++) { d[i].read(); } r: cout<<"\nEnter the VAT rate: "; cin>>rt; if(rt<0) { cout<<"\nInvalid rate!"; goto r; } amt=d[0].bill(rt,d,n); system("cls"); cout<<"\n -------------Bill-------------\n"; cout<<"\nCustomer : "; display(fname,sname); cout<<"\n\n\n " ; cout<<"\n"<<"Model"<<setw(24)<<"Mobile name"<<setw(20)<<" price"<<setw(20)<<"Quantity"; cout<<"\n"<<"----------"<<setw(20)<<"------------"<<setw(20)<<"-------------"<<setw(20)<<"------"; for(int i=0;i<n;i++) Department of Computer Science, Christ University

C++ Lab

39

{ display(d[i].model,d[i].mobname,d[i].price,d[i].quan); } cout<<"\n\n\n \t\t Bill: "<<setw(40); cout<<"\n-------------------------------------"; display(amt); getch(); return 0; }

Department of Computer Science, Christ University

C++ Lab

40

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

41

Program Number Program Name Date implemented

: 11 : Implement Stack Operations. : February 4, 2013

This program is aimed at accepting inputs such as mobile name, quantity, and price. The details are stored and displayed according to user choice.
/* Lab 11. Implementation of Stack Rajesh G */ #include<iostream> #include<conio.h> #define max 10 using namespace std; template<class stk> class stack { stk a[10]; int top; public: stack() { top=-1; } //push logic void push(stk p) { if((top+1)>=max) cout<<"\n Overflow"; else { top++; a[top]=p; } } // pop operation stk pop() { stk item; if(top==-1) { cout<<"\n Underflow"; return(-1); } else { item=a[top]; top--; return item; } } }; class mobile { stack <int> modelno; stack <float> price; stack <int> qua; public: Department of Computer Science, Christ University

C++ Lab

42

//To input data void read() { int id,quan; float cost; char name; cout<<"\n Enter model no: "; cin>>id; cout<<"\n Enter number of mobiles purchased: " ; cin>>quan; cout<<"\n Enter cost: "; cin>>cost; modelno.push(id); price.push(cost); qua.push(quan); } //To display details void disp() { int x=modelno.pop(); int a=qua.pop(); float y=price.pop(); if(x!=-1) { cout<<"\n Details "; cout<<"\n Model number: "<<x<<"\n Quantity: "<<a<<"\n Price: "<<y; float total; total=a*y; cout<<"\n\n Total is : "<<total; } } }; int main() { mobile m; int ch; do { cout<<"\n ------------Mobile Management system---------------"; cout<<"\n 1.Insert details \n 2.Display details with bill amount \n 3.Exit"; cout<<"\n Enter choice: "; cin>>ch; switch(ch) { case 1: m.read(); getch(); break; case 2: m.disp(); getch(); break; case 3:exit(1); break; } }while(ch!=3); getch(); return 0; }

Department of Computer Science, Christ University

C++ Lab

43

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

44

Program Number Program Name Date implemented

: 12 : Implement I/O Manipulators. : February 11, 2013

This program is aimed at accepting the inputs like dealer id, name and number of mobiles sold with validation. User can search for a dealer details or display the details in neat format as result.
/* Lab 12 Implement I/O Manipulators functions used : hex, setw,setfill,setprecisio,endl,dec */ #include<iostream> #include<conio.h> #include<string.h> #include<iomanip.h> using namespace std; int check=0,n; class Dealer { public: string Dealer_name,pass; int Did,num; float com,tcom; /* to Input the dealer Details */ void input() { cout<<"\nEnter the Dealer ID : "; cin>>Did; cout<<"\nEnter the Name : "; cin.ignore(); getline(cin,Dealer_name); cout<<endl<<"Enter number of mobiles sold: "; cin>>num; cout<<"\nEnter the commission : "; cin>>com; comp(); } /* To Display the dress details */ void display() { cout<<endl; cout<<"Dealer Id : "<<Did; cout<<endl<<endl<<"Name : "<<Dealer_name; cout<<endl<<endl<<"Password: "<<hex<<com; cout<<endl<<endl<<"Commission : "<<dec<<com; cout<<endl<<endl<<"Total commision: "<<setprecision(6)<<tcom; cout<<"\n-----------------------------"; } // compute commission amount void comp() { if(num<5) { tcom=com+(0.05*com); } else { tcom=com+(0.10*com); Department of Computer Science, Christ University

C++ Lab

45

} } }; /* To Search for a particular Id being passed as reference */ int searchId(int *num,int lim,Dealer b[]) { int i=0,flag=0; for(i=0;i<lim;i++) { if(b[i].Did==(*num)) { flag=1; break; } } if(flag==1) return i; else return -1; } // to print domain header void print() { cout<<setfill('*'); cout<<setw(134); cout<<"\n\n-------------Mobile Management System---------------\n\n"; cout<<setw(90); } int main() { int k,c,search,temp,flag=0; Dealer b[50]; char ch='y'; system("cls"); print(); while((ch=='y') ||(ch=='Y')) { cout<<"\n\n Menu"; cout<<"\n1. Enter details"; cout<<"\n2. Search for a dealer "; cout<<"\n3. Display details"; cout<<"\n4. Exit: "; cout<<"\n\n Enter your choice : "; cin>>c; switch(c) { case 1:system("cls"); cout<<endl<<"Enter Number Of Dealers: "; cin>>n; for(k=0;k<n;k++) { b[k].input(); system("cls"); } break;

Department of Computer Science, Christ University

C++ Lab

46

case 2:system("cls"); cout<<"\nEnter the Dealer Id you want to search for : "; cin>>search; flag=searchId((&search),n,b); if(flag==-1) cout<<"\n\nSorry! No records found!\n\n"; else cout<<"\n\nThis Dealer is present!\n\n"; b[flag].display(); break; case 3:system("cls"); for(int i=0;i<n;i++) { b[i].display(); } break; case 4:cout<<"\n\nThank You"; getch(); exit(0); default:cout<<"\nWrong Choice!"; break; } cout<<"\nDo You Want to Continue ? (Y/N) : "; cin>>ch; } cout<<"\n\nEnd Of Pgm !!!"; cout<<setfill('$'); cout<<setw(20); getch(); }

Department of Computer Science, Christ University

C++ Lab

47

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

48

Program Number Program Name Date implemented

: 13 : Implement Overloading of << and >> operators. : February 18, 2013

This program is aimed at accepting inputs like model number, mobile name, price per piece, installment option. If installment is taken then discount is not allowed. Otherwise bill amount with discount in calculated and details are displayed as result.
/* Lab 13. Implementation of overloading << & >> Rajesh G */ #include<iostream> #include<conio.h> #include<stdio.h> using namespace std; class mobile { float dis,pri,pri1,tot,tot1; int mno,qua; char mname[20]; public: mobile operator +(mobile y); friend ostream& operator <<(ostream& out,mobile x); friend istream& operator >>(istream& in,mobile &x); //To input mobile information void read() { cout<<"\n Enter the model number: "; cin>>mno; cout<<"\n Enter the model name: "; cin>>mname; cout<<"\n Enter the number of pieces purchased: "; cin>>qua; cout<<"\n Enter the price of 1 piece: "; cin>>pri1; pri=qua*pri1; } }; // To overload + operator mobile mobile::operator +(mobile y) { mobile temp; cout<<"\n Is amount paid in installments? (y/n) "; char ch; cin>>ch; if(ch=='y') { cout<<"\n Sorry no discount can be given !!! "; } else { cout<<"\n Enter the discount percentage: "; cin>>dis; temp.qua=qua+y.qua; temp.tot1=(dis/100)*(pri+y.pri); temp.tot=(pri+y.pri)-temp.tot1; cout<<"\n \n The total amount is :"<<temp.tot; } } Department of Computer Science, Christ University

C++ Lab

49

//To overload extraction operator istream& operator >>(istream& in,mobile &x) { cout<<"\n\n Enter the model number: "; in>>x.mno; cout<<"\n Enter the model name: "; in>>x.mname; cout<<"\n Enter the number of pieces purchased: "; in>>x.qua; cout<<"\n Enter the price of 1 piece: "; in>>x.pri1; x.pri=x.qua*x.pri1; return in; } //To overload insertion operator ostream& operator <<(ostream& out,mobile x) { cout<<"\n The model number: "; out<<x.mno; cout<<"\n The model name: "; out<<x.mname; cout<<"\n Number of pieces purchased: "; out<<x.qua; return out; getch(); } int main() { mobile m1,m2,m3; char ch2; do { cout<<"\n Mobile Management system "; cout<<"\n ----------------------------"; getch(); cout<<"\n\n Enter details of mobile 1 "; cout<<"\n-----------------------------"; cin>>m1; cout<<"\n\n Enter details of mobile 2"; cout<<"\n-----------------------------"; cin>>m2; system("cls"); cout<<"\n Displaying details" ; cout<<"\n --------------------------------"; cout<<m1; getch(); cout<<"\n "; cout<<m2; getch(); m3=m1+m2; cout<<"\n Do you wish to continue? "; cin>>ch2; system("cls"); }while(ch2=='y'); getch(); return 0; }

Department of Computer Science, Christ University

C++ Lab

50

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

51

Program Number Program Name Date implemented

: 14 : Implement Files. : February 25, 2013

This program is to enter the type of product arrived. The details of the product such as name, id and price are the input. The entered details are stored in a text file. The details of the product are read from the file and displayed.
/* Lab 14. Implementation of Files Rajesh G */ #include<iostream> #include<fstream> #include<conio.h> #include<string> using namespace std; class mobile { public: int mid; string mname; float price; mobile() { mid = 0; mname = ""; price = 0; } ~mobile() { } void write(); void read(); void search(); }; /* function to write into a text file */ void mobile::write() { system("cls"); cout<<"\n\n\t\tEnter the Model number:"; cin>>mid; cout<<"\n\n\t\tEnter the Mobile name:"; cin.ignore(); getline(cin,mname); cout<<"\n\n\t\tEnter the Mobile price:"; cin>>price; } /* function that searches the id for the user */ void mobile::search() { system("cls"); char line[20]; fstream fin; fin.open("mobile.txt",ios::in); cout<<"Enter the model number to know if the mobile is available: "; char searchitem[30]; cin>>searchitem; fin.seekg(0,ios::beg); Department of Computer Science, Christ University

C++ Lab

52

while(!fin.eof()) { fin.getline(line,4); if(strcmp(line,searchitem)==0) { cout<<"\n\n\t\t Mobile found\n"; cout<<line<<"\t\t is available in the mobile"; getch(); } } cout<<"\n\n\t\t Mobile not found!"; getch(); fin.close(); } // to print domain header inline void print() { cout<<"\n-------------Mobile Mobile Management---------------\n"; } int main() { system("cls"); int option; mobile t; do { system("cls"); print(); cout<<"\n\n\n\n\t\t1.Write \n\n\t\t2.Read\n\n\t\t3.search \n\n\t\t4.exit"; cout<<"\n\n\n\n\t\tENTER 1/2/3/4\t\t"; cin>>option; ifstream fin; char line[60]; switch(option) { case 1:system("cls"); print(); cout<<"\n\n\n\n\t\tENTER RECORDS INTO FILE"; int option; do { mobile s; cout<<"\n\n\n\n\t\t1.Enter 2.exit"; cout<<"\n\n\n\n\t\tENTER 1/2\t\t"; cin>>option; switch(option) { case 1: s.write(); ofstream fout; fout.open("mobile.txt",ios::out|ios::app); if(fout.is_open()) { fout<<"\n"<<s.mid<<"\t"; fout<<s.mname<<"\t"; fout<<s.price; fout.close(); } ofstream ff; fout.open("mid.txt",ios::out|ios::app); if(ff.is_open()) { Department of Computer Science, Christ University

C++ Lab

53

ff<<"\n"<<s.mid<<"\t"; } break; } }while(option!=2); break; case 2: system("cls"); cout<<"\n\ncontents of the file are"; fin.open("mobile.txt",ios::in); while(!fin.eof()) { fin.getline(line,60); cout<<"\n\n\t\t"<<line; } getch(); break; case 3:t.search(); break; case 4:break; default:cout<<"\n\n\t\tInvalid Option"; break; } }while(option!=4); return 0; }

Department of Computer Science, Christ University

C++ Lab

54

OUTPUT:

Department of Computer Science, Christ University

C++ Lab

55

Department of Computer Science, Christ University