You are on page 1of 105

THENI KAMMAVAR SANGAM COLLEGE OF TECHNOLOGY

Electrical & Electronics Engineering

OBJECT ORIENTED PROGRAMMING RECORD NOTE

NAME REGISTRATION NUMBER

THENI KAMMAVAR SANGAM COLLEGE OF TECHNOLOGY KODUVILARPATTI, THENI DISTRICT-625534 BONAFIDE CERTIFICATE

NAME REGISTRATION NUMBER SEMESTER DEPARTMERT SUBJECT SUBJECT CODE

Certified that this is the bonafide record of work done by the above student in Object Oriented Programming Laboratory during the year 2010-2011.

LAB-IN CHARGE (Mr.J.Ayyana prabu)

HEAD OF THE DEPARTMENT (Mr.K.Vel kumar)

Submitted for the Pratical Examination held on 27.10.2010

INTERNAL EXAMINER

EXTERNAL EXAMINER

EX NO 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

DATE

CONTENTS Simple Interest Enumeration Datatype Function Invocation Function Overloading Array Call by Reference Assertions Structure Structure pointer operator Union Diff b/t Structure and Union Class Constructor Destructor Static Member This Pointer ADT Conversions Unary Operator Overloading Binary Operator Overloading Function Selection

PAGE NO

STAFF SIGNATURE

EX NO DATE 21 22 23 24 25 26 27 28 29 30 31 32 33

CONTENTS Pointer Operators Derived Class Virtual Function Exception Handling Class Template Function Template Formatted I/O Operations Unformatted I/O operations Predefined Manipulators User Defined Manipulators File Modes File Pointer and Manipulators File I/O

PAGE NO

STAFF SIGNATURE

Ex.No:

Date: SIMPLE INTEREST

AIM:

ALGORITHM:

PROGRAM:

//Program for Simple Interest #include<iostream.h> #include<conio.h> void main() { int p,q,r,i; clrscr(); cout<<" Program for simple Interest "<<"\n"; cout<<"==============================="<<"\n"; cout<<" Enter the values of P, Q and R "<<"\n"; cin>>p>>q>>r; i=(p*q*r)/100; cout<<"Simple Interest="<<i; getch();

OUTPUT:

RESULT:

Ex.No: Date:
ENUMERATION DATATYPE

AIM:

ALGORITHM:

PROGRAM:
//Program for Enumeration Datatype #include<iostream.h> #include<conio.h> void main() { clrscr(); enum day { sunday, monday, tuesday, wednesday, thursday, friday, saturday }; day theday; int j; cout<<" Program for Enumerators "<<"\n"; cout<<"====================================="<<"\n"; cout<<"Please enter the day of a week(0-6):"; cin>>j; theday=day(j); if(theday==sunday||theday==saturday) { cout<<"Answer:Holiday"; } else { cout<<"Answer:Working day"; } getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
FUNCTION INVOCATION

AIM:

ALGORITHM:

PROGRAM:
//Program for Function Invocation #include<iostream.h> #include<conio.h> void swap(int a,int b) { int c; cout<<"Before swaping:"; cout<<"a="<<a<<" "<<"b="<<b<<"\n"; c=a; a=b; b=c; cout<<"After swaping:"; cout<<"a="<<a<<" "<<"b="<<b; } void main() { int a,b; clrscr(); cout<<" Program for Function Invocation "<<"\n"; cout<<"==============================="<<"\n"; cout<<"Enter the values of a and b "<<"\n"; cin>>a>>b; swap(a,b); getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
FUNCTION OVERLOADING

AIM:

ALGORITHM:

PROGRAM:
//Program for Function Overloading #include<iostream.h> #include<conio.h> void volume(int a) { int c; cout<<"First Function"<<"\n"; c=a+a+a; cout<<"Answer c="<<c<<"\n"; } void volume(double a,int b) { int c; cout<<"Second Function"<<"\n"; c=3.14*a*a*b; cout<<"Answer c="<<c<<"\n"; } void volume(long l,int b,int h) { int c; cout<<"Third Function"<<"\n"; c=l+b+h; cout<<"Answer c="<<c<<"\n"; } void main() { clrscr(); cout<<" Program for Function Overloading "<<"\n"; cout<<"================================"<<"\n"; volume(10); volume(2.5,8); volume(100,85,15); getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
ARRAY

AIM:

ALGORITHM:

PROGRAM:
//Program for Array #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[5][5],b[5][5],c[5][5],m,n,i,j; cout<<" Program for Array "<<"\n"; cout<<"============================================="<<"\n"; cout<<"Enter the row & column size for first matrix:"<<"\n"; cin>>m>>n; cout<<"Enter the elements:"<<"\n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>a[i][j]; } } cout<<"Enter the row & column size for second matrix:"<<"\n"; cin>>m>>n; cout<<"Enter the elements:"<<"\n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>b[i][j]; } } cout<<"The added matrix is:"<<"\n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; cout<<"\t "<<c[i][j]; } cout<<"\n"; } getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
CALL BY REFERENCE

AIM:

ALGORITHM:

PROGRAM:
//Program for Call by Reference #include<iostream.h> #include<conio.h> void swap(int *a,int *b) { int c; cout<<"Before swaping:"; cout<<"a="<<*a<<" "<<"b="<<*b<<"\n"; c=*a; *a=*b; *b=c; cout<<"After swaping:"; cout<<"a="<<*a<<" "<<"b="<<*b; } void main() { int a,b; clrscr(); cout<<" Program for Call By Reference "<<"\n"; cout<<"============================="<<"\n"; cout<<"Enter the values of a and b "<<"\n"; cin>>a>>b; swap(&a,&b); getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
ASSERTIONS

AIM:

ALGORITHM:

PROGRAM:
//Program for Assertion #include<iostream.h> #include<conio.h> #include<assert.h> void post(int e,int f) { int g; cout<<"Checking for post condition"<<"\n"; g=e; e=f; f=g; assert((f==g)&&(f!=e)); cout<<"Post condition checked"<<"\n"; cout<<"The value of a and b after swaping"<<"\n"; cout<<"__________________________________"<<"\n"; cout<<"The Value of a is:"<<e<<"\n"; cout<<"The Value of b is:"<<f<<"\n"; } void pre(int c,int d) { int e; cout<<"Checking for precondition"<<"\n"; assert(c!=d); cout<<"Precondition checked"<<"\n"; post(c,d); } void main() { clrscr(); cout<<" Program for swaping using assert() "<<"\n"; cout<<"=================================="<<"\n"; int a,b; cout<<"Enter the value of a:"; cin>>a; cout<<"enter the value of b:"; cin>>b; pre(a,b); getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
STRUCTURE

AIM:

ALGORITHM:

PROGRAM:
//Program for Structure #include<iostream.h> #include<conio.h> struct student { int rollno; char name[25]; char branch[15]; int marks; }; void main() { clrscr(); student s1; cout<< Program for structure <<\n; cout<<==================<<\n; cout<<"Enter the data of student"; cout<<"Roll no:"; cin>>s1.rollno; cout<<"Name:"; cin>>s1.name; cout<<"Branch:"; cin>>s1.branch; cout<<"Marks"; cin>>s1.marks; cout<<"Student report:"<<"\n"; cout<<"_____________"<<"\n"; cout<<"Roll no:"<<s1.rollno<<"\n"; cout<<"Name:"<<s1.name<<"\n"; cout<<"Branch:"<<s1.branch<<"\n"; cout<<"Percentage:"<<s1.marks/5<<"\n"; getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
STRUCTURE POINTER OPERATOR

AIM:

ALGORITHM:

PROGRAM:
//Program for Structure Pointer Operator #include<iostream.h> #include<conio.h> void main() { clrscr(); struct date { int month; int day; int year; }; struct date *date_ptr; date_ptr->month=12; date_ptr->day=12; date_ptr->year=1987; cout<<" Program for Structure Pointer Operator "<<"\n"; cout<<"======================================"<<"\n"; cout<<"Month:"<<date_ptr->month<<"\n"; cout<<"Day:"<<date_ptr->day<<"\n"; cout<<"Year:"<<date_ptr->year<<"\n"; getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
UNION

AIM:

ALGORITHM:

PROGRAM:
//Program for Union #include<iostream.h> #include<conio.h> union student { int rollno; char name[25]; char branch[15]; int marks; }; void main() { clrscr(); student s1,s2,s3,s4; cout<< Program for Union <<\n; cout<<==================<<\n; cout<<"Enter the data of student"; cout<<"Roll no:"; cin>>s1.rollno; cout<<"Name:"; cin>>s2.name; cout<<"Branch:"; cin>>s3.branch; cout<<"Marks"; cin>>s4.marks; cout<<"Student report:"<<"\n"; cout<<"_____________"<<"\n"; cout<<"Roll no:"<<s1.rollno<<"\n"; cout<<"Name:"<<s2.name<<"\n"; cout<<"Branch:"<<s3.branch<<"\n"; cout<<"Percentage:"<<s4.marks/5<<"\n"; getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
DIFF B/T STRUCTURE AND UNION

AIM:

ALGORITHM:

PROGRAM:
//Program for Diff B/T Structure and Union #include<iostream.h> #include<conio.h> struct boy { char name[25]; int id; }st; union girl { char name[25]; int id; }un; void main() { clrscr(); cout<<" Program for Diff B/T Structure and Union "<<"\n"; cout<<"========================================="<<"\n"; cout<<" The Size of Structure is "<<sizeof(st)<<" Bytes"<<"\n"; cout<<" The Size of Union is "<<sizeof(un)<<" Bytes"<<"\n"; getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
CLASS

AIM:

ALGORITHM:

PROGRAM:
//Program for Class #include<iostream.h> #include<conio.h> class item { int number; float cost; public: void getdata(int a,float b) { number=a; cost=b; } void putdata() { cout<<"No:"<<number<<"\n" ; cout<<"Cost:"<<cost<<"\n"; } }; void main() { clrscr(); item x; cout<<" Program for class "<<"\n"; cout<<"================="<<"\n"; cout<<"object x"<<"\n"; x.getdata(100,45.5); x.putdata(); item y; cout<<"object y"<<"\n"; y.getdata(200,75.5); y.putdata(); getch(); }

OUTPUT:

RESULT:

Ex.No: Date: CONSTRUCTOR

AIM:

ALGORITHM:

PROGRAM:
//Program for Constructor #include<iostream.h> #include<conio.h> class Bag { private: int contents[25]; int itemcount; public: Bag() { itemcount=0; } void put(int item) { contents[itemcount++]=item; } void show() { for(int i=0;i<itemcount;i++) cout<<contents[i]<<" "; cout<<"\n"; } }; void main() { clrscr(); cout<<" Program for Constructor "<<"\n"; cout<<"===================================="<<"\n"; int item; Bag bag; while(1) { cout<<"Enter the item number put into bag:"; cin>>item; if(item==0) break; bag.put(item); cout<<"Items in bag"; bag.show(); } }

OUTPUT:

RESULT:

Ex.No: Date:
DESTRUCTOR

AIM:

ALGORITHM:

PROGRAM:
//Program for Destructor #include<iostream.h> #include<conio.h> class test { public: test(); ~test(); }; test::test() { cout<<"constructor of class test called"<<"\n"; } test::~test() { cout<<"destructor of class test called"<<"\n"; } void main() { clrscr(); cout<<" Program For Desructor "<<"\n"; cout<<"==============================="<<"\n"; test x; cout<<"terminating main()"<<"\n"; getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
STATIC MEMBER

AIM:

ALGORITHM:

PROGRAM:
//Program for Static Member #include<iostream.h> #include<conio.h> class test { int code; static int count; public: void setcode() { code=++count; } void showcode() { cout<<"Object Number:"<<code<<"\n"; } static void showcount() { cout<<"count="<<count<<"\n"; } }; int test::count; void main() { clrscr(); cout<<Program fot static Member<<\n; cout<<==================== <<\n; test t1,t2; t1.setcode(); t2.setcode(); test::showcount(); test t3; t3.setcode(); test::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); getch(); }

OUTPUT:

RESULT:

Ex.No: Date: THIS POINTER

AIM:

ALGORITHM:

PROGRAM:
//Program for this pointer #include<iostream.h> #include<conio.h> class student { private: int a,b; public: void setdata(int a) { b=a; cout<<"Address of my object:"<<this<<"\n"; this->showdata(); } void showdata() { cout<<"Data accessed in normal way:"<<b<<"\n"; cout<<"Data accessed by using this pointer:"<<this->b<<"\n"; } }; void main() { clrscr(); cout<<" Program for this pointer "<<"\n"; cout<<"====================================="<<"\n"; student N; N.setdata(25); getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
ADT CONVERSIONS

AIM:

ALGORITHM:

PROGRAM:
//Program for ADT Conversions #include<iostream.h> #include<conio.h> void main() { clrscr(); int a=20000,b=20000,c; cout<<" Program for ADT Conversions "<<"\n"; cout<<"==========================="<<"\n"; cout<<"Implicit Type Conversion"<<"\n"; c=a+b; cout<<"Answer:"<<c<<"\n"; cout<<"Explicit Type Conversion"<<"\n";; int x=20000,y=20000; unsigned int z=(unsigned int) x + (unsigned int )y; cout<<"Answer:"<<z; getch(); }

OUTPUT:

RESULT:

Ex.No: Date: UNARY OPERATOR OVERLOADING

AIM:

ALGORITHM:

PROGRAM:
//Program for Unary Operator Overloading #include<iostream.h> #include<conio.h> class space { private: int x,y,z; public: void getdata(int a,int b) { x=a; y=b; } void display() { cout<<"x="<<x<<"\n"; cout<<"y="<<y<<"\n"; } void operator++() { x=++x; y=++y; } void operator--() { x=--x; y=--y; } }; void main() { clrscr(); space s; cout<<" Program for Unary Operator Overloading "<<"\n"; cout<<"======================================"<<"\n"; cout<<"UNARYPLUS OPERATOR"<<"\n"; cout<<"======================"<<"\n"; cout<<"Initial values of x and y"<<"\n"; s.getdata(10,-20); s.display(); ++s; cout<<"Final values of x and y "<<"\n";

s.display(); cout<<"UNARY MINUS OPERATOR"<<"\n"; cout<<"======================="<<"\n"; cout<<"Initial Values of x and y"<<"\n"; s.getdata(10,-20); s.display(); --s; cout<<"Final values of x and y "<<"\n"; s.display(); getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
BINARY OPERATOR OVERLOADING

AIM:

ALGORITHM:

PROGRAM:
//Program for Binary Operator Overloading #include<iostream.h> #include<conio.h> class complex { private: float real; float imag; public: complex() { real=imag=0.0; } void getdata() { cout<<"Real part:"; cin>>real; cout<<"Imag part:"; cin>>imag; } void outdata() { cout<<"The Final Answer is:"; cout<<"("<<real; cout<<","<<imag<<")"; } complex operator+(complex c2) { real=real+c2.real; imag=imag+c2.imag; return *this; } }; void main() { clrscr(); complex c1,c2,c3; cout<<" Program for Binary operator Overloading "<<"\n"; cout<<"======================================="<<"\n"; cout<<"Enter the complex no c1"<<"\n"; c1.getdata();

cout<<"Enter the complex no c2"<<"\n"; c2.getdata(); c3=c1+c2; c3.outdata(); getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
FUNCTION SELECTION

AIM:

ALGORITHM:

PROGRAM:
//Program for Function Selection #include<iostream.h> #include<conio.h> void volume(int a) { int c; cout<<"1)Exact Match"<<"\n"; c=a+a+a; cout<<"Answer:"<<c<<"\n"; } void volume(float b,int d) { int c; cout<<"2)Standard Type Promotion"<<"\n"; c=b+d; cout<<"Answer:"<<c<<"\n"; } void volume(long d,double e,double f) { int c; cout<<"3)Standard Type Conversion"<<"\n"; c=d+e+f; cout<<"Answer:"<<c<<"\n"; } void main() { clrscr(); cout<<" Program for Function Selection Algorithm "<<"\n"; cout<<"========================================"<<"\n"; volume(65); volume(5.5,65500); volume(10L,85,100); getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
POINTER OPERATORS

AIM:

ALGORITHM:

PROGRAM:
//Program for Pointer Operators #include<iostream.h> #include<conio.h> void main() { clrscr(); int a ,*ptr; cout<<" Program for pointer operators "<<"\n"; cout<<"======================================="<<"\n"; cout<<"Enter the value of a:"; cin>>a; ptr=&a; cout<<"The Value of variable a is:"<<a<<"\n"; cout<<"The Address of variable a is:"<<ptr<<"\n"; *ptr=(*ptr)/2; cout<<"Now The Value of a is:"<<(*ptr)<<"\n"; getch(); }

OUTPUT:

RESULT:

Ex.No: Date: DERIVED CLASS AIM:

ALGORITHM:

PROGRAM:
//Program for Derived Class(Inheritance) #include<iostream.h> #include<conio.h> class student { protected: int rollno,percentage; public: void getmarks(int a,int b) { rollno=a; percentage=b; } }; class girl:public student { public: void display() { cout<<"rollno:"<<rollno<<"\n"; cout<<"percentage:"<<percentage<<"\n"; } }; class boy:public student { public: void display() { cout<<"rollno:"<<rollno<<"\n"; cout<<"percentage:"<<percentage<<"\n"; } }; void main() { clrscr(); girl s; boy t; cout<<"program for derived class"<<"\n"; s.getmarks(1990,98); t.getmarks(1991,99);

cout<<"anna university result"<<"\n"; s.display(); t.display(); getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
VIRTUAL FUNCTIONS

AIM:

ALGORITHM:

PROGRAM:
//Program for Virtual Function #include<iostream.h> #include<conio.h> class base { public: virtual void display() { cout<<"Display base"<<"\n"; } virtual void show() { cout<<"Show base"<<"\n"; } }; class derived:public base { public: void display() { cout<<"Display derived"<<"\n"; } void show() { cout<<"Show derived"<<"\n"; } }; void main() { clrscr(); base B; derived D; base *point; cout<<" Program for Virtual functions "<<"\n"; cout<<"============================="<<"\n"; cout<<"Pointer points Base class object"<<"\n"; point=&B; point->display(); point->show();

cout<<"Pointer points Derived class object"<<"\n"; point=&D; point->display(); point->show(); getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
EXCEPTION HANDLING

AIM:

ALGORITHM:

PROGRAM:
//Program for Exception Handling #include<iostream.h> #include<conio.h> #include<exception.h> void divide(int x,int y,int z) { cout<<"We are inside the function"<<"\n"; if((x-y)!=0) { int r=z/(x-y); cout<<"Result="<<r<<endl; } else { throw(x-y); } } void main() { cout<<" Program For Exception Handling "<<"\n"; cout<<"=============================="<<"\n"; try { cout<<"We are inside the try block"<<"\n"; divide(10,20,30); divide(10,10,20); } catch(int i) { cout<<"Caught the Exception"<<"\n"; } getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
CLASS TEMPLATE AIM:

ALGORITHM:

PROGRAM:
//Program for Class Template #include<iostream.h> #include<conio.h> template<class t1,class t2> class test { private: t1 a; t2 b; public: test(t1 x,t2 y) { a=x; b=y; } void show() { cout<<"Answer:"<<"a="<<a <<" and "<<"b="<<b<<"\n"; } }; void main() { clrscr(); cout<<" Program for Class Template "<<"\n"; cout<<"=========================="<<"\n"; test<float,int> test1(1.23,123); test<int,char> test2(100,'w'); test1.show(); test2.show(); getch(); }

OUTPUT:

RESULT:

Ex.No: Date: FUNCTION TEMPLATE

AIM:

ALGORITHM:

PROGRAM:
//Program for Function Template #include<iostream.h> #include<conio.h> template<class T> void swap(T &x,T &y) { T temp =x; x=y; y=temp; } void fun(int m,int n,float a,float b) { cout<<"Values of m and n before swap:"<<m<<" "<<n<<"\n"; swap(m,n); cout<<"Values of m and n after swap:"<<m<<" "<<n<<"\n"; cout<<"Values of a and b before swap:"<<a<<" "<<b<<"\n"; swap(a,b); cout<<"Values of a and b after swap:"<<a<<" "<<b<<"\n"; } void main() { clrscr(); cout<<" Program for Function Template "<<"\n"; cout<<"=========================================="<<"\n"; fun(100,200,11.22,22.33); getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
FORMATTED I/O OPERATION

AIM:

ALGORITHM:

PROGRAM:
//Program for Formatted I/O Operations #include<iostream.h> #include<conio.h> void main() { clrscr(); char *name[5]={"Ram","Raj","Raju "}; float cut[5]={195.253,196.642,197.582}; cout<<" Students Cutoff Marks "<<"\n"; cout<<"========================="<<"\n"; cout<<"Name "<<" "<<" Result "<<"\n"; cout<<"========================="<<"\n"; for(int i=0;i<3;i++) { cout.setf(ios::left,ios::adjustfield); cout.fill('*'); cout.width(12); cout<<name[i]; cout<<" "; cout.fill('*'); cout.precision(2); cout.width(12); cout<<cut[i]<<"\n"; } cout<<"========================="<<"\n"; getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
UNFORMATTED I/O OPERATIONS

AIM:

ALGORITHM:

PROGRAM:
//Program for Unformatted I/O Operations #include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); cout<<" Program for Unfomatted I/O Operations "<<"\n"; cout<<"====================================="<<"\n"; int count=0; char c,test[40]; cout<<"Enter your Name:"; cin.get(c); cout<<"My name is "; while(c!='\n') { cout.put(c); count++; cin.get(c); } cout<<"\n"; cout<<"Enter Your Native Place:"; cin.getline(test,40); cout<<"My Native Place is "; int len=strlen(test); cout.write(test,len); cout<<"\n"; getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
STANDARD MANIPULATOR

AIM:

ALGORITHM:

PROGRAM:
//Program for Standard Manipulator #include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { clrscr(); char *name[5]={"Raj","Ram","Rajesh"}; float cut[5]={195.253,196.642,197.582}; cout<<" Program for standard manipulators "<<"\n"; cout<<"================================="<<"\n"; cout<<" students cutoff marks "<<"\n"; cout<<"================================="<<\n; cout<<" name "<<" "<<" result "<<"\n"; cout<<"================================="<<\n; for(int i=0;i<3;i++) { cout<<setw(10)<<setfill('*')<<name[i]<<" " << setw(10)<<setprecision(2)<<cut[i]<<\n; } cout<<"================================="<<"\n"; getch(); }

OUTPUT:

RESULT:

Ex.No: Date: USER DEFINED MANIPULATOR

AIM:

ALGORITHM:

PROGRAM:
//Program for Userdefined Manipulatots #include<iostream.h> #include<conio.h> ostream & sp(ostream & os) { os<<" "; return os; } void main() { clrscr(); int w=1,x=2,y=3,z=4; cout<<" Program for User Defined Manipulator "<<"\n"; cout<<"===================================="<<"\n"; cout<<"Answer:"; cout<<"w="<<w<<sp<<"x="<<x<<sp<<"y="<<y<<sp<<"z="<<z<<sp<<"\n"; getch(); }

OUTPUT:

RESULT:

Ex.No: Date:
FILE MODES

AIM:

ALGORITHM:

PROGRAM:
//Program for Filemodes #include<iostream.h> #include<conio.h> #include<iomanip.h> #include<fstream.h> void main() { clrscr(); char *name[5]={"Sachin","Ramesh","Tendulkar"}; float cut[5]={195.253,196.642,197.582}; ofstream outfile("prabu.txt",ios::out); outfile<<" program for file modes "<<"\n"; outfile<<"======================"<<"\n"; outfile<<" students cutoff marks "<<"\n"; outfile<<"======================"<<"\n"; outfile<<" name "<<" "<<" result "<<"\n"; outfile<<"======================"<<"\n"; for(int i=0;i<3;i++) { outfile<<setw(10)<<setfill('*')<<name[i]<<"" <<setw(10)<<setprecision(2)<<cut[i]<<\n; } outfile<<"======================"<<"\n"; getch(); }

OUTPUT: cse.txt Program for File Modes ================== Students cut off Marks ================== Name Result ================== Sachin 195.25 Ramesh 196.64 Tendulkar 197.58 ==================

RESULT:

Ex.No: Date:
FILE POINTER AND MANIPULATIONS

AIM:

ALGORITHM:

PROGRAM:
//Program for FilePointer and Manipulations a)Program for seekg() and tellg() #include<iostream.h> #include<fstream.h> #include<conio.h> void main () { clrscr(); cout<<" Program for seekg() and tellg() "<<"\n"; cout<<"==============================="<<"\n"; ifstream myfile("eee.txt"); myfile.seekg(0,ios::end); cout<<"The size of Input File is:"<<myfile.tellg()<<"\n"; getch(); }

Input File: eee.txt We are EEE Rockers

OUTPUT:

b)Program for seekp() and tellp() #include<iostream.h> #include <fstream.h> void main () { int pos; ofstream outfile; outfile.open ("123.txt"); outfile.write ("This is an apple",16); pos=outfile.tellp(); outfile.seekp (pos-7); outfile.write (" sam",4); outfile.close(); }

Output File:

123.txt This is a sample

RESULT:

Ex.No: Date:
FILE I/O OPERATIONS

AIM:

ALGORITHM:

PROGRAM:
//Program for File I/O Operations #include<iostream.h> #include<conio.h> #include<fstream.h> void main() { fstream infile,outfile; int i,count,percentage; char name[30]; infile.open("eee.txt",ios::in); outfile.open("cse.txt",ios::out); infile>>count; outfile<<" Anna University Results "<<"\n"; outfile<<"======================="<<"\n"; outfile<<" Name "<<" "<<" Result "<<"\n"; outfile<<"======================="<<"\n"; for(i=0;i<count;i++) { infile>>name; infile>>percentage; outfile<< name <<" "<<percentage<<"\n"; } outfile<<"======================="<<"\n"; infile.close(); outfile.close(); }

OUTPUT: Input File: eee.txt 3 Sachin 97 Ramesh 98 Tendulkar 99 Output File: cse.txt Anna University Results =================== Name Result ================== Sachin 97 Ramesh 98 Tendulkar 99 ==================

RESULT:

You might also like