Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
tintin++ ogg sound player script for linux
Author: Robert Smith
Submitted by: Vladaar
6Dragons ogg Soundpack
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.4
Author: Vladaar
Submitted by: Vladaar
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
Users Online
CommonCrawl, Yandex

Members: 0
Guests: 8
Stats
Files
Topics
Posts
Members
Newest Member
481
3,735
19,368
618
Micheal64X
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » General » Coding » abstact class inheritance que...
Forum Rules | Mark all | Recent Posts

abstact class inheritance questions
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Oct 7, 2009, 1:39 am   Last edited Oct 7, 2009, 2:42 am by Andril
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

I decided to rewrite my new module system from the ground up. The old way was far too complicated and was reliant upon an instance of OBJ_INDEX_DATA for much of its functionality. That could cause major problems for players down the road if they decided they wanted to remove an installed module from the ship and that particular object had been deleted at some point.

I've gone the route of using an abstract base class,
class Module
{
   short type, size, condition;

   public:
   Module *next; // pointer to next installed module in ship.. null if this is last module
   Module *prev; // pointer to previous installed module in ship.. null if this is first module
   short set_con(int);

   virtual short get_stat( string ) const = 0; // most of the time i'll be creating a Module* pointer and this lets me 
                                         // use the get_stat function on all derived classes
   Module( OBJ_DATA * );
   virtual ~Module();   // virtual destructor required because the class is abstract
};

which the actual module classes, based on type, inherit from. Here's the standard laser one.
class LaserModule : public Module
{
   short strength; // how much damage
   short fdrain;   // how much energy is drained when fired
   short idrain;   // how much energy is drained when turned on, not fired
   short reset;    // how long does it take this laser to be ready to fire
   short range;    // should be self explanatory
   short state;    // current laser state: off, warming up or ready
   bool shielded;  // is this laser resistant against ion weapons?
   public:
      short get_stat( string stat ) const;
      LaserModule(OBJ_DATA*);
};

One of my questions is, do I even need a constructor for the base Module class? I know that I can't create an instance of the Module class, only derived classes, due to the fact that it's abstract so, is there any point to doing so?
Secondly, if I do have the constructor in there, would passing an OBJ_DATA* pointer to the LaserModule class constructor get passed down to the Module class constructor automatically as well? Or should I just change the LaserModule constructor from this:
LaserModule::LaserModule( OBJ_DATA *obj )
   :strength(obj->value[3]),
    range(obj->value[4]),
    idrain(0),
    fdrain(0),
    state(0),
    reset(0)
{}

to this:
LaserModule::LaserModule( OBJ_DATA *obj )
   :Module::type(obj->value[0]),
    Module::size(obj->value[1]),
    Module::condition(100),
    Module::shielded(obj->value[2]),
    Module::next(NULL),
    Module::prev(NULL),
    strength(obj->value[3]),
    range(obj->value[4]),
    idrain(0),
    fdrain(0),
    state(0),
    reset(0)
{}
?

Thanks for any help.
       
Post is unread #2 Oct 7, 2009, 4:12 am
Go to the top of the page
Go to the bottom of the page

Andril
Magician
GroupMembers
Posts147
JoinedJun 9, 2009

Got it!
LaserModule::LaserModule( OBJ_DATA *obj )
   :Module( obj ),
    state(0),
    strength(obj->value[3]),
    fdrain(0),
    idrain(0),
    reset(0),
    range(obj->value[4])
{}
       
Post is unread #3 Oct 7, 2009, 10:15 am
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

Andril said:

One of my questions is, do I even need a constructor for the base Module class? I know that I can't create an instance of the Module class, only derived classes, due to the fact that it's abstract so, is there any point to doing so?

Typically you would put any common initialization into the abstract base class's constructor. If the abstract base class doesn't have common initialization, then you would (typically) not have a constructor.

And yes, that is how you call the super class's constructor.
       
Pages:<< prev 1 next >>