Question about objects and constructors.

J

JoeC

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.


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);


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.

I have a working copy of the game:
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=10766&lngWId=3
 
A

Alan Johnson

JoeC said:
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.


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);


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.

I have a working copy of the game:
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=10766&lngWId=3

If I understand your problem correctly, you want to use initialization
lists. That is:

class unit
{
protected:
coord loc;
public:
unit(int x, int y)
: loc(x, y)
{
// whatever
}
};
 
L

LR

JoeC said:
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.

Depends. How are all those things related?
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:

Is that where a coord should set itself to be if the user isn't going to
explicitly give it values? If so, then probably yes.


Doing something like this:
loc.x = x; loc.y = y;
In code that isn't in a member function of your class/struct is almost
always a bad idea.




coord * loc; and in my place function:

loc = new coord(x, y);

Pointers are probably not a good solution for this. And it'll cause you
problems. And if you're going to use pointers, then please look into
std::auto_ptr and the boost pointers.



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.

Learning to make choices that make it easier to expand and maintain code
is part of the process.

And there are many ways of doing things, however, people often settle on
a particular way because of the many advantages.

I haven't tried compiling this, so beware.

class Coordinate {
int x_;
int y_;
public:
void swap(Coordinate &c) {
std::swap(c.x_,x_);
std::swap(c.y_,y_);
}
Coordinate() : x_(0), y_(0) {}
Coordinate(const int x, const int y) : x_(x), y_(y) {}
Coordinate(const Coordinate &c) : x_(c.x_), y_(c.y_) {}
Coordinate &operator=(const Coordinate &c) {
Coordinate temp(c);
swap(temp);
return *this;
}
friend bool operator==(const Coordinate &c1, const Coordinate &c2);
};

bool operator==(const Coordinate &c1, const Coordinate &c2) {
const bool result = c1.x_ == c2.x_ && c1.y_ == c2.y_;
return result;
}

Of course, YMMV.

Then you can make Unit like this.
class Unit {
Coordinate coord_;
....
public:
void swap(Unit &u) {
u.coord_.swap(coord_);
...
}

// I'm aware that your class has other members
// edit code appropriately
Unit() : coord_() {}

// if you're ctoring a unit with an explicit Coordinate.
Unit(const Coordinate &c) : coord_(c) {}

Unit(const Unit &u) : coord_(u.coord_) {}
Unit &operator=(const Unit &u) {
Unit temp(u);
swap(temp);
return *this;
}

// if you want to set the coordinate try something like this
void coord(const Coordinate &c) {
coord_ = c;
}

// if you want a getter function maybe this
const Coordinate &coord() const {
return coord_;
}

// if you want to see if two Units have the same
// Coordinates, perhaps this.
// note that this is a const member function
bool hasSameCoordinateAs(const Unit &u) const {
return coord_ == u.coord_;
}
....
};

Or you could make a global like:
bool UnitsAtSameCoordinate(const Unit &u1, const Unit &u2) {
return u1.coord() == u2.coord();
}

BTW, I still feel that my earlier advice applies.

LR
 
D

Daniel T.

JoeC said:
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.)
 
J

JoeC

Daniel said:
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.


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_; }
};


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.)

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, coord> keys; //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(); */
};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top