Free Hosting

C++ Distance Program (using Classes)

C:\Users\Jatinder\Desktop\DistanceProgram.html <!-- Exported by C-Free C/C++ IDE

#include<iostream.h>
#include<conio.h>
class Distance
{
private:
 int m,cm;
 int convert_Distance(int met,int cemt)
 {
  return((met*100)+cemt);
 }
public:
 Distance()
 {
  set_Distance(0,0);
 }
 Distance(int mm,int cmt)
 {
  set_Distance(mm,cmt);
 }
 Distance(int cmt)
 {
  set_Distance(cmt);
 }
 void set_Distance(int mt,int cmt)
 {
  set_Distance(convert_Distance(mt,cmt));
 }
 void set_Distance(int cmt)
 {
  m=cmt/100;
  cm=cmt%100;
 }
 int operator<(Distance De)
 {
  return(convert_Distance(m,cm)<convert_Distance(De.m ,De.cm )?1:0);
 }
 int operator>(Distance De)
 {
  return(convert_Distance(m,cm)>convert_Distance(De.m ,De.cm )?1:0);
 }
 Distance operator+(Distance De)
 {
  Distance temp;
  temp.m=m+De.m;
  temp.cm=De.cm;
  return(temp);
 }
 friend ostream & operator<<(ostream &o,Distance &t)
 {
           o<<t.m<<":::"<<t.cm;
           return o;
           }
     friend istream & operator>>(istream &i,Distance &t)
 {
           i>>t.m>>t.cm;
           return i;
           }
};

int main()
{
 Distance d1(4500);
 Distance d2(45,300);
 cout<<"Distance 1=   "<<d1<<endl;
 cout<<"Distance 2=   "<<d2<<endl;
 if(d1<d2)
   cout<<"Distance 1 is smaller then Distance 2"<<endl;
 return 0; 
}

-->

0 comments:

Blogger Template by Clairvo