Design of class question

T

Tony Johansson

Hello!

Here I have one base klass and three subklasses.

I Just want to have your opinion about the design of these klasses.
I don't think that my design is good.
I think that I instead should not have done the base class abstract it is
so now but that is wrong I think. The base class should be concrete

Define then method fetchWeaponName, fetchAllowedAnimal and getPrice in the
base class which mean that these can be removed from the subclasses. Change
the access specifier defined as protected to private in the base class.
virtual string fetchWeaponName() const
{ return Weapon::weaponName; }

string fetchAllowedAnimal() const
{ return Weapon::allowedAnimal; }

virtual int getPrice() const
{ return Weapon::price; }



//Tony

#ifndef WEAPON_H
#define WEAPON_H
#include "common.h"
class Weapon
{
public:


Weapon(int pris, string vapen_namn, string djur) :
price(pris),weaponName(vapen_namn), allowedAnimal(djur) {}
Weapon() {}

virtual ~Weapon() {}

virtual string fetchAllowedAnimal() const = 0;

virtual string fetchWeaponName() const = 0;

virtual int getPrice() const = 0;

protected:
int price;
string weaponName;
string allowedAnimal;
};
#endif

#ifndef MOUSERIFLE_H
#define MOUSERIFLE_H
#include "common.h"
class MooseRifle : public Weapon
{
public:
MooseRifle(int price,string weapon_name,string animal) : Weapon(price,
weapon_name, animal)
{}

MooseRifle()
{}

virtual ~MooseRifle()
{}


virtual int getPrice() const
{ return Weapon::price; }


virtual string fetchWeaponName() const
{ return Weapon::weaponName; }

string fetchAllowedAnimal() const
{ return Weapon::allowedAnimal; }

private:
};
#endif

#ifndef WINCHESTER_H
#define WINCHESTER_H
#include "common.h"
class Winchester : public Weapon
{
public:
Winchester(int price,string weapon_name,string animal) : Weapon(price,
weapon_name, animal)
{}
virtual ~Winchester()
{}

virtual string fetchWeaponName() const
{ return Weapon::weaponName; }

virtual int getPrice() const
{ return Weapon::price; }

string fetchAllowedAnimal() const
{ return Weapon::allowedAnimal; }

private:
};

#endif

#ifndef SHOTGUN_H
#define SHOTGUN_H
#include "common.h"
class Shotgun : public Weapon
{
public:


Shotgun(int price,string weapon_name,string animal) : Weapon(price,
weapon_name, animal)
{}

virtual ~Shotgun()
{}

string fetchWeaponName() const
{ return Weapon::weaponName; }

virtual int getPrice() const
{ return Weapon::price; }


string fetchAllowedAnimal() const
{ return Weapon::allowedAnimal; }

private:
};
#endif
 
K

Karl Heinz Buchegger

Tony said:
Hello!

Here I have one base klass and three subklasses.

I Just want to have your opinion about the design of these klasses.
I don't think that my design is good.
I think that I instead should not have done the base class abstract it is
so now but that is wrong I think. The base class should be concrete

'Abstract base class' does not mean that *all* function members are 'virtual pure'.
Just having one as 'virtual pure' is enough to fullfil the intendent purpose:
to not be able to create on object of that type.

If there is no other function that serves that purpose, you can always make
the destructor pure.
Define then method fetchWeaponName, fetchAllowedAnimal and getPrice in the
base class which mean that these can be removed from the subclasses.

Right you should do that.
As a guideline: try to organize your code in a way such that the class that
holds some member variables also holds the functions needed to maniulate
that members.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top