473,395 Members | 1,977 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.

Two inheritance trees

Hello

I'm trying to create application with as much as possible methods
derived from base class. I use two base classes "Document" and
"DocumentItem", each document has collection of documentItems. When I
cast object of Document class on one of its subclasses collection of
documentItems collection remains in base class. I already know that I
cannot do it with generic collections. Can anybody help me with this,
or point in right direction?

--
Regards
Wojciech Antonik

Nov 24 '06 #1
6 1517
"wojtas" <wo******@gmail.coma écrit dans le message de news:
11*********************@l39g2000cwd.googlegroups.c om...

| I'm trying to create application with as much as possible methods
| derived from base class. I use two base classes "Document" and
| "DocumentItem", each document has collection of documentItems. When I
| cast object of Document class on one of its subclasses collection of
| documentItems collection remains in base class. I already know that I
| cannot do it with generic collections. Can anybody help me with this,
| or point in right direction?

Without any code as example, it is difficult to see what your problem is.

However, why would you say generics doesn't help ?

class DocumentItem
{
}

class DocumentItemCollection<T: List<Twhere T : DocumentItem { }

class Word : DocumentItem
{
}

class Paragraph : DocumentItem
{
private DocumentItemCollection<Wordwords = new
DocumentItemCollection<Word>();

public DocumentItemCollection<WordWords
{
get { return words; }
}
}

class Document : DocumentItem
{
private DocumentItemCollection<Paragraphparagraphs = new
DocumentItemCollection<Paragraph>();

public DocumentItemCollection<ParagraphParagraphs
{
get { return paragraphs; }
}

private DocumentItemCollection<Documentdocuments = new
DocumentItemCollection<Document>();

public DocumentItemCollection<DocumentDocuments
{
get { return documents; }
}
}

Or am I missing something ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 24 '06 #2

Joanna Carter [TeamB] napisa³(a):

Thanks for detailed reply, provided code sample is very helpful,
however my problem is slightly different (below).
Without any code as example, it is difficult to see what your problem is.
However, why would you say generics doesn't help ?
I've thought that generic collections prevent using inheritance.

I've planned structure similiar to described in folloving code:

class DocumentItem
{
}

class DocumentItem1 : DocumentItem
{
}

class DocumentItem2 : DocumentItem
{
}

class DocumentItemCollection<T: List<Twhere T : DocumentItem
{
}

class Document
{
private DocumentItemCollection<DocumentItemitems = new
DocumentItemCollection<DocumentItem>();
public virtual DocumentItemCollection<DocumentItemitems
{
get { return words; }
}
}

class Document1 : Document
{
private DocumentItemCollection<DocumentItem1items = new
DocumentItemCollection<DocumentItem1>();
public override DocumentItemCollection<DocumentItem1items
{
get { return words; }
}
}

class Document2 : Document
{
private DocumentItemCollection<DocumentItem1items = new
DocumentItemCollection<DocumentItem1>();
public override DocumentItemCollection<DocumentItem1items
{
get { return words; }
}
}

I'm aware of errors in presented code, but not quite aware of good
solution :)

--
Regards
Wojciech Antonik

Nov 24 '06 #3
This code is closer to what I want to achieve:

class DocumentItem
{
public string getText(){
return "a";
}
}

class DocumentItem1 : DocumentItem
{
public string getText(){
return "a1";
}
}

class DocumentItem2 : DocumentItem
{
public string getText(){
return "a2";
}
}

class DocumentItemCollection<T: List<Twhere T : DocumentItem
{
}

class Document
{
private DocumentItemCollection<DocumentItemitems = new
DocumentItemCollection<DocumentItem>();
public DocumentItemCollection<DocumentItemItems
{
get { return items; }
}
}

class Document1 : Document
{
private DocumentItemCollection<DocumentItem1items = new
DocumentItemCollection<DocumentItem1>();
public new DocumentItemCollection<DocumentItem1Items
{
get { return items; }
}
}

class Document2 : Document
{
private DocumentItemCollection<DocumentItem2items = new
DocumentItemCollection<DocumentItem2>();
public new DocumentItemCollection<DocumentItem2Items
{
get { return items; }
}
}
I want to get items returning "a2" when I cast object of Document class
on Document2 class. Maybe this code is completly wrong, and there is
better way to achieve this, I'll appreciate any help.

--
Regards
Wojciech Antonik

Nov 24 '06 #4
"wojtas" <wo******@gmail.coma écrit dans le message de news:
11**********************@m7g2000cwm.googlegroups.c om...

| I've thought that generic collections prevent using inheritance.

Not necessarily, it depends on the design :-)

| I've planned structure similiar to described in folloving code:

How about this ?

class CompositeDocument<Twhere T : Document
{
private List<Titems = new List<T>();

public List<TItems
{
get { return items; }
}
}

class Document : CompositeDocument<Document{ }

class Document1 : CompositeDocument<Document1{ }

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 24 '06 #5


On Nov 24, 12:24 pm, "Joanna Carter [TeamB]" <joa...@not.for.spam>
wrote:
Not necessarily, it depends on the design :-)
Now I see that inheritance with generics is possible, but I'm still
confused :)
How about this ?

class CompositeDocument<Twhere T : Document
{
private List<Titems = new List<T>();

public List<TItems
{
get { return items; }
}
}

class Document : CompositeDocument<Document{ }

class Document1 : CompositeDocument<Document1{ }
I've to admit, You've lost me :) I don't see how this code would
compile, is "Document" class supposed to inherit from
"CompositeDocument" class, which is derived from "Document" class?

I dont have problems with single inheritance tree, but two trees, when
object from one tree uses objects from analogic location in second
tree.

--
Regards
Wojciech Antonik

Nov 24 '06 #6
"wojtas" <wo******@gmail.coma écrit dans le message de news:
11**********************@k70g2000cwa.googlegroups. com...

| How about this ?
| >
| class CompositeDocument<Twhere T : Document
| {
| private List<Titems = new List<T>();
| >
| public List<TItems
| {
| get { return items; }
| }
| }
| >
| class Document : CompositeDocument<Document{ }
| >
| class Document1 : CompositeDocument<Document1{ }
|
| I've to admit, You've lost me :) I don't see how this code would
| compile, is "Document" class supposed to inherit from
| "CompositeDocument" class, which is derived from "Document" class?

Sorry, I forgot to paste in the correct code :

class DocumentItem
{
}

class CompositeDocument<T: DocumentItem where T : DocumentItem
{
private List<Titems = new List<T>();

public List<TItems
{
get { return items; }
}
}

class Document : CompositeDocument<Document{ }

class Document1 : CompositeDocument<Document1{ }

Is that any better ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 24 '06 #7

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

Similar topics

17
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...
6
by: C++ Shark | last post by:
Hi, which stl class is good for creating search trees? I am looking for something flexible, that allows me to search for a required state (of matrices, graphs, etc.) quickly. thanks in...
1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
3
by: ptrSriram | last post by:
Can someone help me with an algorithm to merge two binary search trees. One method I thought of was to flatten both the trees into sorted lists(inorder traversal),merge those two sorted lists,...
8
by: Mike - EMAIL IGNORED | last post by:
I have a class that may or may not be inherited. Its constructor calls a function that should be called only if the class is not inherited. Is there a way to tell, other than simply passing a...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
5
Ganon11
by: Ganon11 | last post by:
Hey guys, I'm learning C++ for the first time and have found it quite similar to Java in many ways. However, Inheritance differed a little bit, and I seem to have trouble with it, especially when...
2
by: parasuram | last post by:
Hi friends ............. this is a question regarding the data structures trees Pleas post it if possible with in 2 days I will thankful if some body could help doing this. Operating...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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
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,...

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.