Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // Class Phan so
- class Fraction {
- //default private(*)
- int numerator, denominator;
- public: //* Definition inside Class
- Fraction() {
- numerator = 1;
- denominator = 1;
- }
- Fraction(int n, int d) {
- numerator = n;
- if (d==0)
- {
- cout << "ERROR" << endl;
- }
- else
- denominator = d;
- }
- void show() // Display method
- {
- if (denominator == 1) // e.g. fraction 2/1 will display simply as 2
- cout << numerator << endl;
- else
- cout << numerator << "/" << denominator << endl;
- }
- };
- int main()
- {
- Fraction a(1,2);
- Fraction b(1,4);
- Fraction c;
- a.show();
- // Result: 1/2
- b.show();
- // Result: 1/4
- c.show();
- // Result: 1
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement