473,661 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

derivable classes

mem
Concrete classes are ones that can be instantiated directly, as long as it
doesn't have pure virtual function.

However, people suggest that "Don't derive from concrete classes."

Does this mean that either make it abstract base class for it to be
derivable or keep them as "concrete" not to be derived at all? Then what's
the purpose of "concrete classes" of ordinary virtual function(s)?

Thanks!

Jul 22 '05 #1
11 1935
mem wrote:
Concrete classes are ones that can be instantiated directly, as long as it
doesn't have pure virtual function.

However, people suggest that "Don't derive from concrete classes."

Does this mean that either make it abstract base class for it to be
derivable or keep them as "concrete" not to be derived at all? Then what's
the purpose of "concrete classes" of ordinary virtual function(s)?


OO is not about classes and objects; it is about messages and methods.

The odds you will derive from a concrete class are low. But you must divorce
the idea of "a class without pure virtual functions" from that of a "class
that should be instantiated". Pragmatically, some classes that should be
instantiated will also be inherited.

Your design should duplicate no behavior. This means, in practice, that
interfaces tend to migrate up an inheritance graph, and implementation
migrates down, into non-pure functions. But the rule against duplication is
more important than the rule "Don't derive from concrete classes".

A related rule, "Don't derive unless you then override a method," is more
important. Read the /Effective C++/ books for a good explanation.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2
class Rectangle
{

};
class Square : Rectangle
{
};

Above is an instance in which a Concrete Class has been inherited from.
There's nothing wrong with it because:

A) A Rectangle is a fully-fledged thing, you can describe one and talk about
it very specificly. You can simply have a rectangle, no frills.

B) A Square is a Rectangle.

The reason people may have come up with the rule "Don't inherit from a
concrete class", I shall explain as follows:
Class Vehicle
{
};
Class Car : public Vehicle
{
};

Let's assume that Vehicle is a concrete class, with no virtual functions,
pure or otherwise. Thus, it's a concrete class. Thus, you can create an
instance of it:

Vehicle FunkyVehicle;

And similarly you can create an object Car:

Car FunkyCar;
But what you'll see here is that a Vehicle is NOT a fully-fledged object!
Try describe a Vehicle. How does a Vehicle work? How many wheels does a
vehicle have? Does it run on petrol or deisel. You can't just simply have a
Vehicle! But you CAN just simply have a Rectangle.

In summation:

You're human, you have a brain - decide for yourself if what you're doing is
good and proper.

-JKop

Jul 22 '05 #3

"JKop" <NU**@NULL.NULL > wrote in message
news:C5******** **********@news .indigo.ie...
class Rectangle
{

};
class Square : Rectangle
{
};


B) A Square is a Rectangle.


Actually, this is one of the best examples of what is *not* a good example
for inheritance. :-)

In mathematical terms, sure, we all know that a square is a special case of
a rectangle, where all sides are of equal length. But that's simply adding
a *restriction* on the rectangle in order to define the square, not
providing a new version of the rectangle with special behavior or added
properties, which is what inheritance is more about.

To illustrate the problem, think about what a rectangle can have as
properties: length and width. And either of those can potentially be
changed, independently! It makes perfect sense for a rectangle to have
SetWidth() and SetHeight() functions to set those properties. But calling
such a function in the square class would be disastrous! Sure, you could
override those to set both width and height whenever either is called, but
that would be *really* confusing to someone using your objects, (especially
if using polymorphism through pointers to the base class).

I believe Meyers covers this exact case in one of his books.

Sorry if this muddles things for the OP, but I think we need to give a
better simple example of inheritance to newbies, something like "A
SalariedEmploye e is an Employee".

-Howard

Jul 22 '05 #4

"mem" <me**@supnet.co m> wrote in message
news:ZY******** ***********@bgt nsc04-news.ops.worldn et.att.net...
Concrete classes are ones that can be instantiated directly, as long as it
doesn't have pure virtual function.
That's redundant. If it has a pure virtual function, it's abstract, not
concrete.
However, people suggest that "Don't derive from concrete classes."

Does this mean that either make it abstract base class for it to be
derivable or keep them as "concrete" not to be derived at all? Then what's
the purpose of "concrete classes" of ordinary virtual function(s)?


Yes, I've heard that guideline and generally I don't follow it and I've
never run into problems. I think Meyers wrote about this in "Effective C++"
or "More Effective C++". I don't understand your last sentence. But I
think you can derive from concrete classes without running into trouble.
Jul 22 '05 #5

"mem" <me**@supnet.co m> wrote in message
news:ZY******** ***********@bgt nsc04-news.ops.worldn et.att.net...
Concrete classes are ones that can be instantiated directly, as long as it
doesn't have pure virtual function.

However, people suggest that "Don't derive from concrete classes."

Does this mean that either make it abstract base class for it to be
derivable or keep them as "concrete" not to be derived at all? Then what's
the purpose of "concrete classes" of ordinary virtual function(s)?

Thanks!


I'm unsure what a "concrete" class is. Is it one that is "intended" to be
instantiated from?

If so, then in real life you are bound to have cases where a class that is
intended to be instantiated may also end up being inherited from.

In that case, you'll want to be sure to look at what functions you make
virtual, because someone deriving from you class will make decisions about
what to override based upon the default behavior, and if you later change
that default behavior, then they will need to revisit the idea of overriding
that behavior again.

This, I think, is the basic reason given to avoid deriving from such
classes, and to instead make an abstract class from which both you and
others can derive. That way, if you change your class' behavior, it won't
break theirs.

If by "concrete" you mean that no virtual functions exist at all, then that
class is not "intended" to be derived from, and obviously shouldn't be.
(But I doubt that's the meaning, since it's too obvious such a class is not
intended as a base class.)

-Howard

Jul 22 '05 #6

"Howard" <al*****@hotmai l.com> wrote in message
news:8e******** *********@bgtns c05-news.ops.worldn et.att.net...

"mem" <me**@supnet.co m> wrote in message
news:ZY******** ***********@bgt nsc04-news.ops.worldn et.att.net...
Concrete classes are ones that can be instantiated directly, as long as it doesn't have pure virtual function.

However, people suggest that "Don't derive from concrete classes."

Does this mean that either make it abstract base class for it to be
derivable or keep them as "concrete" not to be derived at all? Then what's the purpose of "concrete classes" of ordinary virtual function(s)?

Thanks!
I'm unsure what a "concrete" class is. Is it one that is "intended" to be
instantiated from?


Yes. It's a normal class.
If so, then in real life you are bound to have cases where a class that is
intended to be instantiated may also end up being inherited from.
This can only happen if a programmer makes it so. The suggestion that the
OP was asking about was that programmers do not make it so.
If by "concrete" you mean that no virtual functions exist at all,...
No.
then that
class is not "intended" to be derived from, and obviously shouldn't be.


No, that's not necessarily true (but it could be.) If a designer writes a
class with some functions that are not virtual, he might still intend for
that class to be derived from, but is saying those functions should not be
overridden.
Jul 22 '05 #7

"Howard" <al*****@hotmai l.com> wrote in message
news:22******** *********@bgtns c05-news.ops.worldn et.att.net...

"JKop" <NU**@NULL.NULL > wrote in message
news:C5******** **********@news .indigo.ie...
class Rectangle
class Square : Rectangle

B) A Square is a Rectangle.
Actually, this is one of the best examples of what is *not* a good example
for inheritance. :-) In mathematical terms, sure, we all know that a

square is a special case ofa rectangle, where all sides are of equal length. But that's simply adding
a *restriction* on the rectangle in order to define the square, not
providing a new version of the rectangle with special behavior or added
properties, which is what inheritance is more about.


Howard, good point. However, this might make more sense

class Square
class Rectangle: public Square

A lot of this sort of thing is context dependent. It depends on which
constraints you are going to be modeling, and what you want your objects to
be able to do. If all you're going to do is ask things like area,
perimeter, and corner to corner distance, then this design works OK. The
invocation of the base class (Square) constructor from the Rectangle
constructor looks a little goofy, but it works. If different things are
important in your design, then this won't work. For example, if you're
writing polymorphic code to put square shapes into square holes, then a
rectangle isn't substitutable for a square.

But these design problems are nothing new. You always model what's
important. If you're modeling the animal world, and you get down to the
point in your hierarchy where you split egg layers from mammals, then what
do you do with a duck billed platypus? It depends on whether egg laying is
important to you, or if hair and warm-bloodedness are important to you.

class Square
{
public:
Square(int);
virtual int area();
private:
side;
};
class Rectangle
{
public:
Rectangle(int, int);
int area();
private:
side2;
};
Square::Square( int val) : side(val) {}
int Square::area()
{ return side*2; }

Rectangle::Rect angle(int val1, int val2) : Square(val1), side2(val2) {}
int Rectangle::area ()
{ return side * side2; }
Jul 22 '05 #8

"jeffc" <no****@nowhere .com> wrote in message
news:40******** @news1.prserv.n et...
But these design problems are nothing new. You always model what's
important. If you're modeling the animal world, and you get down to the
point in your hierarchy where you split egg layers from mammals, then what
do you do with a duck billed platypus? It depends on whether egg laying is important to you, or if hair and warm-bloodedness are important to you.


By the way, this problem doesn't just exist for computer programmers. It
also exists for zoologists. They didn't know where to put the duck billed
platypus either. There are often no perfect solutions. They decided to put
it with the mammals. They decided that being hairy and warm-blooded in this
case were more important distinctions than laying eggs. But if they wanted
they could have created a completely different split off the hierarchy and
made a special case for this animal.
Jul 22 '05 #9
jeffc posted:
class Square
class Rectangle: public Square

Let's say I'm a painter and I paint portraits. I demand that all my
paintings be upon a square canvas. The customer supplies the canvas:
PaintPortrait(S quare* Canvas);
But, if a Rectangle IS_A square, as you've specified, then the customer
could supply the painter with a rectangle:
int main(void)
{
Rectangle Canvas;

PaintPortrait(& Canvas);
}

Then again, you may simply just ignore the IS_A principle. It's your code
after all, and if it functions correctly, then it functions correctly.
-JKop
Jul 22 '05 #10

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

Similar topics

1
741
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
9
1634
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these applications need to serialise the classes to/from the same files but only the GUI app needs the full range of class methods. Now, the rendering app needs to be ported to multiple OS's but the GUI doesn't. In order to reduce the time/cost of porting I'd...
2
1844
by: Amit | last post by:
please see the following lines of codes : class ABase; // i want to make it non-derivable class Super { private: Super () {}; friend class ABase; };
2
9494
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how to do that? Thanks. Tsung-Yu
18
2033
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise. Yet, in my understanding, attributes are only __gc and __value class specific and do not apply to __nogc classes. Is this correct ? If so, how is the packing alignment of __nogc classes stored ?
2
2413
by: baba | last post by:
Hi all, I'm quite new to C#. I am trying to implement some basics reusable classes using this language and the .NET Framework technology. What I'm trying to do now is to implement a singleton class. I did have a look at the Microsoft "Patterns and Practices" article "Implementing Singleton in C#" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/ImpSingletonInCsharp.asp)
6
2934
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by calling Mesh class functions. Let's say they point to each other like this: class Vertex { HalfEdge *edge; }; class HalfEdge { Vertex* vert;
0
2026
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid this topic getting lost before people interested in the problem notice it. The important tricks to the solution are two: 1) make the custom classes take a TEMPLATE argument which defines their BASE class 2) EMBED the custom classes in a "Traits"...
5
1888
by: Amal P | last post by:
Dears, I have a question. class Data { }; class DisableDerive:virtual protected Data // A base class { private:
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7364
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.