• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 697
  • Last Modified:

What is polymorphic hierarchy

Hi all,

I was reading one of the papers I got from IEEE regarding polymorphism.
 It said about polymorphic heirarchy and something called core interface. But I was unable to understand what they are telling.

http://csdl.computer.org/dl/proceedings/apaqs/2000/0825/00/08250053.pdf

I know about heirarchy in c++ and polymorphism, but dont know about polymorphic heirarchy.

what is the difference between polymorphic and non-polymorphic heirarchy in c++.

thanks in advance
sijith
0
techbie
Asked:
techbie
  • 6
  • 4
1 Solution
 
rstaveleyCommented:
If you know about heirarchy in c++ and polymorphism, you know about polymorphic heirarchy. Either you've over-simplified the former or you are assuming things are more complicated than they are in the latter.
0
 
techbieAuthor Commented:
hi,

in inheritance we have a base class and a derived class. In polymorphism, we have different functions with the same name but different parameter.  

I will give my understanding of polymorphic heirarchy and non-polymorphic heirarchy . just correct me if i am wrong.

In polymorphism, there is a concept called core interface. This is a set of polymorphic methods that several classes share. For a hierarchy to be in polymorphic hierarchy, all its classes must share a core interface. If these classes don’t share a core interface, then it is called non-polymorphic hierarchy.

Let S be a set of classes. Then hierarchy S is in polymorphic if there exist a core class. This core class may or may not belong to the S set. All the classes in the hierarchy should share this interface.

Is my understanding of polymorphic hierarchy correct ?

thanks
0
 
rstaveleyCommented:
I suspect that you haven't completely clicked with the power of polymorphism.

Polymorphism is to most people exemplified by dynamic polymorphism rather than static polymorphism.

Dynamic polymorphism is where a reference or pointer to a base class is used to call a *virtual* method which is overriden in the derived class instance which the pointer/reference actually points to. The virtual method points to the appropriate implementation for the derived object. It means that you can write code for an interface defined by the base class and make use of overrides in derived classes.

There is a simple example in http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

Static polymorphism is the stuff of templates. It is ultimately similar in many ways, but unlikely to help your understanding. [My advice: Ignore it for now.]

To understand polymophic hierarchy is to get your head around the implications of polymorphism (hard) and then get your head around hierarchy (easy).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
techbieAuthor Commented:
Hi,

but what exactly is polymorphic heirarchy. Is it another name for dynamic polymorphism.  

I know that if we declare a virtual function in the base class, we can have their implementation in the derived class. Compiler will see if the function is declared in the derived class. If yes, it takes from the derived class. Else it takes from base class. Is this polymoprphic heirarchy ?

In that case, to see if a heirarchy is polymorphic, I need to open the base class to see if it has declared any function as virtual. Then  check the derived class to see if the function is also declared and defined in the derived class. In this case, I conclude it as a polymorphic heirarchy. Else it is non-polymorphic heirarchy.

please correct me if i am wrong.

Thanks
Sijith
0
 
rstaveleyCommented:
Polymorphic hierarchy is hierarchy that is polymorphic. That's all.

If you are looking for a text-book definition, you'd be wrong to ignore static polymorphism. However, if you want to understand polymorphism, you can ignore static polymorphism until you get up and running with generic programming (i.e. templates).

There are some important considerations in dynamic polymorphism. In order to work with dynamic polymorphism you cannot work with copy by value (or what text books call "value semantics"), because you wind up with polymorphic objects being "sliced". You need references or pointers.

--------8<--------
#include <iostream>
#include <string>
using namespace std;

class animal {
protected:
      const string name;
public:
      animal(const string& name) : name(name) {}
      virtual void run() const {
            cout << name << " can't run\n";
      }
};

class runner : public animal {
      const int legs;
public:
      runner(const string& name,int legs) : animal(name),legs(legs) {}
      void run() const {
            cout << name << " runs with " << legs << " legs\n";
      }
};

void get_it_to_run(const animal& a)
{
      a.run();
}

// Value semantics here result in the derived animal being sliced
void get_a_slice_to_run(animal a)
{
      get_it_to_run(a);
}

// Reference semantics leave the derived animal intact
void get_a_reference_to_run(animal& a)
{
      get_it_to_run(a);
}

int main()
{
      animal worm("worm");
      runner person("person",2);
      get_it_to_run(worm);
      get_it_to_run(person);
      get_a_slice_to_run(person);
      get_a_reference_to_run(person);
}
--------8<--------

Nasty business slicing a person.
0
 
rstaveleyCommented:
NB: slicing isn't only an issue when a class is polymorphic, but the need for reference semantics is more apparent with polymorphic objects.
0
 
ruizpaukerCommented:
Is this question still open?

I could give you some information on inheritance, static inheritance, virtual inheritance, abstract classes, interfaces, virtual functions and pure virtual functions.

Paul Ruiz Pauker
0
 
rstaveleyCommented:
Hi Paul,

If you reckon that directing techbie to use Wikipedia or some other ref to look up those terms would answer http:#a18873221 or the main question, go for it. It is still open. techbie says that he is OK with polymorphism and hierarchy, but wanting to sort out what polymorphic hierarchy is. I've had a go at answering his question, but we're all human!

     Rob
0
 
techbieAuthor Commented:
Hi,

I went through the code given by rstaveley.  Is this code having polymorphic heirarchy. Is yes, then having a virtual function and implementing them in the base class would make it polymorphic heirarchy.?

I also note that to be a class in polymorphic heriarchy, the call by value needs to be called.  is it correct ?

 If a person look at a code, how can he know (few things to look at) to confirm that the class heirarchy is a polymorphic heirarchy.

I am basically looking for clear cut difference between a polymorphic and non-polymorphic heirarchy.
like - polymorphic heirarchy has this while non-polymorphic heirarchy dont have this. etc.

thanks in advance

sijith
0
 
techbieAuthor Commented:
Hi all,

Can anyone give me some good links where I can learn more about polymorphic and non-polymorphic heirarchy

Thanks

Sijith
0
 
rstaveleyCommented:
Sorry I missed your previous post, Sijith.

> Is yes, then having a virtual function and implementing them in the base class would make it polymorphic heirarchy.?

Hmm.... Not a programming issue but an English language issue here. You wouldn't say it like that, but you would say "classes derived from that base had polymorphic hierarchy". I think you get the idea though.

> I also note that to be a class in polymorphic heriarchy, the call by value needs to be called.  is it correct ?

No. Reference semantics are appropriate for polymorphism. Value semantics are a *problem*, because only the base class copy constructor is called when the copy is created, if you invoke a copy of the base like I did with get_a_slice_to_run(animal a). The get_a_slice_to_run function is a good example of value semantics working badly for polymorphism. I urge you to compile and execute that code to see what it does. [You get a slice of a human that is nothing more than an animal - i.e. with no legs. I hope I'm not offending any religions with my example!]

> If a person look at a code, how can he know (few things to look at) to confirm that the class heirarchy is a polymorphic heirarchy.

If you get an animal to run and that animal is a human, you expect the animal to run with two legs. That's polymorphism. Polymorphism means that the code called in your base class invokes an implementation in the derived class. If you are using dymanic polymorphism the mechanism used is virtual functions. [If you are using static polymorphism the mechanism used is template expansion.]

Your best links are probably in Wikipedia. See http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming if you haven't already done so.

I believe that you are getting snagged on the word "hierarchy" and that may be an English language issue. If you understand polymorphism, you already understand hierarchy.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now