Data Member of type char

This is a discussion on Data Member of type char within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm trying to declare a data member of a class as char (array type) and then want to ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    Data Member of type char

    Hi all,

    I'm trying to declare a data member of a class as char (array type) and then want to use it outside the class as well. I may use it by getter function but 'm not declaring it properly, I think. Any help, please?


    Code:
    class color
    {
     private:
             int code;
             char color[25];
     public:
            color()
            {
                   code=0;
                   color[0]='\0';
            }
    
            void setCd(int cd)
            {  code = cd;  }
            
            int getCd()
            {  return code;  }
            
            void setCl(char cl[])
            {  color[] = cl[];  }
            
            char getCl()
            {  return color;  }
            
            void input()
            {  
               int cod=0; char clr[25];
               cout << "Code: ";  cin >> cod;
               cout << "Color: ";  cin >> clr;
               setCd(cod); setCl(clr[]);
            }
          
            ~color()
            {  }
    };

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    To return a character array, you have to use char* and return a pointer to the first element (just returning the array variable will work fine as you are already doing).

    Your set function is wrong though. You cannot assign one array to another with operator=.

    What you should be using is the C++ string class. It will work as you would expect it to, like built-in types int, double, etc. Just #include <string> and define your variable as string color; and you won't have to worry about arrays at all.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    Thanks for the reply.

    Ok, I may use strings. But I want to know how to do that with character type. Now, see, If I use char*, like

    Code:
    class Colors
    {
     private:
             int code;
             char* color;
     public:
            Colors()
            {
                   code=0;
                   color=NULL;
            }
    
            void setCd(int cd)
            {  code = cd;  }
            
            int getCd()
            {  return code;  }
            
            void setCl(char* cl)
            {  color = cl;  }
            
            char* getCl()
            {  return color;  }
            
            void input()
            {  
               int cod=0; char clr[25];
               cout << "Code: ";  cin >> cod;
               cout << "Color: ";  cin >> clr;
               setCd(cod); setCl(clr);
            }
          
            ~Colors()
            {  }
    };
    And try to run the program as..

    Code:
    int main()
    {
        Colors c1, c2;
        
        cout << "Object1\n";
        c1.input();
        cout << "\n\nObject2\n";
        c2.input();
    
        cout << "\nColor Code for Object1: " << c1.getCd() ;
        cout << "\nColor for Object1: " << c1.getCl() ;
        cout << "\nColor Code for Object2: " << c2.getCd() ;
        cout << "\nColor for Object2: " << c2.getCl() ;    
        
        getch();
        return 0;
    }
    It don't return the exact values for COLORS as input by the user. That's why I was looking for array, if it may return the exact one, but couldn't do properly.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    With character arrays, there are two different ways you can go. You can declare a statically sized array like you did in your first post where you set the size when you declare the array. You can also use a dynamically sized array where your variable is just a char* and you allocate the array with new [] (and delete it with delete []).

    For simplicity, I would use the first option. So from your original piece of code, instead of changing char color[25] to char* color, just leave that there. Only change the return type of the get function to char*.

    To fix your set function in either case, you need to use strcpy to copy the string, not operator=. It just won't work as you want it to without that function.

    If you decide to stick with the char* variable and dynamic sizing, then you have to add code to allocate space for the array with new [] and deallocate it with delete [] (probably in the constructor and destructor). You could also add extra code in the set function to allocate space when the string is set. You would also need to add a copy constructor and copy assignment operator to your class. This is a lot of extra work that can easily be done wrong, which is why C++ string should be used. Using a constant sized character array like in your first post would also avoid a lot of these headaches.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Code:
            void setCl(char* cl)
            {  color = cl;  }
            
            char* getCl()
            {  return color;  }
    This is REALLY BAD C++ code.
    1. Class member variables should not point to stuff outside of the class.
    2. The class should not return pointers to it's innards, especially non-const pointers.
    That breaks the whole OO purpose of C++ to encapsulate the data behind an interface.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That reminds me. When I said your get function should return a char*, I should have said const char*.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    Quote Originally Posted by Salem
    Code:
            void setCl(char* cl)
            {  color = cl;  }
            
            char* getCl()
            {  return color;  }
    This is REALLY BAD C++ code.
    1. Class member variables should not point to stuff outside of the class.
    2. The class should not return pointers to it's innards, especially non-const pointers.
    That breaks the whole OO purpose of C++ to encapsulate the data behind an interface.
    Sorry Sir. I'm new to OOP and confused about different things, as well. Besides this confusion, pointers weren't my first preference, as evident from my first post.

    BTW, thanks for the guidance.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    Quote Originally Posted by Daved
    For simplicity, I would use the first option. So from your original piece of code, instead of changing char color[25] to char* color, just leave that there. Only change the return type of the get function to char*.

    To fix your set function in either case, you need to use strcpy to copy the string, not operator=. It just won't work as you want it to without that function.

    Thanks a lot. It worked really well with my code. See, if 'm wrong at any point, by any aspect, let me know please.

    Code:
    class Colors
    {
     private:
             int code;
             char color[25];
     public:
            Colors()
            {
                   code=0;
                   color[0]='\0';
            }
    
            void setCd(int cd)
            {  code = cd;  }
            
            int getCd()
            {  return code;  }
            
            void setCl(char cl[])
            {  strcpy(color,cl);  }
            
            char* getCl()
            {  return color;  }
            
            void input()
            {  
               int cod=0; char clr[25];
               cout << "Code: ";  cin >> cod;
               cout << "Color: ";  cin >> clr;
               setCd(cod); setCl(clr);
            }
          
            ~Colors()
            {  }
    };

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    A bit better, you're still returning a pointer to your class data.

    Slightly better
    Code:
            const char* getCl()
            {  return color;  }

    Better still
    Code:
            void getCl( char *answer)
            {  strcpy(answer,color);  }
    Further improvements could be made by
    - including some length checks to make sure your char arrays do not overflow.
    - using memory allocation in place of fixed-length arrays
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Further improvements could be made by using memory allocation in place of fixed-length arrays
    I specifically suggested not doing this because of the added complexity, although I guess now that a working version is done it could be attempted. However, if one really wanted to improve the class, switching to C++ strings is the obvious answer.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Yeah, std::string is the best.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    Thank you so much for all the guidance. ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21