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

Concrete class

What kind of classes is qualified as "concrete classes"?

When should a member function in a class defined as "pure virtual" and when
as "virtual"?

Thanks!
Jul 22 '05 #1
9 5853
On Sat, 01 May 2004 20:15:59 GMT, "mead" <ph******@light.com> wrote:
What kind of classes is qualified as "concrete classes"?
One you can instantiate. directly. In the ubiquitous "Shape" class
example, with a base class named "Shape" and then derived classes such
as "Circle", "Rectangle", etc., it makes sense to say
Circle c;
but not
Shape s;

(Although
Shape *s
would make perfect sense, as long as you don't try to do:
Shape *s = new Shape;

When should a member function in a class defined as "pure virtual" and when
as "virtual"?
That's actually quite related to your first question. The way to make
a class be abstract is to give it at least one pure virtual function.
If a base class contains one or more pure virtual functions, there are
really two things going on. One is that the class is abstract; but for
that alone, a single pure virtual function would have been sufficient.
The other thing going on is that classes which derive from the base
class must implement /each/ of the pure virtual functions they inherit
in order to qualify as a concrete class. So if a derived class only
implements 7 out of 8 of the pure virtual functions it inherits, it is
still an abstract class. But a further derived class will inherit only
a single pure virtual function, and that further derived class would
become concrete by implementing that one function.
-leor

Thanks!


Jul 22 '05 #2
Leor Zolman <le**@bdsoft.com> wrote in message
news:tf********************************@4ax.com...
On Sat, 01 May 2004 20:15:59 GMT, "mead" <ph******@light.com> wrote:
What kind of classes is qualified as "concrete classes"?


One you can instantiate. directly. In the ubiquitous "Shape" class
example, with a base class named "Shape" and then derived classes such
as "Circle", "Rectangle", etc., it makes sense to say
Circle c;
but not
Shape s;

(Although
Shape *s
would make perfect sense, as long as you don't try to do:
Shape *s = new Shape;
If "Shape" has a pure virtual function and this function is implemented in
Circle class as derived, then Circle becomes a class that can be
instantiate, thus Circle is concrete class but Shape isn't. Is it correct?

If Shape has no pure virtual function but virtual function(s), will it be a
concrete class?

When should a member function in a class defined as "pure virtual" and whenas "virtual"?


That's actually quite related to your first question. The way to make
a class be abstract is to give it at least one pure virtual function.
If a base class contains one or more pure virtual functions, there are
really two things going on. One is that the class is abstract; but for
that alone, a single pure virtual function would have been sufficient.
The other thing going on is that classes which derive from the base
class must implement /each/ of the pure virtual functions they inherit
in order to qualify as a concrete class. So if a derived class only
implements 7 out of 8 of the pure virtual functions it inherits, it is
still an abstract class. But a further derived class will inherit only
a single pure virtual function, and that further derived class would
become concrete by implementing that one function.
-leor


When defining a pure virtual function and when defining a virtual function
in a class?
Jul 22 '05 #3
"mead" <ph******@light.com> wrote...
Leor Zolman <le**@bdsoft.com> wrote in message
news:tf********************************@4ax.com...
On Sat, 01 May 2004 20:15:59 GMT, "mead" <ph******@light.com> wrote:
What kind of classes is qualified as "concrete classes"?
One you can instantiate. directly. In the ubiquitous "Shape" class
example, with a base class named "Shape" and then derived classes such
as "Circle", "Rectangle", etc., it makes sense to say
Circle c;
but not
Shape s;

(Although
Shape *s
would make perfect sense, as long as you don't try to do:
Shape *s = new Shape;

If "Shape" has a pure virtual function and this function is implemented in
Circle class as derived, then Circle becomes a class that can be
instantiate, thus Circle is concrete class but Shape isn't. Is it

correct?

Yes.
If Shape has no pure virtual function but virtual function(s), will it be a concrete class?
The fact that virtual functions are all defined (not pure) basically
indicates that the class has specific behaviour. That usually means
that the class is concrete.
[..]
When defining a pure virtual function and when defining a virtual function
in a class?


I am not sure I understand the question (it's not a proper English
sentence), but, usually, you define a virtual function pure only if
it doesn't make sense (or is impossible) to specify any concrete
definition for it. Take the same Shape vs Circle example Leor gave
and try to declare/define the member function 'getArea'. I always
was a believer that doing it is much better way to understand than
listening to or reading an explanation.

Victor
Jul 22 '05 #4
On Sat, 01 May 2004 21:35:01 GMT, "mead" <ph******@light.com> wrote:
Leor Zolman <le**@bdsoft.com> wrote in message
news:tf********************************@4ax.com.. .
On Sat, 01 May 2004 20:15:59 GMT, "mead" <ph******@light.com> wrote:
>What kind of classes is qualified as "concrete classes"?
One you can instantiate. directly. In the ubiquitous "Shape" class
example, with a base class named "Shape" and then derived classes such
as "Circle", "Rectangle", etc., it makes sense to say
Circle c;
but not
Shape s;

(Although
Shape *s
would make perfect sense, as long as you don't try to do:
Shape *s = new Shape;
>If "Shape" has a pure virtual function and this function is implemented in
Circle class as derived, then Circle becomes a class that can be
instantiate, thus Circle is concrete class but Shape isn't. Is it correct?

Yes, assuming no (other) unimplemented pure virtuals in Circle.

If Shape has no pure virtual function but virtual function(s), will it be a
concrete class? Yes.

>When should a member function in a class defined as "pure virtual" andwhen >as "virtual"?
That's actually quite related to your first question. The way to make
a class be abstract is to give it at least one pure virtual function.
If a base class contains one or more pure virtual functions, there are
really two things going on. One is that the class is abstract; but for
that alone, a single pure virtual function would have been sufficient.
The other thing going on is that classes which derive from the base
class must implement /each/ of the pure virtual functions they inherit
in order to qualify as a concrete class. So if a derived class only
implements 7 out of 8 of the pure virtual functions it inherits, it is
still an abstract class. But a further derived class will inherit only
a single pure virtual function, and that further derived class would
become concrete by implementing that one function.
-leor


When defining a pure virtual function and when defining a virtual function
in a class?

Are you asking when is it appropriate to define a function as pure virtual
vs. just plain virtual? I was trying to answer that in what I said above.
The short version: make it pure virtual if you require a derived class to
implement it in order for that derived class to qualify as a concrete
class. Or, just make /something/ virtual (typically the destructor) if you
want a class to be abstract but not to require some derived class to
necessarily have to implement anything in particular.
-leor
If you have a more specific question, ask away...
-leor


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #5

"mead" <ph******@light.com> wrote in message
news:3A*******************@bgtnsc05-news.ops.worldnet.att.net...
What kind of classes is qualified as "concrete classes"?

When should a member function in a class defined as "pure virtual" and when as "virtual"?

Thanks!

Hi,
may i answer your question a little differently. its good practise to
program to an interface. by interface in c++ i mean an
abstract class with ALL pure virtual functions.
So imagine you have your example of Shape and Circle. If you use the
above philosophy then it means that your Shape is an interface. This is a
lil different philosophy and there will be some non takers
for this but as a thumb rule almost never have a class with pure virtual
function and some non pure virtual functions. So if there is a
class with a pure virtual function, then let it just have such as those. You
can imagine this to be an interface.
The benefit of doing this is to keep the inheritance hierarchy at a low
depths!!

kutty
Jul 22 '05 #6
Kutty Banerjee <ku****@wpi.edu> wrote in message
news:c7**********@bigboote.WPI.EDU...

"mead" <ph******@light.com> wrote in message
news:3A*******************@bgtnsc05-news.ops.worldnet.att.net...
What kind of classes is qualified as "concrete classes"?

When should a member function in a class defined as "pure virtual" and when
as "virtual"?

Thanks!

Hi,
may i answer your question a little differently. its good practise to
program to an interface. by interface in c++ i mean an
abstract class with ALL pure virtual functions.
So imagine you have your example of Shape and Circle. If you use the
above philosophy then it means that your Shape is an interface. This is a
lil different philosophy and there will be some non takers
for this but as a thumb rule almost never have a class with pure virtual
function and some non pure virtual functions. So if there is a
class with a pure virtual function, then let it just have such as those.

You can imagine this to be an interface.
The benefit of doing this is to keep the inheritance hierarchy at a low depths!!

kutty

I like what you said about "program to an interface." However, "The benefit
of doing this is to keep the inheritance hierarchy at a low depths" is
confusing since adding an interface is always making inheritance hierarchy
deeper. Am I right on this?
Jul 22 '05 #7

"mead" <ph******@light.com> wrote in message
news:n3********************@bgtnsc04-news.ops.worldnet.att.net...
Kutty Banerjee <ku****@wpi.edu> wrote in message
news:c7**********@bigboote.WPI.EDU...

"mead" <ph******@light.com> wrote in message
news:3A*******************@bgtnsc05-news.ops.worldnet.att.net...
What kind of classes is qualified as "concrete classes"?

When should a member function in a class defined as "pure virtual" and when
as "virtual"?

Thanks!

Hi,
may i answer your question a little differently. its good practise to program to an interface. by interface in c++ i mean an
abstract class with ALL pure virtual functions.
So imagine you have your example of Shape and Circle. If you use the above philosophy then it means that your Shape is an interface. This is a lil different philosophy and there will be some non takers
for this but as a thumb rule almost never have a class with pure virtual
function and some non pure virtual functions. So if there is a
class with a pure virtual function, then let it just have such as those.

You
can imagine this to be an interface.
The benefit of doing this is to keep the inheritance hierarchy at a

low
depths!!

kutty

I like what you said about "program to an interface." However, "The

benefit of doing this is to keep the inheritance hierarchy at a low depths" is
confusing since adding an interface is always making inheritance hierarchy
deeper. Am I right on this?

Hi,
Yes i was not clear on the part about keeping hierarchy depths low. Consider
the
following example.

Method One:
class A
method 1
method 2
method 3
//All virtual methods!!

Class B: public A
method 1
method 2
method 3
//All the above methods are different implementations than those of
//class A.

Class C:public B
method 1
method 2
method 3
method 4
method 5
So we have three level hierarchy here!! how to make this two level?
to begin with, class B should inherit from Class X where class X is
the interface of class A(exact same method signaturres but no body, so
abstract class)
There must be another interface Class Y with methods 4 and 5.
Class C now inherits from abstract classes X and Y ( im mixing abstract
class and interface intentionally)
and not from class B. If however Class C must have exactly the same
funcftionality for methods 1,2, 3
as those of class B then instead of inheriting from class B it could use
delegation. and if it doesnt ,
then completely forget about class B.

So finally we have class A inheriting from abstract class X and Class B
inheriting from abstract class X, Y.
If necessary it will delegate to Class B (meaning have composition of Class
B ). but in short we have two levels
of inheritance now.

kutty
Jul 22 '05 #8
>The benefit of doing this is to keep the inheritance hierarchy at a low
depths!

Why is this a benefit?

When is such a rule best applied?

Citing your example, what if there is a legitimate "isa" relationship between
class C and class B? Do you still support class C containing a B and
delegating to it and providing appropriate conversion operators so that the
substitutiability of a C for a B is maintained?


Jul 22 '05 #9

"DaKoadMunky" <da*********@aol.com> wrote in message
news:20***************************@mb-m12.aol.com...
The benefit of doing this is to keep the inheritance hierarchy at a low depths!

Why is this a benefit?

When is such a rule best applied?

Citing your example, what if there is a legitimate "isa" relationship

between class C and class B? Do you still support class C containing a B and
delegating to it and providing appropriate conversion operators so that the substitutiability of a C for a B is maintained?

Hi,
this is a contentious issue in the world of OO and is called inheritance vs
delegation (composition).
In fact if there is a genuine isa relation between c and b classes, thats
when delegation comes to the
rescue. The advantage of using delegation is as follows:

Ask your self the question why would C be isa B? Well maybe because B has
say 3 virtual (or may not be virtual)
that it uses as is (does not override) and in addition has 2 other methods
of its own. Rite? Now in this case since
C uses the same interface as B(refer to the initial eg i d given), therefore
it still has the 3 methods of B only that
inside each of these methods , it invokes a contained B objects
corresponding methods. with me so far?

Question is what is the advantage of doing this. wait, lets see the
disadvantages of this first!! You are increasing the
size of the object. Since you are delegating, therefore there is an extra
overhead involved, so purists could even
argue that it is making things slower . Correct!!

Advantage? Consider that instead of class B, there is a class D which has
hte same methods of B but implements
them differently. At runtime you want your class C to choose either the
implementation of B or D . Thats where
delegation comes to the rescue. Delegation gives you runtime flexibility
whereas inheritance gives you compile
time flexibility.

Last question? In the original example i have talked about inheriting from
interface (aka abstract class with only
pure virtual methods). So each class inherits from the interface. Isnt this
inheritance too? shouldnt we get rid of this
too? the answer is, try and differentiate between class inheritance and
interface inheritance. Interfae inheritance is
what i just mentioned. Class inheritance is what we got rid of .
Jul 22 '05 #10

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

Similar topics

2
by: Wat | last post by:
Is it doable to have a abstract class derived from a concrete class? Is it a good practice? If so in what situation is this necessary? Thanks in advance!
7
by: Anitha | last post by:
What are abstract classes and concrete classes
4
by: Julia | last post by:
Hi, I have a EmailMessage and a UrgentMessage objects both derived from IMessage i also have BadMedia and GoodMedia objects both derived from IMedia GoodMedia:IMedia string Subject string...
3
by: Julia | last post by:
Hi, In general my question is how can I set properties on a concrete class while programming against interfaces For example I have a MailServerFatory which can create two types of Mail...
8
by: al pacino | last post by:
hi, i want to know how does c++ compiler prohibits the creation of instance of an abstract class? i think it must be checking on the vtable entry if there is an entry with zero(indicting...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
3
by: peter | last post by:
Hi, a "style" question. There is a method which creates a user in a database. I have to instantiate a user object, pass it to the method, and I get a completed user object back. public IUser...
1
by: Steve K. | last post by:
I added a property to an interface like this: <code> public interface ISimpleAddress { string CarrierFax{ get; set; } } </code>
1
by: Stodge | last post by:
Yet another SWIG question (YASQ!). I'm having a problem with using an abstract base class. When generating the Python bindings, SWIG thinks that all the concrete classes that derive from this...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.