Consider the following:
class BaseInterface
{
public:
virtual void something() = 0;
};
class AbstractSubclas s : public BaseInterface
{
public:
virtual void somethingElse() = 0;
virtual void something() = 0; // is this declaration preferred or not?
};
First of all, I am "assuming" (since I don't know for sure) if the
declaration of pure virtual "something" method in AbstractSubclas s won't
hide the one in BaseInterface. If it doesn't hide, then is this
redeclaration preferred since the subclasses of AbstractSubclas s can see
this and implement it or is it better it leave it alone, meaning declare it
in BaseInterface only?
Thanks. 6 1340
cppsks posted: Consider the following:
class BaseInterface { public: virtual void something() = 0; };
class AbstractSubclas s : public BaseInterface { public: virtual void somethingElse() = 0; virtual void something() = 0; // is this declaration preferred or not? };
First of all, I am "assuming" (since I don't know for sure) if the declaration of pure virtual "something" method in AbstractSubclas s won't hide the one in BaseInterface.
It doesn't hide it. Or then again you could say that it does. Either way it
doesn't make a difference.
If it doesn't hide, then is this redeclaration preferred since the subclasses of AbstractSubclas s can see this and implement it or is it better it leave it alone, meaning declare it in BaseInterface only?
I myself would only put it in "BaseInterface" . One reason being that I
couldn't be bothered typing it out. The second reason being:
class UltimateBase
{
public:
virtual int Monkey() const = 0;
virtual int Ape() const = 0;
};
class DerivedAlpha : virtual public UltimateBase
{
public:
virtual int Monkey() const
{
return 1;
}
};
class DerivedBeta : virtual public UltimateBase
{
public:
virtual void Ape() const
{
return 1;
}
};
class Fullyfledged : virtual public DerivedAlpha, virtual public DerivedBeta
{
};
int main()
{
Fullyfledged cheese;
}
-JKop
cppsks wrote: Consider the following:
class BaseInterface { public: virtual void something() = 0; };
class AbstractSubclas s : public BaseInterface { public: virtual void somethingElse() = 0; virtual void something() = 0; // is this declaration preferred or not? };
First of all, I am "assuming" (since I don't know for sure) if the declaration of pure virtual "something" method in AbstractSubclas s won't hide the one in BaseInterface.
It _overrides_ it.
If it doesn't hide, then is this redeclaration preferred since the subclasses of AbstractSubclas s can see this and implement it or is it better it leave it alone, meaning declare it in BaseInterface only?
If you're going to use 'AbstractSubcla ss' definition as documentation,
then, sure, every little bit helps. Otherwise, it shouldn't matter. The
language doesn't require it. As to the style, everybody's got their own.
V
cppsks wrote: First of all, I am "assuming" (since I don't know for sure) if the declaration of pure virtual "something" method in AbstractSubclas s won't hide the one in BaseInterface.
A name in defined in a derived class hides names in the base class. Doesn't
matter if it's pure or virtual or not.
The function AbstractSubclas s::something() overrides the BaseInterface
function of the same signature.
If it doesn't hide, then is this redeclaration preferred since the subclasses of AbstractSubclas s can see this and implement it or is it better it leave it alone, meaning declare it in BaseInterface only?
It's not preferred one way or the other really. In this case there is no
pracitcal affect to the second declaration. It's sort of like repeating
virtual on derived overriders.
"cppsks" <sk*****@hotmai l.com> wrote in message
news:cm******** **@news.lsil.co m... Consider the following:
class BaseInterface { public: virtual void something() = 0; };
class AbstractSubclas s : public BaseInterface { public: virtual void somethingElse() = 0; virtual void something() = 0; // is this declaration preferred or not? };
First of all, I am "assuming" (since I don't know for sure) if the declaration of pure virtual "something" method in AbstractSubclas s won't hide the one in BaseInterface. If it doesn't hide, then is this redeclaration preferred since the subclasses of AbstractSubclas s can see this and implement it or is it better it leave it alone, meaning declare it in BaseInterface only?
Thanks.
There is no standard answer, but I personally don't copy the definition of
the function in a derived class. It is fundamental to good maintainability
that everything be defined in exactly one place if at all possible.
--
Cy http://home.rochester.rr.com/cyhome/
Ron Natalie wrote:
<snip> It's not preferred one way or the other really. In this case there is no pracitcal affect to the second declaration. It's sort of like repeating virtual on derived overriders.
Greetings!
Wouldn't this be necessary in the following situation?
class TheBase
{
public:
virtual void theFunc() = 0;
};
class TheDerived : public TheBase
{
public:
virtual void theFunc(){ do stuff while(!done);re turn; }
};
class TheDoublyDerive d : public TheDerived
{
virtual(?) void theFunc(){ do other_stuff while(!done);re turn; }
};
or is this invalid, or redundant? Is a function virtual through all
generations if it's declared as such at some point in the inherital
hierarchy? I'm (fairly) new at C++, so this is something that I'm kinda
unsure about.
-Henrik W Lund
Henrik W Lund wrote: Ron Natalie wrote:
<snip>
It's not preferred one way or the other really. In this case there is no pracitcal affect to the second declaration. It's sort of like repeating virtual on derived overriders.
Greetings!
Wouldn't this be necessary in the following situation?
class TheBase { public: virtual void theFunc() = 0; };
class TheDerived : public TheBase { public: virtual void theFunc(){ do stuff while(!done);re turn; }
'virtual' is redundant here too.
};
class TheDoublyDerive d : public TheDerived { virtual(?) void theFunc(){ do other_stuff while(!done);re turn; } };
or is this invalid, or redundant?
It's valid and redundant. Good practice, however, suggests that you
should put 'virtual' there to save the others the need to look for and
at the original class. Redundancy is not always bad.
There are cases, of course, where declaring the overrider 'virtual' can
be seen as a mistake. Imagine that you change the interface of 'TheBase'
and forget to change the same function in 'TheDerived'. If you don't
have 'virtual' in the declaration of 'theFunc', the compiler is likely
to warn you that "non-virtual TheDerived::the Func hides TheBase::theFun c"
(or something of that sort) and you'll know. If 'theFunc' is declared
virtual in 'TheDerived' too, you are less likely to get any kind of
a warning. Although I've seen compilers that don't emit any warnings
in either case.
Is a function virtual through all generations if it's declared as such at some point in the inherital hierarchy?
Yes.
I'm (fairly) new at C++, so this is something that I'm kinda unsure about.
Understandable.
V This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Merlin |
last post by:
Hi
Been reading the GOF book and started to make the distinction between
Class and Interface inheritance. One question though:
Do pure abstract classes have representations? (data members?)
What about abstract classes?
Should abstract classes have a destructor and or constructor? What
about pure abstract classes?
|
by: WittyGuy |
last post by:
Hi all,
Though I know the concepts of both abstract class & virtual
function (like derived class pointer pointing to base class...then
calling the function with the pointer...), what is the real
implementation usage of these concepts? Where these concepts will be
used. Please provide some illustration (real-time), so that it can be
easily comprehended.
Thanks,
wittyGuy
|
by: Dev |
last post by:
Hello,
Why an Abstract Base Class cannot be instantiated ?
Does anybody know of the object construction internals ?
What is the missing information that prevents the construction ?
TIA.
Dev
|
by: placid |
last post by:
Hi
if a have the following classes
class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
|
by: WithPit |
last post by:
I am trying to create an managed wrapper but have some problems with it by
using abstract classes.
In my unmanaged library code i had the following three classes with the
following hierarchy
Referenced (class)
Object (abstract class, inheriting from referenced)
Node (class, inheriting from object)
| |
by: sudhir |
last post by:
Q 1. I defined a class with 10 functions . It contains declaration of 5
functions and 5 functions are declared and defined. Is this class is
said to a abstract class ?
Q 2. Which one is correct?
If a class contains a virtual class then it is said to a abstract
class ?
Or a abstract class always contains a virtual function ?
|
by: skishorev |
last post by:
and what is object delagation, and how it can implemented?
|
by: Eric |
last post by:
I was wondering what people thought about the information found at:
http://g.oswego.edu/dl/mood/C++AsIDL.html
Specifically, I am interested in the following recommendation:
----
Since interface classes cannot be directly instantiated, yet serve as
virtual base classes for implementations, the constructors should take
no arguments and should be listed as protected. Also, for similar
|
by: Jess |
last post by:
Hello,
If I have a class that has virtual but non-pure declarations, like
class A{
virtual void f();
};
Then is A still an abstract class? Do I have to have "virtual void
f() = 0;" instead? I think declaring a function as "=0" is the same
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |