473,803 Members | 3,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

seeking an opinion from the collective wisdom here

[edited to try and eliminate word wrapping in code]

I was simply wondering if this was a mal-formed class header:

#ifndef _items_h_
#define _items_h_

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Item
{
public:
Item( float weight = 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink= false,\
bool set_can_eat = false,\
bool set_can_use = false,\
bool set_is_consumab le = false,\
bool set_is_magical = false );
//Below are the set and get functions for
//all variables in this class

void set_weight( float weight )
{ item_weight = weight; }
float get_weight()
{ return item_weight; }

void set_can_equip( bool set_can_equip )
{ can_equip = set_can_equip; }
bool get_can_equip()
{ return can_equip; }

void set_can_attack( bool set_can_attack )
{ can_attack = set_can_attack; }
bool get_can_attack( ) { return can_attack; }

void set_can_defend( bool set_can_defend )
{ can_defend = set_can_defend; }
bool get_can_defend( ) { return can_defend; }

void set_can_drink( bool set_can_drink )
{ can_drink = set_can_drink; }
bool get_can_drink() { return can_drink; }

void set_can_eat( bool set_can_eat )
{ can_eat = set_can_eat; }
bool get_can_eat() { return can_eat; }

void set_can_use( bool set_can_use )
{ can_use = set_can_use; }
bool get_can_use() { return can_use; }

void set_is_consumab le( bool set_is_consumab le )
{ is_consumable = set_is_consumab le; }
bool get_is_consumab le() { return is_consumable; }

void set_is_magical( bool set_is_magical )
{ is_magical = set_is_magical; }
bool get_is_magical( ) { return is_magical; }

void describe(); // simply returns description (see below)
void clear_describe( ); // clears description
void add_describe( string line_to_add );
/*Line above checks to see if description is set to either
need_set or desc_cleared, and if so description.cle ar()
then push_back( line_to_add ), else just
push_back( line_to_add)*/

private:
// These are stats that will be set for every item created
string need_set; // description is initialized with this
string desc_cleared; // description will be set to this
//after it has been cleared
vector<stringde scription;
// This is to allow multi-line descriptions
float item_weight; // These are pretty self explanatory I think
bool can_equip;
bool can_attack;
bool can_defend;
bool can_drink;
bool can_eat;
bool can_use;
bool is_consumable;
bool is_magical;
};

#endif

but as I read along I was confused over something. The above works just
fine, but I was also wondering should the constructor be this:

Item( float weight = 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink = false,\
bool set_can_eat = false,\
bool set_can_use = false,\
bool set_is_consumab le = false,\
bool set_is_magical = false );

with a constructor body like this in the cpp file:

Item::Item( float weight,\
bool set_can_equip,\
bool set_can_attack, \
bool set_can_defend, \
bool set_can_drink,\
bool set_can_eat,\
bool set_can_use )
{
item_weight = weight;
can_equip = set_can_equip;
can_attack = set_can_attack;
can_defend = set_can_defend;
can_drink = set_can_drink;
can_eat = set_can_eat;
can_use = set_can_use;
is_consumable = set_is_consumab le;
is_magical = set_is_magical;
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.cle ar();
description.pus h_back( need_set );
}

or this:

Item() : float weight( 0 ),\
bool can_equip( false ),\
bool can_attack( false ),\
bool can_defend( false ),\
bool can_drink( false ),\
bool can_eat( false ),\
bool can_use( false ),\
bool is_consumable( false ),\
bool is_magical( false );

and lose the entire body of the constructor in the cpp file? I will need
to pass in parameters to override the defaults when I instantiate the
objects.

Question 1: Is this a mal-formed class header?
Question 2: Which should I use for the initialization list?

Thanks in advance.
DN
--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
Jun 2 '07
15 1215
Jim Langston wrote:
The users of the class shouldn't worry about what my variable is called.
Drinkable is already a boolean concept (yes or no) so why should I have to
say IsDrinkable? And if I want to set it, why have to say SetDrinkable?
Doesn't Drinkable( true ) express that? I saw the trailing underscores used
and found it the best to my liking, so I use it.

Thank you for explaining that. I thought maybe it was a matter of the
"accepted" way of doing things. If I understand you correctly it is a
matter of personal taste. That is the reason for the name is_drinkable
and such like. I ask myself whether an item is_drinkable. Just an easier
way to remember it (for me.) I did adopt the underscore idea though.
Even with overloading and the such-like, I don't really feel comfortable
naming more than one object the same names unless it makes operational
sense. This pointed that out to me.

Thanks again
DN
--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
Jun 7 '07 #11
On Jun 3, 2:24 am, Devon Null <theronnights.. .@xgmailx.comwr ote:
void set_weight( float weight )
{ item_weight = weight; }
float get_weight()
{ return item_weight; }
etc. etc.

There's a huge amount of redundant code for all of these accessors.
You can use a public templated functor object to provide all of this
for free. Personally I wouldn't use the set_ or get_ in C++ either.

This sort of thing will do it (not run through a compiler):

template< typename T >
class Accessor {
T m_t;
public:
Accessor() : m_t() {}
Accessor( const T &t ) : m_t( t ) {}

const T &operator() () const { return m_t; }
void operator() ( const T &t ) { m_t = t; }
};

class Item {
public:
Accessor< float weight /* etc. */;
Accessor< bool can_equip, can_attack /* etc. */;
};

http://www.kirit.com/C%2B%2B%20kille...et%20accessors
K

Jun 7 '07 #12
On Jun 2, 3:24 pm, Devon Null <theronnights.. .@xgmailx.comwr ote:
[snip]
class Item
{
public:
Item( float weight = 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
Heh heh. "There are twisty passages in the immediate vicinity."

[snips]
//Below are the set and get functions for
//all variables in this class

void set_weight( float weight )
{ item_weight = weight; }
float get_weight()
{ return item_weight; }
Another poster has already talked about all these
get-set functions.

Think about this object in a different way. What is
the purpose of this object in your program? What
service does it provide? What job does it do?

Sometimes it is appropriate to have that job be,
in effect, "carry around all these parameters like
a big basket full of data, hold them up when asked,
and take new values when asked." And if that is the
task, then certainly get-set may be appropriate.
And the template method of doing them may be a good
idea as well.

But the name, and some of the member variable names,
suggest you may be missing a chance for some good
object oriented design practice. What about making
that job description be a bit more closely related
to the task your application is intended for. How
about something like: Act as one object that the
character can manipulate?

Then you might not want all the get-set functions.
Instead, you may want things like

bool PutInPack(){// etc.}

with appropriate conditions and reports. Maybe it does
it if it can, but if it can't it leaves it where it
was and reports to the user "you can't put that in
your backpack." Or a function

bool Eat(){// etc.}

and if it works, the object is removed (destroyed)
but if it fails, it reports "You can't eat that!"
or something. Or maybe it should be

int Eat(){// etc.}

and return the number of turns of food that worked
out to, with reporting the result. And so on. Maybe
eating an apple gives you, say, 10 turns of food
and converts the apple into an apple core.

If you do all that, it's quite likely that you
won't ever call many of those get-set functions.
And you will start to think about where the code
to do something like DefendWith() belongs. Which
may (or may not) be in the Item class.

You will also start to think about which classes
have to know about which other classes. Does the
Item class have to know about the Backpack class?
Or does the Backpack class have to know about the
Item class? Or neither? Or both? And that gets you
into lots of other good OOP practice.
Socks

Jun 7 '07 #13
Puppet_Sock wrote:
Think about this object in a different way. What is
the purpose of this object in your program? What
service does it provide? What job does it do?

Sometimes it is appropriate to have that job be,
in effect, "carry around all these parameters like
a big basket full of data, hold them up when asked,
and take new values when asked." And if that is the
task, then certainly get-set may be appropriate.
And the template method of doing them may be a good
idea as well.
That is exactly my goal. This is meant to be a base class for all
objects in the game that could be called items - weapons, armor, food,
potions, scrolls, etc. I would have made this pure virtual if I could
think of a function that would need to be defined differently for all
the derived classes. If I figure it out I will add it.
>
What about making that job description be a bit more closely related
to the task your application is intended for. How
about something like: Act as one object that the
character can manipulate?
That is the job of my derived classes, at least in the class hierarchy
in my head.
Then you might not want all the get-set functions.
I use the get and set names simply for ease of remembrance on my part.
To me (the only one I am writing code for ATM) they are self-explanatory.
>
bool PutInPack(){// etc.}
The Bag base class I came up with takes care of that. (See the post
titled "Am I reinventing the wheel?")
>
with appropriate conditions and reports. Maybe it does
it if it can, but if it can't it leaves it where it
was and reports to the user "you can't put that in
your backpack." Or a function

bool Eat(){// etc.}

and if it works, the object is removed (destroyed)
but if it fails, it reports "You can't eat that!"
or something. Or maybe it should be

int Eat(){// etc.}

and return the number of turns of food that worked
out to, with reporting the result. And so on. Maybe
eating an apple gives you, say, 10 turns of food
and converts the apple into an apple core.
I was going to handle that outside of any classes, but I might have to
rethink that. I may add that to a Food class or the such like. Maybe
that (replace item with another item after certain condition are met)
will be my pure virtual function.
You will also start to think about which classes
have to know about which other classes. Does the
Item class have to know about the Backpack class?
Or does the Backpack class have to know about the
Item class? Or neither? Or both? And that gets you
into lots of other good OOP practice.
Socks
See my last comment, but this is the kind of advice and direction I can use.

I do appreciate all the constructive advice I am getting. I know it is
ambitious to use something of this complexity (the game as a whole) as a
way to learn the language, but I have tried other ways and this one
seems to be working. I have learned a great many concepts, and others
FINALLY make sense (i.e. classes).

Thanks again for everyone's help
DN
--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
Jun 7 '07 #14
On Jun 7, 4:40 pm, Devon Null <theronnights.. .@xgmailx.comwr ote:
[snip]
I was going to handle that outside of any classes,
but I might have to rethink that.
The more C++ I write, the less code I put outside
of classes. The most recent app I wrote had nearly
everything inside classes. The main function checked
there were the right number of user inputs, created
the object that did most of the work, then called that
object's "do most of the work" function. Altogether
about 10 lines of code.

Now there were quite a few utility functions. But
these were entirely generic. So, things like I was
noticing I was doing a particular format conversion
in many places, so I made a func to do that.

And I'm getting more into the habit of, and getting
more adept at, using templates and standard library
features. So the number of util funcs I write in a
code is falling also.
Socks

Jun 7 '07 #15
Puppet_Sock wrote:
>
And I'm getting more into the habit of, and getting
more adept at, using templates and standard library
features. So the number of util funcs I write in a
code is falling also.
Socks
Templates are a weak point for me (read: I know jack about them.) I am
trying to use as many standard library features as I can. I add them as
I learn them. Unfortunately the only person I have to teach me the vast
majority of C++ is myself, both usage and the language itself. Kind of
like writing a debugger and using it to debug itself.

DN

--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
Jun 8 '07 #16

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

Similar topics

0
1749
by: Craig S. Ugoretz | last post by:
Dear newgroup readers, I am pleased to announce the presentation of the new open source software project I have created, called the "Wisdom Seeker IDE". For a description of what open source software is about, see the web site http://www.opensource.org/. The web site for my(our) project is http://ws-ide.sourceforge.net/ws-ide.htm. It has a philosophical/political theme to it, based upon a work by the Greek philosopher Plato called...
0
1473
by: Melody Droid | last post by:
I recently made some conceptual breakthroughs regarding the interval permutation pre-sorting and the pitch-class-set/scale post-sorting aspects of my envisioned symmetrical melody thesaurus software, so that it will take the absolute minimum of time and effort to write a computer program for it and bring it up to the demo level in order to shop the project around. I've created a Yahoo Group for the project over here: ...
42
9753
by: Steven O. | last post by:
I am seeking some kind of tool that I can use for GUI prototyping. I know how to use Visual Basic, but since a lot of software is being coded in Java or C++, I'd like to learn a Java or C++ -based tool. Back when I took my Java and C++ classes (two or three years ago), the available tools -- at least the ones I could find -- were still not as easy, not as "drag-and-drop", as Visual Basic. Has that changed? Is there some software out...
28
2610
by: Maboroshi | last post by:
Hi I am fairly new to programming but not as such that I am a total beginner From what I understand C and C++ are faster languages than Python. Is this because of Pythons ability to operate on almost any operating system? Or is there many other reasons why? I understand there is ansi/iso C and C++ and that ANSI/ISO Code will work on any system If this is the reason why, than why don't developers create specific Python Distrubutions...
12
2785
by: David | last post by:
I am a full-time freelance writer. I am seeking an established, professional web designer who has designed more than one successful website for freelance writers. The individual needs to be able to do the whole job including content, design, graphics and logo. I may want some input or to supply text to work with for the content for obvious reasons. They need to be able to demonstrate that the sites they have done for writers have been...
250
10506
by: Sugapablo | last post by:
Just out of curiosity, while checking on a site I was working on, I decided to throw a couple of the web's most popular URLs into the W3C Markup Validator. Out of microsoft.com, google.com, amazon.com, yahoo.com, aol.com, and mozilla.org, only Mozilla's site came back "Valid HTML". So if all these places, with their teams of web developers don't seem to care, should the rest of us small time web devs concern ourselves with standards?...
20
2043
by: Jim | last post by:
Hi, I am hoping that someone here can help me out. I am for the first time trying to implement a page design using only CSS instead of HTML tables. I've been able to get most of the page done ok. I have my header area and below it 3 columns. My problem is within the header area.
24
1231
by: Devon Null | last post by:
I was simply wondering if this was a mal-formed class header: #ifndef _items_h_ #define _items_h_ #include <iostream> #include <string> #include <vector> using namespace std;
5
1407
by: dhcrusoe | last post by:
Greetings, My apologies if this message comes across as spam; it isn't meant to be. A group of colleagues and I have built a project to mobilize technology volunteers for small nonprofit organizations, located at http://www.codekindness.org . It's a simple technology, and simply acts to link potential volunteers with individual projects. It's nonprofit.
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10548
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
5500
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
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
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.