473,549 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question regarding class inheritance

Hi,

I've got a question regarding class inheritance. The following code
reproduces the problem I'm dealing with:

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

class IDefinition : public IBase
{
};

class CCoImpl
{
public:
void Method(void){}
};

class CImpl : public IDefinition, public CCoImpl
{
};

In the above 4 classes, IBase and IDefinition are both abstract classes.
CCoImpl contains a non-virtual method (actual implementation) . CImpl is
derived from both IDefinition and CCoImpl. But when I try to initiate a new
instance of CImpl, the compiler complains about that CImpl is an abstract
class because void IBase::Method(v oid); isn't defined.

Why is that? CImpl is derived from CCoImpl, doesn't it inherit the "void
Method(void);" method from CCoImpl? If it does, this method should be
implemented and CImpl shouldn't be an abstract class.

Given that IBase and IDefinition can't be changed, how do I create a CImpl
class that uses the "Method(voi d);" implementation from CCoImpl?

Thanks,
--
He Shiming

Jul 23 '05 #1
2 1481
He Shiming wrote:
I've got a question regarding class inheritance. The following code
reproduces the problem I'm dealing with:

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

class IDefinition : public IBase
{
};

class CCoImpl
{
public:
void Method(void){}
};

class CImpl : public IDefinition, public CCoImpl
{
Add here

void Method() { CCoImpl::Method (); }
};

In the above 4 classes, IBase and IDefinition are both abstract classes.
CCoImpl contains a non-virtual method (actual implementation) . CImpl is
derived from both IDefinition and CCoImpl. But when I try to initiate a new
instance of CImpl, the compiler complains about that CImpl is an abstract
class because void IBase::Method(v oid); isn't defined.
Right. It isn't.
Why is that? CImpl is derived from CCoImpl, doesn't it inherit the "void
Method(void);" method from CCoImpl?
Inherit, yes. But 'CCoImpl::Metho d' isn't virtual. If it isn't virtual,
it cannot be an overrider, can it?
If it does, this method should be
implemented and CImpl shouldn't be an abstract class.
Nope. That's not how overriding works.
Given that IBase and IDefinition can't be changed, how do I create a CImpl
class that uses the "Method(voi d);" implementation from CCoImpl?


See above.

V
Jul 23 '05 #2

"He Shiming" <mailbill(NOSPA M)@21cn.com.nos pam> wrote in message
news:d8******** **@news.yaako.c om...
Hi,

I've got a question regarding class inheritance. The following code
reproduces the problem I'm dealing with:

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

class IDefinition : public IBase
{
};

class CCoImpl
{
public:
void Method(void){}
};

class CImpl : public IDefinition, public CCoImpl
{
};

In the above 4 classes, IBase and IDefinition are both abstract classes.
CCoImpl contains a non-virtual method (actual implementation) . CImpl is
derived from both IDefinition and CCoImpl. But when I try to initiate a
new
instance of CImpl, the compiler complains about that CImpl is an abstract
class because void IBase::Method(v oid); isn't defined.

Why is that? CImpl is derived from CCoImpl, doesn't it inherit the "void
Method(void);" method from CCoImpl? If it does, this method should be
implemented and CImpl shouldn't be an abstract class.


In the current situation you have name collision between IDefinition::Me thod
and CCoImpl::Method .

CCompl::Method cannot hide the abstract one as there is no inheritance
relation between the IDefinition and CCoImpl classes. To resolve this issue
you need corresponding Method function defined explicitly in the CImpl
class:

class CImpl : public IDefinition, public CCoImpl
{
public:
void Method(void)
{
CCoImpl::Method ();
}
};
Regards,
Janusz
Jul 23 '05 #3

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

Similar topics

3
2535
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
1
272
by: He Shiming | last post by:
Hi, I've got a question regarding class inheritance. The following code reproduces the problem I'm dealing with: class IBase { public: virtual void Method(void)=0; };
0
1856
by: JKJ | last post by:
I'm not an OOP guru, but I would say ". . .not necessarily" What if the base class and derived classes are internal? Then the base class has to be within the same assembly to be inherited from. You may want to create several classes that are used throughout the assembly, but are not accessible outside the assembly. . . If your base...
6
1756
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that the individual elements in an array of ImageList's could be identified by the ID, thereby allowing re-ordering the array without harm. A person could...
8
1447
by: ^MisterJingo^ | last post by:
Hi all, I have a question regarding inheritance. I'll use the following code for an example (its been stripped down to the minimum): // code start using System; class Animal {
2
1228
by: et | last post by:
I am new to asp.net. I am writing a program that will revolve around an extensive client database, and wonder what the best way to design the program is, using classes. I have about 10 different sections, or categories if you will, about a client. For instance, some clients have data regarding our Estate Planning section, some clients have...
3
2055
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual destructor? When I am defining such a class with default destructor then my compiler is giving warning that class XXX has virtual functions but...
3
1815
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I have a snippet of this class: Public Class CustomDDL Inherits DropDownList
1
2446
by: Oskar Bennet | last post by:
Hi everybody, I am supposed to draw a simple UML diagram for a very small project that consists of less than a dozen classes. I have never been working with UML before, I have read some tutorials but I cannot find the connection between UML and my C++ code. What I have found out so far is that inheritance is indicated by an arrow like this...
0
7723
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. ...
0
7965
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...
1
7483
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...
0
7817
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
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...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1949
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
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.