Create a class Distance containing Feet and Inches as data members. Write a C++ program to read distance from user, store it in the file and to read it from the file and display it to the user. Use operator overloading for the following: 1. << to write distance object in inches format to a file 2. >> to read inches from File.


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

class Distance
{
  int feet;
  int inches;

  public:
  friend istream& operator>>(istream &in,Distance &d);
  friend ostream& operator<<(ostream &out,Distance &d);
};//End of class

fstream file;

istream& operator>>(istream &in,Distance &d)
{
  cout<<endl<<"Enter distance(feet inches): ";
  in>>d.feet>>d.inches;
  file.write((char*)&d,sizeof(d));
  return in;
}

ostream& operator<<(ostream &out,Distance &d)
{
  file.seekg(0);
  file.read((char*)&d,sizeof(d));
  out<<"Distance is: "<<d.feet<<" feet and "<<d.inches<<" inches!";
  return out;
}

int main()
{
Distance d1,d2;
clrscr();
file.open("D:/arjun.txt",ios::out|ios::in);
cin>>d1;
cout<<d2;
file.close();
getch();
return 0;
}
getch();

2 comments:

  1. Thanks for it i would pass just brcause u....................

    ReplyDelete