operator overloading problem

Pages: 12
Um, yes, actually, we were giving you clear help which you proceeded to ignore.
i tried your way , it didn't work
i also gave my term class
Last edited on
finally got it worked :)
line 208 and 209.. I was not initializing by ZERO.


By the way Its nice to get acquainted.
Hi, I I don't understand what you want your program to do. However, i tried to modify your code a little and it just run. Here the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
The project include this file:
    Term.h - definition and implemantion of template class Term
    Polynomial.h - definition and implemantion of template class Polynomial
    main.cpp - Main of the program
*/

//---------------- Term.h ----------------
#ifndef TERM_H_INCLUDED
#define TERM_H_INCLUDED

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <sstream>
#include <math.h>
#include <string>

using namespace std;

#define ALL 7
#define COEFF 1
#define EXP 2
#define VAR 4

template <class T>
class Term{
    private:
        int exp;
        T base;
        char lit;
        bool Compatible(const Term&,int op)const;
    public:
        bool Same(const Term&,int op=ALL)const;
        //---- Costructors ----
        Term();
        Term(T b,int e,char c);
        Term(const Term&);
        //---- operators ----
        Term& operator = (const Term&);
        Term& operator += (const Term&);
        Term& operator -= (const Term&);
        Term& operator *= (const Term&);
        Term& operator /= (const Term&);
        bool operator == (const Term& t)const{
            return Same(t,ALL);}
        bool operator != (const Term& t)const{
            return !Same(t,ALL);}
        template<class N>friend ostream& operator << (ostream&,const Term<N>&);
        //---- Getter & setter ----
        T Coeff()const{return base;}
        void Coeff(T t){base=t;}
        int Exp()const{return exp;}
        void Exp(int e){exp=e;}
        char Variable()const{return lit;}
        void Variable(char c){lit=c;}

};

//---- Implementation ----
template<class T>
bool Term<T>::Compatible(const Term<T>& t, int op)const{
    switch(op){
        case 0:
        case 1:return (lit==t.lit && exp==t.exp);
        case 2:
        case 3: return (lit==t.lit || lit=='\0' || t.lit=='\0');
    }
    return false;
}

template<class T>
bool Term<T>::Same(const Term& t,int op)const{
    bool result=true;
    if (op && COEFF)result &= (base==t.base);
    if (op && EXP)result &= (exp==t.exp);
    if (op && VAR)result &= (lit==t.lit);
    return result;
}

//----Costructors ----
template<class T>
Term<T>::Term():exp(0),base(0),lit('\0'){};

template<class T>
Term<T>::Term(T b,int e,char c){
    lit=c;
    base=b;
    exp=e;
}

template<class T>
Term<T>::Term(const Term& t){
    lit=t.lit;
    base=t.base;
    exp=t.exp;
}

//---- operators ----

template<class T>
Term<T>& Term<T>::operator = (const Term<T>& t){
    if(this == &t)return *this;
    lit=t.lit;
    base=t.base;
    exp=t.exp;
    return *this;
}

template<class T>
Term<T>& Term<T>::operator += (const Term<T>& t){
    if (!Compatible(t,0))return *this;
    base+=t.base;
    return *this;
}

template<class T>
Term<T>& Term<T>::operator -= (const Term<T>& t){
    if (!Compatible(t,1))return *this;
    base-=t.base;
    return *this;
}

template<class T>
Term<T>& Term<T>::operator *= (const Term<T>& t){
    if (!Compatible(t,2))return *this;
    base*=t.base;
    exp+=t.exp;
    return *this;
}

template<class T>
Term<T>& Term<T>::operator /= (const Term<T>& t){
    if (!Compatible(t,3))return *this;
    base/=t.base;
    exp-=t.exp;
    return *this;
}

template<class T>
ostream& operator << (ostream& out,const Term<T> &t){
    out<<t.Coeff()<<""<<t.Variable()<<"^"<<t.Exp();
    return out;
}

#endif // TERM_H_INCLUDED

//---------------- Polynomial.h ----------------
#ifndef POLYNOMIAL_H_INCLUDED
#define POLYNOMIAL_H_INCLUDED

#include "Term.h"

using namespace std;

//template<class T>
//class Polynomial; //forward declaration
//template<class T>
//istream& operator>>(istream& in,Polynomial<T>& obj);//forward declaration
//template<class T>
//ostream& operator <<(ostream& out,const Polynomial<T>& obj);
//---------------------------------------------------------------------------
//class of Term that has two variables of type T

//--------------------------------------------------------
//class of polynomial that will contain a variable of term

template<class T>
class Polynomial{
    private:
        Term<T>* arrOfTerm;
        int num;
        Polynomial& Swap(Polynomial&);
    public:
        Polynomial();//will intialize a polynomial form coeffi 1 and exp1
        Polynomial(const Polynomial& obj);//copy constructor
        Polynomial(const Term<T> a[],int size);//the poly will be init by an array of
        Polynomial(const T[],int,char);
        //term which contains coeffi and exp and size is the size of array of term
        Polynomial(string);//a poly will be given in string form
        ~Polynomial();

        // String, UnString
		string toString()const; //   --> added const
		void pasrePolynomial(string);

        //methods
        Term<T> XthTerm()const; //added
        void XthTerm(int x);//done
        void showXthTerm(int x)const;//done --> added const
        T evaluate();//done

        //input and display functions
            //void getInput();//done
            //static Polynomial input();//done
        void display(ostream& out=std::cout)const;//done
        int NumOfTerms()const{return num;}

        //operators  =, +=, -=, +, -, ==, !=, <<, []
        Polynomial& operator = (const Polynomial& obj);//done
        Polynomial& operator +=(const Polynomial& obj);
        Polynomial& operator -=(const Polynomial& obj);
            //Polynomial& operator *=(const Polynomial& obj);
        Polynomial operator + (const Polynomial&);
        Polynomial operator - (const Polynomial&);
        bool operator ==(const Polynomial& obj)const;//done
        bool operator !=(const Polynomial& obj)const;//done
            //template<class N> friend istream& operator >> (istream& in,Polynomial<N>& obj);//done
        template<class N> friend ostream& operator << (ostream& out,const Polynomial<N>& obj);//done
        const Term<T> & operator [] (int x)const;//void operator [](int index);//done
        Term<T> & operator[] (int x); //new added
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
template<class T>
Polynomial<T> & Polynomial<T>::Swap(Polynomial<T> &p){
    if (this == &p)return *this;
    Term<T> *t=arrOfTerm;
    arrOfTerm=p.arrOfTerm;
    p.arrOfTerm=t;
    int n=num;
    num=p.num;
    p.num=n;
    return *this;
}

//----Costructors----
template<class T>
Polynomial<T>::Polynomial()
{
    arrOfTerm = new Term<T>;
    num=1;
}
template<class T>
Polynomial<T>::Polynomial(const Polynomial<T>& obj)
{
    /*int size;
    size = obj.arrOfTerm[0].getIndex();
    delete []arrOfTerm;
    arrOfTerm = new Term<T>[size];
    for(int i =0; i < size;i++)
    {
        arrOfTerm[i].setCoeffi( obj.arrOfTerm[i].getCoeffi());
        arrOfTerm[i].setExp(obj.arrOfTerm[i].getExp());
        arrOfTerm[i].setIndex(i+1);
    }*/
    num=obj.NumOfTerms();
    arrOfTerm=new Term<T>[num];
    for(int i=0;i<num;++i)
        arrOfTerm[i]=obj[i];
}

/*template<class T>
Polynomial<T>::Polynomial(T coefficient [], int order)
{
    delete []arrOfTerm;
    int pow = order;
    arrOfTerm = new Term<T>[order];
    for(int i = 0 ; i < order; i++,pow--)
    {
        arrOfTerm[i].setCoeffi( coefficient[i]);
        arrOfTerm[i].setExp(pow);
        arrOfTerm[i].setIndex(order);
    }
}*/

template<class T>
Polynomial<T>::Polynomial(const Term<T> a[],int size){
    if(size<1)num=1;
    else num=size;
    arrOfTerm=new Term<T>[num];
    for(int i=0;i<num;++i)
        arrOfTerm[i]=a[i];
}

template<class T>
Polynomial<T>::Polynomial(const T a[],int n,char var){
    if(n<=0)num=1;
    else num=n;
    arrOfTerm=new Term<T>[num];
    for(int i=0;i<num;++i){
        arrOfTerm[i].Coeff(a[i]);
        arrOfTerm[i].Exp(num-i-1);
        arrOfTerm[i].Variable(var);
    }
}

template<class T>
Polynomial<T>::~Polynomial()
{
    delete []arrOfTerm;
}

//input and display functions
template<class T>
Polynomial<T> Polynomial<T>::input()
{
    Polynomial obj;
    obj.getInput();
    return obj;
}
template<class T>
void Polynomial<T>::getInput()
{
    delete []arrOfTerm;
    T coefficient;
    T exponent;
    int noOfTerms;
    cout << "Enter how many terms you want to enter : ";
    cin >> noOfTerms;
    arrOfTerm = new Term<T>[noOfTerms];
    for(int i =0 ; i < noOfTerms;i++)
    {
        cout <<"Enter the coefficient of term  " <<i << "  : ";
        cin >>coefficient;
        cout <<"Enter  the  exponenet of term  " <<i << "  : ";
        cin >>exponent;
        arrOfTerm[i].setExp(exponent);
        arrOfTerm[i].setCoeffi(coefficient);
        arrOfTerm[i].setIndex(noOfTerms);
    }
}
template<class T>
void Polynomial<T>::display(ostream& out)const{
    for(int i =0 ; i < num ; i++)
        out << arrOfTerm[i]<<" ";
}
//operators
template<class T>
ostream& operator<<(ostream& out,const Polynomial<T>& obj)
{
    obj.display(out);
    return out;
}
template<class T>
istream& operator>>(istream& in , Polynomial<T>& obj)
{
    obj.getInput(in);
    return in;
}
template<class T>
Polynomial<T> & Polynomial<T>::operator =(const Polynomial<T> & obj)
{
    /*int size;
    size = obj.arrOfTerm[0].getIndex();
    delete []arrOfTerm;
    arrOfTerm = new Term<T>[size];
    for(int i =0; i < size;i++)
    {
        arrOfTerm[i].setCoeffi( obj.arrOfTerm[i].getCoeffi());
        arrOfTerm[i].setExp(obj.arrOfTerm[i].getExp());
        arrOfTerm[i].setIndex(size);
    }*/
    if(this == &obj)return *this; //control self-assignment
    delete [] arrOfTerm;
    num=obj.NumOfTerms();
    arrOfTerm=new Term<T>[num];
    for(int i=0;i<num;++i)
        arrOfTerm[i]=obj[i];
    return *this;
}

template<class T>
bool Polynomial<T>::operator ==(const Polynomial<T>& obj)const
{
    /*if(arrOfTerm[0].getIndex()==obj.arrOfTerm[0].getIndex())
    {
        for(int i =0  ; i < arrOfTerm[0].getIndex();i++)
        {
            if(obj.arrOfTerm[i].getCoeffi()!=arrOfTerm[i].getCoeffi()
               || obj.arrOfTerm[i].getExp()!=arrOfTerm[i].getExp())
            {
                return 0;
            }
        }
    }
    return 1;*/
    if (this == &obj)return true;
    if(num!= obj.NumOfTerm())return false;
    for(int i=0;i<num;++i)
        if(arrOfTerm[i]!=obj[i])return false;
    return true;
}
template<class T>
bool Polynomial<T>::operator !=(const Polynomial<T>& obj)const
{
    /*if(arrOfTerm[0].getIndex()==obj.arrOfTerm[0].getIndex()){
        for(int i =0  ; i < arrOfTerm[0].getIndex();i++){
            if(obj.arrOfTerm[i].getCoeffi()!=arrOfTerm[i].getCoeffi()
               || obj.arrOfTerm[i].getExp()!=arrOfTerm[i].getExp())
                return 1;
        }
    }
    return 0;
    */
    return !(*this==obj);
}

template<class T>
const Term<T> & Polynomial<T>::operator[](int i)const{
    const Term<T> *t=arrOfTerm;
    if(i<0 || i>=num)return *t;
    return *(t+i);
}

template<class T>
Term<T> & Polynomial<T>::operator[](int i){
    Term<T> *t=arrOfTerm;
    if(i<0 || i>=num)return *t;
    return *(t+i);
}

template<class T>
Polynomial<T> & Polynomial<T>::operator+=(const Polynomial<T>& obj)
{
    /*Polynomial a;
    delete []a.arrOfTerm;
    int newSize;
    int noOfSimilarTerms;
    for(int i =0 ;  i< arrOfTerm[0].getIndex();i++)
    {
        for(int j =0; j<obj.arrOfTerm[0].getIndex();j++)
        {
            if(arrOfTerm[i].getExp() == obj.arrOfTerm[j].getExp())
            {
                noOfSimilarTerms++;
            }
        }
    }
    newSize = (obj.arrOfTerm[0].getIndex() + arrOfTerm[0].getIndex()) - noOfSimilarTerms;
    a.arrOfTerm = new Term<T>[newSize];
    for(int i =0 ;  i< arrOfTerm[0].getIndex();i++)
    {
        for(int j =0; j<obj.arrOfTerm[0].getIndex();j++)
        {
            if(arrOfTerm[i].getExp() == obj.arrOfTerm[j].getExp())
            {
               a.arrOfTerm[i].setCoeffi(arrOfTerm[i].getCoeffi()+obj.arrOfTerm[j].getCoeffi());
            }
        }
    }*/
    Term<T> t[num+obj.num];
    int n=0;
    for(n=0;n<num;++n)
        t[n]=arrOfTerm[n];
    for(int i=0;i<obj.num;++i){
        int j=0;
        for(j=0;j<n;++j)
            if(t[j].Same(obj[i],EXP||VAR))break;
        if(j==n)t[n++]=obj[i];
        else t[j]+=obj[i];
    }
    Polynomial<T> p(t,n);
    p.Swap(*this);
    return *this;
}

template<class T>
Polynomial<T> & Polynomial<T>::operator-=(const Polynomial<T>& obj){
    Term<T> t[num+obj.num];
    int n=0;
    for(n=0;n<num;++n)
        t[n]=arrOfTerm[n];
    for(int i=0;i<obj.num;++i){
        int j=0;
        for(j=0;j<n;++j)
            if(t[j].Same(obj[i],EXP||VAR))break;
        if(j==n)t[n++]=obj[i];
        else t[j]-=obj[i];
    }
    Polynomial<T> p(t,n);
    p.Swap(*this);
    return *this;
}

template<class T>
Polynomial<T> Polynomial<T>::operator + (const Polynomial<T> & obj){
    Polynomial<T> a(*this);
    a+=obj;
    return a;
}

template<class T>
Polynomial<T> Polynomial<T>::operator - (const Polynomial<T> & obj){
    Polynomial<T> a(*this);
    a-=obj;
    return a;
}

#endif // POLYNOMIAL_H_INCLUDED

//---------------- main.cpp ----------------
#include <iostream>
#include "Polynomial.h"

using namespace std;

int main()
{
    //checking addition operator
    int n=3;
    int a[3] ={2,2,2};
    Polynomial<int> p(a,3,'x');
    Polynomial<int> q(a,3,'x');
    Polynomial<int> r(a,3,'z');
    cout<<p<<q<<r;
    //cin >>p;
    //cin >>q;
    r += p+q;
    cout << r;
    return 0;
}
Hellooo
mahinkhan...!

yaar apni assignments to khud solve kia kro..
agar help chahye ho to sirf utna hi poocho jitni zroort ho.
poori ki poori assignment upload ni krty...
wesy hui ya nhi????
me ne bhi karni the...
Topic archived. No new replies allowed.
Pages: 12