1

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;
}
Himadri Das
  • 31
  • 1
  • 8
  • 1) Get rid of the empty destructor. Writing empty destructors are not as benign as you think they are. 2) You have no `operator +` defined for `Poly`. – PaulMcKenzie Sep 13 '20 at 04:07
  • *Is it possible to write a constructor such that Poly c = Poly(3,4) + Poly(2,2) + Poly(7,1) + Poly(-5,0); writing this is possible?* -- The quick answer is "yes". How could something like `std::string s = std::string("a") + std::string("b") + std::string("c"):` be able to work if it isn't possible? – PaulMcKenzie Sep 13 '20 at 04:10
  • @PaulMcKenzie Not that I'm disagreeing with the advice, but what's the danger in an empty destructor? – john Sep 13 '20 at 04:33
  • @john Unless the destructor needs to be `virtual`, writing needless empty destructors changes the traits of the class that could prevent the compiler from optimizing the code to its fullest. [See this](http://coliru.stacked-crooked.com/a/82b96bfd4193aa61) – PaulMcKenzie Sep 13 '20 at 04:37
  • @PaulMcKenzie : I was asking help to write the code for operator + for Poly. – Himadri Das Sep 13 '20 at 15:49
  • @PaulMcKenzie : I can use a little help here. – Himadri Das Sep 15 '20 at 04:33

1 Answers1

2

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

Pascal Getreuer
  • 2,906
  • 1
  • 5
  • 14
  • Can you give a sketch of code required to overload + for the problem at hand. – Himadri Das Sep 13 '20 at 15:50
  • Check out example 4 in . In Poly's case, the body of the operator could be something like: (1) default-construct a new Poly object, (2) set its .C member to the concatenation of the lhs and rhs C vectors (see SO post [What is the best way to concatenate two vectors?](https://stackoverflow.com/questions/3177241/what-is-the-best-way-to-concatenate-two-vectors)), and (3) similarly set .E to the concatenation of the E vectors. – Pascal Getreuer Sep 13 '20 at 18:10
  • "I am not overloading + on objects but i want to work on constructors." Sorry, you can't apply operators on constructors, that's not how C++ work. Let me walk through a small example in detail and hopefully make the terminology clearer. In the expression `Poly(3,4) + Poly(2,2)`, the first term `Poly(3,4)` invokes constructor Poly::Poly(int c, int e), resulting in a `Poly` object. Similarly `Poly(2,2)` constructs and results in another `Poly` object. Finally, the `+` invokes `operator+(Poly, Poly)` to compute their sum. – Pascal Getreuer Sep 16 '20 at 09:03
  • Thanks @Pascal Getreuer – Himadri Das Sep 24 '20 at 16:43