473,415 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,415 software developers and data experts.

Question about objects and constructors.

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/s...10766&lngWId=3

Oct 9 '06 #1
4 1722

JoeC wrote:
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/s...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
}
};

--
Alan Johnson

Oct 9 '06 #2
LR
JoeC wrote:
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
Oct 9 '06 #3
"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.
Oct 9 '06 #4

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

Oct 9 '06 #5

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

Similar topics

7
by: Master of C++ | last post by:
Hello Folks, I have a programming dilemma with templates and "genericity". I can think of a few solutions, but I am not sure which one is the best in terms of genetic C++ style. Any suggestions...
8
by: Gonçalo Rodrigues | last post by:
Hi all, I have a template class (call it Object) whose instances have a variable size part - an array of of T objects. But this variable size part is fixed at creation time so instead of...
27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
4
by: lars.uffmann | last post by:
Hey everyone! I am (still) working on a project that I took over from former students, so don't blame me for the criminal approach on coding *g* The problem I have is fairly easy and while I...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
14
by: petermichaux | last post by:
Hi, Hopefully the group doesn't mind an(other) inheritance question. Maybe the prototype inheritance style is starting to become a low dim light in my brain...probably not yet. ---- If I...
31
by: cctv.star | last post by:
I was wondering, why you always have to remember to call bases' constructors explicitly from the derived class constructor? Why hasn't this been enforced by the language?
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
17
by: Richard Harter | last post by:
On Tue, 04 Nov 2008 16:43:49 -0500, Eric Sosman <Eric.Sosman@sun.comwrote: I may address the rest of your comments later (for which, thank you very much) but this seems to be a serious point...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.