Created
July 31, 2020 08:48
-
-
Save abhineet-space/a153ec94e362b5f47d7fa2cb16984643 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Fraction { | |
private : | |
int numerator; | |
int denominator; | |
public : | |
Fraction(int numerator, int denominator) { | |
this -> numerator = numerator; | |
this -> denominator = denominator; | |
} | |
void print() { | |
cout << this -> numerator << " / " << denominator << endl; | |
} | |
void simplify() { | |
int gcd = 1; | |
int j = min(this -> numerator, this -> denominator); | |
for(int i = 1; i <= j; i++) { | |
if(this -> numerator % i == 0 && this -> denominator % i == 0) { | |
gcd = i; | |
} | |
} | |
this -> numerator = this -> numerator / gcd; | |
this -> denominator = this -> denominator / gcd; | |
} | |
//Operator overloading ++ | |
// pre-incresement | |
Fraction operator++(){ | |
numerator = numerator + denominator; | |
simplify(); | |
return *this; | |
} | |
}; | |
int main() { | |
Fraction f1(10, 2); | |
f1.print(); | |
++(++f1); | |
f1.print(); | |
//same value in another variable | |
cout << "Second Time Output"<< endl; | |
Fraction f2(10, 2); | |
f2.print(); | |
Fraction f3 = ++(++f2); | |
f3.print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check Operator overloading urinary Operator..