C++ Multiple Inheritance

S

shintu

Hi,

For the following code snippet, the compiler complains for "mi *mi1=new
mi;" statement

//*******************************************************
#include <iostream>
using namespace std;
class base{
public :
virtual void f()=0;
virtual void g()=0;
};

class der1:public base
{
public :
void f()
{
cout << " In Der1"<< endl;
}
};

class der2: public base
{
public:
void g()
{
cout << " In Der2"<< endl;
}
};

class mi : public der1,public der2
{
};

int main(int argc, char* argv[])
{
mi *mi1=new mi;
return 0;
}
//========================================
where as, it does not complain if the the base class is derived
virtually.
Please provide any explaination for this.

Thanks,
Sachin
 
A

Alf P. Steinbach

* shintu:
For the following code snippet, the compiler complains for "mi *mi1=new
mi;" statement

//*******************************************************
#include <iostream>
using namespace std;
class base{
public :
virtual void f()=0;
virtual void g()=0;
};

class der1:public base
{
public :
void f()
{
cout << " In Der1"<< endl;
}
};

class der2: public base
{
public:
void g()
{
cout << " In Der2"<< endl;
}
};

class mi : public der1,public der2
{
};

int main(int argc, char* argv[])
{
mi *mi1=new mi;
return 0;
}
//========================================
where as, it does not complain if the the base class is derived
virtually.
Please provide any explaination for this.

With ordinary inheritance you're inheriting from two unrelated abstract
classes and since your class does not implement the pure virtual
functions it's still abstract in each of the two subobjects.

With virtual inheritance you have a single base subobject in your most
derived class object, and for that base subobject both pure virtual
functions have been implemented -- it's no longer abstract.

This is, by the way, mostly how inheritance works in Java, and why it's
often a good idea to inherit virtually from interface classes.
 
T

Thomas Maier-Komor

Alf said:
* shintu:
For the following code snippet, the compiler complains for "mi *mi1=new
mi;" statement

//*******************************************************
#include <iostream>
using namespace std;
class base{
public :
virtual void f()=0;
virtual void g()=0;
};

class der1:public base
{
public :
void f()
{
cout << " In Der1"<< endl;
}
};

class der2: public base
{
public:
void g()
{
cout << " In Der2"<< endl;
}
};

class mi : public der1,public der2
{
};

int main(int argc, char* argv[])
{
mi *mi1=new mi;
return 0;
}
//========================================
where as, it does not complain if the the base class is derived
virtually.
Please provide any explaination for this.

With ordinary inheritance you're inheriting from two unrelated abstract
classes and since your class does not implement the pure virtual
functions it's still abstract in each of the two subobjects.

With virtual inheritance you have a single base subobject in your most
derived class object, and for that base subobject both pure virtual
functions have been implemented -- it's no longer abstract.

This is, by the way, mostly how inheritance works in Java, and why it's
often a good idea to inherit virtually from interface classes.

using virtual inheritance will not be enough. You still have to do an
implementation of f and g in mi. Something like the following is sufficient:

class mi : public der1, public der2
{
public:
void f()
{ der1::f(); }
void g()
{ der2::g(); }
};

Tom
 
A

Alf P. Steinbach

* Thomas Maier-Komor:
using virtual inheritance will not be enough. You still have to do an
implementation of f and g in mi.

I'm sorry, your statement is incorrect.
 
S

Sumit Rajan

using virtual inheritance will not be enough. You still have to do an
implementation of f and g in mi. Something like the following is
sufficient:

class mi : public der1, public der2
{
public:
void f()
{ der1::f(); }
void g()
{ der2::g(); }
};

I don't think that is required.
Am I missing something here?

Regards,
Sumit.
 
T

Thomas Maier-Komor

Alf said:
* Thomas Maier-Komor:

I'm sorry, your statement is incorrect.

I thought your statement is correct until I tried it with g++ and Sun's CC.

g++ says:
a.cc: In function `int main(int, char**)':
a.cc:41: error: cannot allocate an object of type `mi'
a.cc:41: error: because the following virtual functions are abstract:
a.cc:5: error: virtual void base::f()
a.cc:6: error: virtual void base::g()

Sun's CC says:
"a.cc", line 35: Error: Cannot create a variable for abstract class mi.
"a.cc", line 35: Error: base::g() has not been overridden.
"a.cc", line 35: Error: base::f() has not been overridden.

Either both compilers are wrong, these functions are needed, or I do
misunderstand something completely. Could you please point me into the
right direction so I am able to understand this issue?

Tom
 
A

Alf P. Steinbach

* Thomas Maier-Komor:
Alf said:
* Thomas Maier-Komor:

I'm sorry, your statement is incorrect.

I thought your statement is correct until I tried it with g++ and Sun's CC.
[snip]

Either both compilers are wrong, these functions are needed, or I do
misunderstand something completely. Could you please point me into the
right direction so I am able to understand this issue?

#include <string>
#include <iostream>
#include <ostream>

using namespace std;

class base{
public :
virtual void f()=0;
virtual void g()=0;
};

class der1:public virtual base
{
public :
void f()
{
cout << " In Der1"<< endl;
}
};

class der2: public virtual base
{
public:
void g()
{
cout << " In Der2"<< endl;
}
};

class mi : public der1,public der2
{
};

int main(int argc, char* argv[])
{
mi *mi1=new mi;
mi1->f();
mi1->g();
return 0;
}
 
S

shintu

Thank you gentlemen for your wise thoughts and time. I run it, before
posting, on VC++(6.0) copiler and found that with virtual derivation
there was no error, whatsoever.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top