topical media & game development

talk show tell print

basic-program-solutions-08-Soln8-1.c

? / basic-program-solutions-08-Soln8-1.c


  // Soln8_1.cpp
  
  include <iostream>                   // For stream input/output
  
  using std::cout;
  using std::endl;
  
  define TRUE 1
  define FALSE 0
  
  class CEstimatedInteger
  {
  private:
     int val;
     int bEst;
  
  public:
    CEstimatedInteger(int i=0, int e=0) : val(i), bEst(e){}
  
     void setEstimated(int e)
     {
        bEst = (!e) ? FALSE : TRUE;
     }
  
     void print();
  
     // Helper functions
     CEstimatedInteger Add(const CEstimatedInteger& b) const;
  };
  
  void CEstimatedInteger::print()
  {
     if (bEst)
        cout << 'E';
     cout << val;
  }
  
  CEstimatedInteger CEstimatedInteger::Add(const CEstimatedInteger& b) const
  {
     CEstimatedInteger t(val+b.val);
     if (bEst || b.bEst)
        t.bEst = TRUE;
  
     return t;
  }
  
  CEstimatedInteger operator+(const CEstimatedInteger& a, const CEstimatedInteger& b)
  {
     return a.Add(b);
  }
  
  int main()
  {
     CEstimatedInteger a=3, c;
     CEstimatedInteger b(5,TRUE);
  
     cout << "a=";
     a.print();
     cout << endl;
  
     cout << "b=";
     b.print();
     cout << endl;
  
     c = a + b;
     cout << "c=";;
     c.print();
     cout << endl;
  
     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.