473,779 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inheritance with one base class and three subclasses

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 , fetchAllowedAni mal 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.
These three methode below will consequently be moved to the base class.

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

string fetchAllowedAni mal() const
{ return Weapon::allowed Animal; }

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

class Weapon
{
public:
Weapon(int pris, string vapen_namn, string djur) :
price(pris),wea ponName(vapen_n amn), allowedAnimal(d jur) {}
Weapon() {}
virtual ~Weapon() {}
virtual string fetchAllowedAni mal() const = 0;
virtual string fetchWeaponName () const = 0;
virtual int getPrice() const = 0;
protected:
int price;
string weaponName;
string allowedAnimal;
};

class MooseRifle : public Weapon
{
public:
MooseRifle(int price,string weapon_name,str ing animal) :
Weapon(price,
weapon_name, animal) {}

MooseRifle() {}
virtual ~MooseRifle() {}

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

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

string fetchAllowedAni mal() const
{ return Weapon::allowed Animal; }
private:
};

class Winchester : public Weapon
{
public:
Winchester(int price,string weapon_name,str ing animal) :
Weapon(price,
weapon_name, animal) {}

virtual ~Winchester() {}

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

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

string fetchAllowedAni mal() const
{ return Weapon::allowed Animal; }
private:
};

class Shotgun : public Weapon
{
public:
Shotgun(int price,string weapon_name,str ing animal) : Weapon(price,
weapon_name, animal) {}

virtual ~Shotgun() {}

string fetchWeaponName () const
{ return Weapon::weaponN ame; }

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

string fetchAllowedAni mal() const
{ return Weapon::allowed Animal; }
private:
};
Jul 23 '05 #1
1 1311

"Tony Johansson" <jo************ *****@telia.com > wrote in message
news:J%******** *************@n ewsc.telia.net. ..
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 , fetchAllowedAni mal 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.
These three methode below will consequently be moved to the base class.

[SNIP]

Apart from some technical things like passing strings as const references,
I'd say, that you should move the functions getPrice(), fetchWeaponName () &
fetchAllowedAni mal to the base class, as you already planned to do. The
reason for this is that the functionality and also the implementation is the
same for all of your classes no matter if it is a MooseRifle or a
Winchester. It's just the value which would be different. However, there is
another thing I'd suggest to re-think. At the moment you have a function
which is called fetchAllowedAni mal and this somehow indicates a temptation
to put knowledge about possible victims of the weapons into the weapon
classes. IMHO this is something which should be taken care of at a high
level because for the weapon the possible target is not much of a
difference. Whether this type can/should be used should be decided somewhere
else based on the type information or the name of the object. Hence, I'd
suggest to at least rename the function to something like fetchType. Or you
can drop it alltogether and base the usage on the name of the object because
one object could be used for more than one different targets.

HTH
Chris
Jul 23 '05 #2

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

Similar topics

17
1827
by: Andrew Koenig | last post by:
Suppose I want to define a class hierarchy that represents expressions, for use in a compiler or something similar. We might imagine various kinds of expressions, classified by their top-level operator (if any). So, an expression might be a primary (which, in turn, might be a variable or a constant), a unary expression (i.e. the result of applying a unary operator to an expression), a binary expression, and so on. If I were solving...
4
2901
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
2
1222
by: Tony Johansson | last post by:
Hello Experts!! Assume we have the following inheritance tree. At the top we have the class Vehicle class and the subclass to vehicle is a class called Engine_vehicle and below this class we have three subclasses which are Car, Bus and Truck. If I have a pointer variable to a base class Vehicle like this Vehicle *vehicle_ptr;
6
1862
by: Andrew Ducker | last post by:
Let's say I have a root class called RootBusinessService and I then want to have 25 business service classes based off of it. And each class has a property that's shared between all instances of it - called 'State'. I therefore want all of them to have a static property State. However, I can't declare this static property in RootBusinessService - because if I do, the subclasses will all point to that single property.
10
1228
by: Kevin Buchan | last post by:
I searched the news group and could not find an answer to this question, so I'll go ahead and post it. Let's say I have a class A with a couple different constructors... nothin' special. Now, let's say that I have class B that inherits from A. B automagically gets all of the properties and methods of A, but I cannot leverage any of A's contructors without redefining them on B and delegating. I'm sure that there is a very good reason...
8
1453
by: ^MisterJingo^ | last post by:
Hi all, I have a question regarding inheritance. I'll use the following code for an example (its been stripped down to the minimum): // code start using System; class Animal {
6
1506
by: Jackson | last post by:
I've got an inheritance question and was hoping brighter minds could guide me. I am in the strange situation where some of the methods in a subclass are actually more general than methods in a superclass. What is the preferred way to handle such situations. My original thought was to do something like this: class AA(object): def general_method(): pass class A(AA):
13
2503
by: stephenpas | last post by:
We are trying to monkey-patch a third-party library that mixes new and old-style classes with multiple inheritance. In so doing we have uncovered some unexpected behaviour: <quote> class Foo: pass class Bar(object): pass
11
1719
by: George Sakkis | last post by:
I have a situation where one class can be customized with several orthogonal options. Currently this is implemented with (multiple) inheritance but this leads to combinatorial explosion of subclasses as more orthogonal features are added. Naturally, the decorator pattern comes to mind (not to be confused with the the Python meaning of the term "decorator"). However, there is a twist. In the standard decorator pattern, the decorator...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
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
10138
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
10074
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
9930
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5373
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...
1
4037
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
3
2869
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.