473,609 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1614
* 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.goo glegroups.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::GetC lass(iId);}
};

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

CTaskList
m_cChildList;

public:
virtual void Run(CBaseContex t* pcContext); // calls run on all
childtasks
virtual CObjectID* GetClass(DWORD iId){return (iId == OID_TASK) ? this :
CObjectID::GetC lass(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.goo glegroups.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 CompositeDrawab leObject : public DrawableObject
{
public:
// ...
void addComponent(Dr awableObject *pComponent);
void draw(Surface &surface); // draws each component
private:
std::vector<Dra wableObject*> mComponents;
};

// a function somewhere
void printAnObject(D rawableObject *pOb)
{
pOb->draw(GetPrinte r().GetDrawSurf ace());
}

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 CompositeDrawab leObject. Then suppose that some components of that
CompositeDrawab leObject are themselves CompositeDrawab leObjects.

DW

Jul 22 '05 #4

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

Similar topics

2
3411
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
1626
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 fun(){printf("HIIIIII\n");}
9
2622
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 more OO approach to fit my needs. Altough I used an OO approach previously for some subparts of the library it became somewhat difficult to maintain all those parts since they really are the same thing coded for each part (code duplication). So...
1
7560
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 dropdown in UC1 _________________________ 1) MainPage_Load 2) User Control_1 Load
1
1832
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 GridDataLangs(DataTable vDataTable){ DataGrid myGrid = new DataGrid(); myGrid.AutoGenerateColumns = false;
3
5521
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 time. My doubt is that During compilation the data types of the arguments are known to us the compiler will store the starting address of the fuction based on the arguments Where is the concept of dynamic binding used??
3
3258
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 data source which contains a few records with a picture link and a label. That's simple so far but now I would
13
1681
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
9665
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 example, in c, if a function pointer is used. say something like that float (*pt2Func)(float, float); so the compiler doesn't know what is pt2Func to be called until at run-time. Is it dynamic binding? Thanks.
26
2797
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
8127
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8527
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8215
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8398
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4015
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2529
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.