proper way of using string array inside a class

class myclass{

string myarray[5];

myarray[0] = "one"; myarray[1] = "two"; myarray[2] = "three"; myarray[3] = "four";
myarray[4] = "five";

};

this works in main function but doesn't work in the class;

1. Why this methods doesn't work inside class but works in main function?
2. What is the proper way of using string array inside a class?
You need to put the code in one of the class's member functions, like in the constructor:

1
2
3
4
5
6
7
8
9
10
11
class myclass{

   string myarray[5];

public:

   myclass() {
      myarray[0] = "one"; myarray[1] = "two"; myarray[2] = "three"; myarray[3] = "four"; myarray[4] = "five";
   }

};


More about classes: http://www.cplusplus.com/doc/tutorial/classes/
Last edited on
seems like classes can't have regular c++ command statements(I don't know proper word)

classes can only have data members and methods.

Is it so???
Yes.
thanks programmerdog297 I got it, thankyou very much. :)
Topic archived. No new replies allowed.