c++ programs

70
Q:- Wap to input a number and find whether No. is Prime Number or Not. #include<iostream.h> #include<conio.h> #include<stdlib.h> void main() { int i=2,n; clrscr(); cout<<"Enter number : "; cin>>n; while(i<n) { if(n%i == 0) { cout<<"No.Is not Prime"; getch(); exit(0); } i++; if(i == n || n == 1) cout<<"No.Is Prime"; } getch(); } Q:- Wap to input No. in an Array and Replace value in certain place. #include<iostream.h> #include<conio.h> void main() { int n[5],i,a,b,e; clrscr(); cout<<"Enter Value : "; for(i=0;i<5;i++) { cin>>n[i]; } cout<<"\nEnter Position To Be Replaced : "; cin>>e; 1

Upload: vikash-sharma

Post on 10-Nov-2014

15 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: C++ Programs

Q:- Wap to input a number and find whether No. is Prime Number or Not. #include<iostream.h>#include<conio.h>#include<stdlib.h>void main(){int i=2,n;clrscr();

cout<<"Enter number : ";cin>>n;while(i<n){if(n%i == 0){

cout<<"No.Is not Prime";getch();exit(0);

}i++;if(i == n || n == 1)cout<<"No.Is Prime";}

getch();}

Q:- Wap to input No. in an Array and Replace value in certain place.

#include<iostream.h>#include<conio.h>void main(){int n[5],i,a,b,e;clrscr();

cout<<"Enter Value : ";for(i=0;i<5;i++){

cin>>n[i];}cout<<"\nEnter Position To Be Replaced : ";cin>>e;cout<<"\nEnter Value To Be Replaced : ";cin>>a;for(i=0;i<5;i++){if((e-1) == i){ n[i] = a;}

1

Page 2: C++ Programs

}for(i=0;i<5;i++){ cout<<n[i]<<endl;}

getch();}

Q:- Wap to Sum the Series (1 - 1/!1 + 2/!2 - 3/!3 + 4/!4 - …….n/!n ).

#include<iostream.h>#include<conio.h>void main(){

float s=1,j,n,f;int i;

clrscr();cout<<"Enter the number [n!] -:- ";cin>>n;for(i=0;i<n;i++){f=1;

for(j=i;j>=1;j--){

f=f*j;}

if(i%2==0){

s=s+i/f;}else{

s=s-i/f;}

}cout<<"Series (1-1/!1+2/!2-3/!3+4/!4) sum is => "<<s;

getch(); }

Q Wap to Multiple two, Two D-Array and display the result in one two D-Array.

#include <iostream.h>#include <conio.h>void main(){int n[2][2],n1[2][2],m[2][2],a,b,c,t;clrscr();cout<<"Enter First Martix : ";

2

Page 3: C++ Programs

for(a=0;a<2;a++){

for(b=0;b<2;b++){

cin>>n[a][b];}

}cout<<"Enter Second Martix : ";

for(a=0;a<2;a++){

for(b=0;b<2;b++){

cin>>n1[a][b];}

}

cout<<"Matrix Is : \n";for(a=0;a<2;a++){

for(b=0;b<2;b++){

m[a][b] = 0;for(c=0;c<2;c++) m[a][b] = m[a][b] + n[a][c] * n1[c][b];

}}

for(a=0;a<2;a++){

for(b=0;b<2;b++){

cout<<" "<<m[a][b];}

cout<<"\n";}

getch();}

Q:- Wap to input no. in one D-Array and Search the position of the number.

#include<iostream.h>#include<conio.h>#include<stdio.h>void main(){int n[100],a,i,t,x,pos,mid,first,last;clrscr();

3

Page 4: C++ Programs

cout<<"Enter No. Upto Which Term : ";cin>>a;cout<<"Enter Numbers : ";for(i=0;i<a;i++){

cin>>n[i];first = 0;pos = -1;last = a-1;

}cout<<"Search Element : ";cin>>x;while((first <= last) && (pos == -1)){

mid = (first+last)/2;if(n[mid] == x)

pos = mid;elseif(n[mid] < x)

first = mid+1;else

last = mid-1; }if(pos > -1)

cout<<"The Position : "<<++pos;else

cout<<"Unsuccessful";

getch();}

Q:- Wap to input Student information and Sort them in Ascending order in marks basics. #include<iostream.h>#include<conio.h>#include<stdio.h>#include<string.h>void main(){char na[5][20],temp[20];int ro[5],marks[5],i,j,lar,pos,t,rn;clrscr();

for(i=0;i<5;i++){

cout<<"Enter Name Roll No. & Marks : ";cin>>na[i]>>ro[i]>>marks[i];

}cout<<"Name\tRoll No.\tMarks\n";for(i=0;i<5;i++)

4

Page 5: C++ Programs

{cout<<na[i]<<"\t"<<ro[i]<<"\t\t"<<marks[i]<<"\n";

}

for(i=0;i<4;i++){

lar = marks[i];pos = i;for(j=i+1;j<5;j++){

if(lar<marks[j]){

lar= marks[i];pos = j;

}}t = marks[i];marks[i] = marks[pos];marks[pos] = t;

strcpy(temp,na[i]);strcpy(na[i],na[pos]);strcpy(na[pos],temp);

rn = ro[i];ro[i] = ro[pos];ro[pos] = rn;

} cout<<"Name\tRoll No.\tMarks\n";

for(i=0;i<5;i++){

cout<<na[i]<<"\t"<<ro[i]<<"\t\t"<<marks[i]<<"\n";}

getch();}

#define o cin#include <iostream.h>#include <conio.h>#include <dos.h>#define c coutvoid main(){float n=12.2354;int a;clrscr();

a = int(n);o>>a;

5

Page 6: C++ Programs

c<<"Value is : "<<n;c<<"\nValue is : "<<a;

getch();}

Q:- Wap to input marks in an array and pass them in a function.

#include<iostream.h>#include<conio.h>#include<stdio.h>void dis(int marks[]);void main(){int i,marks[4];clrscr();

cout<<"Enter Value : ";for(i=0;i<4;i++){

cin>>marks[i];}

dis(marks);getch();}

void dis(int m[]){int j;

for(j=0;j<4;j++){

cout<<m[j]<<endl;}

}

Q:- Wap to input number of different vehicle sold in year 2000, 2001, 2003 to display the selling percentage using Class. #include<iostream.h>#include<conio.h>#include<stdio.h>class Vehicle{private:

float y,y1,y2,ts,tr,tc,sp,sp1,sp2;public:

void one(){

ts = 400;tr = 500;

6

Page 7: C++ Programs

tc = 100;}void two(){

cout<<"Enter no. of splender sold in 2000,2001 & 2002 : ";cin>>y>>y1>>y2;

sp = y * .4;sp1 = y1 * .5;sp2 = y2 * .1;

}void three(){

cout<<"Enter no. of rickshaw sold in 2000,2001 & 2002 : ";cin>>y>>y1>>y2;

sp = y * .4;sp1 = y1 * .5;sp2 = y2 * .1;

}void four(){

cout<<"Enter no. of scorpio sold in 2000,2001 & 2002 : ";cin>>y>>y1>>y2;

sp = y * .1;sp1 = y1 * .1;sp2 = y2 * .1;

}void put(){

cout<<"Selling percentage of splender in 2000,2001 & 2002 : "<<sp<<"\t"<<sp1<<"\t"<<sp2;

}void print(){

cout<<"\nSelling percentage of rickshaw in 2000,2001 & 2002 : "<<sp<<"\t"<<sp1<<"\t"<<sp2;

}void show(){

cout<<"\nSelling percentage of scorpio in 2000,2001 & 2002 : "<<sp<<"\t"<<sp1<<"\t"<<sp2;

}};void main(){clrscr();

Vehicle ob,ob1,ob2,ob3;ob.one();ob1.two();ob2.three();

7

Page 8: C++ Programs

ob3.four();ob1.put();ob2.print();ob3.show();

getch();}

Q:- Wap to input number of student Name, Roll and Marks using Class Array. #include<iostream.h>#include<conio.h>#include<stdio.h>class A{private:

char na[20];int ro,e,m;

public:void getdata();void putdata();

};void A :: getdata()

{cout<<"Enter Student Name : ";gets(na);cout<<"Enter Roll no.Marks Of Maths & English : ";cin>>ro>>m>>e;

}void A :: putdata()

{cout<<"\nStudent Name : "<<na<<"\nRoll No. : "<<ro<<"\nMaths : "<<m<<"\nEnglish : "<<e;

}void main(){int i;clrscr();

A ob[3];for(i=0;i<3;i++){

ob[i]. getdata();}for(i=0;i<3;i++){

ob[i]. putdata();}

getch();}

8

Page 9: C++ Programs

Q:- Wap to input number of student Name, Roll using Class Array Pointer up to N term.

#include<iostream.h>#include<conio.h>class student{

char name[20];int roll;

public:void getdata(){

cout<<"\nEnter Name : ";cin>>name;cout<<"Enter Roll No : ";cin>>roll;

}void showdata(){

cout<<"\nName : "<<name<<"\nRoll No : "<<roll<<endl;}

};

void main(){int i,q;clrscr();cout<<"Enter How Many Time : ";cin>>q;

student *s[100];for(i=0;i<q;i++){

s[i] = new student;s[i]->getdata();

}for(i=0;i<q;i++){

s[i]->showdata();}getch();

}

Q:- Wap to input two number and sum them using pointer object.

9

Page 10: C++ Programs

#include<iostream.h>#include<conio.h>class PRA{

private:int a,b,s;

public:void getdata(){

cout<<"Enter Numbers : ";cin>>a>>b;s = a + b;

}void putdata(){

cout<<"\nSum of numbers : "<<s;}

};

void main(){int i;clrscr();

PRA *ptr;for(i=0;i<3;i++){

(ptr+i)->getdata();}for(i=0;i<3;i++){

(ptr+i)->putdata();}

getch();}

Q:- Wap to input Name of Item and Price calculate them using object pointer, while Ans is ‘Y’.

#include<iostream.h>#include<conio.h>#include<string.h>class pal{

char na[20];int p;

public:void get(void)

10

Page 11: C++ Programs

{cout<<"Enter Item Name : ";cin>>na;cout<<"Enter Price : ";cin>>p;

}void put(void){

cout<<"\nItem Name : "<<na<<"\n"<<"Price : "<<p<<endl;}

};

void main(){clrscr();

pal * p[100];int n = 0;char ch;int i;do{

p[n] = new pal;p[n]->get();n++;cout<<"Enter Another (y/n) : ";cin>>ch;

}while(ch == 'y');for(i=0;i<n;i++){

cout<<"\nItem No "<<i+1;p[i]->put();

}getch();}

Q:- Wap to input Names of Student using object pointer, while Ans is ‘Y’ and Display them.

#include <iostream.h>#include <conio.h>#include <string.h>class per{

char na[20];public :

void setname(void){

11

Page 12: C++ Programs

cout<<"Enter Name : ";cin>>na;

}void prname(void){

cout<<"\nName is : "<<na;}

};void main(){clrscr();

per* ptr[100];int n=0;char ch;do{

ptr[n] = new per;ptr[n]->setname();n++;cout<<"Enter Another (y/n) :";cin>>ch;

}while(ch=='y');for(int j=0;j<n;j++){

cout<<"\nPerson Number "<<j+1;ptr[j]->prname();

}getch();}

Q:- Wap to input a number and display them using Constructor. #include<iostream.h>#include<conio.h>class int1{

private:int i;

public:void getdata(){

cout<<"Enter Value : ";cin>>i;

}

void setdata(int j){

12

Page 13: C++ Programs

i = j;}int1(){

//no argument//}

int1(int j) //one argument//{

i = j;}

void disp(){

cout<<i<<endl;}

};void main(){clrscr();int1 i1(100),i2,i3;

i1.disp();i2.setdata(200);i2.disp();i3.getdata();i3.disp();

}

Q:- Wap to input a size of list of the numbers and calculate them using Constructor.

#include<iostream.h>#include<conio.h>class sum{private:

int count,total,n,val;public:

sum(){

count = 0;total = 0;

}int compute_sum();disp(){

cout<<"Sum : "<<total;}

};

13

Page 14: C++ Programs

int sum :: compute_sum(){

cout<<"Enter the size of the list : ";cin>>n;while(count<n){

count++;cout<<"Enter the value : ";cin>>val;total = total + val;

}return(total);

}void main(){clrscr();

sum ob;sum(ob);ob.compute_sum();ob.disp();

getch();}

Q:- Wap to input Temperature in centigrade to convert in Fahrenheit and Fahrenheit to convert in centigrade using Constructor.

#include<iostream.h>#include<conio.h>class tempconvt{

float c,f;public :

float ctof(float i);float ftoc(float j);

};

float tempconvt :: ctof(float i){

f = 1.8 * i + 32;return f;

}float tempconvt :: ftoc(float j){

c = (j - 32) / 1.8;return c;

}void main()

14

Page 15: C++ Programs

{tempconvt ctemp;

float val,res;clrscr();

cout<<"\nEnter the value in degree centigrade : ";cin>>val;res = ctemp.ctof(val);cout<<"\nthe equivalent value in Fahrenheit is = "<<res;cout<<"\nEnter the value in Fahrenheit : ";cin>>val;res = ctemp.ftoc(val);cout<<"\nthe equivalent value in centigrade is = "<<res;

getch();}

Q:- Wap to input Principle, Time and Rate to find Simple Interest using Constructor. #include<iostream.h>#include<conio.h>class A{

private :float c;

public :A(int i,int j,float z){

cout<<"Principle : "<<i;cout<<"\nTime : "<<j;cout<<"\nRate : "<<z;c = (i*j*z)/100;

}

void disp(){

cout<<"\nSimple intrest : "<<c;}

};void main(){clrscr();

A i1(1000,4,12.5);i1.disp();

getch();}

Q:- Wap to input Name of Item, Price and Qty to find discount, using constructor.

15

Page 16: C++ Programs

#include<iostream.h>#include<conio.h>class sale{

private:int a,dis,ta;

public:sale(char na[20],int p,int q){

cout<<"Item name : "<<na;cout<<"\nPrice : "<<p;cout<<"\nQuantity : "<<q;a = p * q;if(a >= 1000) {

cout<<"\ndis : 10%";dis = a *.10;

}elseif(a >= 500){

cout<<"\ndis : 5%";dis = a * .05;

}elseif(a >=200){

cout<<"\ndis : 2%";dis = a * .02;

}else

cout<<"\nno discount";}void disp(){

cout<<"\nAmount : \t"<<a;cout<<"\nLess : \t"<<dis;cout<<"\n\t ------";cout<<"\nTotal Amount : "<<a-dis;

}};

void main(){

char ba[20];clrscr();

cout<<"Enter Name : "; cin>>ba;

sale p1(ba,50,25);

16

Page 17: C++ Programs

p1.disp();getch();}

Q:- Wap to Define Principle amount, Rate and Time to find Simple Intrest using Constructor.

#include<iostream.h>#include<conio.h>#include<math.h>class compound{

float a,co;public:

compound();void show();

};compound(float p,float r,float t){

cout<<"Principle Amount : "<<p;cout<<"\nRate Of Interest : "<<r;cout<<"\nTime Period : "<<t;a = p * pow((1 + r/100),t);co = a - p;

}void compound :: show(){

cout<<"\nCompound Interest : "<<co;}void main(){clrscr();

compound ob(1000,10,2);ob.show();getch();

}

Q:- Wap to input Principle amount, Rate and Time to find Simple Intrest using Constructor.

#include<iostream.h>#include<conio.h>#include<math.h>class compound{

float a,co;public:

17

Page 18: C++ Programs

compound(float p,float r,float t){

cout<<"Principle Amount : "<<p;cout<<"\nRate Of Interest : "<<r;cout<<"\nTime Period : "<<t;a = p * pow((1 + r/100),t);co = a - p;

}void show(){

cout<<"\nCompound Interest : "<<co;}};void main(){

float p1,r2,t2;clrscr();

cout<<"Enter P,R,T :";cin>>p1>>r2>>t2;

compound ob(p1,r2,t2);ob.show();getch();

}

Q:- Wap to input Student Name, Maths Marks, Eng Marks to find Total Marks, Avg, Division, using Constructor. #include<iostream.h>#include<string.h>#include<conio.h>#include<stdio.h>class std{

private:float tot[2],avg[2];char div[2][20];

public:std(char na[][20],int m[],int e[]){int i;for(i=0;i<2;i++){

cout<<"\nStudent Name : "<<na[i];cout<<"\nMarks of maths : "<<m[i];cout<<"\nMarks of eng : "<<e[i];tot[i] = m[i] + e[i];avg[i] = tot[i]/2;if(avg[i] >= 60)

18

Page 19: C++ Programs

{ strcpy(div[i],"First");

}elseif(avg[i] >= 45 && avg[i] <= 60){

strcpy(div[i],"Second");}elseif(avg[i] >= 30){

strcpy(div[i],"Third");}else

strcpy(div[i],"Fail");}

}void disp(){

int i;for(i=0;i<2;i++){

cout<<"\nTotal : "<<tot[i];cout<<"\nAverage : "<<avg[i];cout<<"\nDivison : "<<div[i];

}}

};void main(){

char ma[2][20];int p[2],c[2],i;clrscr();

for(i=0;i<2;i++){

cout<<"Enter Name : ";gets(ma[i]);cout<<"Marks : ";cin>>p[i]>>c[i];

}

std m1(ma,p,c);m1.disp();

getch();}

Q:- Wap to input the size of Matrix and input value to display Matrix using Class.

19

Page 20: C++ Programs

#include<iostream.h>#include<conio.h>class mat{

int i,j;int matrix[10][10];

public :void readmat(int,int);void printmat(int,int);

};void mat :: readmat(int m,int n){

for(i=0;i<m;i++)for(j=0;j<n;j++)cin>>matrix[i][j];

}void mat :: printmat(int m,int n){

cout<<"\n The matrix is\n";for(i=0;i<m;i++){

cout<<"\n";for(j=0;j<n;j++)

cout<<matrix[i][j]<<" ";}

}void main(){int m,n;clrscr();

mat ob;cout<<"\nEnter the size of the matrix : m,n : ";cin>>m>>n;

ob.readmat(m,n);ob.printmat(m,n);

getch();}

Q:- Wap to input Number in a Matrix and Sum the Rows Value of the Matrix using Class.

#include<iostream.h>#include<conio.h>class mat{

int matrix[3][3],r,c,t,s[3];

public:void readmat();void printmat();

20

Page 21: C++ Programs

void summat();};void mat :: readmat(){

cout<<"Enter Matrix : \n";for(r=0;r<3;r++){

for(c=0;c<3;c++){

cin>>matrix[r][c];}

}}void mat :: printmat(){

cout<<"Matrix Is : \n";for(r=0;r<3;r++){t = 0;

for(c=0;c<3;c++){

t = t + matrix[r][c];cout<<matrix[r][c]<<" ";

}cout<<" = "<<t<<"\n";

}}void mat :: summat(){

for(r=0;r<3;r++){t = 0;

for(c=0;c<3;c++){

t = t + matrix[c][r];}s[r] = t;cout<<s[r]<<" ";

}}void main(){clrscr();

mat ob;ob.readmat();ob.printmat();ob.summat();

getch();}

21

Page 22: C++ Programs

Q:- Wap to input two value and find the Average using Friend Function.

#include<iostream.h>#include<conio.h>class ABC{

int x;int y;

public:void getvalue(void){

cout<<"Enter the value of x & y : ";cin>>x>>y;

}friend float avg(ABC A);

};

float avg(ABC A){

return (A.x + A.y)/2;}

void main(){clrscr();

ABC ob1;ob1.getvalue();float av;av = avg(ob1);cout<<"AVERAGE : "<<av;

}

Q:- Wap to input two value and sum them using Friend Function.

#include<iostream.h>#include<conio.h>class PRA{

int x;int y;

public:void g etvalue(void){

cout<<"Enter the value of x & y : ";cin>>x>>y;

}friend int add(PRA P)

22

Page 23: C++ Programs

};int add(PRA P){int z;

z = P.x + P.y;return(z);

}void main(){clrscr();

PRA ob1;ob1.getvalue();int ad;ad = add(ob1);cout<<"Addition : "<<ad;

}

Q:- Wap to input Principle Amount, Rate, Time to find the Compound Interest using Friend Function.

#include<iostream.h>#include<conio.h>#include<math.h>class compound{

float p,r,t;public:

void data(){

cout<<"Enter Principle Amount : ";cin>>p;cout<<"Enter Rate Of Interest : ";cin>>r;cout<<"Enter Time Period : ";cin>>t;

}friend float cal(compound c);

};

float cal(compound c){float a,co;

a = c.p * pow((1 + c.r/100),c.t);co = a - c.p;return co;

}

void main(){

23

Page 24: C++ Programs

clrscr();compound ob;

ob.data();float ca;ca = cal(ob);cout<<"Compound Interest : "<<ca;

getch();}

Q:- Wap to input Length in Meter, Feet, Inches to Display either in Meter and Feet or in Feet and Inches using Friend Function.

include <iostream.h>#include <conio.h>class met;class feet{private :

int fe,in;public :

void get(){

cout<<"Enter Feet & Inches : ";cin>>fe>>in;

}friend int fir(feet,met);};

class met{private :

int me,fet;public :

void getdata(){

cout<<"Enter Meter & Feet : ";cin>>me>>fet;

}

friend int fir(feet,met);};

int fir(feet a,met d){ char f;

24

Page 25: C++ Programs

cout<<"Enter m to Convert in Meter f to convert in Feet :";cin>>f;

if(f=='m'){

return(a.fe + a.in + d.me + d.fet);}else{

return(a.fe * a.in * d.me * d.fet);}

}

void main(){

clrscr();feet ob;met ob1;

ob.get();ob1.getdata();

int res = fri(ob,ob1);cout <<"Value is : "<<res;

getch();}

Q:- Wap to input Employee Name and Salary to find Ta, Tax and Gsal using Friend Function.

#include<iostream.h>#include<conio.h>#include<stdio.h>class PRA{

char na[20];long ph;int sa;

public:void getdata(void){

cout<<"Enter Employee Name : ";gets(na);cout<<"Enter Phone no. & Salary : ";cin>>ph>>sa;

}

friend float cal(PRA P)friend char na(PRA A)

void disp()

25

Page 26: C++ Programs

{cout<<"\nPhone No. : "<<ph;cout<<"\nSalary : "<<sa;

}};float cal(PRA P){float ta,tax,gs;

ta = P.sa * .08;tax = P.sa * .10;cout<<"\nTa : "<<ta;cout<<"\nTax : "<<tax;gs = (P.sa + ta) - tax;return (gs);

}char na(PRA A){

cout<<"\nName is : " <<A.na;return 0;

}void main(){clrscr();

PRA ob1;ob1.getdata();na(ob1);ob1.disp();float ca;ca = cal(ob1);cout<<"\nGross Salary : "<<ca;

}

Q:- Wap to display a Message using simple Inheritence.

#include<iostream.h>#include<conio.h>class A{public:

void as(char *msg){

cout<<msg<<endl;}

};

class B : public A{public:

26

Page 27: C++ Programs

void ad(char *msg){

cout<<msg<<endl;}

};

void main(){clrscr();

B ob;ob.as("HELLO");ob.ad("HOW R U");

getch();}

Q:- Wap to input Item Name, Price and Qty to find Tax Using Simple Inheritance.

#include<iostream.h>#include<conio.h>class A{protected:

char na[20];float p,q;

public:void get();void put();

};void A :: get(){

cout<<"Enter item name : ";cin>>na;cout<<"Enter price : ";cin>>p;cout<<"Enter quantity : ";cin>>q;

}void A :: put(){

cout<<"\nItem Name : "<<na<<endl;cout<<"Price : "<<p<<endl;cout<<"Quantity : "<<q<<endl;

}

class B : public A{

27

Page 28: C++ Programs

protected:float tp,tax,ta;

public:void cal();void show();

};void B :: cal(){

tp = p * q;if(tp >= 1000){

tax = tp * .10;}elseif(tp >= 500 && tp <1000){

tax = tp * .05;}elseif(tp < 500){

tax = tp * .02;}ta = tp - tax;

}void B :: show(){

put();cout<<"Total Price : "<<tp<<endl;cout<<"Tax : "<<tax<<endl<<"Net Amount : "<<ta<<endl;

}void main(){int i;clrscr();

B ob[3];for(i=0;i<3;i++){

ob[i].get();ob[i].cal();

}for(i=0;i<3;i++){

ob[i].show();}

getch();}

Q:- Wap to input Book Name, Price and Qty to find Discount and Net Price Using

28

Page 29: C++ Programs

Multilevel Inheritance.

#include<iostream.h>#include<conio.h>#include<stdio.h>class A{protected:

char na[20];public:

void getname();void putname();

};void A :: getname(){

cout<<"Enter the name of book : ";gets(na);

}void A :: putname(){

cout<<"\nName Of Book : "<<na<<endl;}

class B : public A{protected:

float p,q;public:

void getprice();void putprice();

};void B :: getprice(){

cout<<"Enter the price : ";cin>>p;cout<<"Enter the quantity : ";cin>>q;

}void B :: putprice(){

cout<<"Price : "<<p<<endl;cout<<"Quantity : "<<q<<endl;

}

class C : public B{

29

Page 30: C++ Programs

private:float tot,dis,tp;

public:void disp();

};void C :: disp(){

tot = p * q;dis = tot * .07;tp = tot - dis;putname();putprice();cout<<"Total Price : "<<tot<<endl;cout<<"Discount : "<<dis<<endl;cout<<"Net Price : "<<tp<<endl;

}

void main(){clrscr();

C ob;ob.getname();ob.getprice();ob.disp();

getch();};

Q:- Wap to input three number in different class and add them in fourth class using Multiple Inheritance.

#include<iostream.h>#include<conio.h>class A{protected:

int a;public:

void get();void put();

};void A :: get(){

cout<<"Enter First Number : ";cin>>a;

}

void A :: put(){

30

Page 31: C++ Programs

cout<<"First Number : "<<a<<endl;}

class B{protected:

int b;public:

void enter();void show();

};void B :: enter(){

cout<<"Enter Second Number : ";cin>>b;

}void B :: show(){

cout<<"Second Number : "<<b<<endl;}

class C{protected:

int c;public:

void cal();void disp();

};void C :: cal(){

cout<<"Enter Third Number : ";cin>>c;

}void C :: disp(){

cout<<"Third Number : "<<c<<endl;}

class D : public A,public B,public C{

int d;public:

void all();};void D :: all(){

put();show();

31

Page 32: C++ Programs

disp();d = (a + b) - c;cout<<"Value : "<<d<<endl;

}

void main(){clrscr();

D ob;ob.get();ob.enter();ob.cal();ob.all();

getch();}

Q:- Wap to input Price of LG Products, Sony Item, Onida Item and Calculate them using .

#include<iostream.h>#include<conio.h>class tele{protected:

int t;public:

void get(int);void put(void);

};void tele :: get(int x){

t = x;}void tele :: put(){

cout<<"\nPrice of Television LG : "<<t<<endl;}

class iron{protected:

int i;public:

void get1(int);void put1(void);

};

void iron :: get1(int y){

i = y;

32

Page 33: C++ Programs

}void iron :: put1(){

cout<<"Price of iron LG : "<<i<<endl;}

class frz{protected:

int f;public:

void get2(int);void put2(void);

};void frz :: get2(int z){

f = z;}void frz :: put2(){

cout<<"Price of freeze LG : "<<f<<endl;}

class lg : public tele,public iron,public frz{public:

void put3(void);};

void lg :: put3(){

put();put1();put2();cout<<"Total price of lg items : "<<t+i+f<<endl;

}

class sony{protected:

int s;public:

void in(int);void out(void);

};void sony :: in(int a){

s = a;}void sony :: out(){

cout<<"Price of sony items : "<<s<<endl;

33

Page 34: C++ Programs

}

class onida{protected:

int o;public:

void enter(int);void show(void);

};void onida :: enter(int b){

o = b;}void onida :: show(){

cout<<"Price of onida items : "<<o<<endl;}

class item : public sony,public lg,public onida{public:

void disp(void);};

void item :: disp(){

put3();out();show();cout<<"\nTotal Price Of All Items : "<<t+i+f+s+o<<endl;

}

void main(){int a,b,c,d,e;clrscr();

item ob;cout<<"Enter the price of tele : ";cin>>a;

ob.get(a);cout<<"Enter the price of iron : ";cin>>b;

ob.get1(b);cout<<"Enter the price of freeze : ";cin>>c;

ob.get2(c);cout<<"Enter the price of sony : ";cin>>d;

ob.in(d);

34

Page 35: C++ Programs

cout<<"Enter the price of onida : ";cin>>e;

ob.enter(e);ob.disp();

getch();}

Q:- Wap to Display Statement using Virtual Functions accessed from Pointer.

#include <iostream.h>#include <conio.h>class base{public :

virtual void show() = 0;};class derv1 : public base{public :

void show(){

cout<<"\nDerv1";}

};class derv2 : public base{public :

void show(){

cout<<"\nDerv2";}

};void main(){clrscr();// base *list[2];

derv1 dv1;derv2 dv2;base *list[2];list[0] = &dv1;list[1] = &dv2;list[0]->show();list[1]->show();

getch(); }Q:- Wap to input Numbers in different class and find values using Virtual Function accessed form Pointer.

#include<iostream.h>

35

Page 36: C++ Programs

#include<conio.h>#include<math.h>class base{public:

virtual void cal() = 0;virtual void disp() = 0;

};

class square : public base{protected:

float a,sq;public:

void cal(){

cout<<"Enter Number : ";cin>>a;

}void disp(){

sq = sqrt(a);cout<<"Square Root : "<<sq<<endl;

}};class power : public base{protected:

float x,y;public:

void cal(){

cout<<"\nEnter Number : ";cin>>x;

}void disp(){

y = pow(x,3);cout<<"Power : "<<y;

}};void main(){clrscr();

square s;power p;base *b[2];

b[0] = &s;b[1] = &p;

36

Page 37: C++ Programs

b[0]->cal();b[0]->disp();b[1]->cal();b[1]->disp();

getch();}

Q:- Wap to input Numbers in different class and find values using Virtual Function accessed form Pointer Array.

#include<iostream.h>#include<conio.h>#include<math.h>class base{public:

virtual void cal() = 0;virtual void disp() = 0;

};

class square : public base{protected:

float a,sq;public:

void cal(){

cout<<"Enter Number For Square Root : ";cin>>a;

}void disp(){

sq = sqrt(a);cout<<"\nSquare Root : "<<sq<<endl;

}};class power : public base{protected:

float x,y;public:

void cal(){

cout<<"Enter Number For Power : ";cin>>x;

}void disp()

37

Page 38: C++ Programs

{y = pow(x,3);cout<<"Power : "<<y;

}};void main(){int i;clrscr();

square s;power p;base *b[4];

b[0] = &s;b[1] = &p;b[2] = &s;b[3] = &p;for(i=0;i<4;i++){

b[i]->cal();}for(i=0;i<4;i++){

b[i]->disp();}

getch();}

Q:- Wap to find Area of Triangle and Perimeter of Triangle using Multiple Inheritance with Object Pointer. #include<iostream.h>#include<conio.h>class base{public:

virtual void cal(){

cout<<"HELLO";}virtual void show(){

cout<<"MY SWEET FRIEND";}

};

class area : public base{protected:

float w,h,t,a;

38

Page 39: C++ Programs

public:void cal(){

cout<<"Enter sides of triangle : ";cin>>w>>h>>t;

}void show(){

a = (w + h + t)/2;cout<<"Area Of Triangle : "<<a<<endl;

}};

class peri : public base{protected:

float a,b,p;public:

void cal(){

cout<<"\nEnter sides of triangle : ";cin>>a>>b;

}void show(){

p = .5 * (a + b);cout<<"Perimeter Of Triangle : "<<p<<endl;

}};

void main(){clrscr();

area r1;peri e1;base *s;

s = &r1;s->cal();s->show();s = &e1;s->cal();s->show();

getch();}

Q:- Wap to Display Message using Virtual Function accessed from Pointer.

#include <iostream.h>

39

Page 40: C++ Programs

#include <conio.h>class base{public :virtual void show()

{cout<<"\nBase : ";

}};

class derv1 : public base{public :

void show(){

cout<<"\nDerv1";}

};

class derv2 : public base{public :

void show(){

cout<<"\nDerv2";}

};

void main(){clrscr();

derv1 dv1;derv2 dv2;base *ptr;ptr = &dv1;ptr->show();ptr = &dv2;ptr->show();

getch();}

Q:- Wap to input Roll No., Maths, Eng Marks find Total Marks using Virtual Class.

#include<iostream.h>#include<conio.h>

40

Page 41: C++ Programs

class stud{protected:

int roll;public:

void get_roll(int);void put_roll(void);

};void stud :: get_roll(int r){

roll = r;}void stud :: put_roll(){

cout<<"\nRoll Number : "<<roll;}

class test : public virtual stud{protected:

int math,eng;public:

void get_test(int,int);void put_test(void);

};void test :: get_test(int m,int e){

math = m;eng = e;

}void test :: put_test(){

cout<<"\nMarks of Math : "<<math;cout<<"\nMarks of Eng : "<<eng;cout<<"\nRool : "<<roll;

}

class spo : public virtual stud{protected:

int sco;public:

void get_sco(int);void put_sco(void);

};void spo :: get_sco(int s){

sco = s;}

41

Page 42: C++ Programs

void spo :: put_sco(){

cout<<"\nSports Score : "<<sco;cout<<"\nStud Roll : "<<roll;

}

class result : public test,public spo{

int tot;public:

void disp(void);};void result :: disp(){

tot = math + eng + sco;put_roll();put_test();put_sco();cout<<"\nTotal Score : "<<tot;

}

void main(){

int ro,en,ma,so;clrscr();result ob;cout<<"Enter Roll No. : ";cin>>ro;

ob.get_roll(ro);cout<<"Enter Marks Math : ";cin>> ma;cout<<"Enter Marks of Eng : ";cin>>en;

ob.get_test(ma,en);cout<<"Enter Marks of Soprts : ";cin>>so;

ob.get_sco(so);ob.disp();getch();}

Q:- Wap to input Side of Triangle to find Area and perimeter using Virtual Class.

#include<iostream.h>#include<conio.h>

42

Page 43: C++ Programs

class angle{protected:

float w,h,t;public:

void get();void put();

};void angle :: get(){

cout<<"Enter sides of triangle : ";cin>>w>>h>>t;

}void angle :: put(){

cout<<"\nSides Of Triangle : "<<w<<"\n"<<h<<"\n"<<t<<endl;}

class area : public virtual angle{protected:

float a;public:

void get1();void put1();

};void area :: get1(){

a = (w + h + t)/2;}void area :: put1(){

cout<<"Area Of Triangle : "<<a<<endl;}

class peri : public virtual angle{protected:

float p;public:

void get2();void put2();

};

void peri :: get2(){

p = .5 * (w + h);}

43

Page 44: C++ Programs

void peri :: put2(){

cout<<"Perimeter Of Triangle : "<<p<<endl;}

class cal : public area,public peri{public:

void disp();};

void cal :: disp(){

put();put1();put2();

}

void main(){clrscr();

cal ob;ob.get();ob.get1();ob.get2();ob.disp();

getch();}

Q:- Wap to input number in an array Template to sum the value and multiple them.

#include<iostream.h>#include<conio.h>class vector{

int *v;int size;

public:vector(int m){

v = new int[size = m];for(int i=0;i<size;i++){

v[i] = 0;}

}vector(int *a){

for(int i=0;i<size;i++){

44

Page 45: C++ Programs

v[i] = a[i];}

}int operator *(vector &y){

int sum = 0;for(int i=0;i<size;i++){

sum += this->v[i] * y.v[i];return sum;

}}

};void main(){clrscr();

int x[3] = {1,2,3};int y[3] = {4,5,6};vector v1(3);vector v2(3);v1 = x;v2 = y;int R = v1 * v2;cout<<"R = "<<R;

getch();}

Q:- Wap to display Name and Age of the person whose Age is Greater then other use this pointer.

#include <iostream.h>#include <conio.h>#include <string.h>class per{

char na[20];int age;

public :per(char *s,int a){

strcpy(na,s);age = a;

}per & per :: great(per & x){

if(x.age >= age)return x;elsereturn *this;

45

Page 46: C++ Programs

}void display(void){

cout<<"Name : "<<na<<"\n"<<"Age : "<<age<<"\n";}};void main(){clrscr();

per p1("Sur",21),p2("Baby",20),p3("Pagal",24);per p('\0',0);p = p1.great(p3);

cout<<"Elder Person is \n";p.display();p = p1.great(p2);

cout<<"Elder Person is \n";p.display();

}

Q:- Wap to display Name Roll and Marks of the Student on the marks basic use this pointer.

#include<iostream.h>#include<conio.h>#include<string.h>class pal{

char na[20];int roll;float marks;

public:pal(char *a,int r,float m){

strcpy(na,a);roll = r;marks = m;

}pal & pal :: great(pal & s){

if(s.marks >= marks)return s;elsereturn *this;

}void display(void){

cout<<"Name : "<<na<<"\n"<<"Roll No : "<<roll<<"\n"<<"Marks : "<<marks<<"\n";

}};

46

Page 47: C++ Programs

void main(){clrscr();

pal p1("sur",3,200),p2("pappu",2,250),p3("anu",1,275);pal p("amit",5,300);p = p1.great(p3);cout<<"Maximum Marks Obtained By \n";p.display();p = p1.great(p2);

cout<<"Maximum Marks Obtained By \n";p.display();

getch();}

Q:- Wap to display Name Roll and Marks of the Student on the marks basic use this pointer. By user input.

#include<iostream.h>#include<conio.h>#include<string.h>class pal{

char na[20];int roll;float marks;

public:pal(char *a,int r,float m){

strcpy(na,a);roll = r;marks = m;

}pal & pal :: great(pal & s){

if(s.marks >= marks)return s;elsereturn *this;

}void display(void){

cout<<"Name : "<<na<<"\n"<<"Roll No : "<<roll<<"\n"<<"Marks : "<<marks<<"\n";

}};

void main(){

47

Page 48: C++ Programs

char x[3][20];int a[3],i;float s[3];clrscr();

for(i=0;i<3;i++){

cout<<"Enter name : ";cin>>x[i];cout<<"Enter roll no : ";cin>>a[i];cout<<"Enter marks : ";cin>>s[i];

}

pal p1(x[0],a[0],s[0]),p2(x[1],a[1],s[1]),p3(x[2],a[2],s[2]);pal p("amit",10,400);p.display();p = p1.great(p3);

cout<<"Maximum Marks Obtained By \n";p.display();p = p1.great(p2);

cout<<"Maximum Marks Obtained By \n";p.display();getch();

}

Q:- Wap to input Name of Employee, Basic Salary, Working Hour to find Over Time Payment, using File Handling.

#include<iostream.h>#include<conio.h>#include<fstream.h>void main(){char na[20];float bs,wh,ot,op,ns;clrscr();

ofstream p("pummy");{

cout<<"Enter Employee Name : ";cin>>na;cout<<"Enter Basic salary : ";cin>>bs;cout<<"Enter Working Hour : ";cin>>wh;if(wh >= 10){

ot = wh - 10;op = ot * 100;

48

Page 49: C++ Programs

ns = bs + op;}else{

ns = bs;op = 0;ot = 0;

}p<<"NAME\tBS\t\WH\tOT\tOP\tNS\n";p<<na<<"\t"<<bs<<"\t"<<wh<<"\t"<<ot<<"\t"<<op<<"\t"<<ns<<endl;}ifstream pal;{

cout<<"\nOvertime : "<<ot;cout<<"\nOvertime Payment : "<<op;cout<<"\nNet Salary : "<<ns;

}getch();}

Q:- Wap to input Item Name,Price, Qty to find Total Price, Discount, Net Price. using File handling in Append Mode. #include<iostream.h>#include<conio.h>#include<fstream.h>void main(){char na[20],ans = 'y';float p,q,tp,dis,np;clrscr();

ofstream fout;fout.open("warshil",ios :: app);fout<<"ITEM\tPRICE\tQTY\tTP\tDIS\tNP\n";while(ans == 'y' || ans == 'Y'){

cout<<"Enter Item Name : ";cin>>na;cout<<"Enter Price : ";cin>>p;cout<<"Enter Quantity : ";cin>>q;tp = p * q;

if(tp >= 1000){

dis = tp * .08;np = tp - dis;

}

49

Page 50: C++ Programs

elseif(tp >= 500 && tp <= 999){

dis = tp * .05;np = tp - dis;

}elseif(tp <= 500){

dis = tp * .02;np = tp - dis;

}

fout<<na<<"\t"<<p<<"\t"<<q<<"\t"<<tp<<"\t"<<dis<<"\t"<<np<<"\n";cout<<"Do You Want To Continue(y/n) : ";cin>>ans;

}fout.close();getch();

}

Q:- Wap to input Employee Name, Basic Salary to find DA, TA, TAX and Net Salary using File Hadling.

#include<iostream.h>#include<conio.h>#include<fstream.h>void main(){char na[10][20],ans;int n,i;float bs[10],da[10],ta[10],tax[10],ns[10];clrscr();cout<<"Enter Number of Time to Input : ";cin>>n;do{

ofstream a("anvita");{

for(i=0;i<n;i++){

cout<<"Enter Employee Name : ";cin>>na[i];cout<<"Enter Basic Salary : ";cin>>bs[i];da[i] = bs[i] * .08;ta[i] = bs[i] * .05;tax[i] = bs[i] * .10;ns[i] = (bs[i] + da[i] + ta[i]) - tax[i];

50

Page 51: C++ Programs

}a<<"NAME\tBASIC SALARY\tDA\tTA\tTAX\tNETSALARY\n";

for(i=0;i<n;i++){

a<<na[i]<<"\t"<<bs[i]<<"\t\t"<<da[i]<<"\t"<<ta[i]<<"\t"<<tax[i]<<"\t"<<ns[i]<<endl;

}}

ifstream somi;{

for(i=0;i<n;i++){

cout<<"\n\nEmployee Name : "<<na[i];cout<<"\nBAsic Salary : "<<bs[i];cout<<"\nDearness Allowance : "<<da[i];cout<<"\nTravelling Allowance : "<<ta[i];cout<<"\nTax : "<<tax[i];cout<<"\nNet Salary : "<<ns[i];

}}cout<<"\n\nDo You Want To Continue [y/n] : ";cin>>ans;clrscr();

}while(ans == 'y' || ans == 'Y');getch();}

Q:- Wap to input Student Name, RollNo. Maths, Phy, Chem to find Total using File Hadling.

#include<iostream.h>#include<conio.h>#include<stdio.h>#include<fstream.h>void main(){char na[3][20];float r[3],m[3],p[3],c[3],tm[3];int i;clrscr();

ofstream s("sur");{

for(i=0;i<3;i++){

cout<<"Enter Student Name : ";

51

Page 52: C++ Programs

gets(na[i]);cout<<"Enter Roll No : ";cin>>r[i];cout<<"Enter Marks Of Maths : ";cin>>m[i];cout<<"Enter Marks Of Phy : ";cin>>p[i];cout<<"Enter Marks Of Chem : ";cin>>c[i];tm[i] = m[i] + p[i] + c[i];

}s<<"Name\tRollNo\tMaths\tPhy\tChem\tTotal\n";for(i=0;i<3;i++){

s<<na[i]<<"\t"<<r[i]<<"\t"<<m[i]<<"\t"<<p[i]<<"\t"<<c[i]<<"\t"<<tm[i]<<endl; }

}ifstream raj;{

for(i=0;i<3;i++){

cout<<"\nTotal Marks : "<<tm[i];}

}getch();}

Q:- Wap to input two number, add them and store them in a File.

#include<iostream.h>#include<conio.h>#include<fstream.h> //HEADER FILE FOR FILE HANDLING IN C++.

void main(){

int a,b,c;clrscr();

ofstream j("Reena"); //'JAMES' IS THE NAME OF OUTFILE & "Reena" is name of text file

{cout<<"Enter the first number => ";cin>>a;cout<<"Enter the second number => ";cin>>b;c=a+b;j<<"First No\tSecond No\tTotal\n";j<<a<<"\t\t"<<b<<"\t\t"<<c;}ifstream jai;

52

Page 53: C++ Programs

{cout<<"Sum of two numbers => "<<c;}

getch();}

Q:- Wap to input Student name and two subject marks find total and avg using File Handling

#include<iostream.h>#include<conio.h>#include<fstream.h>void main(){

char na[10][10],ans;int m[10],p[10],t[10],a[10],i;

clrscr();do{

ofstream jam("Reena");{for(i=0;i<2;i++){

cout<<" Enter the name of students-:-";cin>>na[i];cout<<"Enter the math-:-";cin>>m[i];cout<<"Enter the phy-:-";cin>>p[i];

t[i]=m[i]+p[i];a[i]=t[i]/2;

jam<<na[i]<<"\t"<<m[i]<<"\t"<<p[i]<<"\t"<<t[i]<<"\t"<<a[i]<<"\n";}}ifstream dee;{for(i=0;i<2;i++){

cout<<"\nSum of two numbers-:-"<<t[i];cout<<"\nAverage number is-:-"<<a[i];

}}

cout<<"\n\nDo you want to continue [y/n] -:- ";cin>>ans;clrscr();}while(ans=='y' || ans=='Y');getch();}

53

Page 54: C++ Programs

Q:- Wap to input Item name, Quantity and Rate to find Total price, Discount and Net price using File Handling.

#include<iostream.h>#include<conio.h>#include<fstream.h>void main(){char na[3][10];int q[3],r[3],tp[3],dis[3],np[3],i;clrscr();ofstream ja("Rani");{

ja<<"Item_name\tQuantity\tRate\tT_price\tDiscount\tN_price\n";for(i=0;i<2;i++){

cout<<"Enter the name of item-:-";cin>>na[i];cout<<"Enter the quantity-:-";cin>>q[i];cout<<"Enter the rate-:-";cin>>r[i];tp[i]=q[i]*r[i];

if(tp[i]>=500)dis[i] = tp[i] * (5/100);np[i] = tp[i] - dis[i];

if (tp[i]>=700)dis[i] = tp[i] * (4/100);np[i] = tp[i] – dis[i];

ja<<na[i]<<"\t"<<"\t"<<q[i]<<"\t"<<"\t"<<r[i]<<"\t"<<tp[i]<<"\t"<<dis[i]<<"\t"<<"\t"<<np[i]<<endl;}

}

ifstream jam;{

for(i=0;i<2;i++) {

cout<<"Name of the item-:-"<<na[i];cout<<"\nTotal price is-:-"<<tp[i];cout<<"\nDiscount is-:-"<<dis[i];cout<<"\nNet price-:-"<<np[i];

} }getch();}

54

Page 55: C++ Programs

Q:- Wap to input Name and Roll No. up to ans is Yes, using File Handling.

#include<iostream.h>#include <fstream.h>#include <conio.h>void main(){

ofstream fout;fout.open("shail.dat",ios::app); //out

char ans ='y';int ro;char na[20];

clrscr();while(ans=='y' || ans=='Y'){

cout<<"Enter Name : ";cin>>na;cout<<"Enter Roll : ";cin>>ro;fout<<na<<"\t"<<ro<<"\n";cout<<"Do you want to Continue(y/n)..";cin>>ans;

}fout.close();

}

Q:- Wap to input Student Name and Marks to store in a file and Retrieve form file Using File Handling.

#include <iostream.h>#include <fstream.h>#include <conio.h>void main(){

ofstream fout("Sur");char na[20],ch;int i;float ma;

clrscr();for(i=0;i<5;i++){

cout<<"Student "<<(i+1)<<":\tName :";cin.get(na,20);cout<<"Marks :";cin>>ma;cin.get(ch);

fout<<na<<"\t"<<ma<<"\n";

55

Page 56: C++ Programs

}fout.close();

ifstream fin("Sur");fin.seekg(0);cout<<"\n";for(i=0;i<5;i++){

fin.get(na,20);fin.get(ch);fin>>na;

cout<<"Student : "<<na<<"\t";cout<<"Marks : "<<ma<<"\n";

}fin.close();getch();}

Q:- Wap to input Item Name, Price and Quantity to find discount up user input and store in a file and Retrieve form file Using File Handling.

#include<iostream.h>#include<conio.h>#include<fstream.h>class shop{

char na[20];float p,q,tp,dis,np;

public:void get(void);void put(void);

};void shop :: get(){

char ch;cin.get(ch);cout<<"Enter Item Name : ";cin.getline(na,20);cout<<"Enter Price : ";cin>>p;cout<<"Enter Quantity : ";cin>>q;

}void shop :: put(){

tp = p * q;dis = tp * .10;np = tp - dis;

56

Page 57: C++ Programs

cout<<na<<"\t"<<p<<"\t"<<q<<"\t"<<tp<<"\t"<<dis<<"\t"<<np<<endl;}

void main(){int n,a[100];

clrscr();shop s[100];fstream filin;filin.open("sweety.dat",ios :: in | ios :: out);if(!filin){

cout<<"Cannot Open File";}cout<<"Enter Details Of Sale \n";cout<<"Enter Upto Which Term : ";cin>>n;for(int i=0;i<n;i++){

s[i].get();filin.write((char *) & s[i],sizeof(s[i]));

}filin.seekg(0);cout<<"The Contants Of Sweety.dat Are Shown Below \n";cout<<"Item\tPrice\tQty\tTp\tDis\tNp\n";

for(i=0;i<n;i++){

filin.read((char *) & s[i],sizeof(s[i]));s[i].put();

}filin.close();

Q:- Wap to input Student Name, Grade and Marks to store in a file and Retrieve form file Using File Handling.

#include <fstream.h>#include <conio.h>class stud{

char na[20];char gr;int ma;

public :void get(void);void put(void);

};void stud :: get(){

57

Page 58: C++ Programs

char ch;cin.get(ch);cout<<"Enter Name : ";cin.getline(na,20);cout<<"Enter Grade : ";cin>>gr;cout<<"Enter Marks : ";cin>>ma;

}void stud :: put(){

cout<<na<<"\t";cout<<gr<<"\t";cout<<ma<<"\n";

}int main(){

clrscr();stud sci[3];fstream filin;filin.open("adi.dat",ios::in | ios::out);if(!filin){

cout<<"Cannot Open File ";return 1;

}cout<<"Enter Details for 3 Student \n";

for(int i=0;i<3;i++){

sci[i].get();filin.write((char *) & sci[i],sizeof(sci[i]));

}filin.seekg(0);cout<<"The contants of adi.dat are shown below.\n";

cout<<"Name\tGrade\tMarks\n";for(i=0;i<3;i++){

filin.read((char *) & sci[i],sizeof (sci[i]));sci[i].put();

}filin.close();return 0;}

58