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

//write a program to define a class.

#include<iostream.h> #include<conio.h> class P { public: void show() { cout<<Hello Everybody; } }; void main() { P s; s.show(); getch(); }

Output:
Hello Everybody

//write a program Inside and Outside the class


#include<iostream.h> #include<conio.h> class P { public : void show() { cout<<"Hello"; } void add(int a,int b); }; void P::add(int a,int b) { int s; s=a+b; cout<<\n<<s; } void main() { P o; clrscr(); o.show(); o.add(11,12); getch(); }

Output
Hello 23

/*write a program for implicit and Explicit Conversion.*/


#include<iostream.h> #include<conio.h> class P { public: void implicit() { int a; float b; cout<<"Enter the value for a "; cin>>a; b=a; cout<<"\n"<<b; } void explicit() { float a,b; int c; cout<<"\nEnter values for a and b "; cin>>a>>b; c=a+b; cout<<"\nSum is "<<c; } }; void main() { P o; clrscr(); o.implicit(); o.explicit(); getch(); }

Output:
Enter the value for a 5 5 Enter values for a and b 12.12 23.45 Sum is 35

//write a program for ENUM


#include<iostream.h> #include<conio.h> enum SS { a, m, d, s }; void main() { int x,b,c,code; clrscr(); cout<<"\nEnter value for a and b "; cin>>x>>b; cout<<"\nEnter the value for code "; cin>>code; switch(code) { case a: c=x+b; cout<<"Sum is "<<c; break; case m: c=x*b; cout<<"Multi is "<<c; break; case d: c=x/b; cout<<"Division is "<<c; break; case s: c=x-b; cout<<"subtraction is "<<c; break; default: cout<<"Enter correct value"; break; } getch(); }

Output:
Enter value for a and b 12 10 Enter the value for code 1 Multi is 120

/*write a program for calling a function using Call By Value.*/


#include<iostream.h> #include<conio.h> class P { public: void showab(int p,int q) { p=p+10; q=q+10; cout<<"\nInside the function p and q are"; cout<<p<<"\t"<<q; } }; void main() { int p=10,q=11; P o; clrscr(); cout<<"\nBefore function call p and q are"; cout<<p<<"\t"<<q<<"\n"; o.showab(p,q); cout<<"\nAfter function call p and q are"; cout<<p<<"\t"<<q<<"\n"; getch(); }

Output:
Before function call p and q are 10 11 Inside the function p and q are 20 21 After function call p and q are 10 11

/*write a program for calling a function using Call By Reference*/


#include<iostream.h> #include<conio.h> class P { public: void showab(int *p,int *q) { *p=*p+10; *q=*q+10; cout<<"\nInside the function call p and q\n"; cout<<*p<<"\t"<<*q; } }; void main() { int p=10,q=11; P o; clrscr(); cout<<"\nBefore function call p and q are\n"; cout<<p<<"\t"<<q<<"\n"; o.showab(&p,&q); cout<<"\nafter function call p and q are\n"; cout<<"\n"<<p<<"\t"<<q; getch(); }

Output:
Before function call p and q are 10 11 Inside the function call p and q 20 21 after function call p and q are 20 21

/* write a program to check whether it is Prime or not*/.


#include<iostream.h> #include<conio.h> class P { public: void prime() { int num,i; cout<<"Enter a number"; cin>>num; for(i=2;i<num;i++) { if(num%i==0) { cout<<"Number is not prime"; break; } } if(i==num) { cout<<"number is prime"; } } }; void main() { P o; clrscr(); o.prime(); getch(); }

Output:
Enter a number7 number is prime Enter a number6 Number is not prime

//write a program for getting Factorial of a number.


#include<iostream.h> #include<conio.h> class P { int a; public: void fact(int x) { int i,f=1; a=x; for(i=a;i>=1;i--) { f=f*i; } cout<<"\nFactorial is"<<f; } }; void main() { int num; P o; cout<<"Enter a number "; cin>>num; o.fact(num); getch(); }

Output:
Enter a number 5 Factorial is120

//write a program for printing Fibonacci series.


#include<iostream.h> #include<conio.h> class P { public: void febo() { int a,b,c,i; a=0; b=1; c=a+b; cout<<a<<"\n"; cout<<b<<"\n"; for(i=0;i<=12;i++) { c=a+b; cout<<c<<"\n"; a=b; b=c; i++; } } }; void main() { P o; clrscr(); o.febo(); getch(); }

Output:
0 1 1 2 3 5 8 13 21

/*write a program for checking whether string is Palindrome or not.*/


#include<iostream.h> #include<conio.h> #include<string.h> #define size 26 class P { public: void palin() { char strsrc[size]; char strtmp[size]; clrscr(); cout<<"\n Enter String:= "; cin>>strsrc; strcpy(strtmp,strsrc); strrev(strtmp); if(strcmp(strsrc,strtmp)==0) cout<<"\n Entered string "<<strsrc<<" is palindrome "; else cout<<"\n Entered string "<<strsrc<<" is not palindrom "; } }; void main() { P o; o.palin(); getch(); }

output:
Enter String:= madam Entered string madam is palindrome

//write a program of one Dimensional Array.


#include<iostream.h> #include<conio.h> class P { int a[10]; public: void getarr() { int a[10],i,n; cout<<"How many values "; cin>>n; cout<<"Enter the elements "; for(i=0;i<n;i++) { cin>>a[i]; } cout<<"Array is "; for(i=0;i<n;i++) { cout<<"\t"<<a[i]; } } }; void main() { P o; o.getarr(); getch(); }

Output:
How many values 5 Enter the elements 6 3 2 1 5 Array is 6 3

//write a program to make a Inline Function.


#include<iostream.h> #include<conio.h> class P { public: inline int cube(int a) { return a*a*a; } inline multi(int x,int y) { return x*y; } }; void main() { P o; int c,m; clrscr(); c=o.cube(20); cout<<"cube is "<<c; m=o.multi(11,12); cout<<"multi is "<<m; getch(); }

Output:
cube is 8 multi is 132

/*write an example of Default and Const Arguments.*/


#include<conio.h> #include<iostream.h> class P { public: void show(const int *x,int y=20) { int s; s=(*x)+y; cout<<"\nSum is:\t"<<s; } }; void main() { P o,o1; int x=12; clrscr(); o.show(&x); o1.show(&x,15); getch(); }

Output:
Sum is: 32 Sum is: 27

//write a program for Nesting of Function


#include<iostream.h> #include<conio.h> class P { int a,b; public: int sum(int x,int y) { a=x; b=y; return a+b; } void show() { int s; s=sum(11,12); cout<<"sum is "<<s; } }; void main() { P o; o.show(); getch(); }

output:
sum is 23.

//write a program for Method Overloading.


#include<iostream.h> #include<conio.h> class P { public: void show() { cout<<"\nExample of function overloading"; } void show(int a) { int c; c=a*a*a; cout<<"\nCube is "<<c; } void show(int x,int y) { int s; s=x+y; cout<<"\nsum is "<<s; } }; void main() { P o; o.show(); o.show(5); o.show(11,12); getch(); }

output:
Example of function overloading Cube is 125 sum is 23

/*Write a program for Defining Private Member Function*/


#include<iostream.h> #include<conio.h> class P { private: int a; void square(int x) { int s; a=x; s=a*a; cout<<"\n square is "<<s; } public: void show() { square(10); } }; void main() { P o; clrscr(); o.show(); getch(); }

Output:
Square is 100

/*Write a program for Static Member Function and Static Data Member*/
#include<iostream.h> #include<conio.h> class P { private: int a; static int count; public: void read(int x) { a=x; count++; } static void getcount() { cout<<"\ncount is "; cout<<count; } }; int P::count; void main() { P x,y,z; clrscr(); P::getcount(); x.read(10); P::getcount(); y.read(20); P::getcount(); z.read(30); P::getcount(); getch(); }

output:
count is 0 count is 1 count is 2 count is 3

/* Write a program for Array of Objects.


#include<iostream.h> #include<conio.h> class P { int rollno; int age; public: void getdata() { cout<<"Enter rollno and age "; cin>>rollno>>age; } void putdata() { cout<<"\nRollno is "<<rollno; cout<<"\nAge is "<<age; cout<<"\n"; } }; void main() { P a[3]; int i; clrscr(); for(i=0;i<3;i++) { a[i].getdata(); } for(i=0;i<3;i++) { a[i].putdata(); } getch(); }

output:
Enter rollno and age 1 20 Enter rollno and age 2 20 Enter rollno and age 3 20 Rollno is 1 Age is 20 Rollno is 2 Age is 20 Rollno is 3 Age is 20

//write a program of Object As Argument.


#include<iostream.h> #include<conio.h> class P { int a,s; public: void geta(int y) { a=y; } void sum(P o,P o1) { s=o.a+o1.a; cout<<"Sum is "<<s; } }; void main() { P x,x1,x2; clrscr(); x.geta(10); x1.geta(11); x2.sum(x,x1); getch(); }

Output:
Sum is 21

//write a program for Friend Function.


#include<iostream.h> #include<conio.h> class P { int a,b; public: void setvalues() { a=11; b=12; } friend void multi(P q); }; void multi(P q) { int s; s=q.a*q.b; cout<<"multi is "<<s; } void main() { P o; o.setvalues(); multi(o); getch(); }

Output:
multi is 132

/*write a program for Friend Function more than one class*/


#include<iostream.h> #include<conio.h> class P; class S { int a; public: void seta(int x) { a=x; } friend void max(S s,P ss); }; class P { int b; public: void setb(int y) { b=y; } friend void max(S s,P ss); }; void max(S s,P ss) { if(s.a>ss.b) cout<<"s is greater"<<s.a; else cout<<"ss is greater"<<ss.b; } void main() { S o; P o1; int a,b; clrscr(); cout<<"Enter the numbers "; cin>>a>>b; o.seta(a); o1.setb(b); max(o,o1); getch(); }

Output:
Enter the numbers 12 14 ss is greater14

//write a program for Default Constructor


#include<iostream.h> #include<conio.h> class P { int a,b; public: P() { a=12; b=2; } void show() { cout<<"a is "<<a<<"\n"; cout<<"b is "<<b; } }; void main() { P o; clrscr(); o.show(); getch(); }

Output:
a is 12 b is 2

//write a program for Default Argument in Constructor.


#include<iostream.h> #include<conio.h> class P { int a,b; public: P(int x,int y=10) { a=x; b=y; } void show() { cout<<"a is "<<a<<"\n"; cout<<"b is "<<b; } }; void main() { P o(12); clrscr(); o.show(); getch(); }

Output:
a is 12 b is 10

//write a program for Copy Constructor.


#include<iostream.h> #include<conio.h> class P { int a; public: P(int x) { a=x; } P(P &i) { a=i.a; } void show() { cout<<"a is "<<a<<"\n"; } }; void main() { P o(12); P o1(o); P o2=o; clrscr(); o.show(); o1.show(); o2.show(); getch(); } Output a is 12 a is 12 a is 12

//Write a program for Constructor Overloading.


#include<iostream.h> #include<conio.h> class P { int a,b; public: P() { a=10; b=12; } P(int x,int y) { a=x; b=y; } void show() { cout<<"\na is "<<a; cout<<"\n"; cout<<"b is "<<b; } }; void main() { P o1; P o(2,3); clrscr(); o1.show(); o.show(); getch(); }

Output:
a is 10 b is 12 a is 2 b is 3

//write a program for Destructor.


#include<iostream.h> #include<conio.h> int count=0; class P { public: P() { count++; cout<<"Object created "<<count<<"\n"; } ~P() { cout<<"object destroyed "<<count<<"\n"; count--; } }; void main() { clrscr(); P a; { P b,c,d; } getch(); }

Output:
Object created 1 Object created 2 Object created 3 Object created 4 object destroyed 4 object destroyed 3 object destroyed 2

//write a program for calculating sum of array.


#include<iostream.h> #include<conio.h> class P { public: void sum() { int a[20],i,n,s=0; cout<<"How many values "; cin>>n; cout<<"\nEnter elements one by one "; for(i=0;i<n;i++) { cin>>a[i]; } for(i=0;i<n;i++) { cout<<a[i]; cout<<"\t"; s=s+a[i]; } } }; void main() { P o; clrscr(); o.sum(); getch(); }

Output:
How many vlaues 5 Enter elements one by one 1 2 3 4 5 1 2 3 4 5 Sum of array is 15

//Write a program for adding Two Matrixes.


#include<iostream.h> #include<conio.h> class S { public: int i,j; void arrs() { int arr[3][3]; cout<<"Enter values for matrix arr[][]"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>arr[i][j]; } }

for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<arr[i][j]<<"\t"; } cout<<"\n"; } int arr1[3][3]; cout<<"\nEnter values for matrix arr1[][]"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>arr1[i][j]; } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<arr1[i][j]<<"\t"; } cout<<"\n"; }

int s[3][3] ; cout<<"\nSum of matrixes are\n"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { s[i][j]=arr[i][j]+arr1[i][j]; cout<<s[i][j]<<"\t"; } cout<<"\n"; } } }; void main() { S p; clrscr(); p.arrs(); getch(); }

Output:
Enter values for matrix arr[][]1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 Enter values for matrix arr1[][]9 8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 1 Sum of matrixes are 10 10 10 10 10 10 10 10 10

/*write a program to Multiply to Matrices*/


#include<iostream.h> #include<conio.h> class P { public: void multi() { int a[2][2],b[2][2],c[2][2],i,j,k; clrscr(); for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<"Enter the value "; cin>>a[i][j]; } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<"Enter the value "; cin>>b[i][j]; } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { c[i][j]=0; for(k=0;k<2;k++) { c[i][j]+=a[i][k]*b[k][j]; } } cout<<"\n"; } cout<<"\nmatrix one\n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<"\t"<<a[i][j]; }

cout<<"\n"; } cout<<"\nmatrix two\n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<"\t"<<b[i][j]; } cout<<"\n"; } cout<<"\nMulti plication of matrix\n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<"\t"<<c[i][j]; } cout<<"\n"; } } }; void main() { P o; clrscr(); o.multi(); getch(); }

Output:
Enter the value 1 Enter the value 2 Enter the value 3 Enter the value 4 Enter the value 1 Enter the value 2 Enter the value 3 Enter the value 4

matrix one 1 2 3 4 matrix two 1 2 3 4 Multi plication of matrix 7 10 15 22

//write a program for Single Inheritance


#include<iostream.h> #include<conio.h> class P { int a; public: void showP() { cout<<"\nHello from P"; } }; class S:public P { public: void showS() { cout<<"\nHello from S"; } }; void main() { S p; p.showP(); p.showS(); getch(); }

Output:
Hello P Hello S

//write a program Multi Level Inheritance


#include<iostream.h> #include<conio.h> class A { public: void showA() { cout<<"\n This is class A"; } }; class B:public A { public: void showB() { cout<<"\nHello this is class B"; } }; class C:public B { public: void showC() { cout<<"\nI inherit class B and A"; } }; void main() { C o; o.showA(); o.showB(); o.showC(); getch(); }

Output:
This is class A Hello this is class B I inherit class B and A

//write a program for Multiple Inheritances


#include<iostream.h> #include<conio.h> class A { public: void showA() { cout<<"\nThis is class A"; } }; class B { public: void showB() { cout<<"\nHello this is class B"; } }; class C:public A,public B { public: void showC() { cout<<"\nI inherit two classes"; } }; void main() { C o; o.showA(); o.showB(); o.showC(); getch(); }

Output:
This is class A Hello this is class B I inherit two classes

//write a program for Hierarchical Inheritance.


#include<iostream.h> #include<conio.h> class A { public: void showA() { cout<<"\nThis is class A"; } }; class B:public A { public: void showB() { cout<<"\nHello this is class B"; } }; class C:public A { public: void showC() { cout<<"\nHello everybody"; } }; class D:public B { public: void showD() { cout<<"\nHello from D"; } }; class E:public C { public: void showE() { cout<<"\nHi its C"; } }; void main() {

D o; o.showA(); o.showB(); o.showD(); E o1; o1.showA(); o1.showC(); o1.showE(); getch(); }

Output:
This is class A Hello this is class B Hello from D This is class A Hello everybody Hi its C

//write a program for Hybrid Inheritance.


#include<iostream.h> #include<conio.h> class A { public: void showA() { cout<<"\nThis is class A"; } }; class B:public A { public: void showB() { showA(); cout<<"\nHello this is class B"; } }; class C:public A { public: void showC() { cout<<"\nHello everybody"; } }; class D:public B,public C { public: void showD() { cout<<"\nHello from D"; } }; void main() { D o; o.showD(); o.showC(); o.showD(); getch(); }

//Write a program to solve ambiguity using scope resolution operator.


#include<iostream.h> #include<conio.h> class A { public: virtual void show() { cout<<"\n Hello this is A"; } }; class B:public A { public: void show() { cout<<"\n Hello this is B"; } }; void main() { B o; clrscr(); o.show(); o.A::show(); getch(); }

Output:
Hello this is B Hello this is A

//write a program for Virtual Function.


#include<iostream.h> #include<conio.h> class A { public: virtual void show() { cout<<"\n Hello this is A"; } }; class B:public A { public: void show() { cout<<"\n Hello this is B"; } }; void main() { B o; A a; A *p; clrscr(); p=&o; p->show(); p=&a; p->show(); getch(); }

Output:
Hello this is B Hello this is A

//write a program for Virtual Base class.


#include<iostream.h> #include<conio.h> class A { public: virtual void show() { cout<<"\n Hello this is A"; } }; class B:virtual public A { public: void displayB() { cout<<"\n Hello this is B"; } }; class C:virtual public A { public: void displayC() { cout<<"\nHello this is C"; } }; class D:public B,public C { public: void displayD() { cout<<"\nHello this is D"; } }; void main() { D o; clrscr(); o.show(); o.displayB(); o.displayC(); o.displayD(); getch(); }

Output:
Hello this is A Hello this is B Hello this is C Hello this is D

//write a program for Pure Virtual Function.


#include<iostream.h> #include<conio.h> class A { public: virtual void show()=0; }; class B:public A { public: void show() { cout<<"I derived virtual function for abstract base calss\n"; } }; class C:public A { public: void show() { cout<<"\nI am another derived class for pure virtual function"; } }; void main() { B b; C c; A *p; p=&b; p->show(); p=&c; p->show(); getch(); }

Output:
I derived virtual function for abstract base calss I am another derived class for pure virtual function

//Write a program for Inheritance with Protected access modifier


#include<iostream.h> #include<conio.h> class A { protected: int a,b; public: void setab(int x,int y) { a=x; b=y; } void show() { cout<<"\nHello"; } }; class B:public A { public: void multi() { int m; m=a*b; cout<<"\nMulti is "<<m; } }; class C:public B { public: void redisplay(int x,int r) { a=x; b=r; cout<<"\nNow a and b is "<<a<<"\t"<<b; } }; void main() { C o; clrscr(); o.setab(10,15); o.show(); o.multi();

o.redisplay(20,30); getch(); }

output:
Hello Multi is 150 Now a and b is 20

30

/*write a program for operator overloading using Unary Operator Overload*/


#include<iostream.h> #include<conio.h> class P { int a,b; public: void getdata(int x,int y) { a=x; b=y; } void showdata() { cout<<"\na is "<<a; cout<<"\nb is "<<b; } void operator -(); }; void P::operator -() { a=-a; b=-b; } void main() { P o; o.getdata(10,-12); o.showdata(); -o; o.showdata(); getch(); }

Output:
a is 10 b is -12 a is -10 b is 12

/*write a program for overloading a Prefix and Postfix operator*/


#include<iostream.h> #include<conio.h> class P { int a,b; public: P(){} P(int x) { a=x; } P operator ++(); P operator --(); void showdata() { cout<<"\na is "<<a; } }; P P::operator ++() { a=++a; return a; } P P::operator --() { a=--a; return a; } void main() { P x1(12); clrscr(); x1.showdata(); ++x1; x1.showdata(); --x1; x1.showdata(); getch(); }

Output:
a is 12 a is 13 a is 12

/*write a program for overloading a Binary Operator .*/


#include<iostream.h> #include<conio.h> class P { int a,b; public: P(){} P(int x,int y) { a=x; b=y; }; void showdata() { cout<<"\na is "<<a<<"\n"; cout<<"\nb is "<<b<<"\n"; } P operator +(P o); }; P P::operator +(P o) { P o2; o2.a=a+o.a; o2.b=b+o.b; return o2; } void main() { P o1(10,11); P o2(20,30); P o3; clrscr(); o3=o1+o2; o1.showdata(); o2.showdata(); o3.showdata(); getch(); }

Output:
a is 10 b is 11 a is 20 b is 30 a is 30 b is 41

/*write a program for Operator Overloading using the Friend Function.*/


#include<iostream.h> #include<conio.h> class P { int a,b; public: P(){} P(int x,int y) { a=x; b=y; }; void showdata() { cout<<"\na is "<<a<<"\n"; cout<<"\nb is "<<b<<"\n"; } friend P operator +(P o1,P o2); }; P operator +(P o1,P o2) { P o3; o3.a=o1.a+o2.a; o3.b=o1.b+o2.b; return o3; } void main() { P x1(10,15); P x2(20,30); x1.showdata(); x2.showdata(); P o3; o3=x1+x2; o3.showdata(); o3.showdata(); getch(); }

Output:
a is 10 b is 15 a is 20 b is 30 a is 30 b is 45 a is 30 b is 45

// write a program using Formatted Functions.


#include<iostream.h> #include<conio.h> void main() { int a; float b; clrscr(); cout<<"Enter value for a and b"; cin>>a>>b; cout.width(10); cout.fill('*'); cout<<a; cout.precision(2); cout<<"\n"<<b; getch(); }

Output:
Enter value for a and b10 13.568 ********10 13.57

//write a program for Unformatted Functions.


#include<iostream.h> #include<conio.h> void main() { int count=0; char c; cout<<"enter input"; cin.get(c); while(c!='\n') { cout.put(c); count=count+1; cin.get(c); } cout<<"\ntotal no of character "<<count; getch(); }

Output:
enter input Sandeep singh Sandeep singh total no of character 13

//write a program for opening a file using Constructor.


#include<conio.h> #include<iostream.h> #include<fstream.h> void main() { ofstream outf("names"); char name[30]; clrscr(); cout<<"enter name "; cin>>name; outf<<name; outf.close(); ifstream inf("names"); inf>>name; cout<<"name is "<<name; inf.close(); getch(); }

Output:
enter name Sandeep name is Sandeep

//write a program for opening a file using open() method


#include<conio.h> #include<iostream.h> #include<fstream.h> void main() { ofstream outf; clrscr(); outf.open("name"); outf<<"aa"; outf<<"bb"; outf<<"cc"; outf.close(); outf.open("fname"); outf<<"XYZ"; outf<<"RST"; outf.close(); const int n=50; char line[n]; ifstream inf; inf.open("name"); cout<<"name list is \n"; while(inf) { inf.getline(line,n); cout<<line; } inf.close(); inf.open("fname"); cout<<"list of fname \n"; while(inf) { inf.getline(line,n); cout<<line; } inf.close(); getch(); }

Output:
name list is aabbcc

//write a program using setf() function.


#include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { int n; clrscr(); cout<<"Enter the number "; cin>>n; cout.setf(ios::hex,ios::basefield); cout<<"\n"<<n; cout.setf(ios::internal,ios::adjustfield); cout<<"\n"<<n; cout.setf(ios::oct,ios::basefield); cout<<"\n"<<n; getch(); }

Output:
Enter the number 10 a a 12

//write a program using read(),write() function in file handling


#include<fstream.h> #include<iostream.h> #include<string.h> #include<conio.h> void main() { int marks[5]={10,20,30,40,50}; clrscr(); ofstream ofile("xyz"); ofile.write((char *)&marks,sizeof(marks)); ofile.close(); for(int i=0;i<5;i++) marks[i]=0; ifstream ifile; ifile.open("xyz"); ifile.read((char *)&marks,sizeof(marks)); cout<<"\nmarks are "; for(i=0;i<5;i++) { cout<<marks[i]<<"\t"; } ifile.close(); getch(); }

Output;
marks are 10 20 30 40 50.

//write a program for Sequential Access of afile.


#include<fstream.h> #include<iostream.h> #include<conio.h> #include<string.h> void main() { char string[50]; clrscr(); cout<<"enter the string "; cin>>string; int len=strlen(string); fstream file; file.open("abc",ios::in|ios::out); int i; for(i=0;i<len;i++) file.put(string[i]); file.seekg(0); char ch; while(file) { file.get(ch); cout<<ch; } file.close(); getch(); }

Output:
enter the string sandeep sandeep

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