How to use objectiented programming.

Discussion in 'C++' started by Vitor, Jul 9, 2007.

  1. Vitor

    Vitor Guest

    I have been writing some large programs and games lately and I have had
    people advise me that I need to write more object oriented programs. I
    also I understand the concept and have written some small programs to
    understand the theory.


    class unit{
    protected:
    int locx;
    int locy;

    int atk;
    int dfce;
    void create();
    public:
    unit();
    unit(std::istream);
    virtual ~unit(){};
    virtual unit* copy() const;
    virtual void move();
    virtual void attack();
    virtual void display();
    };

    class hunit{

    unit * p;
    int * cnt;
    public:
    hunit() : cnt(new int(1)), p(new unit) {}
    hunit(char);
    hunit(const hunit& u) : cnt(u.cnt), p(u.p) {++*cnt;}
    hunit& operator = (const hunit&);
    hunit(std::istream);
    ~hunit();
    void move();
    void attack();
    void display();
    };

    This all works. I am trying to show that I understand how to do dynamic
    binding but not so well how to use it and how it can make my programs
    better. I know about the shape example but often times, I don't find
    this kind of programming useful for what I do.

    What I am more interested in is how I can look a programming from an
    object oriented perspective. I also try to use inheritance in my
    programs to save rewriting common parts of different objects. What is a
    good way to go about stating with small but useful programs or solving
    problems that I can learn from to help me see programming more from the
    perspective of object orientation.

    One last question about this program is about friends. That is another
    feature I seldom use. I often find that when I create an object then
    put it in a handle like I have above I often have to duplicate writing
    the accessor functions:

    virtual void move();
    virtual void attack();
    virtual void display();

    void move();
    void attack();
    void display();

    If I add something to the object in development, I have to go back and
    add the functions to the handle. I am not looking for specific answer
    to some piece of code but advice on how I can become a better programmer
    and tap into the power that C++ offers.
    Vitor, Jul 9, 2007
    #1
    1. Advertising

  2. Vitor wrote:
    > I have been writing some large programs and games lately and I have
    > had people advise me that I need to write more object oriented
    > programs. I also I understand the concept and have written some
    > small programs to understand the theory.
    > [..]


    Find a copy of "Advanced C++: Programming Styles and Idioms" by James
    Coplien, and also his "Multi-Paradigm Design for C++". I bet that if
    you go to 'www.accu.org' and look around in the book review section,
    you'll find plenty of interesting OOP works. Also, ask in the forum
    'comp.object' for more book recommendations (mention C++ to avoid
    having to wade through Smalltalk or Eiffel).

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 9, 2007
    #2
    1. Advertising

  3. Vitor

    Daniel T. Guest

    Vitor <> wrote:

    > This all works. I am trying to show that I understand how to do dynamic
    > binding but not so well how to use it and how it can make my programs
    > better. I know about the shape example but often times, I don't find
    > this kind of programming useful for what I do.


    If you don't find it useful, then don't use it. OO concepts do a good
    job of removing duplication in many contexts. For example, if you have
    five 'if/switch' statements all branching off of the same variable it is
    a simple matter to use the virtual dispatch mechanism to replace them
    all.

    This can reduce the cyclometric complexity of your program as a whole
    which makes it easer to understand and maintain.
    Daniel T., Jul 10, 2007
    #3
  4. > This all works. I am trying to show that I understand how to do dynamic
    > binding but not so well how to use it and how it can make my programs
    > better. I know about the shape example but often times, I don't find
    > this kind of programming useful for what I do.


    Well, in OOP (object-oriented-programming) polymorphism is one of the
    most important things.
    Polymorphism means that you call a method like "move" or "attack" on
    an object and different things happen.

    class Actor {
    private :
    m_life_points;
    public:
    virtual void attack( Actor &foo ) = 0;
    virtual void move() = 0;
    virtual void reduce_life_points() {m_life_points--;};
    }

    class Hero : public Actor {
    virtual void attack( Actor &foo ) {
    swing_sword();
    foo.reduce_life_points();
    }
    virtual void move() {
    run_forward();
    }
    }

    class Monster : public Actor {
    virtual void attack( Actor &foo) {
    use_claws();
    foo.reduce_life_points();
    }
    virtual void move() {
    run_forward();
    }
    }


    In this case the main loop might do something like this (pseudo-code):

    for each actor in actors do
    actor.move()

    without caring whether actor is a hero or a monster or any other kind
    of actor that might be added to the game in the future. Unlike using
    an switch statement there is no need to change the loop to add more
    kinds of actors.

    To sum it up. If you want to execute different behavior at _RUN-TIME_
    (in opposite to Compile-time) there is a good chance that OOP is
    beneficial for you.

    For me it was helpful to learn about design pattern to fully
    understand OO. However since design pattern are not easy to understand
    without knowledge of OOP you should try an easy lecture like "Head
    First Design Pattern". It's fun to read and quick helpful to gasp OO.

    Regards,
    Thomas Kowalski
    Thomas Kowalski, Jul 10, 2007
    #4
  5. Vitor

    gpuchtel Guest

    On Jul 10, 4:54 am, Thomas Kowalski <> wrote:

    >
    > For me it was helpful to learn about design pattern to fully
    > understand OO. However since design pattern are not easy to understand
    > without knowledge of OOP you should try an easy lecture like "Head
    > First Design Pattern". It's fun to read and quick helpful to gasp OO.
    >


    I echo Thomas's recommendation regarding the book: "Head First Design
    Patterns". If anyone is interested I converted the examples from this
    book from Java to C++, which are available at https://sourceforge.net/projects/hfdp-cpp

    A quick blurb: Three separate translations are/where planned: The
    'Bronze' version is a literal translation; minimal (syntax) changes
    only; it is a verbatim translation from the book. The 'Silver' version
    is a semantic translation; more C++ idioms and I created additional
    examples for their "leftover patterns", which is still a work-in-
    progress. 'Gold' is (forthcoming and) modern; template based.

    Hopefully, you'll find the C++ versions helpful. But first, a
    disclaimer: the three separate translations were meant to exhibit an
    evolutionary progression from Java to C++ ('Bronze' to 'Gold')
    exhibiting more C++ idioms as it went along, culminating with a modern
    (template) 'Gold' version. I am not totally satisfied with the
    'Silver' version and I have yet to begin work on the 'Gold' version -
    I spend too much time reading these news groups and performing my
    'real' job. If you do choose to download them please take a moment and
    read the 'readme', it will explain some of the motivation for some of
    my conventions.
    gpuchtel, Jul 10, 2007
    #5
  6. Vitor

    Vitor Guest

    Thomas Kowalski wrote:
    >> This all works. I am trying to show that I understand how to do dynamic
    >> binding but not so well how to use it and how it can make my programs
    >> better. I know about the shape example but often times, I don't find
    >> this kind of programming useful for what I do.

    >
    > Well, in OOP (object-oriented-programming) polymorphism is one of the
    > most important things.
    > Polymorphism means that you call a method like "move" or "attack" on
    > an object and different things happen.
    >
    > class Actor {
    > private :
    > m_life_points;
    > public:
    > virtual void attack( Actor &foo ) = 0;
    > virtual void move() = 0;
    > virtual void reduce_life_points() {m_life_points--;};
    > }
    >
    > class Hero : public Actor {
    > virtual void attack( Actor &foo ) {
    > swing_sword();
    > foo.reduce_life_points();
    > }
    > virtual void move() {
    > run_forward();
    > }
    > }
    >
    > class Monster : public Actor {
    > virtual void attack( Actor &foo) {
    > use_claws();
    > foo.reduce_life_points();
    > }
    > virtual void move() {
    > run_forward();
    > }
    > }
    >
    >
    > In this case the main loop might do something like this (pseudo-code):
    >
    > for each actor in actors do
    > actor.move()
    >
    > without caring whether actor is a hero or a monster or any other kind
    > of actor that might be added to the game in the future. Unlike using
    > an switch statement there is no need to change the loop to add more
    > kinds of actors.
    >
    > To sum it up. If you want to execute different behavior at _RUN-TIME_
    > (in opposite to Compile-time) there is a good chance that OOP is
    > beneficial for you.
    >
    > For me it was helpful to learn about design pattern to fully
    > understand OO. However since design pattern are not easy to understand
    > without knowledge of OOP you should try an easy lecture like "Head
    > First Design Pattern". It's fun to read and quick helpful to gasp OO.
    >
    > Regards,
    > Thomas Kowalski
    >



    Yes, to respond to this post and all the others. I know the general
    principle of OO programming. I use objects all the time but seldom use
    dynamic binding. This is not so much about how but why. I know that it
    is a great deal to ask but I will link to some of my work. I have
    written some pretty large programs and in one case I set it up for
    dynamic binding but never added the the different kinds of things that
    would require using all that.

    http://www.games.siten.nl/component/option,com_remository/Itemid,6/
    My latest is SBGII.7 and I have a project called SBGIII unfinished in
    demos. They are my latest works.

    I have some demos and full games. I just want to write better code with
    C++. Basically I would like some clues on how to have better program
    design. I have read some books and I can do the code but I am trying to
    get some ideas to what are the best solutions to common problems.

    I would like some good starting points for object oriented programs.
    Vitor, Jul 11, 2007
    #6
  7. Vitor

    Vitor Guest

    gpuchtel wrote:
    > On Jul 10, 4:54 am, Thomas Kowalski <> wrote:
    >
    >> For me it was helpful to learn about design pattern to fully
    >> understand OO. However since design pattern are not easy to understand
    >> without knowledge of OOP you should try an easy lecture like "Head
    >> First Design Pattern". It's fun to read and quick helpful to gasp OO.
    >>

    >
    > I echo Thomas's recommendation regarding the book: "Head First Design
    > Patterns". If anyone is interested I converted the examples from this
    > book from Java to C++, which are available at https://sourceforge.net/projects/hfdp-cpp
    >
    > A quick blurb: Three separate translations are/where planned: The
    > 'Bronze' version is a literal translation; minimal (syntax) changes
    > only; it is a verbatim translation from the book. The 'Silver' version
    > is a semantic translation; more C++ idioms and I created additional
    > examples for their "leftover patterns", which is still a work-in-
    > progress. 'Gold' is (forthcoming and) modern; template based.
    >
    > Hopefully, you'll find the C++ versions helpful. But first, a
    > disclaimer: the three separate translations were meant to exhibit an
    > evolutionary progression from Java to C++ ('Bronze' to 'Gold')
    > exhibiting more C++ idioms as it went along, culminating with a modern
    > (template) 'Gold' version. I am not totally satisfied with the
    > 'Silver' version and I have yet to begin work on the 'Gold' version -
    > I spend too much time reading these news groups and performing my
    > 'real' job. If you do choose to download them please take a moment and
    > read the 'readme', it will explain some of the motivation for some of
    > my conventions.
    >



    If you wrote the book and that is cool. Will the book help me with what
    I want to learn? I want to get away from syntax and learn how to look
    at problems. I did post a link with some of my work. I have been
    called a good structural programmer but I want to get to where I can
    create object oriented programs.

    I have created the best games I have with the knowledge I have learned
    but I can't seem to break the mold of structural programming and take it
    to the next level. I have read Ruminations and Accelerated C++ by K and
    M. They are great books. I like Ruminations because for me it is so
    interesting although I often get lost. I am self taught and have little
    school. It is not so much the school and training that I lack it is
    mentor ship.

    http://www.games.siten.nl/component/option,com_remository/Itemid,2/func,fileinfo/id,38/

    Here is the best game I have written. I would like to do better.
    Vitor, Jul 11, 2007
    #7
  8. Vitor

    Daniel T. Guest

    Vitor <> wrote:

    > Yes, to respond to this post and all the others. I know the general
    > principle of OO programming. I use objects all the time but seldom use
    > dynamic binding.


    There is nothing wrong with that. It may be that there is no use for
    dynamic binding in the kinds of programs you write.

    > This is not so much about how but why. I know that it
    > is a great deal to ask but I will link to some of my work. I have
    > written some pretty large programs and in one case I set it up for
    > dynamic binding but never added the the different kinds of things that
    > would require using all that.
    >
    > http://www.games.siten.nl/component/option,com_remository/Itemid,6/
    > My latest is SBGII.7 and I have a project called SBGIII unfinished in
    > demos. They are my latest works.


    In the above project... the one place where you use 'virtual' (the
    Bitmap's destructor) is inappropriate, you should remove it.

    You have very little dynamic behavior at all in the program so there
    isn't much that would benefit from OO.

    I notice that you have several (6 or 7 depending on if the *.txt file is
    actually supposed to be code) switches based on the same named variable
    of type UINT. You might benefit from having a Factory class so that you
    can remove all but one of those switch statements. Unfortunately, I
    don't know enough about Windows programming to say if this is a good
    idea.
    Daniel T., Jul 11, 2007
    #8
  9. Vitor

    Daniel T. Guest

    Daniel T., Jul 11, 2007
    #9
  10. On Tue, 10 Jul 2007 20:27:01 -0500, Vitor <> wrote:
    >I have created the best games I have with the knowledge I have learned
    >but I can't seem to break the mold of structural programming and take it
    >to the next level. I have read Ruminations and Accelerated C++ by K and
    >M. They are great books. I like Ruminations because for me it is so
    >interesting although I often get lost.


    To further increase your confusion, 'Accelerated C++' isn't
    object-oriented at all. Koenig and Moo changed their programming
    paradigm between the two books from OO to value-oriented/functional.

    >I am self taught and have little
    >school. It is not so much the school and training that I lack it is
    >mentor ship.


    I'd first structure the domain (e.g. the Simple Board Game) into
    entities (objects) without even thinking of polymorphism. If you
    notice structural similarities between objects or if you start copying
    code for re-use you may think of abstraction mechanisms like
    polymorphism in a second or third step.


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
    Roland Pibinger, Jul 11, 2007
    #10
  11. Vitor

    Vitor Guest

    Daniel T. wrote:
    > Vitor <> wrote:
    >
    >> http://www.games.siten.nl/component/option,com_remository/Itemid,2/func,filein
    >> fo/id,38/
    >>
    >> Here is the best game I have written. I would like to do better.

    >
    > AFAICT, you seem have a whole bunch of globals, I think the next step in
    > your trek to learn programming is work harder at removing them.
    >
    > I don't know the Windows API so I can't help much in that regard, sorry.


    OK, that is useful advice. The reason why I use globals is because the
    functions that control the windows actions have fixed data sets. I am
    not sure how to get around that problem.

    > BOOL CALLBACK startProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){


    These are the set parameters for the function for a dialog box there is
    no other way to send data to the function and get it back for use in the
    rest of the program.
    Vitor, Jul 11, 2007
    #11
  12. Vitor

    Vitor Guest

    Roland Pibinger wrote:
    > On Tue, 10 Jul 2007 20:27:01 -0500, Vitor <> wrote:
    >> I have created the best games I have with the knowledge I have learned
    >> but I can't seem to break the mold of structural programming and take it
    >> to the next level. I have read Ruminations and Accelerated C++ by K and
    >> M. They are great books. I like Ruminations because for me it is so
    >> interesting although I often get lost.

    >
    > To further increase your confusion, 'Accelerated C++' isn't
    > object-oriented at all. Koenig and Moo changed their programming
    > paradigm between the two books from OO to value-oriented/functional.


    Thanks for the advice. AC++ is good for learning the syntax of C++ and
    to be efficient in code not how to write programs in OO. I like it
    because it got me using libraries rather than mucking around with arcane
    code that is a hold over from C.
    >
    >> I am self taught and have little
    >> school. It is not so much the school and training that I lack it is
    >> mentor ship.

    >
    > I'd first structure the domain (e.g. the Simple Board Game) into
    > entities (objects) without even thinking of polymorphism. If you
    > notice structural similarities between objects or if you start copying
    > code for re-use you may think of abstraction mechanisms like
    > polymorphism in a second or third step.


    I did keep that in mind and I tried to do more of than in my next project
    with my graphics objects. I often have to choose between hasa
    relationships vs isa.
    There are some objects that I could better polymorph such as my tbox
    that draws a
    marker around pieces.

    Thanks for looking at my game.
    Vitor, Jul 11, 2007
    #12
  13. Vitor

    Vitor Guest

    Daniel T. wrote:
    > Vitor <> wrote:
    >
    >> Yes, to respond to this post and all the others. I know the general
    >> principle of OO programming. I use objects all the time but seldom use
    >> dynamic binding.

    >
    > There is nothing wrong with that. It may be that there is no use for
    > dynamic binding in the kinds of programs you write.
    >
    >> This is not so much about how but why. I know that it
    >> is a great deal to ask but I will link to some of my work. I have
    >> written some pretty large programs and in one case I set it up for
    >> dynamic binding but never added the the different kinds of things that
    >> would require using all that.
    >>
    >> http://www.games.siten.nl/component/option,com_remository/Itemid,6/
    >> My latest is SBGII.7 and I have a project called SBGIII unfinished in
    >> demos. They are my latest works.

    >
    > In the above project... the one place where you use 'virtual' (the
    > Bitmap's destructor) is inappropriate, you should remove it.


    Thanks that is good advice. Should they be regular ones?
    >
    > You have very little dynamic behavior at all in the program so there
    > isn't much that would benefit from OO.
    >
    > I notice that you have several (6 or 7 depending on if the *.txt file is
    > actually supposed to be code) switches based on the same named variable
    > of type UINT. You might benefit from having a Factory class so that you
    > can remove all but one of those switch statements. Unfortunately, I
    > don't know enough about Windows programming to say if this is a good
    > idea.


    The TXT files are code that didn't work and I save and start over. I
    probibly went back and make significant changes because that function
    didn't work but wanted to refer to the parts that did work.
    Vitor, Jul 11, 2007
    #13
  14. Vitor

    James Kanze Guest

    On Jul 11, 11:25 pm, Vitor <> wrote:
    > Roland Pibinger wrote:
    > > To further increase your confusion, 'Accelerated C++' isn't
    > > object-oriented at all. Koenig and Moo changed their programming
    > > paradigm between the two books from OO to value-oriented/functional.


    I think it's more a question of the books having different
    subjects.

    > Thanks for the advice. AC++ is good for learning the syntax
    > of C++ and to be efficient in code not how to write programs
    > in OO. I like it because it got me using libraries rather
    > than mucking around with arcane code that is a hold over from
    > C.


    No one book can teach you all you need to know. AC++, I
    believe, is about learning C++, and not about software design,
    project management, how to use an editor, or any of a number of
    other things you'll need to know to successfully write a large
    program. From what I've heard, it's an exceptionally good book
    for learning C++. I'll admit that I've never read it. By the
    time it appeared, I didn't feel the need to read an introductory
    text in C++.

    I have a similar problem in recommending a book on OO. I
    learned OO from the Booch, with cloud diagrams, etc., and I've
    not felt a need to read an introductory text recently. So any
    recommendations I could make would likely be out of date.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 12, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grey

    Use SQL or ASP.NET programming

    Grey, Sep 23, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    352
    Scott Allen
    Sep 23, 2004
  2. Matt
    Replies:
    35
    Views:
    10,563
    George Neuner
    Jul 22, 2004
  3. jm
    Replies:
    3
    Views:
    508
    John Harrison
    Feb 16, 2004
  4. Casey Hawthorne
    Replies:
    4
    Views:
    984
    Jarek Zgoda
    Aug 4, 2006
  5. Joe Mayo
    Replies:
    168
    Views:
    3,246
    David Thompson
    Oct 22, 2007
Loading...

Share This Page