Re: Inheritance question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thanks guys!

I was simply trying to get a better understanding of working with
inheritance.

I guess I did need to be a lot clearer on intent. Lets assume that my base
class is a Person Class. The derived classes are Employee,Customer,Vendor.
I was attempting to create a list of Persons which I figured would probably
be a static collection in the base class. But after thinking about it a bit
more would it make more sense to have a static collection in each derived
class that holds only objects of the appropriate type?

I am trying to learn inheritance so i dont really know the questions to ask.
I am just trying to sort through handling a one to many relationship when
inheritance is involved.

Ron




"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:1331tu911bi5c09@xxxxxxxxxxxxxxxxxxxxx
"RSH" <way_beyond_oops@xxxxxxxxx> wrote in message
news:eCNsYLDiHHA.1216@xxxxxxxxxxxxxxxxxxxxxxx
Peter,

Thanks for the reply. Sorry about that.

I do want to have the objects to be added to the list on
instantiation...I just didn't know how to represent that.

There are a number of ways to implement something like that. Without
knowing more about your specific intent, preferences, etc. I can't offer
advice on a particular implementation to choose. However, here's one
example of doing what you want:

class BaseClass
{
static private List<BaseClass> _grbcInstances;

public BaseClass()
{
_grbcInstances.Add(this);
}

public void Remove()
{
_grbcInstances.Remove(this);
}
}

The BaseClass can then provide methods for manipulating, searching, etc.
the list of objects. Note that you need to provide an explicit "Remove"
method to ensure that objects are actually released when you want them to
be. This puts the onus on you to figure out when you're actually done
with an object, at least as it relates to the master list of objects. I
have seen mentions of something called a "weak reference" that may or may
not help you around this issue; I don't know enough about weak references
to offer you advice on that.

Finally, I agree with Jon's statement about the possibility of using
virtual methods to deal with per-type behaviors. It's unusual to need to
know the actual type of an object and while I took the shortcut of simply
answering the direct question you asked, I agree that it's more likely
that whatever reason you think you need the type for, that can be more
properly addressed using virtual methods.

Pete


.



Relevant Pages

  • Re: Composition vs. inheritance
    ... In comp.lang.c++.moderated was a short thread "titled deriving ... baseclass interface including the ability to write ASCII text to it ... protected nor any virtual interface, only to add one more member function. ... without inheritance in a plain old function. ...
    (comp.lang.java.programmer)
  • Re: OOP/OOD Philosophy
    ... I agree that this are succinct and precise definitions. ... bothers me because it lacks intent. ... intent, than about inheritance. ... "The aim of science is not to open the door to infinite wisdom, ...
    (comp.object)
  • Re: desgin tables into objects or vice versa?
    ... > to design a class tree according to it? ... virtual methods are probably good. ... If the processing is the same apart from some extra fields the probably best ... different and inheritance and polymorphism would be good. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: error CS0621: virtual or abstract members cannot be private
    ... The OOP languages that are in the mainstream all encourage inheritance. ... They can't override your private virtual method, but you've still got a public method they are going to _want_ to override and instead are going to wind up hiding (which whatever you think about public virtual methods seems to me to be much worse). ... If you want to hide _all_ of the implementation, better to make the base class private to your factory as well, the virtual method protected, and expose the API as an interface. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: error CS0621: virtual or abstract members cannot be private
    ... The OOP languages that are in the mainstream all encourage inheritance. ... But I feel that most of the virtual methods in the .NET framework are not public but protected ... I could make the Base class constructor private so the nested classes can still derive from it but nobody else. ...
    (microsoft.public.dotnet.languages.csharp)