Abstract class and Multiple Inheritance errors??

S

santosh

Hi All ,
Why does the below code doesn't compile??

class Interface
{
public:
virtual void funA() = 0;
virtual void funB() = 0;
virtual void funD() = 0;
Interface();
~Interface();
};

class A : public Interface
{
public:
A();
~A();
void funA()
{
printf("A::funA");
}
};

class B : public Interface
{
public:
B();
~B();
void funB()
{
printf("B::funB");
}
};

class D: virtual public A , virtual public B
{
public:
D();
~D();
void funD()
{
printf("D::funD");
}
};

int _tmain(int argc, _TCHAR* argv[])
{
D *d;
d = new D;

return 0;
}


COMPILER ERRORS::
error C2259: 'D' : cannot instantiate abstract class
due to following members:
'void Interface::funB(void)' : pure virtual function was not defined
d:\TestCPP\TestCPP.cpp(11) : see declaration of 'Interface::funB'
'void Interface::funA(void)' : pure virtual function was not defined
d:\TestCPP\TestCPP.cpp(10) : see declaration of 'Interface::funA'

Thanks
-Sanotsh
//AbstractInterface
 
S

Sagar Choudhary

santosh said:
Hi All ,
Why does the below code doesn't compile??

class Interface
{
public:
virtual void funA() = 0;
virtual void funB() = 0;
virtual void funD() = 0;
Interface();
~Interface();
};
You have abstract class Interface with 3 pure virtual functions
class A : public Interface
{
public:
A();
~A();
void funA()
{
printf("A::funA");
}
};
The derived class doesn't define the two functions
void funB () and void funD ()
The whole purpose of specifying pure virtual functions in abstract
class
is enforce the definition in derived classes
class B : public Interface
{
public:
B();
~B();
void funB()
{
printf("B::funB");
}
};

class D: virtual public A , virtual public B
{
public:
D();
~D();
void funD()
{
printf("D::funD");
}
};

int _tmain(int argc, _TCHAR* argv[])
{
D *d;
d = new D;

return 0;
}


COMPILER ERRORS::
error C2259: 'D' : cannot instantiate abstract class
due to following members:
'void Interface::funB(void)' : pure virtual function was not defined
d:\TestCPP\TestCPP.cpp(11) : see declaration of 'Interface::funB'
'void Interface::funA(void)' : pure virtual function was not defined
d:\TestCPP\TestCPP.cpp(10) : see declaration of 'Interface::funA'

Thanks
-Sanotsh
//AbstractInterface
 
T

tjpayne

In other words, every class that inherits from an abstract base class
must provide definitions for each of the 3 virtual functions.
 
M

Mike Hewson

In other words, every class that inherits from an abstract base class
must provide definitions for each of the 3 virtual functions.

Well, somewhere along the chain of inheritance, anyway. You can't stay
virtual forever. :)

The 'virtual' means "may be redefined later in a class derived from this
one". The '=0' means "some class derived from this must define the
function". Somewhere along the chain of inheritance, that is.

Stroustrup has a good article somewhere on his website called "A Tour Of
C++" ( pdf ). Go to http://www.research.att.com/~bs/homepage.html
 

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
474,254
Messages
2,571,278
Members
47,917
Latest member
eirafida

Latest Threads

Top