title: How to write Rational Fractions using classes in c++
body: <p>I have to implement <code>Rational</code> class in order to get Rational fractions. The <code>header.h</code> file is provided by my instructor so I have to follow up. I also have to write copy constructor in <code>Rational::Rational(const Rational&amp; cRational)</code> function so the <code>object</code> can be copied. I have written my code but the addition of fractions in the output is wrong. Can anybody help me figure this one out? What is wrong with my coding specially in <code>Rational::addition(const Rational &amp;a)</code> or how could i fix it ?</p>
  
  <p>output :</p>
  
  <pre><code> Begin Rational Class Tests
  
  Testing default constructor: 1/1
  
  Testing std constructor: 1/2
  
  Testing copy constructor: 1/2
  
  Testing addition: 4/4 + 1/2 = 4/4    // it should be 1/2 + 1/2 = 4/4
  </code></pre>
  
  <p>main function:</p>
  
  <pre><code>int main()
  {
      cout &lt;&lt; "Begin Rational Class Tests\n\n";
  
      cout&lt;&lt;"Testing default constructor: ";
      Rational n1;
      n1.printRational();
      cout &lt;&lt; endl &lt;&lt; endl;
  
      cout&lt;&lt;"Testing std constructor: ";
      Rational n2(1,2);
      n2.printRational();
      cout &lt;&lt; endl &lt;&lt; endl;
  
      cout&lt;&lt;"Testing copy constructor: ";
      Rational n3(n2);
      n3.printRational();
      cout &lt;&lt; endl &lt;&lt; endl;
  
      cout&lt;&lt;"Testing addition: ";
      n1 = n2.addition(n3);
      n2.printRational();
      cout &lt;&lt;" + ";
      n3.printRational();
      cout &lt;&lt;" = ";
      n1.printRational();
      cout &lt;&lt; endl &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Header File:</p>
  
  <pre><code>class Rational {
    public:
      Rational();  // default constructor
      Rational(int, int); //std (initialisation) constructor
      Rational(const Rational&amp;); //copy constructor
      Rational addition(const Rational &amp;);
      void printRational();
    private:
      int numerator;
      int denominator;
  };
  </code></pre>
  
  <p>my program:</p>
  
  <pre><code>//default constructor
  Rational::Rational() 
  {
     numerator = 1;
     denominator = 1;
  }
  //initialize constructor
  Rational::Rational(int n, int d)
  {
       numerator = n;
       if (d==0) 
       {
          cout &lt;&lt; "ERROR: ATTEMPTING TO DIVIDE BY ZERO" &lt;&lt; endl;
          exit(0); // will terminate the program if division by 0 is attempted
       }
       else
          denominator = d;
  }
  //copy constructor
  Rational::Rational(const Rational&amp; cRational)
  {
      numerator = cRational.numerator;
      denominator = cRational.denominator;
  }
  //addition
  Rational Rational::addition(const Rational &amp;a)
  {
       numerator = numerator * a.denominator + a.numerator * denominator;
       denominator = denominator * a.denominator;
       return Rational(numerator,denominator);
  }
  
  void Rational::printRational()
  {
      cout &lt;&lt; numerator &lt;&lt; "/" &lt;&lt; denominator ;
  }
  </code></pre>