473,473 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

abstract class and pure virtual

Consider the following:

class BaseInterface
{
public:
virtual void something() = 0;
};

class AbstractSubclass : 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 AbstractSubclass won't
hide the one in BaseInterface. If it doesn't hide, then is this
redeclaration preferred since the subclasses of AbstractSubclass can see
this and implement it or is it better it leave it alone, meaning declare it
in BaseInterface only?

Thanks.
Jul 22 '05 #1
6 1319
cppsks posted:
Consider the following:

class BaseInterface
{
public:
virtual void something() = 0;
};

class AbstractSubclass : 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 AbstractSubclass
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 AbstractSubclass 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
Jul 22 '05 #2
cppsks wrote:
Consider the following:

class BaseInterface
{
public:
virtual void something() = 0;
};

class AbstractSubclass : 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 AbstractSubclass won't
hide the one in BaseInterface.
It _overrides_ it.
If it doesn't hide, then is this
redeclaration preferred since the subclasses of AbstractSubclass 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 'AbstractSubclass' 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
Jul 22 '05 #3
cppsks wrote:
First of all, I am "assuming" (since I don't know for sure) if the
declaration of pure virtual "something" method in AbstractSubclass 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 AbstractSubclass::something() overrides the BaseInterface
function of the same signature.
If it doesn't hide, then is this
redeclaration preferred since the subclasses of AbstractSubclass 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.
Jul 22 '05 #4
"cppsks" <sk*****@hotmail.com> wrote in message
news:cm**********@news.lsil.com...
Consider the following:

class BaseInterface
{
public:
virtual void something() = 0;
};

class AbstractSubclass : 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 AbstractSubclass won't
hide the one in BaseInterface. If it doesn't hide, then is this
redeclaration preferred since the subclasses of AbstractSubclass 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/
Jul 22 '05 #5
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);return; }
};

class TheDoublyDerived : public TheDerived
{
virtual(?) void theFunc(){ do other_stuff while(!done);return; }
};

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
Jul 22 '05 #6
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);return; }


'virtual' is redundant here too.
};

class TheDoublyDerived : public TheDerived
{
virtual(?) void theFunc(){ do other_stuff while(!done);return; }
};

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::theFunc hides TheBase::theFunc"
(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
Jul 22 '05 #7

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

Similar topics

16
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?)...
4
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...
8
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....
12
by: placid | last post by:
Hi if a have the following classes class A { public: A(); virtual ~A(); virtual string someFunction() const = 0;
3
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 ...
4
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...
4
by: skishorev | last post by:
and what is object delagation, and how it can implemented?
4
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...
17
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;"...
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...
0
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,...
0
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,...
0
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
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.