I'm a little confused about the teacher solutions to this problem set below.
I thought when you declare a pointer variable and then set to an array you do something like:
char *myPointer;
And then maybe in the constructor you do something like
myPointer = new char[20];
why declare char *symbol[20] as a class member variable? What does that mean exactly? If it's a static array, why not just declare char symbol[20]; ?
Question and Solution
Assume a stock class has symbol (char *), cost (int) and shares (int) as private members. Write the minimum stock class declarations that supports stock s2(“APPL”, 209, 7), s3(“FB”, 80); // 7 shares of APPL at $209, 5 share of FB at $80 << hint: really minimum… no other functions… >> << hint: make it easier… assume symbol can have at most 20 characters…>>
class stock {
public:
stock(char * s, int c, int sh=5);
private:
char * symbol[20];
int cost;
int shares;
}
Write the implementation functions for the above declaration
stock::stock(char *s, int c, int sh) {
strcpy(symbol, s); // or other means of deep copy, no point if no deep copy
cost = c;
shares = s;
}
`````````````````````
char* symbol[20]would be an array of twenty pointers. Not what you want in this scenario.char*, not an array ofchar. However the description sounds like that's not what the teacher actually wants. This is not a question to ask us, though. You should ask your teacher.