C++/C++

Advertisement


Question
Thank you
I m really satisfied with the answer.
I have one more doubt....
In which cases Abstract base classes will be helpful.


Answer
The obvious answer is when designing interfaces. Unlike other newer languages such as Java and C#, C++ has no interface keyword. An interface defines a set of methods that an implementer of the interface must implement. A reference (or pointer) to an instance of any object that implements the interface can be used where a reference (or pointer) to the interface is required. Here is a simplified version of a 2D board game renderer interface from some of my own code:

 class MGameRenderer
 {
 public:
  virtual
  void          DrawBoard
         ( ::CWindowGc & aGc
         , ::TRect const & aRegion
         ) = 0;

   virtual
   void          DrawCell
         ( ::CWindowGc & aGc
         , ::TPoint const & aCell
         , ::TUint32 aNewState
         , ::TUint32 aPrevState
         ) = 0;

   virtual ~MGameRenderer();
 };

The code that makes use of game renderers pass around and make calls to MGameRenderer pointers. Somewhere there is a factory object that creates objects of specific implementations of MGameRenderer e.g. by using new. The factory returns them as a MGameRenderer pointer:

 MGameRenderer * CRenderer::NewGameRenderer
 ( ::TInt aGameDimension
 , ::TRect const & aGameRect
 ) const
 {
   return new CGameRenderer( aGameDimension, aGameRect );
 }

Again this is a simplification of the actual situation. The CGameRenderer class provides implementations for DrawBoard and DrawCell. In the program I provide a basic implementation that draws the game board and the board cells using simple 2D graphics operations such as drawing lines and circles. A fancier implementation might use pre-drawn graphics or even animations for the board and cell states. The renderers are built as separate components so such additional renderers could be written, build and installed at a later date and the user can select from installed renderers the one they wish to use.

However, in languages with direct support for interfaces they seems to have to  contain only public methods and all must be implemented by those classes that implement the interface. In C++ we have more choice. It may be that only one or two methods of your base class have no obvious base implementation and so must be provided by sub-classes. The base class can contain other things: data members and non-virtual member functions and virtual member functions that have a base implementation. Such situations will become apparent during the design process (hopefully).

One technique that C++ can use in its 'interface' classes is to implement the public interface methods as normal non-virtual member functions in the abstract base class, and these call private pure virtual functions that must be implemented by sub-classes. What is the point you ask? Well around the call to the pure virtual function the public member function can perform call contract checks for pre-conditions, post-conditions and invariants. This in fact is how MGameRenderer is designed:

 class MGameRenderer
 {
 public:
   void          DrawBoard
         ( ::CWindowGc & aGc
         , ::TRect const & aRegion
         );

   void          DrawCell
         ( ::CWindowGc & aGc
         , ::TPoint const & aCell
         , ::TUint32 aNewState
         , ::TUint32 aPrevState
         );

   virtual ~MGameRenderer();

 private:
   virtual
   void          DrawBoardImpl
         ( ::CWindowGc & aGc
         , ::TRect const & aRegion
         ) = 0;

   virtual
   void          DrawCellImpl
         ( ::CWindowGc & aGc
         , ::TPoint const & aCell
         , ::TUint32 aNewState
         , ::TUint32 aPrevState
         ) = 0;
 };

DrawBoard for example checks that the rectangle is normalised (i.e. top,left is is at or above and at or to the left of bottom,right) before calling DrawBoardImpl:

 void  MGameRenderer::DrawBoard
 ( ::CWindowGc & aGc
 , ::TRect const & aRegion
 )
 {
 // Check we have a normalised rectangle for the region
   bool condition( aRegion.Height() >= 0 && aRegion.Width() >= 0 );
   __ASSERT_ALWAYS( condition, Panic(EBadRegion) );      

   this->DrawBoardImpl( aGc, aRegion );
 }

This ensures such checks are always performed as they are part of the interface class and not left to the implementations to perform.

Hope this gives you some ideas...  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.