overload

Write your question here.
plz can anyone re write this code without "const" after function and #define
#ifndef POLY_H

2.#define POLY_H

3.

4.#include <iostream>

5.#include <math.h>

6.#include <iostream>

7.#include <fstream>

8.

9.using namespace std;

10.

11.#define max(_a,_b) ((_a)>(_b) ? (_a) : (_b))

12.

13.

14.class Poly

15.{

16.private:

17. int degree;

18. double *coefficient;

19.public:

20. Poly(int deg, const double *coeff);

21. Poly(const Poly& p); // copy constructor

22. static Poly FromStream(istream& is);

23.

24. ~Poly();

25. double& operator[](int index);

26. int getDegree() const;

27. double getCoefficient(int index) const;

28. void setCoefficient(int index, double value);

29. double eval(double x) const;

30. void print(ostream& os = cout) const;

31. void printToFile(ofstream& ofs) const;

32.

33.

34. Poly operator+(const Poly& right) const;

35. Poly operator-(const Poly& right) const;

36. void operator=(const Poly& right); //assignment operator.

37.

38.

39.};

40.

41.#endif

42.

43.

44.

45.Poly::Poly(int deg, const double *coeff)

46. : degree(deg)

47.{

48. coefficient = NULL;

49. if (coeff)

50. {

51. coefficient = new double[deg + 1];

52. for (int i = 0; i < deg + 1; i++)

53. coefficient[i] = coeff[i];

54. }

55.}

56.

57.Poly::Poly(const Poly& p)

58.{

59. degree = p.getDegree();

60. coefficient = new double[degree + 1];

61. for (int i = 0; i < degree + 1; i++)

62. coefficient[i] = p.getCoefficient(i);

63.}

64.

65.Poly Poly::FromStream(istream& is)

66.{

67. int deg;

68. is >> deg;

69.

70. double *tmpCoeff = new double[deg + 1];

71. for (int i = 0; i < deg + 1; i++)

72. {

73. is >> tmpCoeff[i];

74. }

75.

76. Poly tmpPoly(deg, tmpCoeff);

77.

78. delete[] tmpCoeff;

79.

80. return tmpPoly;

81.}

82.

83.Poly::~Poly()

84.{

85. if (coefficient)

86. delete[] coefficient;

87.}

88.

89.double& Poly::operator[](int index)

90.{

91. return coefficient[index];

92.}

93.

94.int Poly::getDegree() const

95.{

96. return degree;

97.}

98.

99.double Poly::getCoefficient(int index) const

100.{

101. return coefficient[index];

102.}

103.

104.void Poly::setCoefficient(int index, double value)

105.{

106. coefficient[index] = value;

107.}

108.

109.double Poly::eval(double x) const

110.{

111. double result = 0.0;

112.

113. for (int i = 0; i < degree + 1; i++)

114. {

115. result += coefficient[i] * pow(x, i);

116. }

117.

118. return result;

119.}

120.

121.void Poly::print(ostream& os) const

122.{

123. double curCoeff;

124. for (int i = 0; i < degree + 1; i++)

125. {

126. curCoeff = coefficient[i];

127. if (curCoeff != 0.0)

128. {

129. if (i> 0 && curCoeff > 0.0)

130. os << "+";

131.

132. if (i == 0 || curCoeff != 1.0)

133. {

134. os << curCoeff;

135. }

136.

137. if (i > 0)

138. os << "X^" << i;

139. }

140. }

141. os << endl;

142.}

143.void Poly::printToFile(ofstream& ofs) const

144.{

145. ofs << degree << " ";

146. for (int i = 0; i < degree + 1; i++)

147. {

148. ofs << coefficient[i] << " ";

149. }

150. ofs << endl;

151.}

152.

153.Poly Poly::operator+(const Poly& right) const

154.{

155. int maxDegree = max(degree, right.getDegree());

156. double *mergedValues = new double[maxDegree + 1];

157. for (int i = 0; i < maxDegree + 1; i++)

158. {

159. mergedValues[i] = getCoefficient(i) + right.getCoefficient(i);

160. }

161.

162. Poly tmp(maxDegree, mergedValues);

163.

164. delete[] mergedValues;

165.

166. return tmp;

167.}

168.

169.Poly Poly::operator-(const Poly& right) const

170.{

171. int maxDegree = max(degree, right.getDegree());

172. double *mergedValues = new double[maxDegree + 1];

173. for (int i = 0; i < maxDegree + 1; i++)

174. {

175. mergedValues[i] = getCoefficient(i) - right.getCoefficient(i);

176. }

177.

178. Poly tmp(maxDegree, mergedValues);

179.

180. delete[] mergedValues;

181.

182. return tmp;

183.}

184.

185.void Poly::operator=(const Poly& right)

186.{

187. if (coefficient)

188. delete[] coefficient;

189.

190. degree = right.getDegree();

191. coefficient = new double[degree + 1];

192. for (int i= 0; i < degree + 1; i++)

193. coefficient[i] = right.getCoefficient(i);

194.}

195.

196.

197.

198.

199.ostream& operator << (ostream& os, const Poly& right)

200.{

201. right.print(os);

202. return os;

203.}

204.istream& operator >> (istream& is, Poly& right)

205.{

206. Poly cur = Poly::FromStream(is);

207. right = cur;

208. return is;

209.}

210.

211.int main()

212.{

213. double p1Coeff[] = { -1, 0, 2, -5 };

214. Poly p1(3, p1Coeff);

215.

216. double p2Coeff[] = { 2, 4, 3, 2, 1 };

217. Poly p2(4, p2Coeff);

218.

219. cout << "Result p1(x=2): " << p1.eval(2) << endl;

220. p1.print();

221.

222. cout << "Result p2(x=1): " << p2.eval(1) << endl;

223. p2.print();

224.

225. Poly p3 = p1 + p2;

226. cout << "Result p3(x=1): " << p3.eval(1) << endl;

227. p3.print();

228. cout << p3 << endl;

229.

230. cout << "----------" << endl;

231. Poly p4(0, 0);

232. cin >> p4;

233. cout << p4 << endl;

234. /*

235. // File Testing.

236. ofstream outFile;

237. outFile.open("mydata.txt");

238. p4.printToFile(outFile);

239. outFile.close();

240.

241. ifstream inFile;

242. inFile.open("mydata.txt");

243. Poly p5(0, 0);

244. p5 = Poly::FromStream(inFile);

245. inFile.close();

246. p5.print();

247. */

248. return 0;

249.}

250.

The code would be more readable (and compact) if you would have used code tags. See http://www.cplusplus.com/articles/jEywvCM9/

Which const and why?

There is std::max in the library.
Here is the code in tags and indented. See comments below.
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
#ifndef POLY_H
#define POLY_H

#include <iostream>
#include <math.h>
#include <iostream>
#include <fstream>

using namespace std;

#define max(_a,_b) ((_a)>(_b) ? (_a) : (_b))


class Poly
{
  private:
    int degree;
    double *coefficient;
  public:
        Poly(int deg, const double *coeff);
        Poly(const Poly & p);   // copy constructor
    static Poly FromStream(istream & is);

       ~Poly();
    double &operator[] (int index);
    int getDegree() const;
    double getCoefficient(int index) const;
    void setCoefficient(int index, double value);
    double eval(double x) const;
    void print(ostream & os = cout) const;
    void printToFile(ofstream & ofs) const;


    Poly operator+(const Poly & right) const;
    Poly operator-(const Poly & right) const;
    void operator=(const Poly & right); // assignment operator.


};

#endif



Poly::Poly(int deg, const double *coeff) :
    degree(deg)
{
    coefficient = NULL;
    if (coeff) {
        coefficient = new double[deg + 1];
        for (int i = 0; i < deg + 1; i++)
            coefficient[i] = coeff[i];
    }
}

Poly::Poly(const Poly & p)
{
    degree = p.getDegree();
    coefficient = new double[degree + 1];
    for (int i = 0; i < degree + 1; i++)
        coefficient[i] = p.getCoefficient(i);
}

Poly
Poly::FromStream(istream & is)
{
    int deg;
    is >> deg;

    double *tmpCoeff = new double[deg + 1];
    for (int i = 0; i < deg + 1; i++) {
        is >> tmpCoeff[i];
    }

    Poly tmpPoly(deg, tmpCoeff);

    delete[]tmpCoeff;

    return tmpPoly;
}

Poly::~Poly()
{
    if (coefficient)
        delete[]coefficient;
}

double &
Poly::operator[] (int index)
{
    return coefficient[index];
}

int
Poly::getDegree() const
{
    return degree;
}

double
Poly::getCoefficient(int index) const
{
    return coefficient[index];
}

void
Poly::setCoefficient(int index, double value)
{
    coefficient[index] = value;
}

double
Poly::eval(double x) const
{
    double result = 0.0;

    for (int i = 0; i < degree + 1; i++) {
        result += coefficient[i] * pow(x, i);
    }

    return result;
}

void
Poly::print(ostream & os) const
{
    double curCoeff;
    for (int i = 0; i < degree + 1; i++) {
        curCoeff = coefficient[i];
        if (curCoeff != 0.0) {
            if (i > 0 && curCoeff > 0.0)
                os << "+";

            if (i == 0 || curCoeff != 1.0) {
                os << curCoeff;
            }

            if (i > 0)
                os << "X^" << i;
        }
    }
    os << endl;
}

void
Poly::printToFile(ofstream & ofs) const
{
    ofs << degree << " ";
    for (int i = 0; i < degree + 1; i++) {
        ofs << coefficient[i] << " ";
    }
    ofs << endl;
}

Poly
Poly::operator+(const Poly & right) const
{
    int maxDegree = max(degree, right.getDegree());
    double *mergedValues = new double[maxDegree + 1];
    for (int i = 0; i < maxDegree + 1; i++) {
        mergedValues[i] = getCoefficient(i) + right.getCoefficient(i);
    }

    Poly tmp(maxDegree, mergedValues);

    delete[]mergedValues;

    return tmp;
}

Poly
Poly::operator-(const Poly & right) const
{
    int maxDegree = max(degree, right.getDegree());
    double *mergedValues = new double[maxDegree + 1];
    for (int i = 0; i < maxDegree + 1; i++) {
        mergedValues[i] = getCoefficient(i) - right.getCoefficient(i);
    }

    Poly tmp(maxDegree, mergedValues);

    delete[]mergedValues;

    return tmp;
}

void
Poly::operator=(const Poly & right)
{
    if (coefficient)
        delete[]coefficient;

    degree = right.getDegree();
    coefficient = new double[degree + 1];
    for (int i = 0; i < degree + 1; i++)
        coefficient[i] = right.getCoefficient(i);
}




ostream & operator <<(ostream & os, const Poly & right)
{
    right.print(os);
    return os;
}

istream & operator >>(istream & is, Poly & right)
{
    Poly cur = Poly::FromStream(is);
    right = cur;
    return is;
}

int
main()
{
    double p1Coeff[] = { -1, 0, 2, -5 };
    Poly p1(3, p1Coeff);

    double p2Coeff[] = { 2, 4, 3, 2, 1 };
    Poly p2(4, p2Coeff);

    cout << "Result p1(x=2): " << p1.eval(2) << endl;
    p1.print();

    cout << "Result p2(x=1): " << p2.eval(1) << endl;
    p2.print();

    Poly p3 = p1 + p2;
    cout << "Result p3(x=1): " << p3.eval(1) << endl;
    p3.print();
    cout << p3 << endl;

    cout << "----------" << endl;
    Poly p4(0, 0);
    cin >> p4;
    cout << p4 << endl;
    /*
     * // File Testing. ofstream outFile; outFile.open("mydata.txt");
     * p4.printToFile(outFile); outFile.close();
     *
     * ifstream inFile; inFile.open("mydata.txt"); Poly p5(0, 0); p5 =
     * Poly::FromStream(inFile); inFile.close(); p5.print(); */
    return 0;
}


- Why do you want to get rid of the const in the functions? They all seem appropriate.
- The code is structured like it should be in 3 different files. Is it? For example, lines 1-41 look like they should be in poly.h
- A minor thing: the copy constructor could be implemented with the assignment operator after mimimal initialization:
1
2
3
4
5
Poly::Poly(const Poly & p) :
    coefficient = nullptr;  // so assignment operator will work
{
     *this = p;
}

- Your assignment operator has a very common bug: it won't work if you try to assign a Poly to itself. You will delete the coefficient vector and then try to copy it to itself. Your assignment operator should start with if (this == &right) return;
- If the polynomials don't have the same degree then oeprators + and - will access coefficients out of bounds.
- When defining operator+() and operator-(), I usually find that if you define += and -=, you can get + and - almost for free. Putting this case and the last one together you get:
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
Poly &
Poly::operator+=(const Poly & right)
{
    int maxDegree = max(degree, right.getDegree());
    double *mergedValues = new double[maxDegree + 1](); // initialize to 0.0
    for (int i=0; i<degree+1; ++i) {
        mergedValues[i] = getCoefficient(i);
    }
    for (int i=0; i<right.degree+1; ++i) {
        mergedValues[i] += right.getCoefficient(i);
    }

    delete[] coefficient;
    coefficient = mergedValues;
    degree = maxDegree;
    return *this;
}

Poly
Poly::operator+(const Poly & right) const
{
    Poly result(*this);
    result += right;
    return result;
}
dhayden wrote:
1
2
3
4
5
Poly::Poly(const Poly & p) :
    coefficient = nullptr;  // so assignment operator will work
{
     *this = p;
}
Have you been getting enough sleep recently? That's not how constructor initializer lists work ;)
Have you been getting enough sleep recently? That's not how constructor initializer lists work ;)

ahahahhahahahaha!!!!!

The answer is "no." And I was actually thinking as I was typing, "Hmm. I'm really tired and will probably screw this up."

Note to self: no more code when tired.

Thanks for pointing out my mistake.
Topic archived. No new replies allowed.