Program To Add Two Rational Numbers (p/q Form) And Print The Output In Form p/q Where p Is The Numerator And q Is The Denominator Using Classes C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code

Code


To copy code: Click View Source “View Source” on the right of this code snippet.


Background and Explanation of the code


In the above program three objects r1, r2, r3 of type rational are created.Then setdata ( ) member function for r1 and r2 object is invoked to set the values of data members num and deno.The statement r3.add (r1, r2); on execution in main ( ) invokes member functions add ( ) through object r3 and pass two objects r1 and r2 as arguments.This function perform addition of passed rational number objects and the result is displayed using show ( ) member function which is invoked through r3 object.

OUTPUT

Sum = 22/15