About abstract classes and Inheritance

B

Barzo

Hi,

I have a newbie doubt. In the following code, why BaseAImpl is
abstract? Deriving it from BaseImpl the IBase::foo() is not defined?




#include<iostream>

using namespace std;

class IBase
{
public:
virtual void foo() = 0;
};

class IBaseA : public IBase
{
public:
virtual void foo_bar_A() = 0;
};

class IBaseB : public IBase
{
public:
virtual void foo_bar_B() = 0;
};


class BaseImpl : IBase
{
public:
void foo(){cout << "BaseImpl::foo" << endl;};
};

class BaseAImpl : public IBaseA, public BaseImpl
{
public:
void foo_bar_A(){cout << "BaseAImpl::foo_bar_A" << endl;};
};

class BaseBImpl : public IBaseB, public BaseImpl
{
public:
void foo_bar_B(){cout << "BaseBImpl::foo_bar_B" << endl;};
};



int _tmain(int argc, _TCHAR* argv[])
{
IBaseA* A = new BaseAImpl;
A->foo_bar_A();

IBase* B = (IBase*)A;
B->foo();

delete A;

return 0;
}
 
V

Victor Bazarov

Barzo said:
I have a newbie doubt.

A doubt? A doubt you have when you are uncertain of some belief or
opinion, or when you lack confidence in your abilities.

You actually just have *two questions*.
> In the following code, why BaseAImpl is
abstract? Deriving it from BaseImpl the IBase::foo() is not defined?

'BaseAImpl' has two objects of type 'IBase'. One inside 'BaseImpl'
subobject, and the other inside the 'IBaseA' subobject. The former
defines the final overrider for 'IBase::foo', the latter does not.
That's why it's abstract. And, no, one base class' final overrider does
NOT become the final overrider for another base class' virtual function.

V
 
B

Barzo

A doubt?  A doubt you have when you are uncertain of some belief or
opinion, or when you lack confidence in your abilities.

You actually just have *two questions*.

Ok, I have two questions.. :p
 > In the following code, why BaseAImpl is


'BaseAImpl' has two objects of type 'IBase'.  One inside 'BaseImpl'
subobject, and the other inside the 'IBaseA' subobject.  The former
defines the final overrider for 'IBase::foo', the latter does not.
That's why it's abstract.  And, no, one base class' final overrider does
NOT become the final overrider for another base class' virtual function.

Thanks, now I understand.

I solved using 'virtual' Inheritance:

class IBase {...};

class IBaseA : virtual public IBase {...};

class BaseImpl : virtual public IBase {...};

class BaseAImpl : public IBaseA, public BaseImpl {...};

In this manner only one IBase object will be created, right?

Daniele.
 
V

Victor Bazarov

Barzo said:
Ok, I have two questions.. :p


Thanks, now I understand.

I solved using 'virtual' Inheritance:

class IBase {...};

class IBaseA : virtual public IBase {...};

class BaseImpl : virtual public IBase {...};

class BaseAImpl : public IBaseA, public BaseImpl {...};

In this manner only one IBase object will be created, right?

Right.

V
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top