A Quick Question for Inheritance: Static Method?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Parent
{
public:

Parent(){};
virtual int Get() const=0;
virtual ~Parent(){};

private:
};

class firstChild : public Parent
{
public:

firstChild();
virtual int Get() const;
virtual ~firstChild(){};

private:
};

class secondChild : public Parent
{
public:

secondChild();
virtual int Get() const;
virtual ~secondChild(){};

private:
};

class thirdChild : public Parent
{
public:

thirdChild();
virtual int Get() const;
virtual ~thirdChild(){};

private:
};




[Question]: What should I do so I can use the firstChild::Get() and secondChild::Get() function in thirdChild::Get() function definition?

i.e.
1
2
3
4
int thirdChild::Get()
{
   return firstChild::Get() + secondChild::Get();
}



Essentially, how can the inherited objects of the same class call each other?

Many thanks!!!
thirdChild does not inherit from firstChild or secondChild, so the question is moot.

If you intended that thirdChild inherited from secondChild which inherited from firstChild
which inherited from Parent, then your syntax is correct.
Thanks for your response.

No, I would like firstChild, secondChild, and thirdChild all inherit from Parent.

How can these objects interact with each other?

How about removing the virtual keyword or placing them under private:?

Thanks again.

thirdChild would need to be given an instance of firstChild or secondChild in order to
call member functions on them. Ex:

1
2
3
4
5
6
7
8
9
10
11
12
class thirdChild : public Parent {
  public:
     thirdChild( const firstChild& fc, const secondChild& sc ) :
         first( fc ), second( sc ) {}

     virtual int Get() const
         { return first.Get() + second.Get(); }

  private:
     const firstChild& first;
     const secondChild& second;
};


(Note: above is just an example. Holding const references may or may not
be what you ultimately want).
Topic archived. No new replies allowed.