Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Abstract class and Multiple Inheritance errors??

Reply
Thread Tools

Abstract class and Multiple Inheritance errors??

 
 
santosh
Guest
Posts: n/a
 
      01-04-2005
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
 
Reply With Quote
 
 
 
 
Sagar Choudhary
Guest
Posts: n/a
 
      01-04-2005
santosh wrote:
> 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


 
Reply With Quote
 
 
 
 
tjpayne@gmail.com
Guest
Posts: n/a
 
      01-04-2005
In other words, every class that inherits from an abstract base class
must provide definitions for each of the 3 virtual functions.

 
Reply With Quote
 
Mike Hewson
Guest
Posts: n/a
 
      01-05-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract class with Inner class - Inheritance sven.furrer@gmail.com Java 1 10-18-2007 12:08 PM
what is the difference between abstract class and pure abstract class? skishorev@yahoo.co.in C++ 4 05-17-2006 08:07 AM
About abstract class and abstract method Sameer Java 4 08-31-2005 12:59 AM
Deriving abstract class from non-abstract class Matthias Kaeppler Java 1 05-22-2005 01:28 PM
Abstract class and Multiple Inheritance errors?? santosh C++ 0 01-05-2005 10:02 AM



Advertisments