Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. // Class Phan so
  4. class Fraction {
  5. //default private(*)
  6. int numerator, denominator;
  7. public: //* Definition inside Class
  8. Fraction() {
  9. numerator = 1;
  10. denominator = 1;
  11. }
  12. Fraction(int n, int d) {
  13. numerator = n;
  14. if (d==0)
  15. {
  16. cout << "ERROR" << endl;
  17. }
  18. else
  19. denominator = d;
  20. }
  21.  
  22. void show() // Display method
  23. {
  24. if (denominator == 1) // e.g. fraction 2/1 will display simply as 2
  25. cout << numerator << endl;
  26. else
  27. cout << numerator << "/" << denominator << endl;
  28. }
  29. };
  30.  
  31. int main()
  32. {
  33. Fraction a(1,2);
  34. Fraction b(1,4);
  35. Fraction c;
  36.  
  37. a.show();
  38. // Result: 1/2
  39.  
  40. b.show();
  41. // Result: 1/4
  42.  
  43. c.show();
  44. // Result: 1
  45.  
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement