473,385 Members | 1,769 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,385 software developers and data experts.

Nested Class vs Inheritance


Hello All,
When is nested class more preferable that Inheritance ? I think with proper
inheritance and friend class concept we can get the same flexibility as nested
classes
Any comments ..
Best Regards
KInd
--
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/
Jul 22 '05 #1
4 3982

"KInd" <no************@f-m.fm> wrote in message
news:20*******************@foorum.com...

Hello All,
When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes
Any comments ..
Best Regards
KInd


Using inheritance or nested class depends very much on what you want to
achieve and how your logical structure (design) looks like.
One example that comes to my mind where nested classes are the method of
choice (IMHO) is an implementation of a generic class which can be used to
obtain heterogeneous containers with the standard library containers.

It would look something like this:

class CGenericObj {

public:
template<typename T>
CGenericObj( const T& WrappedObj) : m_pWrapper( new CWrapper<T>(
WrappedObj ) ) {};
...
class CBaseWrapper {
....
};

template<typename T>
class CWrapper : public CBaseWrapper {
...
}

};

In this case for example inhertiance simply doesn´t make sense logically,
because CWrapper is not a CGenericObj but simply a helper. However, it is a
CBaseWrapper and hence inheritance is perfectly okay.

Regards
Chris
Jul 22 '05 #2
KInd wrote:
When is nested class more preferable that Inheritance ? I think with proper
inheritance and friend class concept we can get the same flexibility as nested
classes
Any comments ..


Nesting of types has a very particular meaning. The nested type
is subordinate to the enclosing one. Its application is essentially
_only_ within the scope of its "parent". Can you misuse that? Yes,
just like anything else. However, properly used it's not in any way
a substitution or even comparable to inheritance.

The connection that exists between the base class and the derived
class and also the connection between them and the rest of the model
have nothing to do with scope, really. Inheriting publicly implies
"is-a" relationship. There are implicit conversions based on the
fact of inheriting the base class. Nothing of that exists between
"enclosing/nested" classes.

So, you cannot really prefer nesting over inheritance since they
are IMO orthogonal (although you cannot really apply both at the same
time).

Victor
Jul 22 '05 #3

"KInd" <no************@f-m.fm> wrote in message
news:20*******************@foorum.com...

Hello All,
When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes
Any comments ..
Best Regards
KInd


Well, there are a few types of inheritance. And the subject matter is large
and difficult to grasp, especially for beginners. I'd recommend reading
Scott Meyers' book "Effective C++", which does a good job describing the
different inheritance strategies.

(I'm not sure where friend-ship comes into play in regards to inheritance,
though...?)

But for a short (attempt at an) answer:

Any time you use public inheritance, you should make sure that any function
or object that expects a base class object will also be satisfied by a
derived class obejct as well, because the derived class object "is a" base
class object. If you're creating a class that "is a" base class (such as a
math student "is a" student), then inheritance is fine.

If your new class needs to utilize the behavior of the base class, but can't
actually be substituted for a base class object (even if just in one special
case), then layering (using a nested class) is a better approach. That's
referred to as the "is implemented in terms of" relationship.

And of course, the obvious use of layering is for something simple like "a
computer has a cpu", so your computer class would contain a cpu object (or
perhaps a pointer to one, asuming the cpu is replaceable :-)). That's call
the "has a" relationship.

(I won't even go nto private or protected inheritance.)

Again though, I'd recommend a good read on the subject.

Hope this helps,
-Howard

Jul 22 '05 #4
KInd <no************@f-m.fm> wrote in message news:<20*******************@foorum.com>...
Hello All,
When is nested class more preferable that Inheritance ? I think with proper
inheritance and friend class concept we can get the same flexibility as nested
classes
Any comments ..
Best Regards
KInd


It's an endlessly arguable topic.

I typically prefer nesting classes. A class heirarchy where the
subclasses depend on the implementation details of the superclass can
become inflexible.Forcing yourself to use the interface of the
contained class keeps the design loosely-coupled. Polymorphism can be
implemented using interfaces, which also tends to provide clearer
code, I find.

OTOH, if you absolutely need access to the innards of the superclass,
and the subclass would inevitably have to change if the design of the
superclass did then inheritance is obviously the way to go.

Others will no-doubt differ...

Jam
Jul 22 '05 #5

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

Similar topics

1
by: Stephane Ninin | last post by:
Hello all, I am trying to play with nested class in a script I am making, and I am not sure I really understand how they work. Here is some code: __all__ =
10
by: Paul Morrow | last post by:
I'm hoping that someone can explain why I get the following exception. When I execute the code... ###################################### class Parent(object): class Foo(object): baz = 'hello...
11
by: C# Learner | last post by:
Is it not possible to declare a nested class in a seperate file from its "parent" class -- i.e. in a similar way to the idea of spreading namespaces over more than one file?
7
by: Ryan Shaw | last post by:
I’m having a small problem with inheritance with a hierarchy of classes The example is Class Class Private m_classB as Class Class Class End Clas End Clas
3
by: Martin Skou | last post by:
I'm experimenting with using Python for a small web interface, using Mark Hammond's nice win32 extensions. I use a small class hierarchy which uses inheritance and nested classes. Here are a...
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
4
by: rach | last post by:
I just started to learn C++. I copied the following code from a data structure textbook to a ".h" file and couldn't compile it. The code contains three template interfaces. One inherits another. The...
18
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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
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...

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.