jeudi 18 août 2016

Operator overloading for add and compare data of Integer and Fraction classes

Similar to this problem posted here. I Need to create three classes:

  1. "Number" class supports three operations,such as, “display”, “==”,
    and “+”;
  2. "Integer class" that represented by integer;
  3. "Fraction" class is represented by numerator and denominator.

Requirements:

  1. It should support the operations: (a) Integer (I) + Fraction (F), (b) F+I, (c) F+F, (d) I+I, and comparing them
  2. The caller of the + operation doesn't need to know the return type.

I could solve the problem till requirement# 1. However, couldn't figure out the second requirement yet. Any help would be appreciated. To keep it brief, I am going to share the header file of my code below, function definition of the code can be shared if needed.

  1. Number.h
#pragma once

template<class T>
class Number
{
public:
    bool operator== (const T&)
    {
        return impl().operator == ();
    }

    T operator+ (const T &) const
    {
        return impl().operator+();
    }


    template <typename Stream>
    void display(Stream& os) const
    {
        impl().display(os);
    }

private:
    T& impl() {
        return *static_cast<T*>(this);
    }
    T const & impl() const {
        return *static_cast<T const *>(this);
    }

};
  1. Integer.h
#pragma once
#include "Number.h"

class Integer : public Number<Integer>
{
    int intValue{0};
public:
    template <typename Stream>
    void display(Stream& os) const
    {
        os << this->intValue << '\n';
    }

    Integer() = default;
    ~Integer() = default;

    Integer(int num);
    int getIntValue() const;
    bool operator== (const Integer &);
    Integer operator+ (const Integer &) const;
};
  1. Fraction.h
#pragma once
#include <math.h>
#include "Number.h"
#include "Integer.h"
#include <iostream>

class Fraction : public Number<Fraction>
{
    int _numerator{0};
    int _denominator{1};
    int gcdCalculate(int val1, int val2) const;
    int lcmCalculate(const int val1, const int val2) const;

public:
    template <typename Stream>
    void display(Stream& os) const

    {int tempNum = this->_numerator;
    int tempDen = this->_denominator;
    double tempFrac = (double)tempNum/(double)tempDen;
    double intpart;

        if (this->_denominator==0)
        {

        std::cout << "Undefined " << this->_numerator << "/" << this->_denominator << "(Divide by zero exception)";
        }
    else if (this->_denominator==1){
        std::cout << this->_numerator << std::endl;
        }

    else {
        os << this->_numerator << "/";
        os << this->_denominator << '\n';}
    }

    Fraction() = default;
    Fraction(int num, int den);

    ~Fraction() = default;

    bool operator== (const Fraction &);
    bool operator== (const Integer &);
    friend bool operator== (const Integer&, const Fraction&);

    Fraction operator+ (const Fraction &) const;
    Fraction operator+ (const Integer &) const;
    friend Fraction operator+ (const Integer&, const Fraction&);
};

main.cpp

#include <iostream>
using namespace std;


template <typename INumberType>
void GenericDisplay(const Number<INumberType>& num) //Here we are calling through the Number<> Interface
{
    num.display(cout);
}

int main()
{
    Fraction fracOne(1,4);
    Fraction fracTwo(2,8);
    Integer intOne(30);
    Integer intTwo(30);
    Fraction sumOfFractionOneTwo = fracOne + fracTwo;
    Integer sumOfIntegerOneTwo = intOne + intTwo;
    Fraction sumOfFractionOneAndIntegerOne = integerOne + fracOne;
    Fraction sumOfFractionTwoAndIntegerTwo = fracTwo + intTwo;
    return 0;
}

Here in this code, caller of the + operator knows the return type Fraction or Integer. It has to be, caller should not know the return type. e.g., Number sumOfFractionOneAndIntegerOne = integerOne + fracOne; Again, any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire