0

I have a base class called Number. Class One and Two are derived from Number. Now I define another class Three, where I need to access individual base classes from the multiple inheritance:


class Number{
  protected:
     int val;
  public:
    Number(){
        val=0;
    }
    void Add(Number n){//Receives another Number class instance and add the value
        val+=n.val;
    }
};

//class One derived from Number
class One:public Number{
  public:
     One(){
          cal=1;
     }
};

//class two derived from Number
class Two:public Number{
  public:
     Two(){
          val=2;
     }
};

class Three:public One,public Two{
  public:
     Three(){
          Two::Add(One);//--How can i pass instance of class One Here
     }
};

I tried One::Number and Two::Number, but no use.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
HariHaraSudhan
  • 1,330
  • 3
  • 15
  • 24

4 Answers4

0

There are a few problems, first val is private and so that needs to be made protected. Next you have a diamond of death so you need to virtual public for One and Two. You also are trying to call Add using a type but you need an instance of each class:

class Number{
protected:
     int val;
public:
    Number(){
        val=0;
    }
    void Add(Number& n){//Receives another Number class instance and add the value
        val+=n.val;
    }
};

//class One derived from Number
class One:virtual public Number{
public:
     One(){
          val=1;
    }
};

//class two derived from Number
class Two:virtual public Number{
public:
     Two(){
          val=2;
     }
};

class Three: public  One,  public  Two{
public:
     Three()
     {
           Two t1 ;
           Add(t1 );//--How can i pass instance of class Two Here
     }
};

It could be argued that using protected data is bad but it depends on your case but to be complete it is also a choice to keep val a private data member and use a protected constructor and that would look like this:

class Number{
private:
     int val;
protected:
   Number( int n ) : val(n) {} 
public:
    Number(){
        val=0;
    }
    void Add(Number& n){//Receives another Number class instance and add the value
        val+=n.val;
    }
};

class One: virtual public Number{
public:
     One() : Number( 1 ) {

     }
};

class Two: virtual public Number{
public:
     Two() : Number(2) {
     }
};  
Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • I wouldn't make `val` protected for the sole purpose to set it in the ctor. You can as well create a protected ctor `Number::Number(int)`. – dyp Apr 02 '13 at 16:47
  • 1
    @DyP I believe it depends on your use case but I amended my answer to cover both and the issues around that. – Shafik Yaghmour Apr 02 '13 at 17:02
-1

You need to create objects of the respective types.

class Three : public One, public Two
{
public:
     Three()
     {
          Add(One());
          Add(Two());
     }
};

But I don't see why you need MI here. Inheriting from Number would be sufficient.

ogni42
  • 1,232
  • 7
  • 11
-1

This should work:

Two::Add(*(One*)this);
Henrik
  • 23,186
  • 6
  • 42
  • 92
  • 1
    I originally posted a hint to the type conversion as a comment, but you have to realize there are serious issues that remain regarding your class design when you solve it like this. – dyp Apr 02 '13 at 13:45
  • @DyP There are legitimate use cases where a base class is inherited twice. This is obviously a simplified example and it's impossible to tell whether the real design has "serious issues". – Henrik Apr 03 '13 at 07:47
  • @Henrik Considering the level of the question I think it is unlikely that is what is intended and the implications should at least be noted. – Shafik Yaghmour Apr 03 '13 at 19:57
-1

The problem is not just the syntax. But what exactly are you trying to do?

You can. But where is the instance of One declared? you have to declare it first. Remember Two::Add(One); is not a declaration rather a call statement.

What you are doing is equivalent to let's say

process_number(One);

Where process number is a function.