Click here to Skip to main content
15,887,880 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1)what is abstract class?
2)when we use abstract class with example?
Posted

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jul-11 3:35am    
My 5.
Please see my answer.
--SA
An abstract class is, well, abstract.

This[^] is what I did when I was learning.

Why use it ? Because it acts like an interface. It defines what a child class needs to do, but perhaps has no value on it's own. For example, a fruit class might be useless, because you always want a TYPE of fruit, but the class defines what a fruit needs to do ( for example, attend the mardi gras )
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Jul-11 3:36am    
Correct, a 5. As C++ implementations of interfaces is a subset of C++ abstract classes, I added a C++-specific explanation.
--SA
Stefan_Lang 8-Jul-11 5:06am    
At the time *I* was learning there was no Google, and the Internet as such didn't exist. Hell, I even bought a booklet(!) to help me solve some of the weirder puzzles in my Infocom text adventures!
Albert Holguin 8-Jul-11 9:39am    
my 5
Niklas L 8-Jul-11 11:59am    
Fived for the example alone.
While all the above solutions are correct, I'd like to add a real world example. Consider a drawing program that lets you draw line graphics using various shapes, such as lines, squares, or circles. As you might be aware of, everytime a window size changes, or when hidden parts of the window get exposed, you have to redraw your image containing these shapes. So you develop a class called 'Scene' that manages the drawing and redrawing:
class Scene {
private:
   std::list<class Shape*> shapes_;
public:
   void redraw();
};

This class contains a list of shapes that it has to redraw. However, you are just starting your program, and you don't know yet what kind of shapes you'd want to support, and the few you already thought of are stored and drawn in different ways. So you make 'Shape' a base class, and derive 'Line', 'Square' and 'Circle' from that base. The code to draw these shapes should also be implemented in each shape seperately, so you add a virtual function 'draw' to Shape, that gets overridden by the concrete, derived shape classes:
class Shape {
public:
   virtual ~Shape(); // don't forget virtual destructor on base classes
   virtual void draw() = 0;
};

class Line : public Shape {
private:
   Point p1_;
   Point p2_;
public:
   virtual ~Line();
   void draw();
};

class Square : public Shape {
private:
   Point position_;
   double width_;
public:
   virtual ~Square();
   void draw();
};

class Circle : public Shape {
private:
   Point position_;
   double radius_;
public:
   virtual ~Circle();
   void draw();
};

It makes little sense to implement draw() in the class Shape, since Shape itself does not define any elements that can be drawn. Therefore I declared draw() a pure virtual function. This, by definition, turns Shape into an abstract class. As a result, you will not be able to create an instance of type 'Shape'.

Objects of type Shape simply don't exist, they exist only in the form of derived classes, Line, Circle, or Square. These classes are considered concrete, since instances of these types actually exist. Shape OTOH is considered abstract, because no objects of this specific type exists, and the class is only used to describe a concept or common property of a certain range of object types.
 
Share this answer
 
v2
Comments
Christian Graus 8-Jul-11 4:51am    
*grin* that's usually my canonical example, but this time my sense of humour got the better of me....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900