Getting derived class specific behavior explicitly

J

Jan

Hi,

I have a base class and a couple of derived classes. The base class
has a common functionality and the derived class obey the base class
contract.

Now these base classes themselves have additional functionality which
might not make sens to be either in the base class or the other
derived classes.

So if the client of my code needs to access this functionality he can
then do a downcast to the required type.

Is there an object oriented way of doing this as in hiding from the
client the casting operation and doing it on our side?

In the classes below, I can know what kind of a class it is based on
the type information.

Let us say the type information is an enum in this case. Then I can
have the

enum ClassInfo {Der1, Der2};

class Base
{
public:
ClassInfo getType() = 0;
virtual void fun1();
virtual void fun2();
virtual void fun3();
virtual ~Base();
};

class Der1:public Base
{
public:
void fun1();
void fun2();
void fun3();
ClassInfo getType() { return Der1;}
//Additional functions
void fun4();
};

class Der2:public Base
{
public:
void fun1();
void fun2();
void fun3();
ClassInfo getType() { return Der2;}

//Additional functions
void fun5();
void fun6();
};

//Client code

Base* ptr = new Der1();
ptr->fun1();

//Case 1 - Client knows what he is currently working on (defeats the
whole purpose of OOP. I know :) )
//How can I do this in a better manner
Der1* ptr2 = dynamic_cast<Der1*>(ptr);
ptr2->fun4();

//Case 2
if (ptr->getType() == Der1)
{
//do Der1 specific thing
}

Is there a better way to do this without letting the clients know
about the types Der1 and Der2?

I was looking at visitor pattern which does not quite solve my problem
as the functions fun4(), fun5() and fun6() may not be exactly related
and if I have to use visitor pattern then I may have to implement all
of these in all my concrete visitors.
 
P

Paul

Jan said:
Hi,

I have a base class and a couple of derived classes. The base class
has a common functionality and the derived class obey the base class
contract.

Now these base classes themselves have additional functionality which
might not make sens to be either in the base class or the other
derived classes.

So if the client of my code needs to access this functionality he can
then do a downcast to the required type.

Is there an object oriented way of doing this as in hiding from the
client the casting operation and doing it on our side?

In the classes below, I can know what kind of a class it is based on
the type information.

Let us say the type information is an enum in this case. Then I can
have the

enum ClassInfo {Der1, Der2};

class Base
{
public:
ClassInfo getType() = 0;
virtual void fun1();
virtual void fun2();
virtual void fun3();
virtual ~Base();
};

class Der1:public Base
{
public:
void fun1();
void fun2();
void fun3();
ClassInfo getType() { return Der1;}
//Additional functions
void fun4();
};

class Der2:public Base
{
public:
void fun1();
void fun2();
void fun3();
ClassInfo getType() { return Der2;}

//Additional functions
void fun5();
void fun6();
};

//Client code

Base* ptr = new Der1();
ptr->fun1();

//Case 1 - Client knows what he is currently working on (defeats the
whole purpose of OOP. I know :) )
//How can I do this in a better manner
Der1* ptr2 = dynamic_cast<Der1*>(ptr);
ptr2->fun4();

//Case 2
if (ptr->getType() == Der1)
{
//do Der1 specific thing
}

Is there a better way to do this without letting the clients know
about the types Der1 and Der2?

I was looking at visitor pattern which does not quite solve my problem
as the functions fun4(), fun5() and fun6() may not be exactly related
and if I have to use visitor pattern then I may have to implement all
of these in all my concrete visitors.

Does making the functions virtual not solve this problem?.
 
J

Jan

news:a31f7f37-4f95-48ae-a976-a478c117ffbb@j25g2000vbr.googlegroups.com...
Does making the functions virtual not solve this problem?.


Hi Paul,

Making what functions virtual? You mean fun4(), fun5() and fun6(). I
don't want to add those functions to my base class as they dont belong
there to begin with. They are specific to a given derived type.
 
P

Paul

news:a31f7f37-4f95-48ae-a976-a478c117ffbb@j25g2000vbr.googlegroups.com...
Does making the functions virtual not solve this problem?.


--Hi Paul,

--Making what functions virtual? You mean fun4(), fun5() and fun6(). I
--don't want to add those functions to my base class as they dont belong
--there to begin with. They are specific to a given derived type.

Hi,

I am having trouble understanding your scenraio.
The clients must know the type in order to know what functions exist that
are specific to that type. For example is you have a base Animal:
class Animal{...}
class Elephant: public Animal{...}
class Cougar:public Animal{...}
Elephant may have a function called Stomp whereas Cougar has a function
called Prowl.
If your client has an animal he/she must know it's a Cougar to know that
Prowl exists.
So I think you already know how to do this with dynamic_cast.

The only way the client can call Prowl on an Animal, without knowing it
exists, is if Animal has some reference to it perhaps in the form of
doSpecialAbility() which would me made virtual.

You could have a generic virtual function that returns a pointer to
function/s. But you'd only be able to return one function type per function.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top