help for inheritance problem

Discussion in 'C++' started by David, Nov 8, 2006.

  1. David

    David Guest

    Hi all,

    I am new in c++. I met such a problem:

    class base{

    protected:
    int a[10];
    int b;
    public:
    .....
    }

    if I have two child classes,
    class child1:public base
    {
    }
    class child2:public base
    {
    }

    I want to do:
    (1) change the values of a[] in child1, for example set a[0]=1;
    (2) change the values of a[] in child 2, for example set a[1]=2 and I
    can get the value a[0]

    how I can do? Thanks a lot
     
    David, Nov 8, 2006
    #1
    1. Advertising

  2. David wrote:
    > I am new in c++. I met such a problem:
    >
    > class base{
    >
    > protected:
    > int a[10];
    > int b;
    > public:
    > .....
    > }
    >
    > if I have two child classes,
    > class child1:public base
    > {
    > }
    > class child2:public base
    > {
    > }
    >
    > I want to do:
    > (1) change the values of a[] in child1, for example set a[0]=1;
    > (2) change the values of a[] in child 2, for example set a[1]=2 and I
    > can get the value a[0]
    >
    > how I can do? Thanks a lot


    Just use the syntax you typed in right here. a[0]=1; a[1]=2; and
    so on. Also read the FAQ 5.8.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 8, 2006
    #2
    1. Advertising

  3. David wrote:

    > I am new in c++. I met such a problem:
    >
    > class base{
    >
    > protected:
    > int a[10];
    > int b;
    > public:
    > .....
    > }
    >
    > if I have two child classes,
    > class child1:public base
    > {
    > }
    > class child2:public base
    > {
    > }
    >
    > I want to do:
    > (1) change the values of a[] in child1, for example set a[0]=1;
    > (2) change the values of a[] in child 2, for example set a[1]=2 and I
    > can get the value a[0]
    >
    > how I can do? Thanks a lot


    why not-

    class child1:public base
    {
    public:
    void update()
    { a[0] = 1; }
    }




    --
    Nick Keighley
     
    Nick Keighley, Nov 9, 2006
    #3
    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. maxw_cc
    Replies:
    1
    Views:
    3,159
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    833
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,053
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,924
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    783
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page