get/set

This is a discussion on get/set within the C++ Programming forums, part of the General Programming Boards category; Here is a real world scenario that may best answer your question. ABC Code: class Image { public: virtual void ...

  1. #16
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Here is a real world scenario that may best answer your question.

    ABC
    Code:
    class Image
    {
    public:
      virtual void *getPixels() = 0;
      virtual void setPixels(void *pxls);
      virtual size_t getWidth() const = 0;
      virtual size_t getHeight() const = 0;
      virtual void setWidth(size_t width) = 0;
      virtual void getHeight(size_t height) = 0;
    };
    Now you have the construct for what any Image would look like. From here out, all your derived classes may do things entirely different. For example, you may have a JPEGImage class which stores a JFIF header and has to decode data in order to getPixels(). Or you may have a BitmapImage that is just a fat array of pixels that are ready to be dumped as-is.

    Since you have a reliable interface that is consistent across the board, it does not matter how things are dealt with internally. Right? That is more what encapsulation is about rather than "hiding data" as some may say. Its not a matter of hiding so much as its a matter of I do not care how you store your pixels, I just need to see them so I can draw to the screen.
    Last edited by master5001; 09-26-2008 at 11:30 AM.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Master5001, that is an excellent example - we do not want to know how pixels are stored in the image, just a way to get the pixels out.

    --
    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. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why thank you Matt. I woke up with a little extra spring in my step this morning. Perhaps there is hope for me yet.

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    It is also useful for things like this:
    Code:
    class Circle{
      private:
        double radius;
      public:
        double getCircumference(){
          return PI*radius*2
        }
    }
    This can then later be changed to a variable that stores the circumference instead, without changing the API of the class.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #20
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    I think one of the main reasons I've shyed away from templates is because the syntax is just so freaking ugly.

    I simply cannot read that code.
    MSVC++ 6.0

  6. #21
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    To which code are you refering? Neither mine nor King Mir's were templates. Mine is an abstract base class and Mir's is an example of applying a principle.

    [Edit]Ah nevermind. You were refering to Elysia's template. Templates are useful. Simply using typedefs will entirely eliminate most of what you find visually unappealing about them. I suggest you learn how to read them though. Elysia's coding style is satisfactory and the code doesn't do anything whacky and hard to understand.[/edit]
    Last edited by master5001; 09-26-2008 at 12:20 PM.

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think one of the main reasons I've shyed away from
    >templates is because the syntax is just so freaking ugly.
    Templates don't scale well when it comes to complexity. Simple uses are simple, but advanced uses are painful.
    My best code is written with the delete key.

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

Similar Threads

  1. private/public get/set
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 05-04-2008, 12:49 AM
  2. Trivial: Get/Set + Function Order in Classes
    By Ashes999 in forum C++ Programming
    Replies: 10
    Last Post: 07-08-2003, 11:09 AM
  3. Elevator program
    By brianptodd in forum C++ Programming
    Replies: 14
    Last Post: 11-01-2002, 10:42 PM

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