How do you write a default constructor? I'm struggling with it :(
I need to use a default constructor that will initialize the data members:
- salesPerson to “Unknown”
- noOfWeek to 13
- amount to point to an array of thirteen 0.00s.
This is my weeklysales class
1 2 3 4 5
class WeeklySales {
char* salesPerson;
double* amount; // Pointer to an array
int noOfWeek; // Size of the array
};
Note that amount is a pointer, so you will need to give it the address of a double, possibly via dynamic allocation. If salesPerson is ever intended to be changed, you'll want to do the same for it.
Your code would be fine for non-pointer variables, so you problems have nothing to do with the concept of writing a constructor itself (expect perhaps you'll want to use an initializer list). Look up how to use pointers, as that's where you're problem lies.
but there is a red curly underline error message underneath [0.00] which says "expression must have integral or enum type".
Well, yes. What sense would it make to have a non-integral number for your array size? How could you possibly have, say, 3.57 elements in an array? Of course the size of the array has to be an integer!