Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > About Inheritance design

Reply
Thread Tools

About Inheritance design

 
 
Jonathan Bartlett
Guest
Posts: n/a
 
      04-28-2005
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
 
Reply With Quote
 
 
 
 
Tony Johansson
Guest
Posts: n/a
 
      04-28-2005
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







 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-28-2005
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
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      04-28-2005

"Tony Johansson" <> wrote in message
news:rR7ce.22836$...
> 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





 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      04-28-2005
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" <> skrev i meddelandet
news:vX7ce.66658$ 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



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-28-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57