fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. class Fraction
  6. {
  7. public:
  8. Fraction( int numerator , int denominator = 1 );
  9. std::string Add( Fraction );
  10. private:
  11. int numerator , denominator;
  12. };
  13.  
  14. Fraction::Fraction( int numerator , int denominator )
  15. : numerator( numerator ) , denominator( denominator ){}
  16.  
  17. std::string Fraction::Add( Fraction obj2 )
  18. {
  19. obj2.numerator *= denominator;
  20. obj2.numerator += numerator * obj2.denominator;
  21. obj2.denominator *= denominator;
  22. std::stringstream ss;
  23. ss << obj2.numerator << '/' << obj2.denominator;
  24. return( ss.str() );
  25. }
  26.  
  27. int main()
  28. {
  29. Fraction obj1( 2, 9 ) , obj2( 5 );
  30. std::cout << obj1.Add( obj2 ) << std::endl;
  31. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
47/9