Himadri Das
Himadri Das

Reputation: 31

Implementing a polynomial in c++ with overloading operator + on constructors

A class Poly is constructed to create and do operations on polynomial. Vectors are used to store exponents and coefficients. Poly(int,int) is a constructor to initialize values of coefficient and exponent of a particular term. Obviously, a polynomial will have more than one term. So, i am planning to use Poly c = Poly(3,4) + Poly(2,2) + Poly(7,1) + Poly(-5,0); to initialize more than one term and add all coefficients and exponents to the vectors which will have exponents and coefficients for a single Poly object with multiple terms.

class Poly
{
    // for writing polynomials
    // friend ostream &operator<<(ostream &Out, const Poly &F);
    // private data and functions
private:
    // coefficient of a term in polynomial
    int term_coefficient;
    // exponent of a term in polynomial
    int term_exponent;
    // public data and functions
public:
    // coefficients of the polynomial
    vector<int> C;
    // exponents of the polynomial
    vector<int> E;
    // constructor for default polynomial
    Poly()
    {
    }
    // constructor for polynomial with arguments
    Poly(int c, int e)
    {
        term_coefficient=c;
        term_exponent=e;
        vector<int>::iterator it;
        int size_of_e_vector;
        int size_of_c_vector;
        size_of_e_vector = E.size();
        size_of_c_vector = C.size();
        if(size_of_e_vector!=0)
        {
            cout<<"There exists previous values"<<endl;
            // cout<<"At Exponent: "<<term_exponent<<", Exponent Value is: "<<E.at(term_exponent)<<endl;
        }
        else
        {
            // cout<<"The Exponent Vector is Empty"<<endl;
            it = E.begin() + term_exponent;
            E.insert(it, term_exponent);
            C.insert(it, term_coefficient);
            cout<<"In Exponent Vector, the Value is: "<<E.at(term_exponent)<<", and in Coefficient Vector, the Value is: "<<C.at(term_exponent)<<endl;
        }
    }
    // copy constructor
    /*
    Poly(const Poly & rhs)
    {

    }
    */
    // destructor
    ~Poly()
    {
    }
    // overloaded operators (members)
    // Poly & operator+=(const Poly & rhs);
    // Poly operator+(Poly(int & co1, int & ex1));
};

// overloaded operators (non-members)
Poly operator+(Poly::Poly(int & co1, int & ex1), Poly::Poly(int & co2, int & ex2));

Poly operator+(Poly::Poly(int & co1, int & ex1), Poly::Poly(int & co2, int & ex2))
{
   if(ex1!=ex2)
    {
        // put co2 in C vector and ex2 in E vector
        // assuming co1 is already in C vector and ex1 already in E vector
        // if not push co1 and ex1 too   
    }
}

How to overload + such that Poly c = Poly(3,4) + Poly(2,2) + Poly(7,1) + Poly(-5,0); writing this is possible? Please Note : I am not overloading + on objects but i want to work on constructors.

int main()
{
    Poly c = Poly(3,4) + Poly(2,2) + Poly(7,1) + Poly(-5,0);
    // cout<<c.term_exponent<<endl;
    return 0;
}

Upvotes: 1

Views: 1205

Answers (1)

Pascal Getreuer
Pascal Getreuer

Reputation: 3271

Overload operator+ to construct a Poly from the concatenation of the C and E vectors.

Upvotes: 2

Related Questions