Question about design class..

This is a discussion on Question about design class.. within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a question about how to design a class. I am really confused about how objects interact with ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    Question about design class..

    Hi, I have a question about how to design a class.
    I am really confused about how objects interact with others.
    I know most of the syntax of c++, polymorphism, inhertiance, template as well.
    But like others said, knowing syntax do not automatically make u a good programmer.

    For eg, if after analysis, I choose to define 10 user define type class A, class B, class C, ..., Class J.

    Is it normal if I instance all this objects globally?
    That means all if method of A wants to communicate B, it can just directly use global instance b1. Is this acceptable in OOP design?

    If I use composition, for example:
    Code:
    class B {
    public:
        void operate() {dosomething;}
    private:
        _data of B;
    }
    class A {
    public:
       void method1() {_pb->operate();} 
    private:
       B* _pb;
    };
    Sometimes I confront of situation tat operate() method of B needs to know some data of A in order to operate.

    But it can't not.

    Then a solution I first think of is to add friendship of class A to B;
    Seems a really bad idea.
    Then I will try to drag B* _pb out of class A
    And instance A a1, B b1, globally.
    Code:
    void A::method1() {
       b1.operate();  // Call global b1 instance
    }
    
    void A::method1() {
       a1.methodxxx() // may need to call a1,too
    }
    If I confront this. How should I resolve this condition?
    If the compsition relation is much larger;
    like

    Code:
    class D {
    public:
        void methodD()  { need to know some information about class A ??}  // what should I do?
    }
    class C {
    public:
         void methodC() { _pd->methodD();}
    private:
         D* _pd;
    }
    class B {
    public:
        void methodB() {_pc->methodC();}
    private:
        C* _pc;
    }
    class A {
    public:
       void methodA() {_pb->methodB();} 
    private:
       B* _pb;
    };
    Does that mean I need to redesign my class?
    Last edited by ovid; 03-16-2010 at 11:44 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ovid View Post
    Hi, I have a question about how to design a class.
    I am really confused about how objects interact with others.
    I know most of the syntax of c++, polymorphism, inhertiance, template as well.
    But like others said, knowing syntax do not automatically make u a good programmer.
    No, but practice will. You need to take your knowledge of syntax and start writing programs. If you are looking for a way to insure that, without practice, you can acquire/be given enough knowledge to ensure that "everything will be okay" -- no one is going to make that promise. You will make mistakes and have to rethink things. Having to do that will probably help to prevent repeating the mistakes, but I think "the heat of the moment" in programming is such that until you learn a lesson, you are unlikely to see it coming. You will do what you think is the best thing, even if it turns out to be wrong.

    Code:
    void A::method1() {
       b1.operate();  // Call global b1 instance
    }
    That will work if object/instance b1 is declared before you even define class A. Since that is not a good plan, here's a better idea:

    Code:
    void A::method1(B &b1) {
       b1.operate();  // Call global b1 instance
    }
    Now, you must supply the instance, which must be possible since you will have defined b1before you call A::method1.

    Nb., you cannot have method prototypes that explicitly refer to B objects in A, if you also need method prototypes that refer to A objects in B. You can get around this with templates or void pointers.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ovid View Post
    Does that mean I need to redesign my class?
    Yeah, a little bit, this is the situation where you will need to use void pointers or templates.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Thx for reply.
    this is the situation where you will need to use void pointers or templates..
    I don't quite get it.
    The class I design I did think they have somewhat "has-A" relationship in it.
    Somehow maybe there exists method of inner objects want to use the data member of outside.
    I don't know how to use it.
    Or does this situation happen imply the class DEFINETELY need to redesign?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here is your problem:
    Code:
    class D {
    public:
        void methodD()  { need to know some information about class A ??}  // what should I do?
    }
    You cannot refer to class A until it is defined. So if class A must also explicitly refer to objects of class D, you are stuck.

    However, you do not need to use the entire definition. You can just the "forward declarations" for each class. So:
    Code:
    void A::method1 (D &obj);
    can be replaced with:
    Code:
    template<typename dtype> void A::method1 (dtype &obj);
    That is all that needs to happen in the forward declaration. In the definition you can use D specific stuff, eg:
    Code:
    template<typename dtype> void A::method1 (dtype &obj) {
         obj.someclassDmethod();
    In fact, class D does not have to be defined before that definition either -- it just needs to be defined before you compile the code, obviously, if you are actually going to call A::method1() somewhere submitting an instance of a D object.

    ps. I was wrong to say you could do this with void pointers -- only in C, not C++.
    Last edited by MK27; 03-16-2010 at 01:26 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Why not just split the definition/implementation and forward-declare A? Problem solved.
    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.

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. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. class and pointer question
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2006, 10:00 AM
  4. question about .net to 6.0 change causing errors
    By jverkoey in forum C++ Programming
    Replies: 17
    Last Post: 03-23-2004, 09:45 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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