Thread: Get/Set Functions

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    9

    Get/Set Functions

    Im trying to develop a set of classes that represent some basic GUI elements that are intractable using a mouse or a keyboard. I have a problem with 2 of my classes.

    *Label: labels are plain text controls. One should be able to change the text and color of a label through get/set functions.

    Code:
    class Label
    {
    private:
        char text;
        char color;
    
    public:
        Label();
    
        void SetText(int x);
        void SetColor(int x);
        int GetColor();
        int GetText();
        ~Label();
    };

    Code:
    Label::Label()
    {
    }
    
    int Label::GetText()
    {
    
        return text;
    }
    
    void Label::SetText(int x)
    {
        text = x;
    }
    
    int Label::GetColor()
    {
    
        return color;
    }
    
    void Label::SetColor(int x)
    {
        color = x;
    
    }
    
    
    
    Label::~Label()
    {
    }
    This is where Im at. I don't really understand how to develop the get/set functions so that Im able to change it.

    Other class I got a problem with is:

    *Imagebox: It is a simple obejct that can contain and draw an image inside it.

    Dont even know where to start with this one.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Why is your text field using an int for its getter and setter? Shouldn't it be a string? Shouldn't it be stored as std::string, rather than char?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    9
    Quote Originally Posted by Elkvis View Post
    Why is your text field using an int for its getter and setter? Shouldn't it be a string? Shouldn't it be stored as std::string, rather than char?
    Like this?

    private:
    std::string text;
    char color;

    void SetText(string x);

    string GetText();


    What about color? any thoughts?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Color can be an int. Colors are often represented as 24-bit or 32-bit values, with groups of 8-bits representing the red, green, blue, and alpha channels. You should look into passing string arguments by (const) reference, rather than by value, which you are doing here.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elkvis View Post
    Color can be an int. Colors are often represented as 24-bit or 32-bit values, with groups of 8-bits representing the red, green, blue, and alpha channels. You should look into passing string arguments by (const) reference, rather than by value, which you are doing here.
    I would definitely not recommend using a bare int for something like color. It would be better to wrap that int with a Color class. That way there is no need to care about the underlying format (whether its RGB, ARGB, whatever).

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    9
    Quote Originally Posted by kmdv View Post
    I would definitely not recommend using a bare int for something like color. It would be better to wrap that int with a Color class. That way there is no need to care about the underlying format (whether its RGB, ARGB, whatever).
    What if I change it like this? Im not 100% sure how I should initilize it in the constructor. And im not sure what to do with my "DrawString" class.

    Code:
    class Label
    {
    private:
        std::string text;
    
    
    public:
        Label();
        Label(string text,int x, int y);
    
    
    
        struct rgb
        {
            int r;
            int g;
            int b;
        };
    
        rgb textColor;
    
        void DrawString(string text, int x, int y);
        void SetText(string Ntext) { Ntext = text; }
        void SetColor(int R,int G, int B) { textColor.r = R; textColor.g = G; textColor.b = B; }
        rgb GetColor() { return textColor; };
        string GetText() { return text; }
        ~Label();
    };
    Code:
    Label::Label()
    {
    }
    
    Label::Label(string text,int x, int y)
    {
    
    }
    
    void Label::DrawString(string text, int x, int y)
    {
    
    }
    
    string Label::GetText() {return text;}
    
    
    void Label::SetText(string Ntext) { Ntext = text; }
    
    
    
    Label::rgb Label::GetColor() { return textColor; }
    
    
    void Label::SetColor(int R, int G, int B) { textColor.r = R; textColor.g = G; textColor.b = B; }
    Last edited by alifarlig; 03-17-2016 at 06:11 AM.

  7. #7
    Guest
    Guest
    It's probably sufficient to use a byte / char (that's 256 different values) for each color in your program.

    You can initialize class members in your constructor using initializer lists.

    You're on a good way, but there's definitely room for improvement, so keep at it

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by alifarlig View Post
    What if I change it like this? Im not 100% sure how I should initilize it in the constructor. And im not sure what to do with my "DrawString" class.

    Code:
    class Label
    {
    private:
        std::string text;
    
    
    public:
        Label();
        Label(string text,int x, int y);
    
    
    
        struct rgb
        {
            int r;
            int g;
            int b;
        };
    
        rgb textColor;
    
        void DrawString(string text, int x, int y);
        void SetText(string Ntext) { Ntext = text; }
        void SetColor(int R,int G, int B) { textColor.r = R; textColor.g = G; textColor.b = B; }
        rgb GetColor() { return textColor; };
        string GetText() { return text; }
        ~Label();
    };
    Code:
    Label::Label()
    {
    }
    
    Label::Label(string text,int x, int y)
    {
    
    }
    
    void Label::DrawString(string text, int x, int y)
    {
    
    }
    
    string Label::GetText() {return text;}
    
    
    void Label::SetText(string Ntext) { Ntext = text; }
    
    
    
    Label::rgb Label::GetColor() { return textColor; }
    
    
    void Label::SetColor(int R, int G, int B) { textColor.r = R; textColor.g = G; textColor.b = B; }
    You are heading in the right direction, but:
    - This type does not look like specific to Label only, so it should be moved outside of the label to be more reusable (you don't want to create new RGB type for each of your controls).
    - The name implies that it is stored in RGB. I would rename this class simply to Color.
    - I would use getters instead of exposing the fields directly. Something like internal color representation (floats vs RGB int vs ARGB int vs bytes) is something that might change in the future. Therefore, it is better to encapsulate it now.

    Code:
    class Color
    {
    public:
    
        explicit Color(unsigned int red, unsigned int green, unsigned int blue) :
            red(red),
            green(green),
            blue(blue)
        {
        }
        
        unsigned int getRed() const
        {
            return red;
        }
        
        unsigned int getGreen() const
        {
            return green;
        }
        
        unsigned int getBlue() const
        {
            return blue;
        }
            
    private:
        
        unsigned char red;
        unsigned char green;
        unsigned char blue;
    };
    Also, your label's SetColor() could be just SetColor(Color color) instead of SetColor(r, g, b).
    Last edited by kmdv; 03-17-2016 at 07:31 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-27-2013, 06:43 PM
  2. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  3. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM