Appropiate class interface design

This is a discussion on Appropiate class interface design within the C++ Programming forums, part of the General Programming Boards category; What is the preferred way to read values of class members from an other class? For example: If class ClassA ...

  1. #1
    C = C - 1
    Join Date
    Mar 2008
    Posts
    15

    Question Appropiate class interface design

    What is the preferred way to read values of class members from an other class? For example: If class ClassA contains variables a,b,c, and ClassB contains a method that prints the values of a,b and c, how should an instance of ClassB that declares one instance of ClassA classA_Inst as one of it's members access the values of a, b and c from classA_Inst?

    Code:
    #include <iostream>
    
    class ClassA
    {
    public:
        int a,b,c;
    }
    
    class ClassB
    {
    private:
        ClassA classA_Inst;
    public:
        void print_abc()
        {
            std::cout << classA_Inst.a << classA_Inst.b << classA_Inst.c << std::endl;
        }
    }
    
    .........
    ClassB classB_Inst;
    classB_Inst.print_abc();
    Or
    Code:
    #include <iostream>
    
    class ClassA
    {
    private:
        int a,b,c;
    public:
        int getA() return a;
        int getB() return b;
        int getC() return c;
    }
    
    class ClassB
    {
    private:
        ClassA classA_Inst;
    public:
        void print_abc()
        {
            std::cout << classA_Inst.getA() << classA_Inst.getB() << classA_Inst.getC() << std::endl;
        }
    }
    
    .........
    ClassB classB_Inst;
    classB_Inst.print_abc();
    Or
    Code:
    #include <iostream>
    
    class ClassB;
    
    class ClassA
    {
    private:
        int a,b,c;
    public:
        void getABC(ClassB* classB_ptr)
        {
            classB_ptr->copy_a = a;
            classB_ptr->copy_b = b;
            classB_ptr->copy_c = c;
        }
    }
    
    class ClassB
    {
    private:
        ClassA classA_Inst;
    public:
        int copy_a, copy_b, copy_c;
        void print_abc()
        {
            classA_Inst.getABC( this );
            std::cout << copy_a << copy_b << copy_c << std::endl;
        }
    }
    
    .........
    ClassB classB_Inst;
    classB_Inst.print_abc();
    Or have I missed something ? There must be some better ways to do that...

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,292
    There is no preferred way. There are many options and, depending on what your objects are doing, trade-offs between those options.

    If the only reason the access is needed is to print the values, a simple way would be for classA to provide a public function that prints them. Then the have classB's print function call classA's print function. The advantage of this is that it allows classA to be updated without touching classB in anyway way.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    27
    Well, all of the three approaches are valid. You can any one of them. But if you consider it from the design perspective. You must look into "Coupling & Cohesion". Your classes have dependency. So it is better to remove this dependency and increase the inter class dependency by putting the print() method in the same class as well.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    What is the preferred way to read values of class members from an other class?
    Normally this is done, as has been said, via public accessors or public 'getters'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Design
    By Dae in forum C++ Programming
    Replies: 0
    Last Post: 09-13-2009, 07:00 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  5. class design and operation overload
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2001, 09:49 PM

Tags for this Thread


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