I need help with 2d arrays strings and classes. I've read through lots of tutorials including the one at this site and i dont get them. Please I need help.
I need help with 2d arrays strings and classes. I've read through lots of tutorials including the one at this site and i dont get them. Please I need help.
i need help on just classes
What exactly don't you understand?
-Prelude
My best code is written with the delete key.
why are they used for
Classes are used for grouping data and the related operations that work with that data. By creating a variable of a class, you have a single object which can manipulate its own set of unique data in the program and nothing else can look at or use that data without the object's permission.
-Prelude
My best code is written with the delete key.
this is a chapter from a book
its all about classes
its easy, and full of examples, hope it ll help u
Programming is a high logical enjoyable art for both programer and user !!
strings are confusing partly because of the variety of strings available. Are you trying to use an array of null terminated char array or an array of strings that are in themselves instances of some string class, like the string class in STL, or the CString class in VC++, or the TString class in BCB, or a user defined string class or whatever?
If you are using instances of a string class like this (uses STL string class)
string names[2];
then there may well be a number of methods and operators associated with the class. For example the assignment operator is overloaded for the STL string class allowing you to do this:
names[0] = "me";
names[1] = "you";
Each string class has it's own set of methods and operators so you can't assume that they are all alike, eventhough the string classes provided by the major compiler vendors are very similar, if not the same as, the STL string class.
(As a side note, if you are using the STL string class, I would also suggest you use the STL vector class rather than a primitive array, assuming you are "allowed" to do so.)
If, as I suspect, you are trying to use null terminated char arrays as the strings then you have a different problem. The first problem is to decide how you want to declare the structure you plan to use. All of the following can be used to declare an array of this type of string
char example1[][];
char * example2[];
char ** example3;
As with all arrays, the []s above all need to have a constant (variable or literal) listed within them. Likewise the *s all represent pointers.
In example1, the leftmost [] will indicate how many strings, and the rightmost will indicate what the maximum size of a given string can be to be "valid". (The maximum possible number of visible char of the string is one less than the value in the right most []).
Therefore you could do this:
char example1[2][10];
or this:
const maxNumber = 2;
const maxSize = 10;
char example1[maxNumber][maxSize];
Either way you get an array of two strings. The two strings can be referred to individually as example1[0] and example1[1]. The maximum possible size of each string is only 9 visible char, with the last char of the string being a null char.
Since the strings weren't initialzed above, you can assign a string to the two strings in example1. To do so you need to use strcpy(), like this::
//copy a string into example1[0];
strcpy(example1[0], "you");
strcpy(example1[1], example1[0]);
cout << example1[0] << " and " << exmaple1[1] << endl;
Remember to include string.h or cstring header file, depending on your compiler, as that/they are the file(s) that contain strcpy().
If you wanted to initialize the strings in example1 you would need to something like this:
char example1[2][10] = {"me", "you"};
To use example3 you would do something like this:
int num = 2;
int size = 10;
char ** example3;
example3 = new char * [num];
for(int i = 0; i < num; i++)
{
example3[i] = new char[size];
}
Assuming I haven't screwed anything up, you can assign C_style strings to example3 using strcpy() like you did with example1.