You are here:

C++/About c++

Advertisement


Question
Hello Sir,
           Can u tell how to get DATABASE Connectivity in c++ Development? and can we take Template Class as Base Class i.e. can we inherit from Template Class? Please Reply I m Looking  for your Reply....

Answer
1/ Database Connectivity:

ISO/ANSI standard C++ contains no support for database connectivity. That is far too specialised for the scope of such a standard. C++ is a very general purpose programming language and the standard C++ library support also tends to the general - language support, numeric functions, basic stream I/O with specific support for memory (string) and file streams, general collection types etc. Note: most of the standard C++ library is for hosted implementations of ISO/ANSI C++, the standard also describes a freestanding implementation type that is for use with systems with no supporting operating system (i.e. no host operating system), that has no library support at all other than that required for language support.

So any support you require must come from 3rd party additions. These could be either additions to the core language such as Embedded-SQL pre-compilers for a specific database (see for example http://en.wikipedia.org/wiki/Embedded_SQL ) or a C or C++ support library, which could either be, again, for a specific database, or access to a more general technology such as ODBC (for which you would require specific 'drivers' or other components for the database engines of interest). Note: C++ is able to easily use the facilities of C libraries.

For example if you are using the Microsoft compiler (Visual C++) on a Microsoft platform (i.e. Windows) then you could look at the Windows Development, Data Access and Storage sections of the MSDN Library ( http://msdn.microsoft.com/library/ee663264%28v=VS.85%29.aspx ), of which the Windows Data Access Components (Windows DAC or MDAC) SDK is probably of most interest.

Alternatively you can look at the raw access techniques and libraries provided with the specific database engine(s) of interest (e.g. the MySQL C API: http://dev.mysql.com/doc/refman/5.0/en/c.html ).

Then again maybe something a little higher up the abstraction levels would be of interest such as a Object Relational Mapper (ORM), such as Wt::Dbo ( http://www.webtoolkit.eu/wt/doc/tutorial/dbo/tutorial.html ), part of the Wt C++ web application library.

Oh, and some C++ GUI framework libraries tend a bit towards the everything including the kitchen sink approach to features so may contain some database support - the Microsoft Foundation Class (MFC) library's DAO, ODBC and OLE DB classes ( http://msdn.microsoft.com/en-us/library/fe1cf721.aspx ), or the Qt library's database module ( http://qt.nokia.com/products/library/modular-class-library#info_database ) for example.

You might like to search the Internet for sites that have C++ or C database libraries. To get you started you can look at the C++ Org site's C++ library database's database category ( http://c-plusplus.org/index.php?option=com_mtree&task=listcats&cat_id=54&Itemid=... ) and the similar category on the thefreecountry.com site ( http://www.thefreecountry.com/sourcecode/database.shtml ).



2/ Inheriting from Class Template

Note: I assume you mean a class template, i.e.:

   template <typename T>
   class TC
   {
   ...
   };
   
and not a class that implements the template (method) pattern  (see for example:

   http://login2win.blogspot.com/2008/06/c-template-pattern.html
   http://en.wikipedia.org/wiki/Template_method_pattern    
)

The answer is technically no. A template class is _not_ a class - it is a template (for the compiler) for producing a whole family of classes, each one specific and defined by its specific set of template arguments and called a specialisation of that template.

However, you can of course use a class template with template arguments as a base class:

   class C : public TC<int>
   {
   ...
   };

A class template can use another class template as a base class:

   template <typename U>
   class TC_Derived : public TC<int>
   {
   ...
   };

Or more commonly, some or all of the derived class template's template parameters are used to specialise the base class template:

   template <typename T, typename U>
   class TC_Derived2 : public TC<T>
   {
   ...
   };

One oddly common use is for the derived class to be used to specialise a base class template:

   class D : public TC<D>
   {
   ...
   };

This pattern crops up frequently enough that it has a name: the curiously recurring template pattern (CRTP) (see
http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern )


Hope this helps.

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

©2012 About.com, a part of The New York Times Company. All rights reserved.