473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Abstract class and Multiple Inheritance errors??

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
Jul 22 '05 #1
3 3344
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


Jul 22 '05 #2
In other words, every class that inherits from an abstract base class
must provide definitions for each of the 3 virtual functions.

Jul 22 '05 #3
tj*****@gmail.com 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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
10
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract...
10
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what...
18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
7
by: tron.thomas | last post by:
Please consider the following code: class Abstract { public: virtual ~Abstract() {} virtual void Method() = 0; }; class Concrete : public virtual Abstract
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.