Hi, I'm making a text RPG.
I decided to begin from the easy ones and make the
weapon class. I want it to be abstract. Then I have a
sword class, which is inherited from
weapon. Finally, I have a
dagger class which is inherited from
sword.
Weapon -> Sword -> Dagger
I want both the weapon and the sword classes to be abstract. I'm doing something like this..
Weapon.h
#ifndef WEAPON_H
#define WEAPON_H
class Weapon
{
public:
Weapon( int health = 100, int power = 1, int value = 0, int weight = 1 );
~Weapon();
virtual void viewAttributes() const = 0;
protected:
int* m_pHealth, *m_pPower, *m_pValue, *m_pWeight;
};
#endif
Weapon.cpp
#include "Weapon.h"
Weapon::Weapon(int health, int power, int value, int weight)
{
m_pHealth = new int( health );
m_pPower = new int( power );
m_pValue = new int( value );
m_pWeight = new int( weight );
}
Weapon::~Weapon()
{
delete m_pHealth; m_pHealth = 0;
delete m_pPower; m_pPower = 0;
delete m_pValue; m_pValue = 0;
delete m_pWeight; m_pWeight = 0;
}
Sword.h
#ifndef SWORD_H
#define SWORD_H
class Weapon;
class Sword : public Weapon
{
public:
Sword( int health = 100, int power = 1, int value = 0, int weight = 1 );
~Sword();
virtual void viewAtrributes() const = 0;
};
#endif
Sword.cpp
#include "Weapon.h"
#include "Sword.h"
Sword::Sword(int health, int power, int value, int weight ): Weapon( health, power, value, weight )
{
}
Dagger.h
#ifndef DAGGER_H
#define DAGGER_H
class Weapon;
class Sword;
class Dagger : public Sword
{
public:
Dagger( int health = 100, int power = 1, int value = 0, int weight = 1 );
virtual void viewAttributes() const;
};
#endif
Dagger.cpp
#include <iostream>
#include "Weapon.h"
#include "Sword.h"
#include "Dagger.h"
Dagger::Dagger(int health, int power, int value, int weight): Sword( health, power, value, weight )
{
}
void Dagger::viewAttributes() const
{
std::cout << "Your dagger's attributes:\n"
<< "Health: " << *m_pHealth << " %\n"
<< "Power: " << (*m_pHealth) * (*m_pPower) << "\n"
<< "Value: " << *m_pValue << " gold\n"
<< "Weight: " << *m_pWeight << " kilograms\n"
;
}
main.cpp
#include <iostream>
#include "Weapon.h"
#include "Sword.h"
#include "Dagger.h"
int main()
{
Weapon* w = new Dagger( 100, 10, 84, 12 );
}
---------
First, I want to know if this is good design (I mean the whole idea of how objects relate). If not, what should I do?
Another question is: I had to include 4 files in main. Is there to way to reduce that number; because it will increase when I add more types of weapons/swords etc.
So, I'm getting some errors:
1>------ Build started: Project: PROJECT1, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>f:\my documents\visual studio 2005\projects\project1\project1\main.cpp(9) : error C2259: 'Dagger' : cannot instantiate abstract class
1> due to following members:
1> 'void Sword::viewAtrributes(void) const' : is abstract
1> f:\my documents\visual studio 2005\projects\project1\project1\sword.h(11) : see declaration of 'Sword::viewAtrributes'
1>Build log was saved at "file://f:\My Documents\Visual Studio 2005\Projects\PROJECT1\PROJECT1\Debug\BuildLog.htm"
1>PROJECT1 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Thanks in advance and sorry for the long post.