Sei sulla pagina 1di 38

Assignment CSE202T

Submitted By Garima Reg No-11000548 Section- E2R43 Roll No-B32 Submitted ToSanyam Anand

Date of allocation: 24/09/2012

Date of Submission: 20/10/2012

PART A
1. Write a program to create a class named as vehicle having data members like vehicle_type, vehicle_no, cost, model etc and perform following operations like: a. Input data b. Display data c. Delete a record d. Insert a record Use functions to perform tasks where required. Ans:class vehicle { private: char vehicle_type[100],vehicle_no[20],model[50]; float cost; public : vehicle() { cout<<\constructor for vehicle; }

void input() { cout<<\nInput Method for Vehicle; cout<<\nEnter vehicle type\t; cin>> vehicle_type; cout<<\nEnter vehicle number\t; cin>> vehicle_no; cout<<\nEnter model of vehicle\t; cin>>model; cout<<\nEnter cost of vehicle\t; cin>>cost; } void insert() { cout<<\nInsert method for vehicle; input(); } void deelete() { cout<<\nDelete Method for vehicle; } void display() { cout<<\nDisplay method for vehicle; cout<<\nVehicle type:\tvehicle_type; cout<<\nVehicle number:\tvehicle_no; cout<<\nModel of vehicle:\tmodel; cout<<\nCost of vehicle:\tcost; } } void main() { vehicle v(); v.insert(); v.display(); v.deelete();

v.display(); } 2. Design an application for student management system and perform following functions: a. Display data of all students b. Calculate result c. Input data d. Check fee status All data members should be private and member function should be public. Define all appropriate constructors (default constructor, copy constructor) and destructors. Use concept of function overloading to implement the search member function i.e. search on title, author and publication. Ans:- #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <conio.h> #include <iomanip> using namespace std; int main() { FILE *fp, *ft; char another, choice; struct student { char first_name[50], last_name[50]; char course[100]; int section; }; struct student e; char xfirst_name[50], xlast_name[50]; long int recsize; fp=fopen("users.txt","rb+");

if (fp == NULL) { fp = fopen("users.txt","wb+"); if (fp==NULL) { puts("Cannot open file"); return 0; } }

recsize = sizeof(e); while(1) { system("cls"); cout << "\t\t====== STUDENT INFORMATION SYSTEM ======"; cout <<"\n\n "; cout << "\n\n"; cout<<" \n\t\t\t======================"; cout << "\n \t\t\t 1. Add Records"; cout << "\n \t\t\t 2. List Records"; cout << "\n \t\t\t 3. Modify Records"; cout << "\n \t\t\t 4. Delete Records"; cout << "\n \t\t\t 5. Exit Program"; cout<<" \n\t\t\t======================"; cout << "\n\n"; cout << "\t\t\t Select Your Choice ::"; fflush(stdin); choice = getche(); switch(choice) { case '1' : fseek(fp,0,SEEK_END); another ='Y'; while(another == 'Y' || another == 'y') { system("cls"); cout << "Enter the First Name : "; cin >> e.first_name;

cout << "Enter the Last Name : "; cin >> e.last_name; cout << "Enter the Course : "; cin >> e.course; cout << "Enter the Section : "; cin >> e.section; fwrite(&e,recsize,1,fp); cout << "\n Add Another Record (Y/N) "; fflush(stdin); another = getchar(); } break; case '2': system("cls"); rewind(fp); cout << "=== View the Records in the Database ==="; cout << "\n"; while (fread(&e,recsize,1,fp) == 1){ cout << "\n"; cout <<"\nName :: " <<e.first_name <<" "<<e.last_name; //cout << "\n"; cout <<"\nCourse :: " <<e.course ; cout <<"\nSection :: "<<e.section; } cout << "\n\n"; system("pause"); break; case '3' : system("cls"); another = 'Y'; while (another == 'Y'|| another == 'y') { cout << "\n Enter the last name of the student : "; cin >> xlast_name; rewind(fp); while (fread(&e,recsize,1,fp) == 1) { if (strcmp(e.last_name,xlast_name) == 0)

{ cout << "Enter new the Firt Name : "; cin >> e.first_name; cout << "Enter new the Last Name : "; cin >> e.last_name; cout << "Enter new the Course : "; cin >> e.course; cout << "Enter new the Section : "; cin >> e.section; fseek(fp, - recsize, SEEK_CUR); fwrite(&e,recsize,1,fp); break; } else cout<<"record not found"; } cout << "\n Modify Another Record (Y/N) "; fflush(stdin); another = getchar(); } break;

case '4': system("cls"); another = 'Y'; while (another == 'Y'|| another == 'y') { cout << "\n Enter the last name of the student to delete : "; cin >> xlast_name; ft = fopen("temp.dat", "wb"); rewind(fp); while (fread (&e, recsize,1,fp) == 1) if (strcmp(e.last_name,xlast_name) != 0) { fwrite(&e,recsize,1,ft); }

fclose(fp); fclose(ft); remove("users.txt"); rename("temp.dat","users.txt"); fp=fopen("users.txt","rb+"); cout << "\n Delete Another Record (Y/N) "; fflush(stdin); another = getchar(); } break; case '5': fclose(fp); cout << "\n\n"; cout << "\t\t THANK YOU FOR USING THIS SOFTWARE"; cout << "\n\n"; exit(0); } }

system("pause"); return 0; }

3. We can access private data members of class outside the function. Comment on this. Give some appropriate example to support your answer. Ans:-Yes, we can access private data members of class outside the function. But this can not be done simply. We must have to use friend function. A function having keyword friend in function header. A friend function is used for accessing the non-public members of a class. A class can allow non-member functions and other classes to access its own private data, by making them friends. Thus, a friend function is an ordinary function or a member of another class. How to define and use Friend Function in C++ -The friend function is written as any other normal function, except the function declaration of these functions is preceded with the keyword friend. The friend function must have the class to which it is declared as friend passed to it in argument.

Some important points to note while using friend functions in C++ -The keyword friend is placed only in the function declaration of the friend function and not in the function definition. -It is possible to declare a function as friend in any number of classes. -When a class is declared as a friend, the friend class has access to the private data of the class that made this a friend. -A friend function, even though it is not a member function, would have the rights to access the private members of the class. -It is possible to declare the friend function as either private or public. -The function can be invoked without the use of an object. The friend function has its argument as objects, seen in example below. Example#include<iostream.h> #include<conio.h> class { private: int val1,val2; public: void get() { cout<<"Enter two values:"; cin>>val1>>val2; } friend float mean(base ob); }; float mean(base ob) { return float(ob.val1+ob.val2)/2; } void main() { clrscr(); base obj; obj.get(); cout<<"\n Mean value is : "<<mean(obj); getch(); } base

Output:
Enter two values: 10, 20 Mean Value is: 15

PART B

4. Define operator overloading and describe which feature of OOPS is implemented by this concept. Write a program to create a class employee and declare data members such as: emp_id, emp_name, doj(date of joining), emp_address, contact no, salary, designation etc. and member functions to a. Input data for employees. b. Display data for employees. c. Delete the details of particular employee. d. Compare two records where they are same or not. Use operator overloading for comparison. Ans:- A language feature that allows a function or operator to be given more than one definition. For instance C++ permits to add two variables of user defined types with the same syntax that is applied to the basic types. The mechanism of giving such special meaning to an operator is known as operator overloading. #include<iostream> #include<cstring> #include<cstdlib> #include<iomanip> #include<windows.h> //#include <ctime> //#include <dos.h> #include<dos.h> #include<conio.h> #include<cstdio> #define max 20 using namespace std; struct employee {

char name[20]; long int code; char designation[20]; int exp; int age; }; int num; employee emp[max],tempemp[max],sortemp[max],sortemp1[max]; int main() { system("cls"); void build(); void list(); void insert(); void deletes(); void edit(); void search(); void sort(); char option; void menu(); menu(); while((option=cin.get())!='q') { switch(option) { case 'b': build(); break; case 'l': list(); break; case 'i': insert(); break; case 'd': deletes(); break; case 'e': edit(); break;

case 's': search(); break; case 'n': sort(); break; } menu(); } return 0; } void menu() { system("cls"); // highvideo(); cout<<" "; printf("\n***** Employees Management System 1.0 ***** ");

//normvideo(); cout<<endl; cout<<" "; cout<<"\n\t\t Press b---->Built The Employee Table "; cout<<" "; cout<<"\n\t\t Press l---->List The Employee Table "; cout<<" "; cout<<"\n\t\t Press i---->Insert New Entry "; cout<<" "; cout<<"\n\t\t Press d---->Delete An Entry "; cout<<" "; cout<<"\n\t\t Press e---->Edit An Entry "; cout<<" "; cout<<"\n\t\t Press s---->Search Arecord "; cout<<" "; cout<<"\n\t\t Press n---->Sort The Table "; cout<<" "; cout<<"\n\t\t Press q---------->Quit Program "; cout<<" "; cout<<"\n\n \t\t Select Your Option Please ====> "; }

void build() { system("cls"); // highvideo(); printf("Build The Table"); cout<<endl; //normvideo(); cout<<"maximum number of entries ----- > 20"<<endl; cout<<"how many do you want ----->"; cin>>num; cout<<"Enter The Following Items"<<endl; for(int i=0;i<=num-1;i++) { cout<<" Name "; cin>>emp[i].name; cout<<"Code "; cin>>emp[i].code; cout<<"Designation "; cin>>emp[i].designation; cout<<"Years of Experience "; cin>>emp[i].exp; cout<<"Age "; cin>>emp[i].age; } cout<<"going to main menu"; Sleep(500); } void list() { system("cls"); // highvideo(); printf(" ********List The Table********"); cout<<endl; //normvideo(); cout<<" Name Code Designation Age "<<endl;

Years(EXP)

cout<<" ------------------------------------------------------"<<endl; for(int i=0;i<=num-1;i++) { cout<<setw(13)<<emp[i].name; cout<<setw(6)<<emp[i].code; cout<<setw(15)<<emp[i].designation; cout<<setw(10)<<emp[i].exp; cout<<setw(15)<<emp[i].age; cout<<endl; } cout<<"going to main menu"; getch(); } void insert() { system("cls"); int i=num; num+=1; // highvideo(); printf("Insert New Record"); cout<<endl; //normvideo(); cout<<"Enter The Following Items"<<endl; cout<<"Name "; cin>>emp[i].name; cout<<"Code "; cin>>emp[i].code; cout<<"Designation "; cin>>emp[i].designation; cout<<"Years of Experience "; cin>>emp[i].exp; cout<<"Age "; cin>>emp[i].age; cout<<endl<<endl; cout<<"going to main menu"; Sleep(500); }

void deletes() { system("cls"); // highvideo(); int code; int check; printf("Delete An Entry"); //normvideo(); cout<<endl; cout<<"Enter An JobCode To Delete That Entry "; cin>>code; int i; for(i=0;i<=num-1;i++) { if(emp[i].code==code) { check=i; } } for(i=0;i<=num-1;i++) { if(i==check) { continue; } else { if(i>check) { tempemp[i-1]=emp[i]; } else { tempemp[i]=emp[i]; } } } num--;

for(i=0;i<=num-1;i++) { emp[i]=tempemp[i]; } } void edit() { system("cls"); int jobcode; // highvideo(); printf(" Edit An Entry "); cout<<endl; cout<<endl; int i; void editmenu(); void editname(int); void editcode(int); void editdes(int); void editexp(int); void editage(int); char option; //normvideo(); cout<<"Enter An jobcode To Edit An Entry---cin>>jobcode; editmenu(); for(i=0;i<=num-1;i++) { if(emp[i].code==jobcode) { while((option=cin.get())!='q') { switch(option) { case 'n': editname(i); break; case 'c':

";

editcode(i); break; case 'd': editdes(i); break; case 'e': editexp(i); break; case 'a': editage(i); break; } editmenu(); } } } } void editmenu() { system("cls"); cout<<" What Do You Want To edit"; cout<<" n--------->Name "; cout<<" c--------->Code "; cout<<" d--------->Designation"; cout<<" e--------->Experience "; cout<<" a--------->Age "; cout<<" q----->QUIT "; cout<<" Options Please ---->>> "; } void editname(int i) { cout<<"Enter New Name-----> "; cin>>emp[i].name; } void editcode(int i) { cout<<"Enter New Job Code-----> "; cin>>emp[i].code; } void editdes(int i)

{ cout<<"enter new designation-----> "; cin>>emp[i].designation; } void editexp(int i) { cout<<"Enter new Years of Experience"; cin>>emp[i].exp; } void editage(int i) { cout<<"Enter new Age "; cin>>emp[i].age; } void search() { system("cls"); // highvideo(); printf("Welcome To Search Of Employee Database "); //normvideo(); cout<<endl; cout<<endl; int jobcode; cout<<"You Can Search Only By Jobcode Of An Employee"; cout<<"Enter Code Of An Employee "; cin>>jobcode; for(int i=0;i<=num-1;i++) { if(emp[i].code==jobcode) { cout<<" Name Code Designation Age "; cout<<" -----------------------------------------------------cout<<setw(13)<<emp[i].name; cout<<setw(6)<<emp[i].code; cout<<setw(15)<<emp[i].designation; cout<<setw(10)<<emp[i].exp; cout<<setw(15)<<emp[i].age; Years(EXP) ";

cout<<endl; } } cout<<"going to main menu"; getch();

} void sort() { system("cls"); // highvideo(); printf("Sort The Databse By JobCode"); //normvideo(); void sortmenu(); void sortname(); void sortcode(); void sortdes(); void sortexp(); char option; void sortage(); cout<<endl; cout<<endl; sortmenu(); while((option=cin.get())!='q') { switch(option) { case 'n': sortname(); break; case 'c': sortcode(); break; case 'd': sortdes(); break;

case 'e': sortexp(); break; case 'a': sortage(); break; } sortmenu(); } }

void sortmenu() { system("cls"); cout<<" What Do You Want To edit"; cout<<" n--------->Name "; cout<<" c--------->Code "; cout<<" d--------->Designation "; cout<<" e--------->Experience "; cout<<" a--------->Age "; cout<<" q----->QUIT "; cout<<" Options Please ---->>> "; }

void sortname() { system("cls"); int i,j; struct employee temp[max]; for(i=0;i<=num-1;i++) { sortemp1[i]=emp[i]; } for(i=0;i<=num-1;i++) { for(j=0;j<=num-1;j++) { if(strcmp(sortemp1[i].name,sortemp1[j].name)<=0)

{ temp[i]=sortemp1[i]; sortemp1[i]=sortemp1[j]; sortemp1[j]=temp[i]; } } } for( i=0;i<=num-1;i++) { cout<<" Name Code Designation Age "; cout<<" -----------------------------------------------------for( i=0;i<=num-1;i++) { cout<<setw(13)<<sortemp1[i].name; cout<<setw(6)<<sortemp1[i].code; cout<<setw(15)<<sortemp1[i].designation; cout<<setw(10)<<sortemp1[i].exp; cout<<setw(15)<<sortemp1[i].age; cout<<endl; } cout<<"Press Any Key To Go Back"; getch(); }} void sortcode() { system("cls"); int i,j; struct employee temp[max]; for(i=0;i<=num-1;i++) { sortemp1[i]=emp[i]; } for(i=0;i<=num-1;i++) { for(j=0;j<=num-1;j++) Years(EXP) ";

{ if(sortemp1[i].code<sortemp1[j].code) { temp[i]=sortemp1[i]; sortemp1[i]=sortemp1[j]; sortemp1[j]=temp[i]; } } } for( i=0;i<=num-1;i++) { cout<<" Name Code Designation Age "; cout<<" -----------------------------------------------------for( i=0;i<=num-1;i++) { cout<<setw(13)<<sortemp1[i].name; cout<<setw(6)<<sortemp1[i].code; cout<<setw(15)<<sortemp1[i].designation; cout<<setw(10)<<sortemp1[i].exp; cout<<setw(15)<<sortemp1[i].age; cout<<endl; } cout<<"Press Any Key To Go Back"; getch(); }} Years(EXP) ";

void sortdes() { system("cls"); int i,j; struct employee temp[max]; for(i=0;i<=num-1;i++) { sortemp1[i]=emp[i]; }

for(i=0;i<=num-1;i++) { for(j=0;j<=num-1;j++) { if(strcmp(sortemp1[i].designation,sortemp1[j].designation)<=0) { temp[i]=sortemp1[i]; sortemp1[i]=sortemp1[j]; sortemp1[j]=temp[i]; } } } for( i=0;i<=num-1;i++) { cout<<" Name Code Designation Age"; cout<<" -----------------------------------------------------for( i=0;i<=num-1;i++) { cout<<setw(13)<<sortemp1[i].name; cout<<setw(6)<<sortemp1[i].code; cout<<setw(15)<<sortemp1[i].designation; cout<<setw(10)<<sortemp1[i].exp; cout<<setw(15)<<sortemp1[i].age; cout<<endl; } cout<<"Press Any Key To Go Back"; getch(); }} void sortage() { system("cls"); int i,j; struct employee temp[max]; for(i=0;i<=num-1;i++) { Years(EXP) ";

sortemp1[i]=emp[i]; } for(i=0;i<=num-1;i++) { for(j=0;j<=num-1;j++) { if(sortemp1[i].age<sortemp1[j].age) { temp[i]=sortemp1[i]; sortemp1[i]=sortemp1[j]; sortemp1[j]=temp[i]; } } } for( i=0;i<=num-1;i++) { cout<<" Name Code Designation Age"; cout<<" -----------------------------------------------------for( i=0;i<=num-1;i++) { cout<<setw(13)<<sortemp1[i].name; cout<<setw(6)<<sortemp1[i].code; cout<<setw(15)<<sortemp1[i].designation; cout<<setw(10)<<sortemp1[i].exp; cout<<setw(15)<<sortemp1[i].age; cout<<endl; } cout<<"Press Any Key To Go Back"; getch(); }} Years(EXP) ";

void sortexp() { system("cls"); int i,j;

struct employee temp[max]; for(i=0;i<=num-1;i++) { sortemp1[i]=emp[i]; } for(i=0;i<=num-1;i++) { for(j=0;j<=num-1;j++) { if(sortemp1[i].exp<sortemp1[j].exp) { temp[i]=sortemp1[i]; sortemp1[i]=sortemp1[j]; sortemp1[j]=temp[i]; } } } for( i=0;i<=num-1;i++) { cout<<" Name Code Designation Age "; cout<<" ------------------------------------------------------ "; for( i=0;i<=num-1;i++) { cout<<setw(13)<<sortemp1[i].name; cout<<setw(6)<<sortemp1[i].code; cout<<setw(15)<<sortemp1[i].designation; cout<<setw(10)<<sortemp1[i].exp; cout<<setw(15)<<sortemp1[i].age; cout<<endl; } cout<<"Press Any Key To Go Back"; getch(); } } Years(EXP)

The general form of an operator function is: Return type class name :: operator op(arg list) { Function body }

5. Design an application for Hotel management system to perform deposit, withdraw, transfer money, check balance etc. Design all appropriate classes and use concept of inheritance. Ans://**THIS PROGRAM DEALS WITH THE MANAGEMENT OPERATIONS OF A HOTEL i.e. **RESERVATION **MODIFICATION **FACILITY **BILLING ------------**************----------------

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> #include<ctype.h> #include<dos.h> #include<stdlib.h> #include<string.h>

/** FUNCTION PROTOTYPES **/ void welcome();

void menu(); void facilities(); void modification(); void showbill(); float facilitybill(int,int); void seebill(char*,float,float,float,char*,float); void curtain(); void curtain1(); /** DECLARING CLASS HOTEL **/ class hotel { public: char name[20]; char add[100]; int type; int days; int faci; int gno; void readdata(); void showdata(); hotel() {gno=0;} };

/** READDATA FUNCTION **/ void hotel::readdata() { clrscr(); cout<<"\t\t\n"<<"enter guest no...."; cin>>gno; cout<<"\t\n"<<"enter the name...."; gets(name); cout<<"\t\n"<<"enter address...."; gets(add); cout<<"\t\n"<<"enter suite type"; cout<<"\nsuite type(1,2,3 or 4)";

cin>>type; cout<<"\t\n"<<"enter number of days...."; cin>>days; cout<<"\t\n"<<"enter number of facilities...."; cin>>faci; cout<<"\n\n\n\t"; } /** SHOWDATA FUNCTION **/ void hotel::showdata() { cout<<"\n"<<"guest number.."<<gno; cout<<"\t\n"<<"name.."; puts(name); cout<<"\t\n"<<"adderess.."; puts(add); cout<<"\t\n"<<"suite type.."; cout<<type; cout<<"\t\n"<<"number of days.."; cout<<days; cout<<"\t\n"<<"facilities.."; cout<<faci<<"\n\t"; } /** FUNCTIONS MAIN **/ void main() { fstream file; hotel guest; int choice; welcome(); do { clrscr(); menu(); cin>>choice; switch(choice)

{ case 1: clrscr(); cout<<"\n"<<"please enter the"; cout<<"information of guests.."; cout<<"\t\n"<<"press any key"; getch(); file.open("records",ios::out|ios::app|ios::binary); guest.readdata(); file.write((char*)&guest,sizeof(guest)); file.close(); break; case 2:clrscr(); int n,guestno; file.open("records",ios::in|ios::beg|ios::binary); cout<<"\n\n\n\t\t\t"; cout<<"menu"<<"\n\n\n\n"; cout<<"\t\t1.see guest record"; cout<<"\n\n\n\n\t\t"; cout<<"enter your choice(1,2)."; cin>>n; if(n==1) { int flag=0; file.seekg(0); cout<<"\n\t"<<"enter guest number"; cin>>guestno; while((!file.eof())&&(!flag)) { file.read((char*)&guest,sizeof (guest)); if(guest.gno==guestno) { flag=1; clrscr(); guest.showdata(); getch(); } } //END OF WHILE if(flag) { clrscr();

cout<<"\n\t the record does not exist"; cout<<"\t press any key to see menu"; getch(); } //END IF } if(n==2) { file.seekg(0); clrscr(); file.read((char*)&guest,sizeof(guest)); do { clrscr(); cout<<"\n\trecord of guest no..."; cout<<guest.gno<<"\n\n"; guest.showdata(); file.read((char*)&guest,sizeof(guest)); getch(); } while(file); //END OF DO WHILE } //END IF if(n!=1 && n!=2) { clrscr(); cout<<"\t"<<"wrong choice entered"<<"\n"; cout<<"\n\n\t"<<"please enter right choice"; cout<<"\n\n\n\t"<<"press any key"; getch(); } file.close(); break; case 3:modification(); break; case 4:showbill(); break; case 5:char q; clrscr(); curtain();

gotoxy(21,6); cout<<"\t"<<"thank you for using our service"; gotoxy(20,8); cout<<"\n"<<"we hope in future you will \n"; gotoxy(20,10); cout<<"give us oppurtunity to serve you\n"; gotoxy(20,19); cout<<"\npress'E' to exit...."; cin>>q; if(q=='e'||q=='E') choice=6; else menu(); break; default:clrscr(); cout<<"wrong choice entered\n"; cout<<"\n please enter right choice\n"; cout<<"\n\n\n\t\t"<<"press any key to see menu\n"; getch(); menu(); cin>>choice; gotoxy (1,0); } }while (choice>0&& choice<6); } void facilities() { getch(); clrscr(); cout<<"\t\n.....other facilities.....\n\n"; cout<<"\t 1.Swimming pool \n"; cout<<"\t 2.Ball room \n"; cout<<"\t 3.Banquet room \n"; cout<<"\t 4.Conference hall \n"; cout<<"\t 5.Coffee shop & resturant \n"; cout<<"\t 6.Cocktail bar \n"; cout<<"\t 7.Games room \n"; cout<<"\t 8.Internet cafe \n"; cout<<"\t 9.Library \n\n\n";

cout<<"\t Charge per head per facility"; cout<<"Is Rs 1000/- per day....."; cout<<"\n\n\n press any key to see menu"; getch(); } //END FUNCTION /***** FUNCTION MENU ***/ void menu() { cout<<"\t******************** \n\n"; cout<<"\t **-----------MENU----------**"<<"\n"; cout<<"\t **-----1. RESERVATION------**"<<"\n"; cout<<"\t **-----2. SEE FILE------**"<<"\n"; cout<<"\t **-----3. MODIFICATION------**"<<"\n"; cout<<"\t **-----4. SHOW BILL------**"<<"\n"; cout<<"\t **-----5. EXIT------**"<<"\n"; cout<<"\t*****************"<<"\n\n\n"; cout<<"\t Please enter choice(1-5)....."; } //END FUNCTION /*** FUNCTION MODIFICATION ***/ void modification() { hotel guest; char ans; int guestno,flag=0; clrscr(); cout<<"\n\n Enter guest no."; cout<<"To be updated"; cin>>guestno; fstream file; file.open("records",ios::in|ios::out); file.open("records",ios::ate|ios::binary); file.seekg(0); while((!file.eof())&&(!flag)) { file.read((char*)&guest,sizeof(guest)); if(guest.gno==guestno)

flag=1; } if(file.eof()) { file.close(); clrscr(); cout<<"\n This guest number doesnot exists"; cout<<"\n Press any key to see menu"; getch(); return; } clrscr(); guest.showdata(); cout<<"\n"<<"Do you want to change this record(Y/N)"; cin>>ans; if(ans=='y'||ans=='Y') { cout<<"\n"<<"To enter new records press any key"; getch(); guest.readdata(); } file.seekp((guestno-1)*sizeof (guest),ios::beg); file.write((char *)&guest,sizeof(guest)); file.close(); } //END FUNCTION /*** FUNCTION SHOW BILL****/ void showbill() { clrscr(); hotel guest; char gname[30],gtype[30]; int gfac,gsuite,guestno,flag=0; float suitecost,facicost,totfaci,lodge; float totbill,totday,tax=0.07; gotoxy(20,8); cout<<"\n\n"<<"Enter guest number"; cin>>guestno; fstream file;

file.open("records",ios::in|ios::out|ios::binary); file.seekg((guestno-1)*sizeof (guest),ios::beg); while((!file.eof())&&(!flag)) { file.read((char *)&guest,sizeof(guest)); if(guest.gno==guestno) flag=1; } if(file.eof()) { file.close(); clrscr(); cout<<"\n This guest number doesnot exists"; cout<<"\n Press any key to see menu"; getch(); menu(); } strcpy(gname,guest.name); totday=guest.days; gsuite=guest.type; switch(gsuite) { case 1:strcpy(gtype,"suite 1"); break; case 2:strcpy(gtype,"suite 2"); break; case 3:strcpy(gtype,"suite 3"); break; case 4:strcpy(gtype,"suite 4"); break; } gfac=guest.faci; suitecost=facilitybill(1,gsuite); lodge=totday*suitecost; facicost=facilitybill(2,0); totfaci=facicost*gfac*totday; totbill=lodge+totfaci+(lodge+totfaci)*tax; seebill(gname,lodge,totfaci,totday,gtype,totbill); file.close(); }

//*** FUNCTION FACILITY BILL***/ float facilitybill(int x,int y=0) { float cost; switch (x) { case 1:if(y==1) cost=5000; else { if(y==2) cost=3000; else { if(y==3) cost=2500; else { if(y==4) cost=1250; } } } break; case 2:if(y==0) cost=1000; break; } return(cost); } //END FUNCTION /*** FUNCTION SEEBILL***/ void seebill(char * gname,float lodge,float totfac,float totday,char* gtype,float totbill) { clrscr(); cout<<"\n\n\n HOTEL NILGIRI "<<"\n"; cout<<"MAIN ROAD,NEW DELHI"<<"\n\n\n";

cout<<"PH=011-2225375,2225378"<<"\n\n\n\n\n"; cout<<"BILL"<<"\n\n\n"; cout<<"NAME"<<gname<<"\n\n"; cout<<"LODGING BILL"<<lodge<<"\n\n"; cout<<"FACILITIES BILL"<<totfac<<"\n\n"; cout<<"TAX 7%"<<"\n\n"; cout<<"DAYS OF STAY"<<totday<<"\n\n"; cout<<"SUITE"<<gtype<<"\n\n"; cout<<"GRAND TOTAL"<<totbill<<"\n\n\n\n"; cout<<"\t\t\t THANK YOU "<<"\n\n\n"; cout<<"PRESS ANY KEY TO GO TO MENU"; getch(); } //*** FUNCTION WELCOME***/ void welcome() { curtain1(); gotoxy(20,6); cout<<"WELCOME TO HOTEL Suman"; gotoxy(20,10); cout<<"WE WISH YOU HAPPY STAY"; gotoxy(20,16); cout<<"PRESS ANY KEY TO SEE FACILITIES"; getch(); //delay(5000); facilities(); } //*** ---------WELCOME-----------**/ void curtain() { clrscr(); int r,c=1,c1=80; while((c<=39)&&(c1>39)) { for(r=1;r<24;r++) { gotoxy(c,r); cout<<"*_*_*";

gotoxy(c1,r); cout<<"*_*_*"; } c++; c1--; delay(100); } } void curtain1() { clrscr(); int r,c=1,c1=80; while((c<=39)&&(c1>39)) { for(r=1;r<24;r++) { gotoxy(c,r); cout<<"*_*_*"; gotoxy(c1,r); cout<<"*_*_*"; } c++; c1--; delay(100); } }

6. Ambiguity can occur when we use hybrid inheritance comment on this. How ambiguity can be resolved. Ans:- In multiple inheritance the ambiguity arises when same method name is being used by two derived class and furthur derivation from these two base classes. This is called Diamond problem. In order to solve this issue virtual base class concept is being used where base class is virtual to derived ones. For example, u have one base class A, and two derived class B and C, Now one more derived class from B and C is D. In this situation 2 subobject of A will be created (one from B and another from C).In order to avoid such situations we use virtual base class. class A {

public: A(int a) { cout<<"Base class A"<<a<<endl; } } class B:public class A { public: B(int b,int a):A(int a) { cout<<"Derived class B"<<b<<endl; } } class C:public class A { public: C(int c,int a):A(int a) { cout<<"Derived class C"<<c<<endl; } } class D:public class C,public class B { public: D(int d,int c,int b):C(int c,int a),B(int b,int a) { } } main() {

D d(1,2,3); }

Output: Base class A : 3 Derived class C : 1 Base class A : 3 Derived class B: 2

This issue is being solved by making class A as virtual and then output will be Base class A : 3 Derived class C : 1 Derived class B: 2

Potrebbero piacerti anche