星期五, 5月 20, 2016

關於this

class部分
class Box
{
   public:
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"建立物件" << endl;
         length = l;
         breadth = b;
         height = h;
      }
      double Volume()
      {
         return length * breadth * height;
      }
      int compare(Box box)
      {
         return this->Volume() > box.Volume();
      }
   private:
      double length;     
      double breadth;     
      double height;     
};
 
程式流程
 
   Box Box1(3.3, 1.2, 1.5);    
   Box Box2(8.5, 6.0, 2.0);    

   if(Box1.compare(Box2))
   {
      cout << "Box2 小於 Box1" <<endl;
   }
   else
   {
      cout << "Box2 大於等於 Box1" <<endl;
   } 

沒有留言:

張貼留言