An inheritance help reqd

N

Naren

Hello Grp,

I have a base class with virtual functions (not pure)
so I can create base class objects.I also have a derived class which extends
some of the base class functionalities
Now I want to have an inteface to the derived class.

How should this interface be is my question since if it is an abstract class
having just the functionalities of derived class declared as pure virtual
functions,how can i access the base class functionalities?

Any suggestions are welcome

Thannx in advance.

Rgds,
Naren.
 
J

John Harrison

Naren said:
Hello Grp,

I have a base class with virtual functions (not pure)
so I can create base class objects.I also have a derived class which extends
some of the base class functionalities
Now I want to have an inteface to the derived class.

How should this interface be is my question since if it is an abstract class
having just the functionalities of derived class declared as pure virtual
functions,how can i access the base class functionalities?

Any suggestions are welcome

Thannx in advance.

Rgds,
Naren.

I don't see the problem

struct Interface
{
virtual void func() = 0;
};

struct Base : Interface
{
void func();
};

struct Derived : Base
{
void func();
};

or did you have something else in mind? It always easier to write code than
describe code. Post a simple example of the problem you are seeing.

john
 
N

Naren

class Base:public Interface
{
private:
Baseattribute
public:
GetBaseattribute();
};

class Derived:public Base
{
private:
Derived_Attribute;
public:
GetDerived_Attribute();
};
class InterFace
{
GetBaseAttribute() = 0;
GetDerivedAttribute() = 0;
};

This makes the base class abstract

So should i write a dummy function GetDerived_Attribute() in the base class
which does nothing or Is there any simpler solution which I am not getting
at?

So help me in redesigning so that I can acheive the requirement.

Thaanx in advance
Rgds,
Naren.
 
G

Gianni Mariani

Naren said:
class Base:public Interface
{
private:
Baseattribute
public:
GetBaseattribute();
};

class Derived:public Base
{
private:
Derived_Attribute;
public:
GetDerived_Attribute();
};
class InterFace
{
GetBaseAttribute() = 0;
GetDerivedAttribute() = 0;
};

This makes the base class abstract

So should i write a dummy function GetDerived_Attribute() in the base class
which does nothing or Is there any simpler solution which I am not getting
at?

If you need to instantiate the class Base by itself, then as you have
designed it, you do need to implement a "do nothing" method.

Some people would argue that if the base class has no knowledge of what
a GetDerivedAttribute() method should do, then it should not be there. I
would argue differently. The interface needs to be rich enough so that
all required functionality is exposed in the interface. Hence if
"GetDerivedAttribute()" is a required functionality, then it makes sense.

But what constitues a "required" functionality ?

"GetDerivedAttribute()" sounds awfully suspicious to me of a design
problem waiting to happen.

What about "GetDerivedAttribute()" is important that you need it. Is
this "thing" you are doing somthing that belongs on the Interface ?

So help me in redesigning so that I can acheive the requirement.

Unfortunately there is not enough information to help you. You need to
draw a few more boxes before we can make sense of it.
 
N

Nils Petter Vaskinn

Wouldn't this work? :

class BaseInterface
{
GetBaseAttribute() = 0;
};

class DerivedInterface : public BaseInterface
{
GetDerivedAttribute() = 0;
};

class Base : public BaseInterFace
{
// blah
};

class Derived : public Base, public DerivedInterface
{
// blah blah
};
 
N

Naren

Can't I have a single interface which I can expose and through which both
the methods can be accessed?
 
N

Nils Petter Vaskinn

Can't I have a single interface which I can expose and through which both
the methods can be accessed?

Not if you want to avoid implementing getDerivedAttribute in the base
class.


regards
NPV
 
S

stelios xanthakis

Naren said:
Hello Grp,

I have a base class with virtual functions (not pure)
so I can create base class objects.I also have a derived class which extends
some of the base class functionalities
Now I want to have an inteface to the derived class.

How should this interface be is my question since if it is an abstract class
having just the functionalities of derived class declared as pure virtual
functions,how can i access the base class functionalities?

Why exactly do you need an "interface" for?
To have a pointer to it?

If so, you can use a pointer to a derrived class directly.

IMHO, pure virtual functions make sense only in diamond hierarchy
with virtual inheritance. Apart from that there is absolutely no
advantage in having an interface class which has only declarations
of pure virtual functions.

At least, not in C++.

stelios
 
N

Nils Petter Vaskinn

IMHO, pure virtual functions make sense only in diamond hierarchy
with virtual inheritance. Apart from that there is absolutely no
advantage in having an interface class which has only declarations
of pure virtual functions.

Write a library that needs to do comparisons/sorts/serialization/anything
on objects given to it. Define an interface with the functions/operators
that you need to force the user of the library to implement them. And
helps document what is needed to use the library.

Any better way without interfaces?

regards
NPV
 
J

Jerry Coffin

class Base:public Interface
{
private:
Baseattribute
public:
GetBaseattribute();
};

class Derived:public Base
{
private:
Derived_Attribute;
public:
GetDerived_Attribute();
};
class InterFace
{
GetBaseAttribute() = 0;
GetDerivedAttribute() = 0;
};

This makes the base class abstract

So should i write a dummy function GetDerived_Attribute() in the base class
which does nothing or Is there any simpler solution which I am not getting
at?

So help me in redesigning so that I can acheive the requirement.

So far there's been quite a bit of discussion, but (in what I've seen)
none of the fundamental question you need to deal with.

When you create a derived class, you're asserting that any and all
possible operations on the base class also make sense for the derived
class. Therefore, if GetBaseAttribute _really_ means what it says (i.e.
that this operation really only makes sense for the base class, not the
derived class) then the problem is simple: you should NOT be using
(public) derivation in this situation.

You haven't said so, but it's possible that DerivedAttribute is intended
to be an _extension_ of the BaseAttribute. I.e. both represent
basically the same thing, but the derived class enriches what is still
basically the same attribute.

In this case, since the derived attribute is an enriched version of the
base, we make that relationship explicit by using public derivation in
defining the attribute classes:

class BaseAttribute {};

class DerivedAttribute : public BaseAttribute {};

Then we can write our classes so a virtual GetAttribute function returns
the right attribute (a BaseAttribute from the Base class and a
DerivedAttribute from the derived class):

class Base {
BaseAttribute b_attr;
public:
virtual BaseAttribute const &GetAttribute() { return b_attr; }
};

class Derived : public Base {
DerivedAttribute d_attr;
public:
virtual DerivedAttribute const &GetAttribute() { return d_attr; }
};

This uses a feature known as covariant return types. If you're using an
older compiler (e.g. VC++ 6), it's possible that this may not work.
 
S

stelios xanthakis

Nils Petter Vaskinn said:
Write a library that needs to do comparisons/sorts/serialization/anything
on objects given to it. Define an interface with the functions/operators
that you need to force the user of the library to implement them. And
helps document what is needed to use the library.

Any better way without interfaces?

You are right.

When all you can have is an interface, pure virtuals are
the choice.

However, the initial question was the other way around:
produce an interface for an existing derrived class.

The initial question is confusing anyway.

Cheers

stelios
 
N

Nils Petter Vaskinn

However, the initial question was the other way around: produce an
interface for an existing derrived class.

Yes and for the classes in the OPs question I can see no need for any
interfaces, but those are examples so there might be something going on
that we don't know about to make the interfaces nessesary in the "real"
program of the OP.
Apart from that there is absolutely no advantage in having an interface
class which has only declarations of pure virtual functions.

That's what I responded to, giving another example of a useful interface,
not saying it had anything to do with the OPs problem.
The initial question is confusing anyway.

Nah, only the OP never tells us why he wants the interface in the first
place.

regards
NPV
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top