Thread: get/set

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Encapsulation.
    If you at any point in the future want to change how you set/get those variables, you're future proof with set/get (you don't need to change the client code that calls the class functions, only the class functions themselves).
    Otherwise you'll have to change the underlying code that calls the class to match the new implementation.
    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.

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    Quote Originally Posted by Elysia View Post
    Encapsulation.
    If you at any point in the future want to change how you set/get those variables, you're future proof with set/get (you don't need to change the client code that calls the class functions, only the class functions themselves).
    Otherwise you'll have to change the underlying code that calls the class to match the new implementation.
    hmmmm but do you actually practice this in real life as well?
    Just curious....

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Another side of this is validation and conversion, for example, let's say we have a graphics library that supports setting pixels to R, G, B colours, and we want to support 24 bpp and 16 bpp (5,6,5)

    We'd first make a base class for pixel colour, which is just there to define the interface. We derive from this class to make all the REAL implementations [this is just a trivial sample - a real example of how to do this would require quite a bit more code, and could probably be done partially using templates]
    Code:
    class BaseColor
    {
       public:
          BaseColor();
          virtual ~BaseColor();
          virtual SetColor(int r, int g, int b) = 0;
          virtual GetColor(int &r, int &g, int &b) = 0;
    };
    
    class Color24BPP: public BaseColor
    {
    private: 
       BYTE m_r;
       BYTE m_g;
       BYTE m_b;
    public:
         Color24BPP();
         ~Color24BPP();
         virtual SetColor(int r, int g, int b) 
         {
             // Clamp colours in 0..255 range.   We assume the number is positive. 
             m_r = min(255, r);
             m_g = min(255, g);
             m_b = min(255, b);
         }
         virtual GetColor(int &r, int &g, int &b) 
         {
             r = m_r;
             g = m_g;
             b = m_b;
         }
    };
    
    
    class Color16BPP: public BaseColor
    {
    private: 
       BYTE m_r;
       BYTE m_g;
       BYTE m_b;
    public:
         Color16BPP();
         ~Color16BPP();
         virtual SetColor(int r, int g, int b) 
         {
             // Clamp colours to 5, 6, 5 bits.   We assume the number is positive. 
             m_r = min(31, r >> 3);
             m_g = min(63, g >> 2);
             m_b = min(31, b >> 3);
         }
         virtual GetColor(int &r, int &g, int &b) 
         {
             // Make it back into 8 bit numbers. Or in the upper bits in the bottom to make 
             // some data in the bottom bits. 
             r = m_r << 3 | m_r >> 3;
             g = m_g << 2 | m_g >> 4;
             b = m_b << 3 | m_b >> 3;
         }
    };
    --
    Mats
    Last edited by matsp; 09-26-2008 at 09:24 AM. Reason: Fix bug.
    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.

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, 11:42 PM