topical media & game development

talk show tell print

basic-program-code-08-Ex8-10.c

? / basic-program-code-08-Ex8-10.c


  // Ex8_10.cpp : main project file.
  // Defining and using overloaded operator
  
  include <stdafx.h>
  using namespace System;
  
  ref class Length
  {
  private:
    int feet;
    int inches;
  
  public:
    static initonly int inchesPerFoot = 12;
  
    // Constructor
    Length(int ft, int ins) : feet(ft), inches(ins){ }
  
    // A length as a string
    virtual String^ ToString() override
    { return feet+L" feet " + inches + L" inches";  }
  
    // Overloaded addition operator
    Length^ operator+(Length^ len)
    {
      int inchTotal = inches+len->inches+inchesPerFoot*(feet+len->feet);
      return gcnew Length(inchTotal/inchesPerFoot, inchTotal\%inchesPerFoot);
    }
  
    // Overloaded divide operator - right operand type double
    static Length^ operator/(Length^ len, double x)
    {
      int ins = safe_cast<int>((len->feet*inchesPerFoot + len->inches)/x);
      return gcnew Length(ins/inchesPerFoot, ins\%inchesPerFoot);
    }
  
    // Overloaded divide operator - both operands type Length
    static int operator/(Length^ len1, Length^ len2)
    {
      return (len1->feet*inchesPerFoot + len1->inches)/
                                        (len2->feet*inchesPerFoot + len2->inches);
    }
  
    // Overloaded remainder operator
    static Length^ operator%(Length^ len1, Length^ len2)
    {
      int ins = (len1->feet*inchesPerFoot + len1->inches)%
                                         (len2->feet*inchesPerFoot + len2->inches);
      return gcnew Length(ins/inchesPerFoot, ins\%inchesPerFoot);
    }
  
    static Length^ operator*(double x, Length^ len); // Multiply - R operand double
    static Length^ operator*(Length^ len, double x); // Multiply - L operand double 
  
    // Pre- and postfix increment operator
    static Length^ operator++(Length^ len)
    {
      ++len->inches;
      len->feet += len->inches/len->inchesPerFoot;
      len->inches %= len->inchesPerFoot;
      return len;
    }
  };
  
  // Multiply operator implementation - right operand double
  Length^ Length::operator*(double x, Length^ len)
  {
    int ins = safe_cast<int>(x*len->inches +x*len->feet*inchesPerFoot);
    return gcnew Length(ins/inchesPerFoot, ins\%inchesPerFoot);
  }
  
  // Multiply operator implementation - left operand double
  Length^ Length::operator*(Length^ len, double x)
  { return operator*(x, len);  }
  
  int main(array<System::String ^> ^args)
  {
    Length^ len1 = gcnew Length(2,6);              // 2 feet 6 inches
    Length^ len2 = gcnew Length(3,5);              // 3 feet 5 inches
    Length^ len3 = gcnew Length(14,6);             // 14 feet 6 inches
  
    // Use +, * and / operators
    Length^ total = 12*(len1+len2+len3) + (len3/gcnew Length(1,7))*len2;
    Console::WriteLine(total);
  
    // Use remainder operator
    Console::WriteLine(
                L"{0} can be cut into {1} pieces {2} long with {3} left over.",
                                                 len3, len3/len1, len1, len3\%len1);
    Length^ len4 = gcnew Length(1, 11);            // 1 foot 11 inches
  
    // Use pre- and postfix increment operator
    Console::WriteLine(len4++);                    // Use postfix increment operator
    Console::WriteLine(++len4);                    // Use postfix increment operator
    return 0;
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.