operators and classes:

This is a discussion on operators and classes: within the C++ Programming forums, part of the General Programming Boards category; I am getting an error on line 8, the constructor. I am just building the outline for my class and ...

operators and classes:

I am getting an error on line 8, the constructor. I am just building the outline for my class and I really hope after this program I understand operators. My class has really picked up with operators and polymorphism back to back. I am trying to get some practice in order to understand each subject fully.

My thought was to initialize the constructor and I am getting an error. Can I have a hint why?

Identifiers with 2 leading underscore characters (or a single leading underscore with a following uppercase character) are reserved for use by the particular implementation in question.

2.

Code:

#include <iostream>
#include <cmath>

Your header does not make use of anything requiring these two headers and should therefore not reference them.

3.

Code:

using namespace std;

Headers should avoid the use of this wherever possible, at least when applied at file scope like you are doing. By including this statement in your header at file scope, you bring everything that might come after it in any of your header/source files into the global namespace which can cause problems with collisions. Since you include the polynomial.h header file in your source file before also including the cmath and array headers, everything in those header files is now effectively in the global namespace.

4.

Code:

const int SIZE=10;

SIZE might be problematic as an identifier since it is a very common word. It is also more of a problem in that your use of this constant has been placed (again) at file scope and therefore is visible to everything that might come after it which could potentially cause issues. It might be better to either change the identifier (maybe call it POLY_SIZE or something like that) and/or put the constant within the class.

5. Be mindful of const correctness where possible and how you pass data around. You have parameters you are passing around by value where it is typically preferred to pass by const reference instead. Class member functions that do not alter the state of the class should be declared const, this is likely going to be the case with your "get" member function(s).

6.

Code:

int poly[SIZE];
...
for(int i=0;i<=10,++i){
poly[i]=0;
}

Valid array index values start at 0 and go up through SIZE-1. Not only should you be using the constant you've declared in this bit of code instead of relying on a magic number, but you need to change the <= to < or you will be accessing an invalid array position.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens

SIZE might be problematic as an identifier since it is a very common word. It is also more of a problem in that your use of this constant has been placed (again) at file scope and therefore is visible to everything that might come after it which could potentially cause issues. It might be better to either change the identifier (maybe call it POLY_SIZE or something like that) and/or put the constant within the class.