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

10041

PRACTICAL FILE OF OOPS LAB

DRONACHARYA COLLEGE OF ENGINEERING KHENTAWAS(GURGAON) AFFILIATED TO MAHARISHI DAYANAND UNIVERSITY,ROHTAK APPROVED BY A.I.C.T.E

Submitted By:Name:- MAHIMA KUNDU Roll No:-10041 Branch:-CSE I(4th SEM) Group:- A

10041

CERTIFICATE

This is to certify that Ms. MAHIMA KUNDU ,Roll No.10041 is student of 4th Semester(COMPUTER SCIENCE) has completed the practical file under the guidance and supervision of undersigned.

Mr. Jitender Yadav (HOD CSE & IT)

Mr. Anil Jain (FACULTY)

10041

CONTENTS

S.NO.

PROGRAM

DATE

SIGN

Write a function called power ( ) that takes a double value for n and an int value for p, and returns the result as double value. Write a program that uses a structure called point to model a point. Write a program to make a four function calculator using calculator. Write a program using friend function that can read values for the class objects and add one object of dm with another object of db. Create a program which represents a numeric value- numerator and denominator. It includes the following: 1. Default Constructor 2. Constructor with arguments. 3. void reduce ( ) 4. Overload + operator 5. Overload >> operator to enable input through cin. 6. Overload << operator to enable output through cout. Write a main ( ) that create objects of the three classes. Show through the pointer to demonstrate polymorphism in action.

10041

II

III

IV

VI

VII

WAP to create a binary file to read the data for the students. It should include 1. Roll no. 2. Name (a string of 30 or lesser no. Of characters) and marks. Create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called triangle and rectangle from the base shape.

VIII

10041

PROGRAM NO.1
Raising a number n to a power p is the same as multiplying n by itself p times. Write a function called power ( ) that takes a double value for n and an int value for p, and returns the result as double value. Use a default argument of 2 for p, so that if this argument is omitted, the number will be squared. Write a main ( ) function that gets values from the user to test this function. CODE: #include<iostream.h> #include<conio.h> #include<math.h> int power(double n,int p); void main() { clrscr(); double n;

10041
int p,x,y; cout<<"Enter the no. for n & p \n"; cin>>n; cin>>p; y= pow (n,p); cout<<"\n"<< y <<"\n"; x=power(n,p); if (y==x) { cout<<"x & y are equal"; } Else { cout<<"x & y are not equal"; } getch(); } int power(double n,int p ) { int x; x=n; for(int i=0;i<(p-1);i++) { X=x*n; } cout << x<<\n; return(x); Output: Enter the no. for n & p 2 3 8 8 x & y are equal Output:

10041

PROGRAM NO. 2
A point on the two dimensional plane can be represented by two numbers: an X coordinate and a Y coordinate. Write a program that uses a structure called point to model a point. Define three points, and have the user input values to two of them. Than set the third point equal to the sum of the other two, and display the value of the new point. Interaction with the program might look like this: Enter coordinates for P1:3 4 Enter coordinates for P2:5 7 Coordinates of P1 + P2 are: 8, 11 . CODE:

10041
#include<iostream.h> struct point { int X1; int Y1; }s; void main() { int x2,y2,sumx,sumy; cout<<"Enter the X coordinates\n"; cin>>s.X1>>x2; cout<<"Enter the Y coordinates\n"; cin>>s.Y1>>y2; sumx=s.X1+x2; cout<<"\n The sum of X coordinates is: "<<"\n"<<sumx; sumy=s.Y1+y2; cout<<"\n The sum of Y coordinates is: "<<"\n"<<sumy; }

Output:
Enter the X coordinates 1 2 Enter the Y coordinates 2 1 The sum of X coordinates is: 3 The sum of Y coordinates is: 3

10041

PROGRAM NO. 3
Create the equivalent of a four function calculator. The program should request the user to enter a number, an operator, and another number. It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. (It should use a switch statement to select the operation). Finally it should display the result. When it finishes the calculation, the program should ask if the user wants to do another calculation. The response can be Y or N. Some sample interaction with the program might look like this.Enter first number, operator, second number: 10/ 3 Answer = 3.333333 Do another (Y/ N)? Enter first number, operator, second number 12 + 100 Answer = 112 Do another (Y/ N) ? N .

10041
CODE: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> #define not_oper(str[i]!='*' && str[i]!='/' && str[i]!='+' && str[i]!='-') void evaluate(int a[],char b[]); void main() { clrscr(); char ch; char str[22],tmp[22],op[22]; int in[22]; do{ cout<<"\n\nINPUT A STRING TO BE EVALUATED :- "; gets(str); int j=0,k=0,a=0; for(int i=0; str[i]!=NULL; i++) { if(not_oper) { tmp[k++]=str[i]; } Else { tmp[k]='\0'; in[a++]=atoi(tmp); op[j++]=str[i]; k=0; } } tmp[k]='\0'; in[a++]=atoi(tmp); in[a]='\0'; op[j]='\0'; cout<<"\nINT :- "; for(i=0; in[i]!=NULL; i++) { cout<<" "<<in[i]; } cout<<"\n\nOP :- "; for(i=0; op[i]!=NULL; i++) { cout<<" "<<op[i]; }

10041
evaluate(in,op); cout<<"\n\n\nDo you want to continue (y or n)"; cin>>ch; }while(ch=='y'); getch(); } void evaluate(int a[],char b[]) { double sum=a[0]; for(int i=0; b[i]!=NULL; i++) { if(b[i]=='+') sum=sum+a[i+1]; else if(b[i]=='-') sum=sum-a[i+1]; else if(b[i]=='/')

sum=sum/a[i+1]; else if(b[i]=='*') sum=sum*a[i+1]; } cout<<"\n\nSUM OF ABOVE EXPRESSION IS :- "<<sum; }

Output:
INPUT A STRING TO BE EVALUATED :- 12/3 INT :- 12 3 OP :- / SUM OF ABOVE EXPRESSION IS :- 4 Do you want to continue (y or n)y INPUT A STRING TO BE EVALUATED :- 5/2 INT :- 5 2 OP :- / SUM OF ABOVE EXPRESSION IS :- 2.5 Do you want to continue (y or n)

10041

PROGRAM NO:4
Create two classes DM and DB which store the value of distances. DM stores distances in meters and centimeters and DB in feet and inches. Write a program that can read values for the class objects and add one ob

RAM NO. 4ject of DM with another object of DB.


Use a friend function to carry out the addition operation. The object that stores the results maybe a DM object or DB objects, depending on the units in which the results are required. The display should be in the format of feet and inches or meters and centimeters depending on the object on display. CODE: #include<iostream.h> #include<conio.h> class DB; class DM { int m,cm; public:

10041
void getdata() { cout<<"\n Enter meter & centimeter "; cin >> m>>cm; } void showdata() { cout << "\n"<<m<<"\t" << cm; } friend void add(DM,DB); }; class DB { int f,ic; public: void getdata() { cout<<"\n Enter feet & inches "; cin >>f>>ic; } void showdata() { cout << "\n"<<f<<"\t" << ic; } friend void add(DM,DB); }; void add(DM x,DB y) { cout<<"\n 1 for Meter \n 2 for Feet \n"; Cout<<\n Enter your choice\n; int op; cin>>op; if(op==1) { x.m=y.f*0.3408; x.cm=y.ic*2.54; cout<<"\n Meter "<<x.m<<"\t"<<"Centimeter "<<x.cm; } else if(op==2) { y.f=x.m*3.281; y.ic=x.cm*0.3937; cout<<"\n Feet "<<y.f<<"\t"<<"Inches "<<y.ic; } Else

10041
{ cout<<"\n Sorry"; } } void main() { clrscr(); DM a; DB b; a.getdata(); b.getdata(); add(a,b); getch(); }

Output:
Enter meter & centimeter 30 60 Enter feet & inches 25 75 1 for Meter 2 for Feet Enter your choice 1 Meter 8 Centimeter 190

10041

PROGRAM NO. 5
Create a class rational which represents a numerical value by two double values- NUMERATOR & DENOMINATOR. Include the following public member Functions: constructor with no arguments (default). 1. constructor with two arguments. 2. void reduce( ) that reduces the rational number by eliminating the highest common factor between the numerator and denominator. 3. Overload + operator to add two rational number. 4. Overload >> operator to enable input through cin. 5. Overload << operator to enable output through cout. Write a main ( ) to test all the functions in the class. CODE: #include<iostream.h> #include<conio.h> void reduce(int,int,int); void main() { int num,den,temp,a,b; clrscr();

10041
cout<<"\nEnter numerator and denominator of a rational number: "; cin>>a>>b; if(a>b) { num=a; den=b; } Else { num=b; den=a; } while(den!=0) { temp=num; num=den; den=temp%den; } cout<<"\nThe HCF of the numerator and denominator is "<<num; reduce(a,b,num); getch(); } void reduce(int a, int b, int hcf) { int ans1,ans2; ans1=a/hcf; ans2=b/hcf; cout<<"\n\nAnswer = "<<ans1<<"/"<<ans2; }

10041

Output:
Enter numerator and denominator of a rational number: 12 10 The HCF of the numerator and denominator is 2 Answer = 6/5

10041

PROGRAM NO. 6
Consider the following class definition class father { protected : int age; public; father (int x) {age = x;} virtual void iam ( ) { cout < < I AM THE FATHER, my age is : << age<< end1:}}; Derive the two classes son and daughter from the above class and for each, define iam ( ) to write our similar but appropriate messages. You should also define suitable constructors for these classes. Now, write a main ( ) that creates objects of the three classes and then calls iam ( ) for them. Declare pointer to father. Successively, assign addresses of objects of the two derived classes to this pointer and in each case, call iam ( ) through the pointer to demonstrate polymorphism in action. CODE: #include<iostream.h> #include<conio.h> class father

10041
{ protected: int age; public: father(int x) { age=x; } virtual void iam() { cout<< " I AM THE FATHER, MY AGE IS: "<<age<<endl; } }; class son:public father { protected: int age; public: son(int x,int y):father(y) { age=x; } void iam() { cout<< " I AM THE SON, MY AGE IS: "<<age<<endl; } }; class daughter:public father { protected: int age; public: daughter(int x,int y):father(y) { age=x; } void iam() { cout<< " I AM THE DAUGHTER, MY AGE IS: "<<age<<endl; } }; void main() { clrscr(); father f(50),*p; son s(25,50); daughter d(20,50);

10041
p=&f; p->iam(); p=&s; p->iam(); p=&d; p->iam(); getch(); }

Output:
I AM THE FATHER, MY AGE IS: 50 I AM THE SON, MY AGE IS: 25 I AM THE DAUGHTER, MY AGE IS: 20

PROGRAM NO. 7
Write a program that creates a binary file by reading the data for the students from the terminal. The data of each student consist of roll no., name (a string of 30 or lesser no. of characters) and marks. CODE: #include<stdio.h> #include<iostream.h> #include<conio.h> #include<fstream.h> class student { int srollno; char sname[20]; float marks; public: void getdata() { cout<<"\n enter roll no "; cin >> srollno; cout<<"\n Enter name "; cin >> sname;

10041
cout<<"\n Enter marks "; cin >> marks; } void showdata() { cout << "\n"<<srollno<<"\t" << sname <<"\t"<< marks;} int getno() { return srollno; } }; void addrec() { student s; ofstream f; f.open("student.dat",ios::binary|ios::app); s.getdata(); f.write((char *)&s, sizeof(s)); f.close(); } void showall() { student s; int c=0; ifstream f; f.open("student.dat", ios::binary); f.read((char *)&s, sizeof(s)); while(!f.eof()) { c++; s.showdata(); f.read((char *)&s, sizeof(s)); } f.close(); cout<<"\n Total Records "<<c; } void search() { student s; int c=0,n; ifstream f; f.open("student.dat", ios::binary); cout<<"\n Enter no to search "; cin>>n; f.read((char *)&s, sizeof(s));

10041
while(!f.eof()) { if(n==s.getno()) { s.showdata(); c=1; break; } f.read((char *)&s, sizeof(s)); } f.close(); if(c==0) { cout<<"\n Not Found "; } } void deleterec() { student s; int c=0,n; ifstream f; ofstream k; f.open("student.dat", ios::binary)

k.open("temp.dat",ios::binary) cout<<"\n Enter no to delete "; cin>>n; f.read((char *)&s, sizeof(s)); while(!f.eof()) \ { if(n==s.getno()) { s.showdata(); c=1; cout<<"\n Record Deleted "; } Else { k.write((char *)&s,sizeof(s)); } f.read((char *)&s, sizeof(s)); } f.close(); k.close(); remove("student.dat"); rename("temp.dat","student.dat");

10041
if(c==0) { cout<<"\n Not Found "; } } void modrec() { student s; int c=0,n; ifstream f; ofstream k; f.open("student.dat", ios::binary); k.open("temp.dat",ios::binary); cout<<"\n Enter no to modify "; cin>>n; f.read((char *)&s, sizeof(s)); while(!f.eof()) { if(n==s.getno()) { s.showdata(); s.getdata(); k.write((char *)&s,sizeof(s));

\c=1; cout<<"\n Record Modified "; } Else { k.write((char *)&s,sizeof(s)); } f.read((char *)&s, sizeof(s)); } f.close(); k.close(); remove("student.dat"); rename("temp.dat","student.dat"); if(c==0) { cout<<"\n Not Found "; } } void modrec1() { student s;

10041
int c=0,n,r; fstream f; f.open("student.dat", ios::binary|ios::in|ios::out); cout<<"\n Enter no to modify "; cin>>n; f.read((char *)&s, sizeof(s)); while(!f.eof()) { r++; if(n==s.getno()) { s.showdata(); s.getdata(); f.seekp((r-1)*sizeof(s),ios::beg); cout<<"\n Try "; f.write((char *)&s,sizeof(s)); cout<<"\n Tata "; getch(); c=1; cout<<"\n Record Modified "; } f.read((char *)&s, sizeof(s));

\ \ \

} f.close(); if(c==0) {cout<<"\n Not Found "; } } void main() { int op; do { clrscr(); cout<<"\n 1 Add"; cout<<"\n 2 Showall"; cout<<"\n 3 Search "; cout<<"\n 4 Delete "; cout<<"\n 5 Modify "; cout<<"\n 6 Exit"; cout<<"\n Enter choice"; cin>> op; switch(op)

10041
{ case 1: addrec(); break; case 2: showall(); break; case 3: search(); break; case 4: deleterec(); break; case 5: modrec1(); break; } getch(); }while(op!=0); }

Output:
1 Add 2 Showall 3 Search 4 Delete 5 Modify 6 Exit Enter choice1 Enter roll no 52 Enter name SONALI Enter marks 99 1 Add 2 Showall 3 Search 4 Delete 5 Modify 6 exit Enter choice2

10041
1 PREETI 75 2 ASHI 75 22 ISHA 90 52 SONALI 99 Total Records 4

PROGRAM NO. 8
Create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called triangle and rectangle from the base shape. Add to the base class, a member function get_data( ) to initialize base class data members and another member function display_area( ) to compute and display the area of figures. Make display_area( ) as a virtual function and redefine this function in the derived classes to suit their requirements. Using these three classes, design a program that will accept dimensions of a triangle or a rectangle interactively and display the area. Remember the two values given as input will be treated as lengths of two sides in the case of rectangles and as base and height in the case of triangles and used as follows: Area of rectangle = x * y Area of triangle =*x*y . CODE: #include<iostream.h> #include<conio.h>

10041
class shape { public: double x,y; void getdata() { cout<<"\n Enter value of x "; cin>>x; cout<<"\n Enter value of y "; cin>>y; } virtual void display_area()=0; }; class rec:public shape { public: void display_area() { cout<<"\n Area "<<x*y; } }; class tri:public shape { public: void display_area() { cout<<"\n Area "<<0.5*x*y; } }; void main() { clrscr(); tri t; rec r; shape *s; s=&t; s->getdata(); s->display_area(); s=&r; s->getdata(); s->display_area(); getch(); }

10041

Output:
Enter value of x 20 Enter value of y 40 Area 400 Enter value of x 50 Enter value of y 70 Area 3500

10041

PROGRAM NO.9
Write a program to make a string as class with length and name as member (a)Use constructor to initialize (b)Overload + operator to concatenate two strings

CODE: #include<conio.h> #include<iostream.h> #include<string.h> class string { char *p; int len; public: string() { p=0; len=0; }

10041
void show(string &t); string(char *s); string(string &s); string operator+(string &s); }; string::string(char *s) { len=strlen(s); p=new char[len+1]; strcpy(p,s); } string::string(string &s) { len=s.len; p=new char[len+1]; strcpy(p,s.p); } string operator+(string &s) { string temp; temp.len=s.len+len; temp.p=new char[temp.len+1]; strcpy(temp.p,p); strcat(temp.p,s.p); return temp; } void string::show(string &t) { cout<<"\nConcatenated string is: "<<t.p; } void main() { string s1="String1"; string s2="String2"; string s3; s3=s1+s2; show(s3); getch(); }

10041

OUTPUT:
STRING1 STRING2 CONCATENATED STRING IS: STRING1STRING2

10041

PROGRAM NO:10
WAP to make Fibonacci series using constructor and overload ++ operator. CODE: #include<iostream.h> #include<conio.h> class fib { int a,b,n; public: fib() { a=b=1; cout<<"number of elements in series"; cin>>n; cout<<a<<"\t"<<b; } void operator++(); }; void fib::operator++() {

10041
for(int i=0;i<n-2;i++) { int c=a+b; a=b; b=c; cout<<"\t"<<c; } } void main() { clrscr(); fib k; ++k; getch(); }

OUTPUT
number of elements in series4 1 1 2 3

10041

PROGRAM NO:11
Class master drives information from both account and admin classes which derive information from person. WAP to create and display in master objects CODE: #include<iostream.h> #include<conio.h> class person { protected: char name[20]; int code; public: void getperson() { cout<<"enter the name of person"<<endl; cin>>name; cout<<"enter the code of person"<<endl; cin>>code; }};

10041
class account:virtual public person { protected: float pay; public: void payment() { cout<<"enter the payment\n"; cin>>pay; }}; class admin:virtual public person { protected: int exp_year; public: void getexp() { cout<<"enter the year of expiereance\n"; cin>>exp_year; } }; class master :public account,public admin { public: void getdata() { getperson(); payment(); getexp(); } void display() { cout<<"name:"<<name; cout<<"code:"<<code; cout<<"pay:"<<pay; cout<<"xperiance"<<exp_year; }}; void main() { clrscr(); int ch; master k; k.getdata(); k.display(); cout<<"do you want to continue : 1 or 0\n";

10041
cin>>ch; while(ch==1) { k.getdata(); cout<<"do you want to continue : 1 or 0\n"; cin>>ch; } getch(); }

OUTPUT
enter the name of person rohit enter the code of person 10087 enter the payment 5000 enter the year of expiereance 9 name:rohit code:10087 pay:5000 experiance9 do you want to continue : 1 or 0

10041

WAP with the following: (a)A function to read two double type no. (b)A function to calculate division of two no. (c)A try block to detect and throw exception if condition divide by zero occurs (d)Appropriate catch block to handle exception thrown. CODE: #include<iostream.h>

PROGRAM NO:12
#include<conio.h> double x,y; void read() { cout<<"enter the two double nuMber"; cin>>x>>y; } void div(double x,double y) { try { if(y==0) throw(x); else cout<<"x"<<x;

10041
cout<<"y"<<y; float a=x/y; cout<<"x/y"<<a; } catch(...) { cout<<"error occur // division by zero"; } } void main() { clrscr(); read(); div(x,y); getch(); }

OUTPUT
Enter the two double number x=5 y=5 a=1 Enter the two double number x=5 y=0 error occur // division by zero

10041

PROGRAM NO:13 Write a function template for finding the minimum value contained in an array . CODE: #include<iostream.h> #include<conio.h> template <class t1> void min(t1 a[]) { for(int i=0;i<10;i++) { for(int j=i;j<10,j++) { if(a[i]>a[j]) { swap(a[i],a[j]); } } } cout<<"minimum value is:"<<a[0]; } template<class t2> void swap(t2 &a,t2 &b) { t2 temp=a;

10041
a=b; b=temp; } void main() { int a[15]={9,8,7,6,5,4,3,2,1}; float b[15]={9.1,8.1,7.1,6.3,5.5,4.1,3.2,2.2,1.9}; cout<<"in case of integer array"; min(a); cout<<"in case of float array"; min(b); getch(); }

OUTPUT
In case of integer array : 1 In case of float array :1.9

10041

PROGRAM NO:14
WAP to read the file containing list of telephone no. and output the list in two columns. The name should be left justified and no. right justified. CODE: #include<iostream.h> #include<conio.h> #include<iomanip.h> #include<fstream.h> class telph_recrd { char name[12]; int ph; public: void read(); void write(); } ; void telph_recrd::read() { cout<<"\nenter the name\n"; cin>>name;

10041
cout<<"enter the phoneno\n"; cin>>ph; } void telph_recrd::write() { cout<<setiosflags(ios::left) <<setw(10)<<name <<setiosflags(ios::right) <<setw(10)<<ph<<"\n"; } void main() { clrscr(); telph_recrd item[2]; fstream f; f.open("sumitgandhi.doc",ios::in|ios::out|ios::binary); cout<<"enter the details for 2 record"; for(int i=0;i<2;i++) { item[i].read(); f.write((char *)&item[i],sizeof(item[i])); } f.seekg(0); cout<<"\n output\n"; for(i=0;i<2;i++) { f.read((char *)&item[i],sizeof(item[i])); item[i].write(); } f.close(); getch(); }

10041

OUTPUT
enter the details for 2 record enter the name john enter the phoneno 23456 enter the name ahmed enter the phoneno 123 output john ahmed 23456 123

10041

PROGRAM NO:15
WAP to create a file person containing information as name,id,address,income CODE: #include<stdio.h> #include<iostream.h> #include<conio.h> #include<fstream.h> class person { char name[20]; int id, income; char address[40]; public: void input() { cout<<"\nenter name: "; cin>>name; cout<<"\nenter address: "; cin>>address; cout<<"\nenter id: ";

10041
cin>>id; cout<<"\nenter the income:"; cin>>income; } void output() { cout<<"Name :"<<name<<endl; cout<<"Id: "<<id<<endl; cout<<"Income:"<<income<<endl; cout<<"Address:"<<address<<endl; } }; void main() { clrscr(); person per[3]; fstream file; file.open("person.data",ios::in|ios::out); for(int i=0;i<3;i++) { cout<<"enter the data for person "<<i+1<<endl; per[i].input(); file.write((char*)&per[i],sizeof(per[i])); } file.seekg(0); cout<<"\noutput\n\n\n"; for(i=0;i<3;i++) { cout<<"\nPerson No:"<<i+1<<endl; file.read((char*)&per[i],sizeof(per[i])); per[i].output(); } file.close(); getch(); }

10041

OUTPUT
enter the data for person 1 enter name: SUMIT enter address: 179 enter id: 0112 enter the income:1234 enter the data for person 2 enter name: RINKU enter address: 129 enter id: 1008 enter the income:3456 enter the data for person 3 enter name: ROHIT enter address: 456 enter id: 10087 enter the income:5678

output
Person No:1 Name :SUMIT Id: 74

10041
Income:1234 Address:179 Person No:2 Name :RINKU Id: 10083 Income:12672 Address:29 Person No:3 Name :ROHIT Id: 10087 Income:5678 Address:456

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