4th sem

57
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

Transcript of 4th sem

Page 1: 4th sem

10041

PRACTICAL FILEOF

OOPS LAB

DRONACHARYA COLLEGE OF ENGINEERINGKHENTAWAS(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

Page 2: 4th sem

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 Mr. Anil Jain(HOD CSE & IT) (FACULTY)

CONTENTS

Page 3: 4th sem

10041

S.NO. PROGRAM DATE SIGN

I 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.

II Write a program that uses a structure called point to model a point.

III Write a program to make a four function calculator using calculator.

IV 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.

V 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.

VIWrite a main ( ) that create objects of the three classes. Show through the pointer to demonstrate polymorphism in action.

Page 4: 4th sem

10041

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.

VIII 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.

IX WAP to make a string as class with length and name as member (a)use constructor to initialize (b)overload + operator to concatenate two strings.

X WAP to make Fibonacci series using constructor and overload ++ operator.

XI Class master drives information from both account and admin classes which derive information from person. WAP to create and display in master objects.

XII 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.

Page 5: 4th sem

10041

XIII Write a function template for finding the minimum value contained in an array .

XIV 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.

XV WAP to create a file person containing information as name,id,address,income.

Page 6: 4th sem

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;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);

Page 7: 4th sem

10041

Output:

Enter the no. for n & p2388x & y are equal Output:

Page 8: 4th sem

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 4Enter coordinates for P2:5 7Coordinates of P1 + P2 are: 8, 11 .

CODE: #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;

}

Page 9: 4th sem

10041

Output:

Enter the X coordinates12Enter the Y coordinates21 The sum of X coordinates is: 3 The sum of Y coordinates is: 3

Page 10: 4th sem

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/ 3Answer = 3.333333Do another (Y/ N)? Enter first number, operator, second number 12 + 100Answer = 112Do another (Y/ N) ? N .

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{

Page 11: 4th sem

10041

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];}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;}

Page 12: 4th sem

10041

Output:

INPUT A STRING TO BE EVALUATED :- 12/3INT :- 12 3OP :- /SUM OF ABOVE EXPRESSION IS :- 4

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

Do you want to continue (y or n)

Page 13: 4th sem

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:

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()

Page 14: 4th sem

10041

{

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{

cout<<"\n Sorry";}

}void main(){

clrscr();DM a;DB b;a.getdata();b.getdata();add(a,b);getch();

}

Page 15: 4th sem

10041

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

Page 16: 4th sem

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(); 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; }

Page 17: 4th sem

10041

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;}

Page 18: 4th sem

10041

Output:

Enter numerator and denominator of a rational number: 1210

The HCF of the numerator and denominator is 2Answer = 6/5

Page 19: 4th sem

10041

PROGRAM NO. 6

Consider the following class definitionclass 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{

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)

Page 20: 4th sem

10041

{

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);p=&f;p->iam();p=&s;p->iam();p=&d;p->iam();getch();

}

Output:

I AM THE FATHER, MY AGE IS: 50

Page 21: 4th sem

10041

I AM THE SON, MY AGE IS: 25I 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;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;

Page 22: 4th sem

10041

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));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 ";}

Page 23: 4th sem

10041

}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");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;

Page 24: 4th sem

10041

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;

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);

Page 25: 4th sem

10041

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) {

case 1:addrec();break;

case 2:

showall();break;

case 3:search();

break; case 4:

deleterec();break;

Page 26: 4th sem

10041

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 1 PREETI 75 2 ASHI 75 22 ISHA 90 52 SONALI 99 Total Records 4

Page 27: 4th sem

10041

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 * yArea of triangle = ½ * x * y .

CODE: #include<iostream.h>#include<conio.h>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;

};

Page 28: 4th sem

10041

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();

}

Page 29: 4th sem

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

Page 30: 4th sem

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; } 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);

Page 31: 4th sem

10041

}

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();}

Page 32: 4th sem

10041

OUTPUT:

STRING1STRING2CONCATENATED STRING IS: STRING1STRING2

Page 33: 4th sem

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++(){for(int i=0;i<n-2;i++){int c=a+b;a=b;b=c;cout<<"\t"<<c;}}void main(){

Page 34: 4th sem

10041

clrscr();fib k;++k;getch();}

OUTPUT

number of elements in series41 1 2 3

Page 35: 4th sem

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;}};

class account:virtual public person{protected:float pay;public:void payment(){cout<<"enter the payment\n";cin>>pay;

Page 36: 4th sem

10041

}};

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";cin>>ch;

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

}

Page 37: 4th sem

10041

getch();}

OUTPUT

enter the name of personrohitenter the code of person10087enter the payment5000enter the year of expiereance9name:rohitcode:10087pay:5000experiance9do you want to continue : 1 or 0

Page 38: 4th sem

10041

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);elsecout<<"x"<<x;cout<<"y"<<y;float a=x/y;cout<<"x/y"<<a;}catch(...){cout<<"error occur // division by zero";}

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>

Page 39: 4th sem

10041

}

void main(){clrscr();read();div(x,y);getch();}

OUTPUT

Enter the two double numberx=5y=5a=1

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

Page 40: 4th sem

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;a=b;b=temp;}

void main(){int a[15]={9,8,7,6,5,4,3,2,1};

Page 41: 4th sem

10041

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 : 1In case of float array :1.9

Page 42: 4th sem

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;cout<<"enter the phoneno\n";cin>>ph;}void telph_recrd::write(){cout<<setiosflags(ios::left) <<setw(10)<<name

Page 43: 4th sem

10041

<<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();}

Page 44: 4th sem

10041

OUTPUT

enter the details for 2 recordenter the namejohnenter the phoneno23456

enter the nameahmedenter the phoneno123

outputjohn 23456ahmed 123

Page 45: 4th sem

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: "; cin>>id; cout<<"\nenter the income:"; cin>>income;

Page 46: 4th sem

10041

}

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();}

Page 47: 4th sem

10041

OUTPUT

enter the data for person 1

enter name: SUMITenter address: 179enter id: 0112enter the income:1234

enter the data for person 2

enter name: RINKUenter address: 129enter id: 1008enter the income:3456

enter the data for person 3

enter name: ROHITenter address: 456enter id: 10087enter the income:5678

output

Person No:1Name :SUMITId: 74Income:1234Address:179

Page 48: 4th sem

10041

Person No:2Name :RINKUId: 10083Income:12672Address:29

Person No:3Name :ROHITId: 10087Income:5678Address:456