Arrays in a class

Im having trouble using arrays in my class Polynomial. It's a question from the Deitel & Deitel book, but I find that most books explain functions first, and [italic]then[/italic] arrays, followed by classes, which makes it difficult to understand how they interact together.

Anyway, I've been trying to have two private arrays ( everything was going fine 'till I realised that I had to store an array of values )
private:
int coeffecients[];
int powers[];

to store data of the polynomial expressions. The overloaded istream takes in the expression e.g 3x^2 + 2x +7, ignores the irelevant bits and does its job, but I can't even compile. A pointer is no good to me in this case. I don't even want to pass the arrays, the overloaded operators can do the dirty work for me.

"Will sing Tom Jones for help with programming"
TenLeftFingers

Comments

  • : Im having trouble using arrays in my class Polynomial. It's a question from the Deitel & Deitel book, but I find that most books explain functions first, and [italic]then[/italic] arrays, followed by classes, which makes it difficult to understand how they interact together.
    :
    : Anyway, I've been trying to have two private arrays ( everything was going fine 'till I realised that I had to store an array of values )
    : private:
    : int coeffecients[];
    : int powers[];
    :
    : to store data of the polynomial expressions. The overloaded istream takes in the expression e.g 3x^2 + 2x +7, ignores the irelevant bits and does its job, but I can't even compile. A pointer is no good to me in this case. I don't even want to pass the arrays, the overloaded operators can do the dirty work for me.

    Huh?!

    What is the actual problem you're having?

    Cheers,
    Eric
  • Okay, Im very confused about this program I'm trying to write as the spec is very vague, but to make a long question short...

    would it be possible for me to have a class with and array in it, but each object's array need not be of the same size??
    That is, object1's array is of 3 elements, so is object2.

    objectTotal = object1 + object2;

    Now objectTotal's array may need to have more elements if types don't match.
    Am I making sense?
  • : would it be possible for me to have a class with and array in it, but each object's array need not be of the same size??

    Yes. You just dynamically allocate the array:
    [code=ffffff]
    [color=000080]class[/color] Array {
    [color=000000][b]public[/b][/color]:
    Array::Array([color=000080]size_t[/color] size)
    : array( [color=000000][b]new[/b][/color] [color=000080]int[/color][black][[/black]size] )
    , size(size)
    {}

    Array::~Array()
    { [color=000000][b]delete[/b][/color][] array; }

    [color=80a0b0][italic]// Create a new array of the appropriate size. Copy as much of the old[/italic][/color]
    [color=80a0b0][italic]// array as will fit in the new array, and delete the old.[/italic][/color]
    [color=000080]void[/color] resize ([color=000080]size_t[/color] newsize) {
    [color=000080]int[/color]* newarray = [color=000000][b]new[/b][/color] [color=000080]int[/color][newsize];
    memcpy(newarray, array, min(size,newsize));
    [color=000000][b]delete[/b][/color][] array;
    array = newarray;
    size = newsize;
    }

    [color=000080]int[/color]& [color=000000][b]operator[/b][/color][]([color=000080]size_t[/color] i)
    { [color=000000][b]return[/b][/color] array[i]; }

    [color=000000][b]private[/b][/color]:
    [color=000080]int[/color]* array;
    [color=000080]size_t[/color] size;
    };
    [/code]
    Cheers,
    Eric

  • Okay, I have my pointer and array notation confused. Back to chapter 5 for a while. Thanks again Eric.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories