LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-15-2004, 11:33 AM   #1
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Rep: Reputation: 30
Question Why use OOP when OBP will do?


I'm curious to hear peoples opinions...

I find that in most of my applicatons - I do not need to use true object oriented programming (ie virtual base classes enabling the dynamic binding mechanism).

I just write object based applications, involving composition and inheritance.

It's just that all of the books seem to promote "OOP" which seems only necessary in massive applications of a certain kind.

Thanks for listening!
 
Old 08-15-2004, 01:07 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
i would say it depends on what kind of thing your coding, i used "dynamic binding" not so long ago when coding the solution to a topcoder problem. the relevant code is below

Code:
struct node
{
    virtual int calc() const =0;
};

struct val_node : public node
{
    int val;
    val_node() : val(0) {}
    int calc() const { return val; }
};

struct binop_node : public node
{
    char op;
    node *left, *right;
    binop_node() : op('p'), left(NULL), right(NULL) {}
    int calc() const {
        int l=left->calc();
        int r=right->calc();

        if(op=='d') return l/r; else return l+r;
    }
};
my function created a tree of nodes from the input and then called topNode->calc(), without virtual functions it would have been possible but a bit more of a pain to do and would have taken longer. This wasnt a massive application, it was only 100 lines of code but virtual functions were the quickest way to do it.

there are many things where dynamic binding can be very useful perhaps you have just never attempted anything where it would provide a significant improvement in some way. try the following examples without polymorphism

a) create a parsing tree for some simple language
b) drawing a list of drawable objects
c) implementing different types of ai players(in a game) with a common interface

you should find these much easier with virtual functions and base classes.
 
Old 08-27-2004, 04:56 PM   #3
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Original Poster
Rep: Reputation: 30
Thanks kev82,

Pardon my late response, but my internet access is limited, and I had to think my way throgh your code. I actually see now, that I could have used this technique in previous applications to my advantage.

Thank you again for the info. I feel like revamping some of my apps now

Regards,
unholy
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OBP commands Quick Reference Card ]un]ie Solaris / OpenSolaris 1 07-13-2010 11:58 AM
OOP in PHP patpawlowski Programming 5 11-20-2005 04:37 PM
OOP Help Please InvisibleSniper Programming 41 09-18-2005 03:19 AM
What is true OOP? tumana Programming 4 09-13-2004 06:51 AM
When to use OOP KptnKrill Programming 10 08-24-2003 01:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration