avr39ripe

cppOperatorsOverloading

Jul 15th, 2021
1,173
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. class Fraction
  4. {
  5.     int numerator;
  6.     int denominator;
  7. public:
  8.     Fraction(int numeratorP, int denominatorP) : numerator{ numeratorP }, denominator{ denominatorP }
  9.     {
  10.         std::cout << "Fraction constructed for -> " << this << '\n';
  11.     }
  12.     Fraction() :Fraction(1, 1) {}
  13.  
  14.     int getNumerator()const { return numerator; }
  15.     int getDenominator()const { return denominator; }
  16.  
  17.     void setNumerator(int num) { numerator = num; }
  18.     void setDenominator(int denom) { denominator = denom; }
  19.  
  20.     Fraction mul(const Fraction& frac) const
  21.     {
  22.         return Fraction{ numerator * frac.numerator, denominator * frac.denominator };
  23.     }
  24.  
  25.     Fraction div(const Fraction& frac) const
  26.     {
  27.         return Fraction{ numerator * frac.denominator, denominator * frac.numerator };
  28.     }
  29.  
  30.     Fraction& print()
  31.     {
  32.         std::cout << '(' << numerator << '/' << denominator << ')';
  33.         return *this;
  34.     }
  35.  
  36.     void print(const Fraction& frac)const
  37.     {
  38.         std::cout << '(' << frac.numerator << '/' << frac.denominator << ')';
  39.     }
  40.  
  41.     const Fraction& print() const
  42.     {
  43.         std::cout << '(' << numerator << '/' << denominator << ')';
  44.         return *this;
  45.     }
  46.  
  47.     ~Fraction()
  48.     {
  49.         std::cout << "Fraction destructed for -> " << this << '\n';
  50.     }
  51.  
  52.     // 2/3 + 1 -. 2/3 + 3/3 -> (2 + 3) / 3 -> 5 / 3
  53.     Fraction& operator++() { numerator += denominator; return *this; }
  54.     Fraction operator++(int)
  55.     {
  56.         Fraction old{ *this };
  57.         //numerator += denominator;
  58.         ++(*this);
  59.         return old;
  60.     }
  61.     Fraction operator*(/*const Fraction* const this, */ const Fraction& right)const
  62.     {
  63.         return Fraction{ numerator * right.numerator, denominator * right.denominator };
  64.     }
  65.  
  66.     Fraction operator*(int right)const
  67.     {
  68.         return Fraction{ numerator * right, denominator };
  69.     }
  70.  
  71.     operator bool() { return numerator; }
  72.     operator int() { return numerator / denominator; }
  73.     operator float() { return numerator / denominator; }
  74.  
  75.     //friend Fraction operator*(const Fraction& left, const Fraction& right);
  76.     //friend Fraction operator*(const Fraction& left, int right);
  77.     friend bool operator==(const Fraction& left, const Fraction& right);
  78. };
  79.  
  80. Fraction operator/(const Fraction& left, const Fraction& right)
  81. {
  82.     return Fraction{ left.getNumerator() * right.getDenominator(), left.getDenominator() * right.getNumerator() };
  83. }
  84.  
  85.  
  86. //Fraction operator*(const Fraction& left, const Fraction& right)
  87. //{
  88. //  return Fraction{ left.numerator * right.numerator, left.denominator * right.denominator };
  89. //}
  90. //
  91. //Fraction operator*(const Fraction& left, int right)
  92. //{
  93. //  return Fraction{ left.numerator * right, left.denominator };
  94. //}
  95. //
  96. //Fraction operator*(int left, const Fraction& right)
  97. //{
  98. //  //return Fraction{ left * right.getNumerator(), right.getDenominator() };
  99. //  //return operator*(right, left);
  100. //  return right * left;
  101. //}
  102.  
  103. bool operator==(const Fraction& left, const Fraction& right)
  104. {
  105.     return left.numerator == right.numerator and left.denominator == right.denominator;
  106. }
  107.  
  108. int main()
  109. {
  110.  
  111.  
  112.     Fraction f1{ 1,3 };
  113.     Fraction f2{ 1,3 };
  114.  
  115.     int a{ 10 };
  116.     int b{ 34 };
  117.  
  118.     int c{ a * b };
  119.  
  120.     Fraction f3{ f1 * 2 };
  121.     if (f1)
  122.     {
  123.  
  124.     }
  125.  
  126.     f3.print(); std::cout << '\n';
  127.        
  128.  
  129. }
RAW Paste Data