473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Design of class question

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
Jul 23 '05 #1
1 1297
Tony Johansson wrote:

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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

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

Similar topics

4
by: Danimal | last post by:
I have been using PHP for a long time... since it was called PHP/FI. I have a programming design question: Let say I have this class: class attrib { var $lenght; var $type; ... }
9
by: Patchwork | last post by:
Hi Everyone, I have a design related question (in C++) that I am hoping someone can help me with. It is related to my previous post but since it was pointed out that I was more or less asking...
1
by: Tony Johansson | last post by:
Hello Experts! I'm reading about design patter in the GAMMA book and there is something that I don't understand. That's why I ask you. It says "Pluggable adpters. A class is more reusable when...
13
by: KV | last post by:
I'm new to OO Design, and I'm fixing to start writing my very first C# program. Given the complexity of OO programming, I would like to run something by this group and get general input. My...
10
by: Saso Zagoranski | last post by:
hi, this is not actually a C# problem but since this is the only newsgroup I follow I decided to post my question here (please tell me where to post this next time if you think this post...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
11
by: John Fly | last post by:
I'm working on a large project(from scratch). The program is essentially a data file processor, the overall view is this: A data file is read in, validated and stored in a memory structure...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...

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.