some design questions

This is a discussion on some design questions within the C++ Programming forums, part of the General Programming Boards category; Hello Im unsure about next situations: Code: class test { public: int someint() { return m_someint; } private: int m_someint; ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    some design questions

    Hello

    Im unsure about next situations:

    Code:
    class test {
    public:
      int someint() { return m_someint; }
    private:
      int m_someint;
    };
    When I want to access m_someint from the test class I would just do m_someint and if I want to access it from another class I would do some_test_object.someint();

    Is this a 'good' way? What do you guys do in this situation?


    Another question:

    Suppose I have some class and I want to modify/change some data in it:

    Code:
    class someclass {
    private:
      int m_somedata;
    public:
      void set_somedata(int i) { m_somedata = i }
    };
    I have seen a lot of code that uses special method/function to change data this way:
    set_somedata(20);
    But I prefer accessing m_somedata directly (so I'd set class that wants to access data as a friend), because I dont like writing new function for each data object..

    What do you think about that?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's a balance between the ability to change the internal data representation, and avoiding to write functions.

    A function will hide your internal data type (so you can change it to be stored behind a pointer, in a different type, etc, and the code using this variable won't have to change), and you can also make checks to certify that the data is correct (e.g. not negative or too big, or whatever).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    This method of coding, in my opinion, makes code a lot easier to understand and manage. This seems to be especially true if you are working with other programmers.

    Code:
    class example {
    private:
        int m_someVariable;
    
    public:
        int  getSomeVariable() { return m_someVariable; }
        void setSomeVariable(int someVariable) { m_someVariable = someVariable; } 
    };
    Something like that would require more time creating the classes, but when you have a project that may require multiple people and/or a lot of changes in the future, it may just pay off for you.

    It may not be worth noting, but Java uses this style of member access everywhere and it makes coding much easier to read even when it's someone elses. Also, I believe VB .NET may have had something similar with it's class properties.
    Last edited by shoutatchickens; 04-02-2008 at 10:18 AM.

  4. #4
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    But isnt this a little weird:

    Code:
    class test {
    private:
      int a;
      int b;
      int c;
      int f;
      int d;
    
    public:
       void set_a();
       void set_b();
       void set_c();
       void set_f();
       void set_e();
      //..
    };
    Would you also use set/get functions if you call it from a local/parent class (class test in this case) ? That doesnt make much sense though.
    Last edited by l2u; 04-02-2008 at 10:19 AM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Well, tell you what:
    The best thing is to think whatfor you need. If it's a member part of a complex class whose value may change depending on factors, then it's better to make set/get functions, because the rest of the code using the class won't need to be rewritten if the internal workings of the class changes.
    But if this isn't a requirement, you can make it public.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It only makes sense to have set functions if your class needs a way for unrelated code to set those values individually.

    Don't add a set function just for the internal class to use unless it does something different than just set the variable.

    If you add a set function because external code needs to set those variables, and you end up with a bunch of them, it's ok (although if you have too many then you may be able to combine some in their own class).

  7. #7
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved View Post
    although if you have too many then you may be able to combine some in their own class.
    Can you be please more specific about that?


    Another question:

    If I decide to use set/get functions from now on, is it okay to define them in a header? Will that affect my application's size?

  8. #8
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    Quote Originally Posted by l2u View Post
    Would you also use set/get functions if you call it from a local/parent class (class test in this case) ? That doesnt make much sense though.
    Well, in the end it's really all about what you're comfortable with. This is the type of thing that really won't hurt your program in one way or another. If you are the only one that will be working on a project and you are more comfortable with using the variables then that's fine.

    And here's some examples of how it would be used in code just to get an idea of how it looks outside the class declaration:

    Code:
    person.setAge(21);
    if(person.getAge() >= 21) drink = true;
    
    // or
    
    person.age = 21;
    if(person.age >= 21) drink = true;
    As you can see, both ways will have the exact same result. It's just a question of what is easier to read. Something to consider in this is what if later you need to add more data to the age. Perhaps you want to store the birthday also. If you have to change the way age is stored, you will simply just change the getAge() and setAge() functions. However if you are using the second method, you may end up getting rid of the age variable and putting it into some data structure. Then you would have to go back and change all the instances of person.age.

    Hope that made sense

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by l2u View Post
    Would you also use set/get functions if you call it from a local/parent class (class test in this case) ? That doesnt make much sense though.
    If setting/getting some value is always accompanied by some other operation, even when the object itself does it, then the object should call the set/get function. If you are even remotely unsure whether you will add such code in the future, just make it easy and use the setter/getter function so that if it does change, everything stays correct.

    However, if your setter/getter functions do nothing but set/get the value of a variable, and nothing else, and you do not expect your implementation to change (perhaps because it's obvious, or incredibly simple), then it might be better to expose those variables as publics instead. But that's a dramatic decision to make, one you shouldn't make unless you're sure.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Can you be please more specific about that?

    For example, if you have a customer class that includes name, ID, street address, city, state, zip, country and other fields, you might realize that the combination of street address, city, state, zip and country are an address by themselves. So you create an address class that encapsulates that concept and have the customer class contain an address instance.

    Similarly, if you notice you have two copies of each of those values, one for mailing and one for billing, then you can combine them into address and have the customer class contain a mailing address variable and a billing address variable.

    It only makes sense to combine member variables into a new object if they logically combine to form an object. However, when you have lots of member variables you can often find such a case.

  11. #11
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    So lets say I have a class with some container member. Now I want to push_back element to this container from another class..

    It would be logicaly to make a new function for each thing I want to do with this container if I would have get/set methods for other things as well..

    For instance (set function in a customer class):
    Code:
    void add_customer_nickname(std::string &str) {
    vector.push_back(str);
    }
    ..or would you do it differently?

    There have been quite some good arguments why to use get/set methods I think, thats why I'm going to use them in a future.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> It would be logicaly to make a new function for each thing I want to do with this container
    Yes it would, as long as you are exposing things that it makes sense for external code to do.

    >> There have been quite some good arguments why to use get/set methods I think, thats why I'm going to use them in a future.

    I thought you were asking about using get and set functions when accessing member variables from inside the class?

  13. #13
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved View Post
    >> It would be logicaly to make a new function for each thing I want to do with this container
    Yes it would, as long as you are exposing things that it makes sense for external code to do.
    Have been thinking - doesnt in this case returning container's reference make more sense since container/s have all the methods already in place and cannot be modified?

    Quote Originally Posted by Daved View Post
    I thought you were asking about using get and set functions when accessing member variables from inside the class?
    From inside the class and from the other classes.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> From inside the class and from the other classes.

    Ok. A set function is usually better than making a member public when deciding how to expose the data to users of the class. From inside the class, if you already have the set function, then using it is fine and often makes sense. Creating a simple set function for use solely within the class makes little sense to me.

    >> doesnt in this case returning container's reference make more sense since container/s
    >> have all the methods already in place and cannot be modified?

    No, because you don't want the outside code to be able to have access to all those methods (in most cases). That's encapsulation. Your object is using a container to hold some data, and your class should decide how that data can be read and manipulated. If you expose the entire container, you no longer have any control over the data.

    Expose only those operations that make sense to the user of the class, and do so behind a sensible interface. Which container you use is an implementation issue and the user of the class should only program to the interface.

  15. #15
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks for help..

    I have one more question regarding this:

    If you have a class customer (for instance) which has a container std::vector<std::string> that stores customer's nicknames and you want to access it from other classes so that you can get the first nickname in the list, x-th name in the list and you want to be able to add nicknames to the list - how would you implement it?

    Considering whats written above I would do it this way:

    Code:
    class customer {
    private:
      std::vector <std::string> m_nicknames;
    
    public:
      std::string get_first_customer_nickname() { return m_nicknames[0];}
      std::string get_customer_nickname(int x) { return m_nicknames[x]; }
      void add_customer_nickname(std::string nickname) { m_nicknames.push_back(nickname); }
      int get_customer_nicknames() { return m_nicknames.size(); }
    };
    Does this look like a sensible implementation?
    What could be improved? Maybe using boost:: optional would be better for get_nickname functions (because list could be empty) ?

    Do you think its okay to write such functions (get_nickname) since list can be empty and I dont think it would really make much sense to check if the list is empty and throw after that.. In this case you should always check if (get_customer_nicknames()) is not 0 before calling them...

    What do you think about those long function names? On one side it is sensible so you can be sure what function does, on other side it doesnt 'look good'..

    Thanks for help!
    Last edited by l2u; 04-04-2008 at 11:20 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. some questions about design
    By jinhao in forum C++ Programming
    Replies: 7
    Last Post: 06-01-2005, 08:51 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  4. are you bored? (interiour design questions)
    By maes in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-04-2004, 03:51 AM

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