Thread: help with arrays,strings and classes

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    Question help with arrays,strings and classes

    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.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    my mistake

    i need help on just classes

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What exactly don't you understand?

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    5
    why are they used for

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

  6. #6
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    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 !!

  7. #7
    Unregistered
    Guest
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM