You are on page 1of 22


Q. W.A.P. using classes and objects to create the class book.

#include<iostream.h> #include<stdio.h> #include<conio.h> class BOOK { int book_no; 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: "<<TOT; }


void main() { clrscr(); BOOK buy; buy.INPUT(); buy.PURCHASE(); getch(); }

Q.2) Write menu driven program to create class to initialize, deposit, withdraw and display the details.
#include<iostream.h> #include<conio.h> #include<process.h> class Bank { char Name[20]; int accno; char acctype; float Balance; public: void Enter(); void Deposit(); void Withdraw(); void Display(); int Getacno(); }; void Bank::Enter() { cout<<"\n Enter the name"; cin>>Name; cout<<"\n Enter account no."; cin>>accno; cout<<"\n Enter account type(saving/current(s/c))"; cin>>acctype; cout<<"\n Enter the Balance"; cin>>Balance; }

void Bank::Deposit() { float depo_amt; cout<<"\n Enter the amount to be deposited"; cin>>depo_amt; Balance+=depo_amt; } void Bank::Withdraw() { float with_amt; cout<<"\n Enter the amount to be withdrawn"; cin>>with_amt; if((Balance-with_amt)<1000) cout<<"\n You can't withdraw money"; else Balance-=with_amt; } void Bank::Display() { cout<<"\n NAME <<"\n Account no. <<"\n Balance } int Bank::Getacno() { return accno; } Bank B; void main() { clrscr(); int choice, an; : "<<Name : "<<accno : "<<Balance;

<<"\n Account type : "<<acctype

char ch='y'; B.Enter(); do { cout<<"\n WELCOME TO BANK" <<"\n CHOOSE WHAT YOU WANT TO DO" <<"\n "<<"\n " <<"\n 1. Deposit" <<"\n 2. Withdraw" <<"\n 3. Exit" <<"\n Enter your choice : "; cin>>choice; switch(choice) { case 1: cout<<"\n Enter your account no "; cin>>an; if(an==B.Getacno()) { B.Deposit(); B.Display(); } break; case 2: cout<<"\n Enter your account no "; cin>>an; if(an==B.Getacno()) { B.Withdraw(); B.Display(); }

break; case 3: exit(0); break; default:cout<<"\n Not a valid choice"; } cout<<"\n Do you want to enter again?(y/n)"; cin>>ch; }while(ch=='y'); getch(); }

Q.3) WAP to create the class ADMISSION that generates two random admission nos. from the list of given admission nos.
#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> int i; class ad{ int adno; 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:"<<n1<<" and "<<n2; } 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(); }

Q.4)WAP to show working of constructor and destructor in a class.
#include<iostream.h> #include<conio.h> class A{ public: 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(); }

Q.5)Write a program to maintain the details of college students and print them using inheritance.

#include<iostream.h> #include<stdio.h> #include<conio.h> const int len = 25 ; class person { char name[len]; int age; public: void readperson(void) ; void displayperson(void) { cout<<"Name : "; cout.write(name,len); cout<<"\t Age : " <<age<<"\n"; } }; void person::readperson(void) { for(int i=0;i<len;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) { } float getaverage(void) { } }; class gradstudent :public student { char subject[len]; char working; public : void readit(void); void displaysubject(void) { } char workstatus(void) { } }; void gradstudent::readit(void) { readstudent(); for(int i=0 ;i<len;i++) subject[i]=' '; cout<<"Enter main subject : "; gets(subject); return working; cout<<"Subject : " ; cout.write(subject,len); return average ; cout<<"Roll no : "<<rollno<<"\n";

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) { } if(score>=60.0) div1++; cout<<"Press y to see report and n to continue"; cin>>ch; if(ch=='y'||ch=='Y') break; } topscore=score; number=i;

i=number; cout<<"\n"<<"\t\t\t\tReport of the year " <<year<<"\n"; cout<<"\t\t\t\t-----------------------\n"; cout<<"Working graduates : " <<num_working<<"\n"; cout<<"Non working graduates : "<<non_working<<"\n"; cout<<"Details of topper \n"; grad[i].displayperson(); grad[i].displaysubject(); nwperc=((float)non_working/(float)total)*100; wperc=((float)div1/(float)total)*100; cout<<"Average marks : "<<grad[i].getaverage()<<"\n"; cout<<nwperc<<"% of the graduates this year are non working and "<<wperc<<"% are first divisioners\n"; getch(); }

Q.6) WAP to show constructor overloading.
#include<iostream.h> #include<conio.h> class Deposit{ long int principal; 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: "<<principal; cout<<"\n Time Period: "<<time; cout<<"\n Rate of interest: "<<rate; cout<<"\n Total Amount: "<<total_amt<<"\n"; } void main() { clrscr(); Deposit D1,D2(2000,2,0.07f),D3(4000,1),D4(3000,0.12f); D1.calc_amt(); D2.calc_amt();

D3.calc_amt(); D4.calc_amt(); cout<<"\n Object1:\n"; D1.display(); cout<<"\n Object2:\n"; D2.display(); cout<<"\n Object3:\n"; D3.display(); cout<<"\n Object4:\n"; D4.display(); getch(); }

Q.7) Write a menu driven program to create the text file that i. ii. iii. iv. v. Count Count Count Count Count the the and the the no of words in a file. no of lines a file. display the words starting with the vowels. words starting with uppercase alphabets. no of spaces.

#include<fstream.h> #include<conio.h> #include<string.h> #include<ctype.h> void file_content() { ofstream fout("xyz.txt"); char ch[200]; cout<<"\nEnter the content of the file: "; cin.getline(ch,200); fout<<ch; fout.close(); } void vowel() { ifstream fin("xyz.txt"); char ch[20]; while(!fin.eof()) { fin>>ch; switch(ch[0]) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u':

case 'U': cout<<"\nWords starting from vowels is: "<<ch<<"\n"; } }

} void word_count() { ifstream fin ("xyz.txt"); char ch; int count=0; while (!fin.eof()) { fin.get(ch); if(ch==' ') ++count; } cout<<"\nNo. of words are: "<<++count<<"\n"; fin.close(); } void count_spaces() { ifstream fin ("xyz.txt"); char ch; int count=0; while (!fin.eof()) { fin.get(ch); if(ch==' ') ++count; } cout<<"\nNo. of spaces are: "<<count<<"\n"; fin.close(); } void count_lines() { char str[100]; int count=0; ifstream fin("xyz.txt"); while(!fin.eof()) { fin.getline(str,100); ++count; } cout<<"\nNo. of lines are: "<<count<<"\n";

} void count_uppercase() { ifstream fin("xyz.txt"); int count=0; char ch; while(!fin.eof()) { fin>>ch; if(isupper(ch)) ++count; } fin.close(); cout<<"\nNo. of upper case alpahabets are: "<<count<<"\n"; } void main() { clrscr(); file_content(); word_count(); count_lines(); vowel(); count_uppercase(); count_spaces(); getch(); }

Q.8) Write a program to create the binary file and display them.
#include<iostream.h> #include<fstream.h> #include<conio.h> class student { char name[50]; char grade; float marks; public: void getdata(void); void display(void); }; void student::getdata(void) { char ch; cin.get(ch); cout<<"\n"<<"ENTER NAME : - "; cin>>name; cout<<"\n"<<"ENTER GRADE : - "; cin>>grade; cout<<"\n"<<"ENTER MARKS : - "; cin>>marks; cout<<"\n"; } void student::display(void) {cout<<"NAME : - "<<name<<"\t"; cout<<"GRADE : - "<<grade<<"\t"; cout<<"MARKS : - "<<marks<<"\t"<<"\n";} int main() { clrscr(); student arts[5]; fstream filin ;"stu.dat",ios::in|ios::out); if(!filin) { cout<<"SORRY CAN NOT OPEN THE FILE"<<"\n"; return 1; } cout<<"ENTER DETAILS OF THE 2 STUDENTS : - "<<"\n"; for(int i=0;i<2;i++)

arts[i].getdata(); filin.write((char*)&arts[i],sizeof(arts[i])); } filin.seekg(0); cout<<"\n"; cout<<"THE CONTENTS OF THE STU.DAT IS AS SHOWN BELOW : "<<"\n"; for(i=0;i<2;i++) {*)&arts[i],sizeof(arts[i]));arts[i].display();} filin.close(); getch(); }

Q.9) Write a program to search a record in a file.

#include<iostream.h> #include<conio.h> #include<fstream.h> class stu { int rollno; char name[25]; char Class[4]; float marks; char grade; public: void getdata() { cout<<"ROLL NUMBER"; cin>>rollno; cout<<"NAME"; cin>>name; cout<<"CLASS"; cin>>Class; cout<<"MARKS"; cin>>marks; } void putdata() { cout<<Name-<<name<<"Roll No-"<<rollno<<Marks=<<marks; } int getrno() { return rollno; } } s1; void main() { clrscr(); int rn; char found='n'; ifstream fi("stu.dat",ios::in); cout<<"ENTER ROLL NUMBER TO BE SEARCHED : - ";

cin>>rn; while (!fi.eof()) {*)&s1,sizeof(s1)); if(s1.getrno()==rn) { s1.putdata(); found='y'; break; } } if(found=='n') cout<<"ROLL NUMBER NOT FOUND IN THE FILE"<<endl; fi.close();}