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.