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

How to use objectiented programming.

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.
Jul 9 '07 #1
11 1486
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
Jul 9 '07 #2
Vitor <no**@yahoo.netwrote:
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.
Jul 10 '07 #3
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

Jul 10 '07 #4
On Jul 10, 4:54 am, Thomas Kowalski <t...@gmx.dewrote:
>
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.

Jul 10 '07 #5
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/...tory/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.
Jul 11 '07 #6
gpuchtel wrote:
On Jul 10, 4:54 am, Thomas Kowalski <t...@gmx.dewrote:
>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/...ileinfo/id,38/

Here is the best game I have written. I would like to do better.
Jul 11 '07 #7
Vitor <no**@yahoo.netwrote:
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/...tory/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.
Jul 11 '07 #8
Vitor <no**@yahoo.netwrote:
http://www.games.siten.nl/component/...,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.
Jul 11 '07 #9
Daniel T. wrote:
Vitor <no**@yahoo.netwrote:
>http://www.games.siten.nl/component/...,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.
Jul 11 '07 #10
Daniel T. wrote:
Vitor <no**@yahoo.netwrote:
>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/...tory/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.
Jul 11 '07 #11
On Jul 11, 11:25 pm, Vitor <n...@yahoo.netwrote:
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:ja*********@gmail.com
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

Jul 12 '07 #12

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

Similar topics

5
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented...
12
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now...
3
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as...
134
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've...
42
by: Kevin Spencer | last post by:
Is it just me, or am I really observing a trend away from analysis and probem-solving amongst programmers? Let me be more specific: It seems that every day, in greater numbers, people are coming...
2
by: - | last post by:
I kinda like programming (Java is the only language I know) but have not been able to develop a complete software or get anywhere close to it. The reason why I like it is because it gives me great...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
30
by: Jakle | last post by:
I have been googling, but can seem to find out about C GUI libraries. My main platform is Windows, but it would be nice to find a cross platform library. I've been programming with php, which...
111
by: Enteng | last post by:
Hi I'm thinking about learning C as my first programming language. Would you recommend it? Also how do you suggest that I learn it?What books/tutorials should I read for someone like me? Thanks...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.