View Single Post
  #1  
Old 24-10-2008, 04:04 PM
shmsa's Avatar
shmsa shmsa is offline
Grown up punk

 
Join Date: Oct 2007
Location: Muslim Town Multan
Posts: 195
Contact Number: 0322-6196713
Program / Discipline: BSIT
Class Roll Number: 07-22
shmsa is a jewel in the roughshmsa is a jewel in the roughshmsa is a jewel in the roughshmsa is a jewel in the rough
Read Use of Binary Operator on class objects using C++

Code:
#include <iostream>
using namespace std;
class dist
{
 private:
   int feet;
   int inches;
 
 public:
  dist()
  {
   feet=2;
   inches=6;
  
  
  }
  dist(int f,int inc)
  {
  
   feet=f;
   inches=inc;
  
  
  }
  dist operator +(dist d2)
  {
  
   dist temp;
   temp.feet=feet + d2.feet;
   temp.inches=inches+d2.inches;
   if(temp.inches >= 12)
   {
    temp.feet++;
    temp.inches-=12;
   
   
   }
   return temp;
  
  }
  
   /////////////////////////////////
  dist operator *(dist d2)
  {
  
   dist temp;
   temp.feet=feet * d2.feet;
   
   temp.inches=inches * d2.inches;
   if(temp.inches >= 12)
   {
    temp.feet+=temp.inches/12;
    temp.inches-=(temp.inches/12)*12;
   
   
   }
   return temp;
  
  }
//////////////////////////////////////////////// copied from bzupages.com

  dist operator -(dist d2)
  {
  
   dist temp;
   if(inches < d2.inches)
   { feet--;
    inches+=12;
   }
   temp.feet=feet - d2.feet;
   temp.inches = inches - d2.inches;
   
   return temp;
  
  }
  bool operator >(dist d2)
  {
  
   bool yes,no;
   //yes=true;
  // no=false;
   if(feet>d2.feet)
   
    return true;
   else
    return false;
  
  }
  bool operator <(dist d2)
  {
  
   bool yes,no;
   //yes=true;
  // no=false;
   if(feet < d2.feet)
   
    return true;
   else
    return false;
  
  }
  void show()
  {
  
   cout<<"the value of feet"<<feet<<endl;
  cout<<"the value of inches"<<inches<<endl;
  
  }

};
void main()
{
 dist d1;//2 6
 dist d2(1,4);
 dist d3(25,9);
 
 d3=d1+d2;
 

 if(d1>d2)
  d3=d1-d2;
 else
  d3=d2-d1;
 d3=d1*d2;
 
 d3.show();
}

Reply With Quote