473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

question about polymorphism

What are the techniques used to implement parametric polymorphism in
C++?

A Polymorphic method can be virtual?

Sep 19 '05 #1
8 1677
Locia wrote:
What are the techniques used to implement parametric polymorphism in
C++?
[Public] Inheritance and virtual functions.
A Polymorphic method can be virtual?


Run-time polymorphism is only achieved through virtual functions.

V
Sep 19 '05 #2
Locia wrote:
What are the techniques used
to implement parametric polymorphism in C++?

A Polymorphic method can be virtual?


The term "polymorphism"
can be applied to lots of different things in C++.

First, take a look at
The Free On-Line Dictionary Of Computing (FOLDOC)

http://wombat.doc.ic.ac.uk/foldoc/

and search for

polymorphism

C++ supports "ad-hoc polymorphism" (function and operator overloading),
"compile-time polymorphism" (class templates) and
"run-time polymorphism" (virtual functions).
Run-time polymorphism is invariably implemented using jump-tables --
virtual function tables
which contain the addresses of the actual functions to be called.
In general, the actual function to be called is not known
until the virtual function is invoked
so this is sometimes called "late-binding".
Sep 19 '05 #3

"Locia" <ro********@yahoo.it> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
| What are the techniques used to implement parametric polymorphism in
| C++?
|
| A Polymorphic method can be virtual?
|

Late binding relies on virtual member functions. That means that the
behaviour called depends on the actual object (or derivative thereof) at
run-time and where the virtual member function(s) is/are defined in the
hierarchy.

If what you are wondering is how C++ achieves this: look up the subject
of the vtable. The vtable is only generated if at least one member
function or d~tor is implemented as virtual.

Example:

#include <iostream>

class B
{
public:
B() { std::cout << "B()\n"; }
virtual ~B() { std::cout << "~B()\n"; }
virtual void foo() { std::cout << "B::foo()\n"; }
void bar() { std::cout << "B::bar()\n"; }
};

class D : public B
{
public:
D() { std::cout << "D()\n"; }
~D() { std::cout << "~D()\n"; }
void foo() { std::cout << "D::foo()\n"; }
};

// note: both ~D() and void D::foo() are inherently virtual

int main()
{
std::cout << "--- B b ---\n";
B b;
b.foo();

std::cout << "--- D d ---\n";
D d;
d.foo();
d.bar();

std::cout << "--- new D ---\n";
B *p_b = new D;
p_b->foo(); // polymorphic upcast
delete p_b;

std::cout << "--- stack unwind ---\n";

return 0;
}

/*

--- B b ---
B()
B::foo()
--- D d ---
B()
D()
D::foo()
B::bar()
--- new D ---
B()
D()
D::foo() // upcast
~D()
~B()
--- stack unwind ---
~D()
~B()
~B()
*/
Sep 19 '05 #4
* Locia:
What are the techniques used to implement parametric polymorphism in
C++?
I had to google for the meaning of "parametric polymorphism".

And found it in <url:
http://en.wikipedia.org/wiki/Polymorphism_(computer_science)>.

It just means generics.

In C++ generics are implemented using the 'template' mechanism. This
mechanism implements _unrestricted_ genericity, which means that some
constructs available with more restricted genericity schemes (e.g. in Eiffel)
are not possible. In particular, you can not have a generic virtual function.

Unfortunately one of the authors of the Google article didn't know anything
about the subject, so currently the Google article states "Some argue that
templates should be considered an example of parametric polymorphism, though
instead of actually reusing generic code they rely on macros to generate
specific code (which can result in code bloat)" which is 100% incorrect.

A Polymorphic method can be virtual?


That is a meaningless question.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 19 '05 #5
Alf P. Steinbach wrote:
Unfortunately one of the authors of the Google article didn't know anything
about the subject, so currently the Google article states ...


You mean the WikiPedia article.
Sep 19 '05 #6
"Alf P. Steinbach" <al***@start.no> wrote in message
news:43****************@news.individual.net

Unfortunately one of the authors of the Google article didn't know
anything about the subject, so currently the Google article states
"Some argue that templates should be considered an example of
parametric polymorphism, though instead of actually reusing generic
code they rely on macros to generate specific code (which can result
in code bloat)" which is 100% incorrect.


Is is the use of the word macro that you object to or the failure to clearly
distinguish source code bloat from object code bloat --- or both or
something else?
--
John Carson

Sep 20 '05 #7
* John Carson:
"Alf P. Steinbach" <al***@start.no> wrote in message
news:43****************@news.individual.net

Unfortunately one of the authors of the Google article didn't know
anything about the subject, so currently the Google article states
"Some argue that templates should be considered an example of
parametric polymorphism, though instead of actually reusing generic
code they rely on macros to generate specific code (which can result
in code bloat)" which is 100% incorrect.


Is is the use of the word macro that you object to or the failure to clearly
distinguish source code bloat from object code bloat --- or both or
something else?


Both of those, + "Some argue that...", + failure to distinguish concept from
language from implementation (e.g., the language has 'export'), and so on; the
worst is perhaps the supposition that some technical drawback, whether it
exists or not, has anything to do with the categorization.
Note: as Red Floyd helpfully pointed out, I did mean the _Wikipedia_ article.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 20 '05 #8
> "compile-time polymorphism" (class templates) and

Can you explain me how c++ implement templates?

Sep 20 '05 #9

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

Similar topics

1
by: Liza | last post by:
Hi guys, i'm trying to build a web service....... is there such thing as polymorphism in web services? i mean could i have two web services of the same name but different arguments such that i...
7
by: DJ.precario | last post by:
In a schematic way, I have a program with a virtual base class (MyClass), and some derived classes (MyClass1, Myclass2...) In main, I want to create a vector of objects of the classes that...
3
by: John Salerno | last post by:
Along with events and delegates, polymorphism has been something I sort of struggle with every now and then. First, let me quote the book I'm reading: "Polymorphism is most useful when you have...
12
by: Meya-awe | last post by:
I am puzzled, what is the purpose of an interface? How does it work, what i mean is how does the compiler treats this? Why when we talk about separating user interface from business logic, an...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
9
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
13
by: Jack | last post by:
I have a class called "Base". This class has a protected member variable "m_base" which can be retrieved using the public member function "GetBaseMember". "m_base" is initialized to "1" and is...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.