473,385 Members | 1,396 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,385 software developers and data experts.

Dynamic Binding.

I have been doing some reading on dynamic bindind and polymorphism. I
know the basics from what I have seen in books. The way virtual
methods were explained was that they were used when methods the same
methods were inhereted into 2 objects and they are parents of new
objects.

I would like some situations where polymorphism is useful.
My next step will to try to understand handle classes.

Jul 22 '05 #1
3 1601
* enki:

I would like some situations where polymorphism is useful.


Try to make a simple arithmetic expression evaluator.

(This is one of my favorite examples.)

Don't bother with converting user input to expressions,
just create the expressions directly in code.

* An Expression has a member function eval() that produces
a double, the result of evaluating the expression.

* An Expression can be Number.

* An Expression can be a Sum of two Expressions.

* An Expression can be a Product of two Expressions.

The code
int main()
{
Expression* expr =
new Sum(
new Number( 2 ),
new Product( new Number( 3 ), new Number( 4 ) )
);

std::cout << expr->eval() << std::endl;
delete expr;
}
should write out the answer "14" and delete all objects new'ed.

(It's not exception safe, but don't bother with exception safety.)

--
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?
Jul 22 '05 #2
PKH

"enki" <en*****@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have been doing some reading on dynamic bindind and polymorphism. I
know the basics from what I have seen in books. The way virtual
methods were explained was that they were used when methods the same
methods were inhereted into 2 objects and they are parents of new
objects.

I would like some situations where polymorphism is useful.
My next step will to try to understand handle classes.


Here's one: I got a project with several different object-types that needs
to be updated (run) on a regular basis, so I use
baseclasses such as this:

// baseclass for type-id
class CObjectID
{
public:
virtual CObjectID* GetClass(DWORD iId){return (iId == OID_OBJECTID) ? this :
NULL;}
};

// 'baseclass' for data-exchange
class CBaseContext : public CObjectID
{
public:
virtual bool Validate() = 0;
virtual CObjectID* GetClass(DWORD iId){return (iId == OID_BASECONTEXT) ?
this : CObjectID::GetClass(iId);}
};

// 'baseclass' for objects
class CTask : public CObjectID
{
private:
CTask
*m_pcParent;

CTaskList
m_cChildList;

public:
virtual void Run(CBaseContext* pcContext); // calls run on all
childtasks
virtual CObjectID* GetClass(DWORD iId){return (iId == OID_TASK) ? this :
CObjectID::GetClass(iId);}
};

Now I can build a tree of pointers to objects inherited from CTask, call Run
on the root and have all the objects updated (objects calls CTask::Run() in
their own Run() function to run it's own children).
When working mostly with pointers to baseclasses, a type-id system is
sometimes useful to enable checking the actual type of an object, or if an
object is inherited from a given class (esp. useful for for asserts() and
validation), so CTask inherits from CObjectID which has a virtual GetClass()
that gives this functionality.

PKH

Jul 22 '05 #3
"enki" <en*****@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have been doing some reading on dynamic bindind and polymorphism. I
know the basics from what I have seen in books. The way virtual
methods were explained was that they were used when methods the same
methods were inhereted into 2 objects and they are parents of new
objects.

I would like some situations where polymorphism is useful.


class DrawableObject
{
public:
virtual void draw(Surface &surface) = 0;
};

class Line : public DrawableObject
{
public:
// c'tor etc.
void draw(Surface &surface);
private:
// data member(s)
};

class Ellipse : public DrawableObject
{
public:
// ...
void draw(Surface &surface);
private:
// ...
};

// classes for other useful shapes

class CompositeDrawableObject : public DrawableObject
{
public:
// ...
void addComponent(DrawableObject *pComponent);
void draw(Surface &surface); // draws each component
private:
std::vector<DrawableObject*> mComponents;
};

// a function somewhere
void printAnObject(DrawableObject *pOb)
{
pOb->draw(GetPrinter().GetDrawSurface());
}

Imagine the different drawings that could be printed when printAnObject is
called, depending on what you pass to it. The actual class of the object
that pOb points to could be Line, Ellipse, or any other class derived from
DrawableObject. It could even be a class that didn't exist when
printAnObject was compiled. Now, suppose that pOb points to an object of
class CompositeDrawableObject. Then suppose that some components of that
CompositeDrawableObject are themselves CompositeDrawableObjects.

DW

Jul 22 '05 #4

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

Similar topics

2
by: festiv | last post by:
Hi there, I want to learn how the compiler is implementing the dynamic binding. where can i read about this subject (the hole process). thanks.
3
by: prashna | last post by:
Hi all, Is'nt a function invocation through a function pointer is dynamic binding? For example consider the following program 1 int main() 2 { 3 int (*fun_ptr)(); 4 int...
9
by: Gibby Koldenhof | last post by:
Hiya, Terrible subject but I haven't got a better term at the moment. I've been building up my own library of functionality (all nice conforming ISO C) for over 6 years and decided to adopt a...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
1
by: benoit | last post by:
Hi, I created a Dynamic Datagrid and i added an EditCommandColumn to it. Works fine, but my Editcommand eventhandler seems to have a problem with PostBack This is my code private DataGrid...
3
by: compgeek.320 | last post by:
Hi Everyone, Can any one explain me about Dynamic binding related to OOPs concepts.I studied in a book that the polymorphic fuction call will be decided in the runtime rather than the compile...
3
by: Colin | last post by:
Hello, I can manage quite well in ASP but would like some advice in the best way to achieve dynamic layout in ASP.NET and still keep the page and code separate. Let's say I already have a...
13
by: Jess | last post by:
Hello, I have some questions to do with dynamic binding. The example program is: #include<iostream> using namespace std; class A{
2
by: 09876 | last post by:
Hi: all I understand the difference between dynamic binding and static binding. But I just wonder what is the point to make the distinction between the dynamic binding and static binding. For...
26
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.