Thread: operators and classes:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    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?

    Code:
     #ifndef __polynomial__
    #define __polynomial__
    
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    const int SIZE=10;
    
    class polynomial {
        
    private:
        int poly[SIZE];
        int x;
        int terms;
    
    public:
        explicit polynomial(int[]=0, int=0, int=0); //default constructor
        
        void setPolynomial();
        int getPolynomial();
        ~polynomial();
        polynomial operator+(int);
        polynomial operator-(int);
    };
    
    #endif
    Code:
     #include "polynomial.h"
    #include <cmath>
    #include <array>
    
    using namespace std;
    
    //constructor
    polynomial::polynomial()
    : x(0),
    terms(0)
    {
        
    }
    
    void setPolynomial()
    {
    
    
    }
    
    int getPolynomial()
    {
    
    
    }
    
    polynomial operator+(polynomial b)
    {
    
        
    }
    
    polynomial operator-(polynomial a)
    {
        
    
    }
    polynomial::~polynomial()
    {
        
        
    }
    Last edited by jocdrew21; 10-20-2013 at 11:38 AM.

  2. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    how do it initialize
    Code:
    int poly[SIZE];
    I tried to use a loop but got another error...

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    polynomial(int[]=0, int=0, int=0)
    Code:
    polynomial()
    These don't look the same to me; you've got to define the functions that you said you were going to define in your class definition.

    And you would need a loop to initialize poly.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Don't think I quite get it...

    Code:
     #include "polynomial.h"
    #include <cmath>
    #include <array>
    
    using namespace std;
    
    //constructor
    polynomial::polynomial(int [], int z, int y)
    : x(z),
    terms(y)
    
    for(int i=0;i<=10,++i){
        poly[i]=0;
    }
    
    {
        
    }
    
    void setPolynomial()
    {
    
    
    }
    
    int getPolynomial()
    {
    
    
    }
    
    polynomial operator+(polynomial b)
    {
    
        
    }
    
    polynomial operator-(polynomial a)
    {
        
    
    }
    polynomial::~polynomial()
    {
        
        
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Presumably you want to use the array that was passed in to initialize terms? (You'd have to give it a name first, of course.)

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1.
    Code:
    #ifndef __polynomial__
    #define __polynomial__
    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

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by hk_mp5kpdw View Post
    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.
    And the class inside unique namespace
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by hk_mp5kpdw View Post
    1.
    Code:
    #ifndef __polynomial__
    #define __polynomial__
    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.
    Actually, all identifiers containing a double underscore are disallowed, not just leading. The other part is true however.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of classes with classes inside
    By questionmark in forum C++ Programming
    Replies: 3
    Last Post: 10-04-2013, 03:39 PM
  2. Conversions between base classes and derived classes
    By tharnier in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2011, 10:50 AM
  3. Classes access other classes local variables
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2010, 04:36 AM
  4. Operators of abstract classes
    By Thanuja91 in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2007, 05:30 AM
  5. Abstract classes and operators
    By ygfperson in forum C++ Programming
    Replies: 11
    Last Post: 06-10-2003, 10:50 PM