Re: seeking an opinion from the collective wisdom here
"Devon Null" <theronnightstar@xgmailx.com> wrote in message
news:u62dnQouwuWVWfzbnZ2dnUVZ_uiknZ2d@comcast.com...
[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_consumable = 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_consumable( bool set_is_consumable )
{ is_consumable = set_is_consumable; }
bool get_is_consumable() { 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.clear()
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<string> description;
// 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_consumable = 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_consumable;
is_magical = set_is_magical;
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.clear();
description.push_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?
I would probably do it similar to this (notice also renaming of variables
and methods):
#ifndef ITEMS_HEADER_
#define ITEMS_HEADER_
#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_consumable = false,
bool set_is_magical = false );
//Below are the set and get functions for
//all variables in this class
void weight( float Weight )
{ weight_ = Weight; }
float weight()
{ return weight_; }
void equipable( bool Can_equip )
{ equipable_ = Can_equip; }
bool equipable()
{ return equipable_; }
void attackable( bool Can_attack )
{ attackable_ = Can_attack; }
bool attackable()
{ return attackable_; }
void defendable( bool Can_defend )
{ defendable_ = Can_defend; }
bool defendable()
{ return defendable_; }
void drinkable( bool Can_drink )
{ drinkable_ = Can_drink; }
bool drinkable()
{ return drinkable_; }
void eatable( bool Can_eat )
{ eatable_ = Can_eat; }
bool eatable() { return eatable_; }
void useable( bool Can_use )
{ useable_ = Can_use; }
bool useable()
{ return useable_; }
void consumable( bool Is_consumable )
{ consumable_ = Is_consumable; }
bool consumable()
{ return consumable_; }
void magical( bool Is_magical )
{ magical_ = Is_magical; }
bool magical()
{ return 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.clear()
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<string> description;
// This is to allow multi-line descriptions
float weight_; // These are pretty self explanatory I think
bool equipable_;
bool attackable_;
bool defendable_;
bool drinkable_;
bool eatable_;
bool useable_;
bool consumable_;
bool magical_;
};
#endif
Item::Item( float weight,
bool equipable,
bool attackable,
bool defendable,
bool drinkable,
bool eatable,
bool useable,
bool consumable,
bool magical ): weight_( weight ), equipable_( equipable ),
attackable_( attackable ), defendable_( defendable ),
drinkable_( drinkable ), eatable_( eatable ),
useable_( useable ), consumable_( consumable ),
magical_( magical )
{
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.clear();
description.push_back( need_set );
}