Solved

What is polymorphic hierarchy

Posted on 2007-04-05
11
655 Views
Last Modified: 2009-07-29
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
Comment
Question by:techbie
  • 6
  • 4
11 Comments
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
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
 
LVL 5

Author Comment

by:techbie
Comment Utility
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
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
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
 
LVL 5

Author Comment

by:techbie
Comment Utility
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
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
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
 
LVL 1

Expert Comment

by:ruizpauker
Comment Utility
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
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
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
 
LVL 5

Author Comment

by:techbie
Comment Utility
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
 
LVL 5

Author Comment

by:techbie
Comment Utility
Hi all,

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

Thanks

Sijith
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 500 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now