|
-
February 22nd, 2009, 01:37 PM
#1
When do we need an abstract class?
I don't understand why i must have an abstract class.
Why must we begin with class 'Shape' instead of class 'TwoDimensionShape'?
-
February 22nd, 2009, 02:12 PM
#2
Re: When do we need an abstract class?
Well, C++ doesn't have a concept of an "abstract class" as such. The closest it has is pure virtual classes.
When would a class *need* to be pure virtual? Never. But it might make sense to make it pure virtual if there's no sense in constructing an object of that type directly. For instance, if "Shape" has an area() method, it doesn't make sense to construct a Shape since there's no formula for the area of a generic shape; only derived classes (maybe Square, Circle, etc) will have such formulas, so it would make sense to give them area() methods and make Shape be pure virtual.
As for why you'd need to use inheritance at all? Well, let's say we have Squares and Circles, which are both derived from Shapes, and we want to store an array of everything we've got. We could do
Code:
vector<Square*> allSquares;
vector<Circle*> allCircles;
and store the two types of things separately. But that might lead to code duplication if we're going to do the same thing to both arrays anyway. Instead, since a Square IS-A Shape and a Circle IS-A shape, we could store *all* of our shapes in one place with
Code:
vector<Shape*> allShapes;
This is the sort of concept that starts to make more sense as you use it.
-
February 22nd, 2009, 02:45 PM
#3
Re: When do we need an abstract class?
Generally, when you want to have a common interface for something, but you don't want to specify one particular type as the default.
Say you're dealing with animals...
Code:
class Animal
{
public:
virtual ~Animal( ) { }
virtual void walk( ) = 0;
virtual void sleep( ) = 0;
virtual bool isHungry( ) = 0;
virtual void eat(Plant& meal) = 0;
virtual void eat(Animal& meal) = 0;
// etc
};
class Horse : public Animal
{
// ...
};
class Lion : public Animal
{
// ...
};
class Sparrow : public Animal
{
// ...
};
It doesn't make sense to allow the construction of a generic Animal. What type of animal should it be? What good is it to have an animal that you don't know anything about? Etc... so we just say in effect "all animals should include the following, but you have to specifically define what they do before you can create them" by using an ABC.
And of course, the same can apply at intermediate levels as well. If you have a lot of animals that are of a similar type, you might want to change the animal example to something like
Code:
// class Animal as above
class Cat : public Animal
{
public:
virtual ~Cat( ) { }
virtual void hunt( ) = 0;
virtual void groom( ) = 0;
};
class Bird : public Animal
{
public:
virtual ~Bird( ) { }
virtual void fly( ) = 0;
};
class Lion : public Cat
{
// ...
};
class Sparrow : public Bird
{
// ...
};
-
February 22nd, 2009, 03:10 PM
#4
Re: When do we need an abstract class?
I suggest that you read these articles (in PDF format)
The third article is really the one that answers your question (see the "Separating Interface from Implementation in C++" section), but you need to know the first two concepts to be able to understand the third.
EDIT:
 Originally Posted by Lindley
Well, C++ doesn't have a concept of an "abstract class" as such. The closest it has is pure virtual classes.
To be fair, Stroustrup defines "abstract class" in his C++ glossary, but does not list "pure virtual class" in that same glossary.
Last edited by laserlight; February 22nd, 2009 at 03:13 PM.
-
February 24th, 2009, 06:52 AM
#5
Re: When do we need an abstract class?
Thanks all of you.
@laserlight: thank you but it's so complicated with me... I just unserstand OCP T.T
-
February 24th, 2009, 07:33 AM
#6
Re: When do we need an abstract class?
I suppose one example of The Liskov Substitution Principle could be the iostreams in the STL.
eg.
int value;
mystream << value;
This is valid regardless of whether the stream is an ofstream, ostringstream or a reference to cout. As long as it's derived from ostream, the calling code doesn't really care.
An example of The Dependency Inversion Principle could be an object that accepts an ostream as a directive as to where the output should be written to. The object is not interested in what the specific stream is, only that it presents the expected interface.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
February 24th, 2009, 06:09 PM
#7
Re: When do we need an abstract class?
 Originally Posted by Emerald214
I don't understand why i must have an abstract class.
Why must we begin with class 'Shape' instead of class 'TwoDimensionShape'?
First of all you don't need abstract classes. But if you do use them your code becomes more general. General code is the objective of abstract classes.
If you use TwoDimensionShape variables in your code, the code will work with objects of that class only. But if you use Shape variables in your code it will work with objects of any class derived from Shape. It will work with TwoDimensionShape objects but that's just for starters. Your code will work with objects of any class you care to derive from Shape.
Last edited by _uj; February 24th, 2009 at 06:16 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|