Another composition

Discussion in 'C++' started by fog, Apr 21, 2004.

  1. fog

    fog Guest

    class A has its children/derived classes B and C. class D is supposed to be
    contained inside both B and C.

    Is the code right for the above requirement?
    class A
    {
    D d;
    };

    class B : public A;
    class C : public A;

    Now should all public and protected members in D be accessible to all
    members of B and C?
    Is this a good design?
    Thanks for your help!
    fog, Apr 21, 2004
    #1
    1. Advertising

  2. "fog" <> wrote
    > class A has its children/derived classes B and C. class D is supposed to be
    > contained inside both B and C.
    >
    > Is the code right for the above requirement?
    > class A
    > {
    > D d;
    > };
    >
    > class B : public A;
    > class C : public A;
    >
    > Now should all public and protected members in D be accessible to all
    > members of B and C?
    > Is this a good design?


    Good design limits the visibility of the internals of a class as much as
    possible, even to its descendents. Therefore, no, exposing the internals of A
    would not be good design.

    Claudio Puviani
    Claudio Puviani, Apr 21, 2004
    #2
    1. Advertising

  3. "fog" <> wrote in message
    news:LGohc.5548$...
    > class A has its children/derived classes B and C. class D is supposed to

    be
    > contained inside both B and C.
    >
    > Is the code right for the above requirement?
    > class A
    > {
    > D d;
    > };
    >
    > class B : public A;
    > class C : public A;


    Seems OK to me.

    >
    > Now should all public and protected members in D be accessible to all
    > members of B and C?


    No they shouldn't. There's no inheritance of B from D or of C from D, so
    there is no automatic access to D's members from B or C.

    > Is this a good design?


    That depends entirely on what you are trying to do. Good design doesn't
    exist in the abstract.

    john
    John Harrison, Apr 21, 2004
    #3
  4. fog

    velthuijsen Guest

    > class A has its children/derived classes B and C. class D is supposed to be
    > contained inside both B and C.
    >
    > Is the code right for the above requirement?
    > class A
    > {
    > D d;
    > };
    >
    > class B : public A;
    > class C : public A;
    >
    > Now should all public and protected members in D be accessible to all
    > members of B and C?
    > Is this a good design?
    > Thanks for your help!


    Seeing that d is declared in the private block of A neither B nor C
    has access to it.
    velthuijsen, Apr 21, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Schutkeker

    Need Composition Editor Tutorial

    John Schutkeker, Oct 17, 2003, in forum: Java
    Replies:
    0
    Views:
    317
    John Schutkeker
    Oct 17, 2003
  2. Rick Osborn

    Aggregation vs. composition

    Rick Osborn, Jan 23, 2004, in forum: Java
    Replies:
    11
    Views:
    57,907
    amitg_amity
    Aug 31, 2009
  3. -
    Replies:
    1
    Views:
    472
    John C. Bollinger
    Apr 11, 2005
  4. -
    Replies:
    3
    Views:
    324
  5. Roedy Green
    Replies:
    0
    Views:
    378
    Roedy Green
    Feb 4, 2006
Loading...

Share This Page