Pages

100 Contemporary Living Room Designs

Sunday, September 9, 2012

C++ PROGRAM TO FIND THE GREATEST AMONG TWO DISTANCES USING FRIEND FUNCTIONS.

Define a class Distance with feet and inch and with a print function to print the distance. Write a non-member function max which returns the larger of two distance objects, which are arguments. Write a main program that accepts two distance objects from the user, compare them and display the larger.


#include<iostream.h>
#include<conio.h>
class distance
{
  int feet;
  float inches;
  public:
  distance()
  {
    feet=0;
    inches=0.0;
  }
  void getdist()
  {
    cout<<"Enter feet:"<<endl;
    cin>>feet;
    cout<<"Enter inches:"<<endl;
    cin>>inches;
  }
  void showdist()
  {
    cout<<endl<<"Feet : "<<feet<<endl<<"Inches:"<<inches<<endl<<endl;
  }
  friend void maxdist(distance d1, distance d2);
};
  void maxdist(distance d1, distance d2)
  {
    d1.feet=d1.feet+ d1.inches/12;
    d2.feet=d2.feet+ d2.inches/12;
    if(d1.feet>d2.feet)
    {
      cout<<"first distance is greater";
    }
    else
    {
      cout<<"second distance is greater";
    }
  }

void main()
{
  clrscr();
  distance d3,d4;
  d3.getdist();
  d3.showdist();
  d4.getdist();
  d4.showdist();
  maxdist(d3,d4);
  getch();
}

OUTPUT


6 comments: