Вы находитесь на странице: 1из 14

SYMCA

Object Oriented Programming

Roll No: 03

Q.11 Declare class person having name, basic salary, & id. Calculate salary of each person. #include<iostream.h> #include<conio.h> #include<string.h> class person { protected: char *name; int id; float sal; int len; public: void getdata() { char *s; s=new char[50]; cout<<"\nEnter Name:"; cin>>s; len=strlen(s); name=new char[len+1]; strcpy(name,s); cout<<"\nEnter ID:"; cin>>id; cout<<"\nEnter Basic Salary:"; cin>>sal; } void salary() { float h,d,p,t; h=sal*0.2; d=sal*0.1; p=sal*0.15; t=sal*0.05; sal=sal-(h+d+p+t); } void display() { cout<<"NAME:"<<name<<endl; cout<<"ID:"<<id<<endl; cout<<"SALARY:"<<sal<<endl;

SYMCA

Object Oriented Programming

Roll No: 03

} }; void main() { person *ptr[10]; int n=1; char ch; clrscr(); do { ptr[n]=new person; ptr[n]->getdata(); ptr[n]->salary(); n++; cout<<"Do you want to enter one more record?(y/n)"<<endl;; cin>>ch; } while(ch!='n'); for(int i=1;i<n;i++) { ptr[i]->display(); } getch(); }

SYMCA

Object Oriented Programming

Roll No: 03

Output:

Enter Name:Nikki Enter ID:03 Enter Basic Salary:10000 Do you want to enter one more record?(y/n) N NAME: Nikki ID:3 SALARY:5000

SYMCA

Object Oriented Programming

Roll No: 03

Q 12.WAP for poiters with derived classes. #include<iostream.h> #include<conio.h> class BC { public: int b; void show() { cout<<"b="<<b<<"\n"; } }; class DC:public BC { public: int d; void show() { cout<<"b="<<b<<"\n"; cout<<"d="<<d<<"\n"; } }; int main() { BC *bptr; BC base; bptr=&base; clrscr(); bptr->b=100; cout<<"bptr points to base object\n"; bptr->show(); DC derived; bptr=&derived; bptr->b=200; cout<<"bptr now points to derived object\n"; bptr->show(); DC *dptr; dptr=&derived; dptr->d=300;

SYMCA

Object Oriented Programming

Roll No: 03

cout<<"dptr is derived type pointer\n"; dptr->show(); cout<<"using ((DC *)bptr)\n"; ((DC *)bptr)->d=400; ((DC *)bptr)->show(); getch(); }

SYMCA

Object Oriented Programming

Roll No: 03

Output: bptr points to base object b=100 bptr now points to derived object b=200 dptr is derived type pointer b=200 d=300 using ((DC *)bptr) b=200 d=400

SYMCA

Object Oriented Programming

Roll No: 03

Q. 13 Imagine publishing company that markets both book & audio cassette, create a class called publication that stores the title as a string & price as a float from this class. Derive two classes book which adds page count as int type & second class is tape which adds playing time in minutes. Each of these three classes should have a getdata function to accept the data from user & putdata function to display the data. Write a main program to create an array of pointers to publication. #include<iostream.h> #include<conio.h> class publication { char title[20]; float price; public: virtual void getdata() { cout<<"Enter title :"<<endl; cin>>title; cout<<"Enter the price:"<<endl; cin>>price; } virtual void putdata() { cout<<"Title is :"<<title<<endl; cout<<"Price is :"<<price<<endl; } }; class book:public publication { int pages; public: void getdata() { publication::getdata(); cout<<"Enter the no of pages of a book:"<<endl; cin>>pages; } void putdata() { publication::putdata(); cout<<"No. of pages is :"<<pages<<endl; }

SYMCA

Object Oriented Programming

Roll No: 03

}; class tape: public publication { int time; public: void getdata() { publication::getdata(); cout<<"Enter time:"<<endl; cin>>time; } void putdata() { publication::putdata(); cout<<"Time is :"<<time<<endl; } }; void main() { publication* pub[10]; int n=0; clrscr(); char choice; do { cout<<"Enter book and tape (b/t) : "; cin>>choice; if(choice=='b') pub[n]=new book; else pub[n]=new tape; pub[n++]->getdata(); cout<<"Do you want to continue(y/n)?"; cin>>choice; }while(choice=='y'); int j; for(j=0;j<n;j++) { pub[j]->putdata(); } getch();}

SYMCA

Object Oriented Programming

Roll No: 03

Output: Enter book and tape (b/t) : b Enter title : cpp Enter the price: 320 Enter the no of pages of a book: 435 Do you want to continue(y/n)? y Enter book and tape (b/t) : t Enter title : ABC Enter the price: 120 Enter time: 5 Do you want to continue(y/n)? n Title is :cpp Price is :320 No. of pages is :435 Title is :ABC Price is :120 Time is :5

SYMCA

Object Oriented Programming

Roll No: 03

Q.14 WAP to input record of students having roll no, class & name. A summary of student records should be displayed on demand & search of specific student on roll no should be done & record of that student should be displayed.

#include<iostream.h> #include<stdlib.h> #include<fstream.h> #include<conio.h> class student { public: int Rn; char name[20]; char cls[10]; void getdata() { cout<<"\nEnter Roll No:"; cin>>Rn; cout<<"\nEnter Student Name:"; cin>>name; cout<<"\nEnter Class Name:"; cin>>cls; } void show() { cout<<"\n\nStudent Record\n"; cout<<"\nRoll No:"<<Rn; cout<<"\nName:"<<name; cout<<"\nClass:"<<cls; } }; void main() { student s[10]; int ch,n,i,no,ss; fstream file; file.open("std",ios::binary||ios::trunc||ios::ate||ios::in||ios::out); clrscr(); do { cout<<"\nSelect Option:";

SYMCA

Object Oriented Programming

Roll No: 03

cout<<"\n1.Enter Record."; cout<<"\n2.Display Record."; cout<<"\n3.Search Record"; cout<<"\n4.Exit."; cout<<"\nEnter Your Choice:"; cin>>ch; switch(ch) { case 1: cout<<"\nHow many records you want to enter:"; cin>>n; for(i=0;i<n;i++) { s[i].getdata(); file.write((char *)&s[i],sizeof(s[i])); } file.seekg(0); break; case 2: file.read((char *)&s,sizeof(s)); for(i=0;i<n;i++) { s[i].show(); file.read((char *)&s[i],sizeof(s[i])); } break; case 3: cout<<"\nEnter Student Roll No:"; cin>>no; for(i=0;i<n;i++) { file.read((char *)&s[i],sizeof(s[i])); if(s[i].Rn!=no) { cout<<"\nRecord Does Not Exist."; } else { s[i].show(); } }

SYMCA

Object Oriented Programming

Roll No: 03

break; case 4: exit(0); } }while(ch!=4); getch(); }

SYMCA

Object Oriented Programming

Roll No: 03

Output:-

Select Option: 1.Enter Record. 2.Display Record. 3.Search Record 4.Exit. Enter Your Choice: 1 How many records you want to enter: 3 Enter Roll No:24 Enter Student Name: Amrita Enter Class Name:SYMCA Enter Roll No:12 Enter Student Name:Farheen Enter Class Name:SYMCA Enter Roll No:21 Enter Student Name:Reshma Enter Class Name:SYMCA Select Option: 1.Enter Record. 2.Display Record. 3.Search Record 4.Exit. Enter Your Choice: 2 Student Record Roll No:24 Name: Amrita Class:SYMCA

SYMCA

Object Oriented Programming

Roll No: 03

Student Record Roll No:12 Name:Farheen Class:SYMCA Student Record Roll No:21 Name:Reshma Class:SYMCA Select Option: 1.Enter Record. 2.Display Record. 3.Search Record 4.Exit. Enter Your Choice: 3 Enter Student Roll No: 24 Student Record Roll No:24 Name: Amrita Class:SYMCA Select Option: 1.Enter Record. 2.Display Record. 3.Search Record 4.Exit. Enter Your Choice: 4

Вам также может понравиться