473,569 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1737

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(cons t int x, const int y) : x_(x), y_(y) {}
Coordinate(cons t Coordinate &c) : x_(c.x_), y_(c.y_) {}
Coordinate &operator=(cons t Coordinate &c) {
Coordinate temp(c);
swap(temp);
return *this;
}
friend bool operator==(cons t Coordinate &c1, const Coordinate &c2);
};

bool operator==(cons t 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(c oord_);
...
}

// 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=(cons t 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 hasSameCoordina teAs(const Unit &u) const {
return coord_ == u.coord_;
}
....
};

Or you could make a global like:
bool UnitsAtSameCoor dinate(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(){ret urn attack;}
float getDefence(){re turn defence;}
void disbersed();
};
This is what I have and am working on:
class unit{
protected:

coord loc;
graphics * gr;
std::vector<col or>colors;
float attack;
float defence;
int move;
int moved;

//void create();

public:
unit();
/*
void display();
float getAttack(){ret urn attack;}
float getDefence(){re turn 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
1637
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 will be greatly appreciated. I am writing a template class Polynomial that encapsulates the functionalities of a Polynomial. Here is a rough...
8
1546
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 allocating two blocks (one for the object and one for the variable-sized part) one can allocate a single block via a placement new operator like ...
27
3370
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 as ff: typedef struct { float *data ; int count ;
4
2002
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 have solutions, none of them seems really "clean" to me and I was wondering if someone here maybe had a better idea: class geometry { ... }
15
1886
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 two? (1)
14
1481
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 do the following...
31
1627
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
2104
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 BufferBase, and customized in order to be able to a type of data (of any kind, such as chunks of audio samples from a sound card). The processor...
55
3932
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 built-in-like behavior for objects of class type. That leads to the "necessity" for the exception machinery so that errors from constructors can be handled....
17
3360
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 of confusion so let me expand. What we can have is the following sequence of events: At time A we create bobble X. Space will be allocated for X...
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.