LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why use OOP when OBP will do? (https://www.linuxquestions.org/questions/programming-9/why-use-oop-when-obp-will-do-217898/)

unholy 08-15-2004 11:33 AM

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!

kev82 08-15-2004 01:07 PM

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.

unholy 08-27-2004 04:56 PM

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


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