Operator Overloading

أنشِئ Class :

اسمهFraction

المتغيرات numerator , denominator

يحوي : default constructor و  set & get و  overloading functions للعمليات التالية :

جمع ، أقل من ، ضرب >> as global functions .

قسمة ، إسناد “=”  >> as member functions .

قراءة وطباعة .

-في main قم باختبار كل الـ operator overloading functions .

الحل:

مفهوم operator overloading هو أن نعيد تعريف الـ built-in operator حتى نتمكن من تطبيقها على الـ objects كأن نجمع 2 objects أو نضربهما ..الخ .

نلاحظ في السؤال أنه لم يتم التوضيح بالنسة لعملية القراءة cin والطباعة cout ماإذا كانت member or global لأنها في الواقع لايمكن ان تكون سوى global أي friend .

-يجب أن نراعي في الحل قوانين ضرب وجمع وقسمة ومقارنة الكسور .

 

//.h
#include <iostream>
using namespace std;

class Fraction
{
private:
 int numerator;
 int denominator;

public:
 Fraction(){ numerator = 0; denominator = 0; }

 void setNumerator(int num)
 {
 numerator = num;
 }
 void setDenominator(int num)
 {
 denominator = num;
 }
 int getNumerator()
 {
 return numerator;
 }
 int getDenominator()
 {
 return denominator;
 }

 //global means (friend) "optional"
 friend Fraction operator +(Fraction &obj1, Fraction &obj2);
 friend bool operator < (Fraction &obj1, Fraction &obj2);
 friend Fraction operator *(Fraction &obj1, Fraction &obj2);

 //member function "optional"
 void operator = (Fraction &obj);
 Fraction operator /(Fraction &obj);

 //must be friend
 friend ostream& operator << (ostream& outs, const Fraction& ob);
 friend istream& operator>>(istream& inc, Fraction& ob);

};
-----------------------------------------------------------
//.cpp
#include "Fraction.h"
#include <iostream>
using namespace std;

Fraction operator +(Fraction &obj1, Fraction &obj2)
{
 Fraction obj3;
 obj3.denominator = obj1.denominator * obj2.denominator;
 obj3.numerator = (obj1.numerator*obj2.denominator) + (obj2.numerator*obj1.denominator);
 return obj3;
}

bool operator < (Fraction &obj1, Fraction &obj2)
{
 if ((obj1.numerator / obj1.denominator) < (obj2.numerator / obj2.denominator))

 return true;

 else return false;
}

Fraction operator *(Fraction &obj1, Fraction &obj2)
{
 Fraction obj3;
 obj3.denominator = obj1.denominator*obj2.denominator;
 obj3.numerator = obj1.numerator*obj2.numerator;
 return obj3;
}

void Fraction::operator = (Fraction &obj)
{
 numerator = obj.numerator;
 denominator = obj.denominator;
}

Fraction Fraction::operator/(Fraction &obj)
{
 Fraction obj3;
 obj3.denominator = denominator*obj.numerator;
 obj3.numerator = numerator*obj.denominator;
 return obj3;
}

ostream& operator << (ostream& outs, const Fraction& ob)
{
 outs << "The result is: ";
 outs << ob.numerator << "/" << ob.denominator << endl;
 return outs;
}

istream& operator >> (istream& inc, Fraction& ob)
{
 cout << "Enter the number numerator: ";
 inc >> ob.numerator;
 cout << "Enter the number denominator: ";
 inc >> ob.denominator;
 return inc;
}
---------------------------------------------------
//main.cpp
#include <iostream>
using namespace std;
#include "Fraction.h"
int main() {

 Fraction f1, f2;

 cin >> f1;
 cin >> f2;

 cout << " << operator:\n";
 cout << f1 << f2;

 cout << " + operator: ";
 Fraction f3 = (f1 + f2);
 cout << f3;

 cout << " < operator: ";
 cout << (f1<f2) << endl;

 cout << " * operator: ";
 Fraction f4 = f1*f2;
 cout << f4;

 cout << " = operator: ";
 Fraction f5 = f1;
 cout << f5;

 cout << " / operator: ";
 cout << (f1 / f2);



 system("pause");
 return 0;
}