avr39ripe

cppFractionExampleDraft

Jul 13th, 2021
1,043
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() { return numerator; }
  15.     int getDenominator() { return denominator; }
  16.  
  17.     void setNumerator(int num) { numerator = num; }
  18.     void setDenominator(int denom) { denominator = denom; }
  19.  
  20.     Fraction mul(Fraction& frac)
  21.     {
  22.         return Fraction{ numerator * frac.numerator, denominator * frac.denominator };
  23.     }
  24.  
  25.     Fraction div(Fraction& frac)
  26.     {
  27.         return Fraction{ numerator * frac.denominator, denominator * frac.numerator};
  28.     }
  29.  
  30.     // BAAAD!!!! DO NOT DO IT AT HOME!! DANGER :)
  31.     //void print(int num, int denom)
  32.     //{
  33.     //  std::cout << '(' << num << '/' << denom << ')';
  34.     //}
  35.  
  36.     //void print()
  37.     //{
  38.     //  std::cout << '(' << getNumerator() << '/' << getDenominator() << ')';
  39.     //}
  40.  
  41.     Fraction& print()
  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.  
  53. // BAAAD!!!! DO NOT DO IT AT HOME!! DANGER :)
  54. //void print(int num, int denom)
  55. //{
  56. //  std::cout << '(' << num << '/' << denom << ')';
  57. //}
  58. //
  59. //void print(Fraction* frac)
  60. //{
  61. //  std::cout << '(' << frac->getNumerator() << '/' << frac->getDenominator() << ')';
  62. //}
  63.  
  64. int main()
  65. {
  66.     Fraction f1{ 1,2 };
  67.     Fraction f2{ 1,3 };
  68.  
  69.     Fraction f3{ f1.mul(f2) };
  70.  
  71.     f1.print();
  72.     f2.print();
  73.     f3.print();
  74.  
  75.  
  76.     return 0;
  77. }
RAW Paste Data