473,404 Members | 2,195 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,404 software developers and data experts.

About Inheritance design

Hello!!

Assume we have the following a base class called Weapon and three derived
classes called
MooseRifle, Winchester and Shotgun.

These weapons have the followings attributes a name and a price. So I would
be able to ask a weapon object about
the name and the price.

So there must be a getName that return a string and a getPrice that return
an int. The price and the name is set in the c-tor for each derived class.

I ask you where do you suggest to place the attribute price and name and the
methods getName and getPrice?
My suggestion is to put these in the Weapon class together of course with
the getName and the getPrice because the derived classes would inherit
everything from the base class Weapon. Do you agree.?

A question here?. This would make the Weapon class a concreate class and not
an abstract class. But it seems strange
to be able to create an object of class Weapon. An object of class Weapon
doesn't have any price and have no name it's only the object of the derived
classes that have price and names.

It would be rather meaningsless to put in a pure virtual funktion in the
Weapon class just to make the class abstract.

Give me a comment about this.

Many thanks

//Tony



Jul 23 '05 #1
5 1295
Tony Johansson wrote:
Assume we have the following a base class called Weapon and three derived
classes called
MooseRifle, Winchester and Shotgun.

These weapons have the followings attributes a name and a price. So I would
be able to ask a weapon object about
the name and the price.

So there must be a getName that return a string and a getPrice that return
an int. The price and the name is set in the c-tor for each derived class.

I ask you where do you suggest to place the attribute price and name and the
methods getName and getPrice?
My suggestion is to put these in the Weapon class together of course with
the getName and the getPrice because the derived classes would inherit
everything from the base class Weapon. Do you agree.?
Given certain assumptions (like the const-ness of the price and the name),
I do. Then again, you didn't provide enough information, and that's why
I have to resort to assuming.
A question here?. This would make the Weapon class a concreate class and not
an abstract class. But it seems strange
to be able to create an object of class Weapon. An object of class Weapon
doesn't have any price and have no name it's only the object of the derived
classes that have price and names.
Seems reasonable.
It would be rather meaningsless to put in a pure virtual funktion in the
Weapon class just to make the class abstract.


Why not? If Weapon has to be abstract, make its destructor virtual (and
you actually should, if Weapon is to be used polymorphically) and pure,
while providing its implementation.

V
Jul 23 '05 #2

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:rR*******************@newsb.telia.net...
Hello!!

Assume we have the following a base class called Weapon and three derived
classes called
MooseRifle, Winchester and Shotgun.

These weapons have the followings attributes a name and a price. So I
would be able to ask a weapon object about
the name and the price.

So there must be a getName that return a string and a getPrice that return
an int. The price and the name is set in the c-tor for each derived class.

I ask you where do you suggest to place the attribute price and name and
the methods getName and getPrice?
In the base class (Weapon). You could make the member variables protected,
and the accessors public. Then just have the constructor for each derived
class set the values for the price and name. Alternatively, you could skip
storing the name and price in variables altogether, and simply use virtual
functions, where each derived class returns a literal constant value. I
prefer the first approach, unless you're trying to minimize the memory
footprint. (But even in that case, you could make the variables static, so
that all class instances share the same variables.)
My suggestion is to put these in the Weapon class together of course with
the getName and the getPrice because the derived classes would inherit
everything from the base class Weapon. Do you agree.?

Yep.
A question here?. This would make the Weapon class a concreate class and
not an abstract class.
Implementing one or more functions does *not* make the class "concrete". It
is an abstract class as long as it contains at least *one* pure virtual
member function.
But it seems strange
to be able to create an object of class Weapon. An object of class Weapon
doesn't have any price and have no name it's only the object of the
derived classes that have price and names.

It would be rather meaningsless to put in a pure virtual funktion in the
Weapon class just to make the class abstract.


If all you want is to prevent instantiation of a Weapon object directly, and
only allow it via a derived class construction, then I think that you can
just make the Weapon class constructor protected instead of public. (On the
other hand, as you develop the class hierarchy more, you may end up
introducing a pure virtual function, anyway.)

-Howard

Jul 23 '05 #3
A few things:

If name and price are the ONLY things changing, it seems silly to make
multiple classes. Just have one class, Weapon.

w = new Weapon("Rifle", 20);

If you have other variance that would really call for inheritance, it is
best to keep the implementation in the subclasses.

For example, if later you decided that price would be based on more than
just the class, and more than just a single variable (like pricing based
on some sort of formula), then your "price" integer becomes wasted
baggage in your new class.

For as long as possible, you should put delay putting actual variables
in base classes.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Jul 23 '05 #4
Hello!!

If there is different price and name for each derived class then this price
attribute and the name attribute must be put in each derived class. Is that
correct?

So this getName and getPrice is made pure virtual in class Weapon

//Tony.
"Victor Bazarov" <v.********@comAcast.net> skrev i meddelandet
news:vX*******************@newsread1.mlpsca01.us.t o.verio.net...
Tony Johansson wrote:
Assume we have the following a base class called Weapon and three derived
classes called
MooseRifle, Winchester and Shotgun.

These weapons have the followings attributes a name and a price. So I
would be able to ask a weapon object about
the name and the price.

So there must be a getName that return a string and a getPrice that
return an int. The price and the name is set in the c-tor for each
derived class.

I ask you where do you suggest to place the attribute price and name and
the methods getName and getPrice?
My suggestion is to put these in the Weapon class together of course with
the getName and the getPrice because the derived classes would inherit
everything from the base class Weapon. Do you agree.?


Given certain assumptions (like the const-ness of the price and the name),
I do. Then again, you didn't provide enough information, and that's why
I have to resort to assuming.
A question here?. This would make the Weapon class a concreate class and
not an abstract class. But it seems strange
to be able to create an object of class Weapon. An object of class Weapon
doesn't have any price and have no name it's only the object of the
derived classes that have price and names.


Seems reasonable.
It would be rather meaningsless to put in a pure virtual funktion in the
Weapon class just to make the class abstract.


Why not? If Weapon has to be abstract, make its destructor virtual (and
you actually should, if Weapon is to be used polymorphically) and pure,
while providing its implementation.

V

Jul 23 '05 #5
Tony Johansson wrote:
If there is different price and name for each derived class then this price
attribute and the name attribute must be put in each derived class. Is that
correct?
First of all, please don't top-post.

Second, no, if those "attributes" simply have different _values_ they
should be member variables but where you put them depends on your design
and your desire, not on the fact that they are different.
So this getName and getPrice is made pure virtual in class Weapon
Whatever. I think you should digest the advice given before attempting
to ask more questions.
[...]


V
Jul 23 '05 #6

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

Similar topics

5
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented...
51
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
3
by: Jack | last post by:
On the 1st of January 1998, Bjarne Stroustrup gave an interview to the IEEE's 'Computer' magazine. Naturally, the editors thought he would be giving a retrospective view of seven years of...
2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
0
by: JKJ | last post by:
I'm not an OOP guru, but I would say ". . .not necessarily" What if the base class and derived classes are internal? Then the base class has to be within the same assembly to be inherited from. ...
29
by: MAHESH MANDHARE | last post by:
Hi , Can Anyone explain me exactly why multiple inheritance not used in java and c# thanks, Mahesh -- Have A Good Day, Mahesh, Maheshmandhare@yahoo.co.in
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.