Daniel T. wrote:
"JoeC" <en*****@yahoo.comwrote:
I am trying to design some complex objects that have quite a bit of
data. I understand most syntax but I am trying to learn how to make
better design choices. The first question is to OK or good design to
have large objects with several has-a relationship with other objects.
OK as in does the language allow it? Yes, it's OK. OK as in is it smart?
Here is a little heuristic I like: "most member-functions should use
most member-data most of the time." If you find there is a strong divide
between member-functions that use this group of data, and
member-functions that use that group of data, with little overlap, then
think about encapsulating those two groups of member-data into two
different classes.
Second, I want my unit to have a coord struct.
struct coord{
int x;
int y;
coord(const int xx, const int yy){x = xx; y = yy;}
bool operator ==(const coord c){return c.x == x && c.y == y;}
};
+++++++++++++++
class unit{
protected:
coord loc;
It give me an error. I want to have a place function that will
actually fill in the coords.
Which is better?
Have a default constuctor that sets the x and y to 0 or to:
loc.x = x; loc.y = y;
coord * loc; and in my place function:
loc = new coord(x, y);
IMHO, it is always better to not new an object if you can think of a
different way to accomplish the same thing. In this case, I would be
inclined to implement a 'reset' member-function for coord.
class coord {
int x, y;
public:
coord( int x_, int y_ ): x( x_ ), y( y_ ) { }
void reset( int x_, int y_ ) { x = x_; y = y_; }
};
I know there are many ways to do things and I they might not be
appearent when I am in an earlier stage of devlopment but these choices
have affects that make it harder to expand and maintain my code.
Which is why you want to encapsulate as much as you can. As best as you
can, deal with a coord as a single thing rather than a bag holding two
things. This means that you should avoid accessing coord's 'x' and 'y'
directly outside of coord member-functions (give them private access so
the compiler can help you with that.)
--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer have anyone to discuss your doubts with,
nor any ability to discuss them.
I want to thank all of you who responded. It is all intersting and
will take all of that into consideration. From my perspective, I like
programming and have almost no formal training none in C++. Much of my
modivation is that I don't like the games that are written today. I
have ideas for creating some games. Being an American if you have an
idea do it. I am teaching myself the skills from the ground up.
Outside the big picture modivation, I like programming. I write games
as a fun way to increase my skills. I havn't gone to school or have a
job despite that I don't think every programmer learns programming the
hard way from trial and error. They are mentored and guided in good
programming, I lack that. If I had the skills to get a job, I would be
at a very basic level and would focus on a very small problem. I would
also be exposed to more code and my work would have to fit into that
design. In my projects, I create the design as best I can.
Like the real world, I have a desire to finish and progress in my
projects so I do the best I can to make things work. I do run into
problems where my design makes it difficult to add on to my projects.
In my current project which a rewrite of a game I just finished, I want
to ask questions on how best to design the program. This problem here
that I am asking about in how I can replace int x,y; //location with
the structure I created. Still I have other things that are part of
the units such as color a bitmap graphic as well as the combat factors
of the unit. I am trying to create a better unit class. I plan to
make it dynamic so it can be a ground air or sea unit. I might want to
have other properties added later.
Here is what I have in my header:
Originial:
class unit : public graphic, public color{
std::map<char, coordkeys; //movement engine
coord n; //directions
coord s;
coord e;
coord w;
bool mark;
bool disburse;
int xloc; //x location
int yloc; //ylocation
float attack; //attack factor
float defence; //defence facor
float move; //movemnt factor
float moved; //the number of factors used
int rs, gs, bs;
float stMove;
float stAt;
void make();
public:
unit();
void SetGr(int, int, int, BYTE * c); //sets graphic info
void SetData(float, float, float);//sets ofensive defensive and move
facors
void Place(int, int); //puts on the map
void newMove(char, board *, terrain * trn);
void show(HWND); //displays the unit
void reset(); //restes movemnt
void tomark(){mark = true;} //marks unit for death;
int getXloc()const {return xloc;} //returns location
int getYloc()const {return yloc;}
bool canMove(); //still has movement factors left.
bool marked(){return mark;}
float getAttack(){return attack;}
float getDefence(){return defence;}
void disbersed();
};
This is what I have and am working on:
class unit{
protected:
coord loc;
graphics * gr;
std::vector<color>colors;
float attack;
float defence;
int move;
int moved;
//void create();
public:
unit();
/*
void display();
float getAttack(){return attack;}
float getDefence(){return defence;}
coord getCoord(return loc;}
virtual void move(char);
virtual void place(); */
};