C++/c++

Advertisement


Question
explain me about abstract class with an exmple please.

Answer
Hi, Ravi.

An abstract base class is a class which contains one or more functions with no implementation (pure-virtual functions).  Abstract classes are useful for defining an interface which will be used by multiple implementations of a base class.  For example, you could write an abstract tree class which declares all of the common functions of the tree, and then create derived classes from the abstract base class which define the implementation of the pure virtual functions.  To declare a function as a pure virtual function, you set it equal to zero.  Sounds weird, but that's how it goes:

   void myAbstractFunction() = 0;

When a class is abstract, it cannot be instantiated.  Instead, whenever you wish to use the class, you must declare an instance of one of the derived classes.  As a quick example using my tree idea from above, here's a (super simple) outline for the class definitions:

   class binTreeBase
   {
   public:
       binTreeBase() { mRoot = NULL; }
       virtual ~binTreeBase() { // perform cleanup }

       void addNode(int myData) = 0;
       void removeNode(treeNode* nodeToRemove) = 0;

   private:
       treeNode* mRoot;
   };

   class balancedBinTree : public binTreeBase
   {
   public:
       void addNode(int myData) { // perform necessary insert logic }
       void removeNode(treeNode* nodeToRemove) { // perform necessary remove logic }
   };

   class redBlackTree : public binTreeBase
   {
   public:
       void addNode(int myData) { // perform necessary insert logic }
       void removeNode(treeNode* nodeToRemove) { // perform necessary remove logic }
   };

In this way, you declare a common interface (and some common member variables) for all binary trees, but you can define unique implementations for different binary tree types.

Please let me know if you need further assistance.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C++ code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2016 About.com. All rights reserved.