You are here:

C++/How c++ is better than c

Advertisement


Question
Hi,

How can we says that c++ is better than c. What we can do in
c++ which we can not do in c. I know c++ have many extra
features than c. But i need an example situation where c++
need to be used better than c..

Please provide me the information..
Thanks in advance.

Answer
What can we do in C++ that cannot be done in C: pretty much nothing, in the same way that what we can do in C that we cannot do in native machine code or assembler is pretty much nothing. In fact some C++ implementations compile to C.

I cannot give such an example of "need" to use C++ over C. C++ may be chosen to be used over C for a project - or maybe mostly used over C. Even if you "need" to use C++ - e.g. because some other component that has to be used for some reason forces use of a C++ compiler - then you could still write (pretty much) C using the C++ compiler, only dipping into C++ styles (if at all) to use the component in question.

However, C does _not_ have support for most of the abstraction mechanisms that C++ has that allows programs to express type and structure well. C++ also provides stronger type checking than C.

Hence C++ is a better C in that is supports the programming styles of C with better type checking (and notational support) without loss of efficiency, and additionally supports several other programming styles such as object oriented programming and generic programming.

Dr. Bjarne Stroustrup says in "The C++ Programming Language" (3rd / Special edition) that a programming language serves two purposes:

   1/ Being "‘close to the machine’ so that all important aspects of a machine are handled
      simply and efficiently in a way that is reasonably obvious to the programmer. The C
      language was primarily designed with this in mind."

   2/ Being "‘close to the problem to be solved’ so that the concepts of a solution can be
      expressed directly and concisely. The facilities added to C to create C++ were
      primarily designed with this in mind."

Any small program - say around 500 - 2000 lines of code can be made to work by brute force even if it is badly structured and breaks every rule in the good coding style book.

This will not be true of larger systems: 100,000 or multi-million lines of code projects. Without decent structure such programs will have errors introduced as fast as you can take other errors out. Also such large projects would typically be worked on by many people - and often use code in the form of libraries from different vendors - and again C++ facilities such as namespaces are there to help with nasty things such as name clashes between separate libraries.

If you want more specifics on such matters and how C++ came be the way it is then I suggest you read "The Design and Evolution of C++" by Bjarne Stroustrup (http://www2.research.att.com/~bs/dne.html).

For more on this sort of thing checkout a few articles on the Internet, e.g.:

   http://unthought.net/c++/c_vs_c++.html
   http://www2.research.att.com/~bs/bs_faq.html (specifically the C and C++ section)

Hope this helps a little.  

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.