473,799 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\Test CPP.cpp(11) : see declaration of 'Interface::fun B'
'void Interface::funA (void)' : pure virtual function was not defined
d:\TestCPP\Test CPP.cpp(10) : see declaration of 'Interface::fun A'

Thanks
-Sanotsh
//AbstractInterfa ce
Jul 22 '05 #1
3 3376
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\Test CPP.cpp(11) : see declaration of 'Interface::fun B' 'void Interface::funA (void)' : pure virtual function was not defined d:\TestCPP\Test CPP.cpp(10) : see declaration of 'Interface::fun A'
Thanks
-Sanotsh
//AbstractInterfa ce


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.c om 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
4653
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
5804
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 the need to have abstract class types in C#? Derived classes from abstract base classes must overrided the abstract method defininition anyway, so why not just provide a complete definition for the abstract method when defining the containing...
10
2982
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 class. The interface has implied abstract methods/properties and derived classes can inherit multiple interfaces. The interface properties/methods have no implementation. Besides definitions of the two, what are some conceptual reasons to use...
10
667
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 circumstacnes does one implement an interface? TIA, -- Joe VBA Automation/VB/C++/Web and DB development
18
3785
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
5202
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
1919
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
4476
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 to be checking that the correct data type is used DataAcess sets an abstract class and methods
6
4035
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 would be useful to have /some/ data defined in such an abstract class for reasons of forming a common block of data existing in or used by all descendant classes (see example below.) In such a case where a common block of data *always* exists,...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.