Computer Science
Total Page:16
File Type:pdf, Size:1020Kb
Computer Science Practical Work (SESSION 2011-2012)
SUBITTED TO: SUBMITTED BY: MR. M L MEENA DHEERAJ KAUSHIK (P.G.T. COMP. SC.) XII-B (I-SHIFT)
ROLL NO. : 9151080 Basic concept of OOP’S
Data Abstraction
Data Encapsulation
Inheritance
Modularity
Polymorphism DATA ABSTRACTION: Abstraction refers to the act of representing essential features without including the background details or explanation.
Example: You are driving a car. You only know the essential features to drive a car e.g., gear handling, steering handling, use of clutch, accelerator, brakes etc. But while you are driving do you get into internal details of the car like wiring, motor working etc.? You just change the gears of apply the brakes etc. What is happening is hidden from you. This is abstraction where you only know the essential things to drive a car without including the background details or explanation. Another example is of ‘switch board’. You only press certain switches according to your requirement. What is happening inside, how it is happening inside, how it is happening etc. you needn’t know. Again, in this abstraction, you only know the essential things to operate on the switch board without the knowledge the background details of the switchboard. DATA ENCAPSULAION: The wrapping up of data and operation/function (that operate on the data) into a single entity (called class) is known as encapsulation.
Example: In a big company, there are so many departments, sales, account, payroll, purchase, production etc. each department has its own personnel that maintain its data. Suppose an employee in the production dept. wants to know how much raw material has been purchased for the next month. The production dept. employee would not be allowed to himself go throughthe purchase dept. files. Rather, he`ll issue a memo to the purchase dept. requesting the desired information. Then some employee of the purchase dept. will go through the data files for him and pertain to his requests. This practice ensures that the data is accessed accurately and that it is not corrupted by inept outsiders. Therefore, ‘Department data and department employees are encapsulated into a single entity, the department.’ INHERITANCE: Inheritance is the capability of one class of things to inherit capabilities of properties from another class.
Example: ‘ Humans’ certain properties, such as the ability to speak, breath, eat, drink, etc. etc. But these properties are not unique to humans. The class ‘Human’ inherits these properties from the class ‘Mammal’ which again inherits some of its properties from another “Animal’. The class “car” inherits some of its properties from the class ‘Automobiles” which inherits some of its properties from another class ‘Vehicles”. The capability to pass down properties is a powerful one. It allows us to describe things in an economical way. ‘Automobiles and ‘Pulled Vehicles’ are subclasses of ‘Vehicles’. ‘Vehicles are base class of ‘Automobiles’ and ‘Pulled Vehicles’. MODULARITY: Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.
Example: You must have seen a complete music system. Let us assume that our program represents the music system. The music system comprises of speakers, cassette-player, record-player, cd-player, tuner etc. Now, these parts are complete units in themselves, yet they are themselves, yet they are a subpart of the music system. This is modularity. Similarly, we can achieve modularity in programs also. A program can be divided into various modules each representing speakers, cassette-player… etc. See each module is a complete unit itself yet it works in accordance with other modules in order to achieve one single goal i.e. music. POLYMORPHISM: Polymorphism is the ability for a message or data to be processed in more than one form.
Example: ‘Human’ is a subclass of ‘Mammal’. Similarly ‘Dog’, ‘Cat’, are also subclasses of ‘Mammals’. Mammals can see through day-light. So if a message ‘see through day light’ is passed to all mammals, they will behave alike. Now if a message ‘see through day light’is passes to all mammals, then humans and dogs will not be able to see at night whereas cat will be able to see at night also. Here cats behave differently than other mammals in response to a message or action. INDEX S.No PROGRAMES DATE SIGN. . 1. WAP USING CLASSES AND OBJECTS TO CREATE CLASS BOOK. 2. WRITE A MENU DRIVEN PROGRAM TO CREATE CLASS BANK TO INITIALISE DEPOSIT, WITHDRAW & DISPLAY DEETAILS OF THREE CUSTOMERS. 3. WAP YO CREATE CLASS ADMISSION THAT GENERATE TWO RANDOM ADMISSION NUMBER FROM THE LIST OF GIVEN ADMISSION NUMBERS. 4. WAP TO SHOW WORKING OF CONSTRUCTOR AND DESTRUCTOR. 5. WAP TO MAINTAIN DETAILS OF COLLEGE STUDE3NTS AND PRINT THEM (INHERITANCE). 6. WAP TO SHOW CONSTRUCTOR OVERLOADING. 7. WAP TO CREATE TEXT FILE WHICH PERFORM THE FOLLOWING ACTIONS i. COUNT NO. OF WORDS ii. COUNT NO. OF LINES iii. COUNT AND DISPLAY WORDS STARTING WITH VOWELS AND STORE THEM IN A FILE iv. COUNT AND DISPLAY WORDS STARTING WITH UPPERCASE ALPHABETS v. COUNT NO. OF BLANK SPACES 8. WAP TO CREATE BINARY FILES WITH FIVE RECORDS AND DISPLAY. 9. WAP TO SEARCH RECORD IN A BINARY FILE. 10. WAP TO APPEND A RECORD IN A BINARY FILE. 11. WAP TO INSERT DATA IN A BINARY FILE. 12. WAP TO DELETE A RECORD IN A BINARY FILE. 13. WAP TO MODIFY A RECORD IN A BINARY FILE. 14. WRITE A MENU DRIVEN PROGRAM FOR BINARY AND LINEAR SEARCH IN A ARRAY. 15. CREATE A MENU DRIVEN INSERT & DELETE AN ELEMENT IN A SORTED FILE. 16. WAP TO MERGE TWO ARRAYS A IN SACENDING ORDER, B IN DESCENDING ORDER INTO THIRD ARRAY C INASCENDING ORDER. 17. CREATE MENU DRIVEN PROGRAM TO CHOOSE SORTING TECHNIQUES SELECTION, BUBBLE. 18. WAP TO CONVERT I-D ARRAY TO 2-D ARRAY. 19. CREATE MENU DRIVEN PROGRAM FOR PUSH & POP IN STACK AS LINKED LIST. 20. WAP TO SHOW INSERTION AND DELETION IN QUEUES. 21. QURIES OF SQL TABLE 1 STUDENT 22. QURIES OF SQL TABLE 2 HOSPITAL 23. QURIES OF SQL TABLE 3 TEACHER 24. QURIES OF SQL TABLE 4 FURNITURE AND ARRIVALS 25. QURIES OF SQL TABLE 5 INTERIORS AND NEWONES Q.1) W.A.P. using classes and objects to create the class book.
#include
#include
#include
char book_title[20];
float price;
float total_cost(int n)
{float total;
total=n*price;
return total;
} public:
void INPUT()
{cout<<"\n Enter Book No.: ";
cin>>book_no;
cout<<"\n Enter Book Title: ";
gets(book_title);
cout<<"\n Enter Price: ";
cin>>price;
}
void PURCHASE()
{int n;
float TOT;
cout<<"\n Enter no of copies to be purchase: ";
cin>>n;
TOT=total_cost(n);
cout<<"\n Your total cost is: "< } }; void main() { clrscr(); BOOK buy; buy.INPUT(); buy.PURCHASE(); getch(); } Output Q.2) Write menu driven program to create class to initialize, deposit, withdraw and display the details. #include #include #include int acc_no; char act; float balance; public: void initial() { cout<<"\n Name: "; cin>>name; cout<<"\n Accoutn No.: "; cin>>acc_no; cout<<"\n Enter Amount: "; cin>>balance; cout< } void deposit(float amt) {balance+=amt; cout<<" Amount Deposited"; } void withdraw(float amt) {if((amt-balance>=1000)) { balance=amt-balance; cout<<"\n Amount Withdrawn"; } else {cout<<"\n Min Bal should be 1000"; cout<<" You can withdraw only: "< } } void display() {cout<<"\n\n\t\t\t !!Your Account Details!!"; cout<<"\n\n Account no.:"< cout<<"\n Accoutn Holder: "; cout< cout<<"\n Balance: "< } int getacno() { return acc_no;} }; void main() { clrscr(); Account bank; int choice; cout<<"\n Menu"; cout<<"\n 1.For Deposit"; cout<<"\n 2.For WithDraw"; cout<<"\n Enter Choice(1/2): "; cin>>choice; switch(choice) { case 1:bank.initial(); bank.deposit(100000); break; case 2:bank.initial(); bank.withdraw(100000); break; default:cout<<"\n !!Wrong Choice!!"; } bank.display(); getch(); } Output Q.3) WAP to create the class ADMISSION that generates two random admission nos. from the list of given admission nos. #include #include #include #include char name[10]; float fees; int clas; public: void readdata(); void draw_nos(); int getadno() { return adno; } }; ad A1[5]; void ad::draw_nos() { int n1=0,n2=0; randomize(); n1=random(4); n2=random(4); for(i=0;i<3;i++) if((A1[i].getadno()==n1)||(A1[i].getadno()==n2)); cout<<"\nRandomly generated admission nos are:"< void ad::readdata() { cout<<"\nEnter admission no:"; cin>>adno; cout<<"Name:"; cin>>name; cout<<"Class:"; cin>>clas; cout<<"Fees:"; cin>>fees; } void main() {clrscr(); for(i=0;i<3;i++) { A1[i].readdata(); } A1[i-1].draw_nos(); getch(); } OUTPUT Q.4)WAP to show working of constructor and destructor in a class. #include #include A() {cout<<"\n Constructor A";} ~A() {cout<<"\n Destructor A";} }; class B{ public: B() {cout<<"\n Constructor B";} ~B() {cout<<"\n Destructor B";} }; class C{ public: A ob1,ob2; B ob3; C() {cout<<"\n Constructor C";} ~C() {cout<<"\n Destructor C";} }; void main() {clrscr(); C oc; B ob; A oa; getch(); } Output Q.5)Write a program to maintain the details of college students and print them using inheritance. #include #include #include { char name[len]; int age; public: void readperson(void) ; void displayperson(void) { cout<<"Name : "; cout.write(name,len); cout<<"\t Age : " < } }; void person::readperson(void) { for(int i=0;i name[i]=' '; cout<<"Enter name of the person :"; gets(name); cout<<"Enter age :"; cin>>age; } class student: public person { int rollno; float average ; public: void readstudent(void) { readperson(); cout<<"Enter roll no. : "; cin>>rollno; cout<<"Enter average marks : "; cin>>average; } void disp_rollno(void) { cout<<"Roll no : "< } float getaverage(void) { return average ; } }; class gradstudent :public student { char subject[len]; char working; public : void readit(void); void displaysubject(void) { cout<<"Subject : " ; cout.write(subject,len); } char workstatus(void) { return working; } }; void gradstudent::readit(void) { readstudent(); for(int i=0 ;i subject[i]=' '; cout<<"Enter main subject : "; gets(subject); cout<<"Working?(y/n) : "; cin>>working; } void main() { clrscr(); const int size = 5 ; gradstudent grad[size]; int year,num_working=0,non_working=0,div1=0,total=0; float topscore = 0,score, number,wperc,nwperc; cout<<"Enter year :"; cin>>year; char ch=' '; int i; for(i=0;i<10;i++) { cout<<"Enter details for graduate "<<(i+1)<<" (maximum 10)\n"; grad[i].readit(); total++; if(grad[i].workstatus() =='y'||(grad[i].workstatus() == 'Y')) num_working++; else non_working++; score=grad[i].getaverage() ; if(score>topscore) { topscore=score; number=i; } if(score>=60.0) div1++; cout<<"Press y to see report and n to continue"; cin>>ch; if(ch=='y'||ch=='Y') break; } i=number; cout<<"\n"<<"\t\t\t\tReport of the year " < cout<<"Average marks : "< cout< getch(); } OUTPUT Q.6) WAP to show constructor overloading. #include #include int time; float rate; float total_amt; public: Deposit(); Deposit(long p,int t,float r); Deposit(long p,int t); Deposit(long p,float r); void calc_amt(void); void display(void); }; Deposit::Deposit() {principal=time=rate=0.0;} Deposit::Deposit(long p,int t,float r) {principal=p;time=t;rate=r;} Deposit::Deposit(long p,int t) {principal=p;time=t;rate=0.08;} Deposit::Deposit(long p,float r) {principal=p;time=2;rate=r;} void Deposit::calc_amt(void) { total_amt=principal+(principal*time*rate)/100; } void Deposit::display(void) { cout<<" Principal Amount: "< cout<<"\n Time Period: "<