<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1232785149712460829</id><updated>2011-11-27T15:41:48.160-08:00</updated><category term='The C++ Annotations are not freely distributable. Be sure to read the legal notes'/><title type='text'>C++ Programming</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cprogramming4u.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1232785149712460829/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://cprogramming4u.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Mohammed Adil</name><uri>http://www.blogger.com/profile/09986294828972956062</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='http://3.bp.blogspot.com/_wFWi1AQ8PoE/SKqBETTZDsI/AAAAAAAAAVo/kn_ljn4WgI0/S220/7_6_batista.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1232785149712460829.post-3488207523964713233</id><published>2008-06-29T03:45:00.000-07:00</published><updated>2008-07-08T00:35:19.620-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='The C++ Annotations are not freely distributable. Be sure to read the legal notes'/><title type='text'>C++</title><content type='html'>&lt;h3&gt;                                                                   History of the C++ Annotations&lt;/h3&gt; &lt;p&gt;The original version of the guide was originally written by Frank and Karel in Dutch and in LaTeX format. After some time, Karel Kubat rewrote the text and converted the guide to a more suitable format and (of course) to English in september 1994. &lt;/p&gt;&lt;p&gt;The first version of the guide appeared on the net in october 1994. By then it was converted to &lt;code&gt;SGML&lt;/code&gt;. &lt;/p&gt;&lt;p&gt;In time several chapters were added, and the contents were modified thanks to countless readers who sent us their comment, due to which we were able to correct some typos and improve unclear parts. &lt;/p&gt;&lt;p&gt;The transition from major version three to major version four was realized by Frank: again new chapters were added, and the source-document was converted from &lt;code&gt;SGML&lt;/code&gt; to &lt;a href="javascript:if(confirm('http://www.icce.rug.nl/docs/programs/yodl/yodl.html%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.icce.rug.nl/docs/programs/yodl/yodl.html'" tppabs="http://www.icce.rug.nl/docs/programs/yodl/yodl.html"&gt;Yodl&lt;/a&gt;. &lt;/p&gt;&lt;blockquote&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Reading the annotations beyond this point implies that you are aware of the restrictions that we pose and that you agree with them.&lt;/strong&gt; &lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;If you like this document, tell your friends about it. Even better, let us know by sending email to &lt;a href="mailto:f.b.brokken@rc.rug.nl"&gt;aaaorganization@gmail.com&lt;/a&gt;.  &lt;/p&gt;&lt;p&gt;&lt;a name="l4"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h2&gt;2.1: What's new in the C++ Annotations&lt;/h2&gt;     &lt;a name="history"&gt;&lt;/a&gt;     This section is modified when the first and second part of the version numbers change. Modifications in versions 1.*.*, 2.*.*, and 3.*.* were not logged. &lt;p&gt;Major version 4 represents a major rewrite of the previous version 3.4.14: The document was rewritten from SGML to  &lt;a href="javascript:if(confirm('http://www.icce.rug.nl/docs/programs/yodl/yodl.html%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.icce.rug.nl/docs/programs/yodl/yodl.html'" tppabs="http://www.icce.rug.nl/docs/programs/yodl/yodl.html"&gt;Yodl&lt;/a&gt;, and many new sections were added. All sections got a tune-up. The distribution basis, however, hasn't changed: see &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus02.html#IntroC" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus02.html#IntroC"&gt;the introduction&lt;/a&gt;. &lt;/p&gt;&lt;p&gt;The upgrade from version 4.1.* to 4.2.* was the result of the inclusion of section &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus03.html#BOOL" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus03.html#BOOL"&gt;3.3.1&lt;/a&gt; about the &lt;strong&gt;bool&lt;/strong&gt; data type in chapter &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus03.html#FirstImpression" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus03.html#FirstImpression"&gt;3&lt;/a&gt;. The distinction between differences between &lt;strong&gt;C&lt;/strong&gt; and &lt;strong&gt;C++&lt;/strong&gt; and extensions of the &lt;strong&gt;C&lt;/strong&gt; programming languages is (albeit a bit fuzzy) reflected in the introdution chapter and the chapter on first impressions of &lt;strong&gt;C++&lt;/strong&gt;: The &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus02.html#IntroC" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus02.html#IntroC"&gt;introduction chapter&lt;/a&gt;  covers some differences between &lt;strong&gt;C&lt;/strong&gt; and &lt;strong&gt;C++&lt;/strong&gt;, whereas the chapter about  &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus03.html#FirstImpression" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus03.html#FirstImpression"&gt;first impressions&lt;/a&gt; of &lt;strong&gt;C++&lt;/strong&gt; covers some extensions of the &lt;strong&gt;C&lt;/strong&gt; programming language as found in &lt;strong&gt;C++&lt;/strong&gt;. &lt;/p&gt;&lt;p&gt;The decision to upgrade from version 4.2.* to 4.3.* was made after realizing that the lexical scanner function &lt;code&gt;yylex()&lt;/code&gt; can be defined in the  scanner class that is derived from &lt;code&gt;yyFlexLexer&lt;/code&gt;. Under this approach the &lt;code&gt;yylex()&lt;/code&gt; function can access the members of the class derived from &lt;code&gt;yyFlexLexer&lt;/code&gt; as well as the public and protected members of &lt;code&gt;yyFlexLexer&lt;/code&gt;. The result of all this is a clean implementation of the rules defined in the &lt;code&gt;flex++&lt;/code&gt; specification file. See section &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus18.html#Flexpp" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus18.html#Flexpp"&gt;18.4.1&lt;/a&gt; for details.  &lt;/p&gt;&lt;p&gt;The version &lt;code&gt;4.3.1a&lt;/code&gt; is a precursor of &lt;code&gt;4.3.2&lt;/code&gt;. In &lt;code&gt;4.3.1a&lt;/code&gt; most of the typos I've received since the last update have been processed. In version &lt;code&gt;4.3.2.&lt;/code&gt; the following modifications will be incorporated as well: &lt;/p&gt;&lt;ul&gt;&lt;li&gt; Function-addresses must be obtained using the &lt;code&gt;&amp;amp;&lt;/code&gt;-operator     &lt;/li&gt;&lt;li&gt; Functions called via pointers to member functions must use the         &lt;code&gt;(this-&gt;*pointer)(...)&lt;/code&gt; construction inside member functions of the         class in which the pointer to member functions is defined. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Version &lt;code&gt;4.4.1&lt;/code&gt; again contains new material, and reflects the  &lt;a href="javascript:if(confirm('ftp://research.att.com/dist/c++std/WP/%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='ftp://research.att.com/dist/c++std/WP/'" tppabs="ftp://research.att.com/dist/c++std/WP/"&gt;ANSI/ISO&lt;/a&gt; standard (well, I try to have it reflect the ANSI/ISO standard). In version 4.4.1. the following sections and chapters were added: &lt;/p&gt;&lt;ul&gt;&lt;li&gt; Releases over 4.4.1 handle typos and suggestions for improvements             I've received by &lt;a href="mailto:f.b.brokken@rc.rug.nl"&gt;email&lt;/a&gt;.      &lt;/li&gt;&lt;li&gt; A section (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus16.html#RTTI" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus16.html#RTTI"&gt;16.6&lt;/a&gt; about &lt;em&gt;Run-Time Type Identification&lt;/em&gt;,             included as of release 4.4.1.     &lt;/li&gt;&lt;li&gt; A section (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus16.html#DYNAMICCAST" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus16.html#DYNAMICCAST"&gt;16.6.1&lt;/a&gt; about the &lt;code&gt;dynamic_cast&lt;/code&gt; cast operator.             included as of release 4.4.1.     &lt;/li&gt;&lt;li&gt; Minor spellingcorrections were made up to release 4.4.0n.     &lt;/li&gt;&lt;li&gt; A reference to &lt;code&gt;icmake&lt;/code&gt; and the &lt;code&gt;C++&lt;/code&gt;-build script was added in  release 4.4.0m (see section &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus02.html#COMPILATION" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus02.html#COMPILATION"&gt;2.2.2&lt;/a&gt;).     &lt;/li&gt;&lt;li&gt; A section (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus03.html#Namespaces" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus03.html#Namespaces"&gt;3.6&lt;/a&gt;) about &lt;em&gt;namespaces&lt;/em&gt;,             included as of release 4.4.0i.     &lt;/li&gt;&lt;li&gt; A section (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus07.html#EXPLICIT" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus07.html#EXPLICIT"&gt;7.6&lt;/a&gt;) about the &lt;code&gt;explicit&lt;/code&gt; keyword,             included as of release 4.4.0h.     &lt;/li&gt;&lt;li&gt; A section about constructing &lt;em&gt;manipulators&lt;/em&gt; (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus12.html#CONSMANIP" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus12.html#CONSMANIP"&gt;12.3.8&lt;/a&gt;),             included as of release 4.4.0h.     &lt;/li&gt;&lt;li&gt; A section about overloading the operators &lt;code&gt;++&lt;/code&gt; and &lt;code&gt;--&lt;/code&gt;             (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus07.html#OVERLOADINCR" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus07.html#OVERLOADINCR"&gt;7.7&lt;/a&gt;),             included as of release 4.4.0h.     &lt;/li&gt;&lt;li&gt; A rewrite of the chapter about Templates (chapter &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus17.html#Templates" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus17.html#Templates"&gt;17&lt;/a&gt;),             included as of release 4.4.0h.     &lt;/li&gt;&lt;li&gt; A section (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus11.html#AUTOPTR" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus11.html#AUTOPTR"&gt;11.2&lt;/a&gt; about &lt;code&gt;auto_ptr&lt;/code&gt; objects,             included as of release 4.4.0g.     &lt;/li&gt;&lt;li&gt; A section (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus05.html#NESTING" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus05.html#NESTING"&gt;5.8&lt;/a&gt;) about nested classes.             included as of release 4.4.0f.     &lt;/li&gt;&lt;li&gt; The chapter (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus12.html#IOStreams" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus12.html#IOStreams"&gt;12&lt;/a&gt;) about &lt;code&gt;iostreams&lt;/code&gt; was modified, and now contains more information about using manipulators and flags, as well as information about using &lt;code&gt;strstream&lt;/code&gt; objects.             Included as of release 4.4.0e.     &lt;/li&gt;&lt;li&gt; A chapter (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus11.html#STL" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus11.html#STL"&gt;11&lt;/a&gt; about the &lt;em&gt;Standard Template Library&lt;/em&gt; and         generic algorithms,             included as of release 4.4.0e.     &lt;/li&gt;&lt;li&gt; The full contents of the &lt;strong&gt;C++&lt;/strong&gt; Annotations can be inspected in parallel with the annotations themselves when the &lt;code&gt;html&lt;/code&gt;-format is used.             Included as of release 4.4.0d.     &lt;/li&gt;&lt;li&gt; The section (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus05.html#INLINE" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus05.html#INLINE"&gt;5.4&lt;/a&gt;) about &lt;code&gt;inline&lt;/code&gt; functions was slightly modified,             included as of release 4.4.0d.     &lt;/li&gt;&lt;li&gt; A section (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus07.html#FUNOBJ" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus07.html#FUNOBJ"&gt;7.8&lt;/a&gt; about &lt;em&gt;function objects&lt;/em&gt;,             included as of release 4.4.0d.     &lt;/li&gt;&lt;li&gt; A chapter (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus08.html#Containers" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus08.html#Containers"&gt;8&lt;/a&gt; about the abstract container types,             included as of release 4.4.0c.     &lt;/li&gt;&lt;li&gt; A section (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus02.html#CPPCASTS" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus02.html#CPPCASTS"&gt;2.5.4&lt;/a&gt; about the new syntax used with &lt;em&gt;casts&lt;/em&gt;,             included as of release 4.4.0b.     &lt;/li&gt;&lt;li&gt; A section about the &lt;code&gt;string&lt;/code&gt; type,             included as of release 4.4.0b.     &lt;/li&gt;&lt;li&gt; A section (&lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus02.html#COMPILATION" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus02.html#COMPILATION"&gt;2.2.2&lt;/a&gt; about compiling &lt;strong&gt;C++&lt;/strong&gt; programs,             included as of release 4.4.0a. &lt;/li&gt;&lt;/ul&gt; Version &lt;code&gt;4.4.0&lt;/code&gt; (and subletters) is a construction version, in which the extras mentioned above are only partially available. &lt;p&gt;In release &lt;code&gt;4.4.1b&lt;/code&gt; the pagesize in the LaTeX file was defined to be &lt;code&gt;din A4&lt;/code&gt;. In countries where other pagesizes are standard the conversion the default pagesize might be a better choice. In that case, remove the &lt;code&gt;dina4&lt;/code&gt; option from &lt;code&gt;cplusplus.tex&lt;/code&gt; (or &lt;code&gt;cplusplus.yo&lt;/code&gt; if you have &lt;code&gt;yodl&lt;/code&gt; installed), and reconstruct the annotations from the &lt;em&gt;TeX&lt;/em&gt;-file or &lt;code&gt;Yodl&lt;/code&gt;-files.  &lt;/p&gt;&lt;p&gt;The Annotations mailing lists was stopped at release &lt;code&gt;4.4.1d&lt;/code&gt;. From this point on only minor modifications were expected, which are not anymore generally announced. &lt;/p&gt;&lt;p&gt;At some point, I considered version &lt;code&gt;4.4.1.&lt;/code&gt; to be the final version of the &lt;strong&gt;C++&lt;/strong&gt; annotations. However, a section on special I/O functions (section &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus12.html#SPECIALFUN" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus12.html#SPECIALFUN"&gt;12.3.5&lt;/a&gt;) was added to cover unformated I/O, and the section about the &lt;code&gt;string&lt;/code&gt; datatype had its layout improved and was, due to its volume, given a chapter of its own (chapter &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus04.html#String" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus04.html#String"&gt;4&lt;/a&gt;). All this eventually resulted in version &lt;code&gt;4.4.2.&lt;/code&gt;. &lt;/p&gt;&lt;p&gt;Considering the volume of the annotations, I'm sure there will be typos found every now and then. Please do not hesitate to send me an email containing any mistakes you find or corrections you would like to suggest. Subreleases like &lt;code&gt;4.4.2a&lt;/code&gt; etc. contain bugfixes and typographical corrections.  &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;&lt;a name="l5"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h2&gt;2.2: The history of C++&lt;/h2&gt;     &lt;a name="intro/history"&gt;&lt;/a&gt;     The first implementation of &lt;strong&gt;C++&lt;/strong&gt; was developed in the eighties at the AT&amp;amp;T Bell Labs, where the Unix operating system was created. &lt;p&gt;&lt;strong&gt;C++&lt;/strong&gt; was originally a `pre-compiler', similar to the preprocessor of &lt;strong&gt;C&lt;/strong&gt;, which converted special constructions in its source code to plain &lt;strong&gt;C&lt;/strong&gt;. This code was then compiled by a normal &lt;strong&gt;C&lt;/strong&gt; compiler. The `pre-code', which was read by the &lt;strong&gt;C++&lt;/strong&gt; pre-compiler, was usually located in a file with the extension &lt;code&gt;.cc&lt;/code&gt;, &lt;code&gt;.C&lt;/code&gt; or &lt;code&gt;.cpp&lt;/code&gt;. This file would then be converted to a C source file with the extension &lt;code&gt;.c&lt;/code&gt;, which was compiled and linked. &lt;/p&gt;&lt;p&gt;The nomenclature of &lt;strong&gt;C++&lt;/strong&gt; source files remains: the extensions &lt;code&gt;.cc&lt;/code&gt; and &lt;code&gt;.cpp&lt;/code&gt; are usually still used. However, the preliminary work of a &lt;strong&gt;C++&lt;/strong&gt; pre-compiler is in modern compilers usually included in the actual compilation process. Often compilers will determine the type of a source file by the extension. This holds true for Borland's and Microsoft's &lt;strong&gt;C++&lt;/strong&gt; compilers, which assume a &lt;strong&gt;C++&lt;/strong&gt; source for an extension &lt;code&gt;.cpp&lt;/code&gt;. The GNU compiler &lt;code&gt;gcc&lt;/code&gt;, which is available on many Unix platforms, assumes for &lt;strong&gt;C++&lt;/strong&gt; the extension &lt;code&gt;.cc&lt;/code&gt;. &lt;/p&gt;&lt;p&gt;The fact that &lt;strong&gt;C++&lt;/strong&gt; used to be compiled into &lt;strong&gt;C&lt;/strong&gt; code is also visible from the fact that &lt;strong&gt;C++&lt;/strong&gt; is a superset of &lt;strong&gt;C&lt;/strong&gt;: &lt;strong&gt;C++&lt;/strong&gt; offers all possibilities of &lt;strong&gt;C&lt;/strong&gt;, and more. This makes the transition from &lt;strong&gt;C&lt;/strong&gt; to &lt;strong&gt;C++&lt;/strong&gt; quite easy. Programmers who are familiar with &lt;strong&gt;C&lt;/strong&gt; may start `programming in &lt;strong&gt;C++&lt;/strong&gt;' by using source files with an extension &lt;code&gt;.cc&lt;/code&gt; or &lt;code&gt;.cpp&lt;/code&gt; instead of &lt;code&gt;.c&lt;/code&gt;, and can then just comfortably slide into all the possibilities that &lt;strong&gt;C++&lt;/strong&gt; offers. No abrupt change of habits is required.  &lt;/p&gt;&lt;p&gt;     &lt;a name="CppComp"&gt;&lt;/a&gt;&lt;a name="l6"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.2.1: Compiling a C program by a C++ compiler&lt;/h3&gt;         &lt;a name="intro/cascpp"&gt;&lt;/a&gt;     For the sake of completeness, it must be mentioned here that &lt;strong&gt;C++&lt;/strong&gt; is `almost' a superset of &lt;strong&gt;C&lt;/strong&gt;. There are some small differences which you might encounter when you just rename a file to an extension &lt;code&gt;.cc&lt;/code&gt; and run it through a &lt;strong&gt;C++&lt;/strong&gt; compiler: &lt;ul&gt;&lt;li&gt; In &lt;strong&gt;C&lt;/strong&gt;, &lt;code&gt;sizeof('c')&lt;/code&gt; equals &lt;code&gt;sizeof(int)&lt;/code&gt;,     &lt;code&gt;'c'&lt;/code&gt; being any ASCII character.  The underlying philosophy is     probably that &lt;code&gt;char&lt;/code&gt;'s, when passed as arguments to functions, are     passed as integers anyway. Furthermore, the &lt;strong&gt;C&lt;/strong&gt; compiler handles a     character constant like &lt;code&gt;'c'&lt;/code&gt; as an integer constant. Hence, in     &lt;strong&gt;C&lt;/strong&gt;, the function calls &lt;pre&gt; putchar(10);&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;and &lt;/p&gt;&lt;pre&gt; putchar('\n');&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;are synonyms. &lt;/p&gt;&lt;p&gt;In contrast, in &lt;strong&gt;C++&lt;/strong&gt;, &lt;code&gt;sizeof('c')&lt;/code&gt; is always 1 (but see also section &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus03.html#WCHAR" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus03.html#WCHAR"&gt;3.3.2&lt;/a&gt;), while     an &lt;code&gt;int&lt;/code&gt; is still an &lt;code&gt;int&lt;/code&gt;. As we shall see later (see     section &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus02.html#FunctionOverloading" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus02.html#FunctionOverloading"&gt;2.5.13&lt;/a&gt;), two function calls &lt;/p&gt;&lt;pre&gt; somefunc(10);&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;and &lt;/p&gt;&lt;pre&gt; somefunc('\n');&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;   &lt;p&gt;are quite separate functions: &lt;strong&gt;C++&lt;/strong&gt; discriminates functions by     their arguments, which are different in these two calls: one function     requires an &lt;code&gt;int&lt;/code&gt; while the other one requires a &lt;code&gt;char&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;&lt;li&gt; &lt;strong&gt;C++&lt;/strong&gt; requires very strict prototyping of external     functions. E.g., a prototype like &lt;pre&gt; extern void func();&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;means in &lt;strong&gt;C&lt;/strong&gt; that a function &lt;code&gt;func()&lt;/code&gt; exists, which returns     no value. However, in &lt;strong&gt;C&lt;/strong&gt;, the declaration doesn't specify which     arguments (if any) the function takes. &lt;/p&gt;&lt;p&gt;In contrast, such a declaration in &lt;strong&gt;C++&lt;/strong&gt; means that the     function &lt;code&gt;func()&lt;/code&gt; takes no arguments at all.  &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;  &lt;p&gt;     &lt;a name="COMPILATION"&gt;&lt;/a&gt;&lt;a name="l7"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.2.2: Compiling a C++ program&lt;/h3&gt;         &lt;a name="intro/compiling"&gt;&lt;/a&gt;     In order to compile a &lt;strong&gt;C++&lt;/strong&gt; program, a &lt;strong&gt;C++&lt;/strong&gt; compiler is needed. Considering the free nature of this document, it won't come as a surprise that a &lt;em&gt;free compiler&lt;/em&gt; is suggested here. The &lt;a href="javascript:if(confirm('http://www.gnu.org/%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.gnu.org/'" tppabs="http://www.gnu.org/"&gt;Free Software Foundation&lt;/a&gt; provides free &lt;strong&gt;C++&lt;/strong&gt; compilers which is, among other places, available in the &lt;a href="javascript:if(confirm('http://www.debian.org/%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.debian.org/'" tppabs="http://www.debian.org/"&gt;Debian&lt;/a&gt; distribution of &lt;a href="javascript:if(confirm('http://www.icce.rug.nl/www.linux.org%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.icce.rug.nl/www.linux.org'" tppabs="http://www.icce.rug.nl/www.linux.org"&gt;Linux&lt;/a&gt;. The version that's currenly available on my computer is 2.95.2. &lt;p&gt; &lt;/p&gt;&lt;p&gt; &lt;a name="l8"&gt;&lt;/a&gt;                &lt;strong&gt;2.2.2.1: C++ under MS-Windows&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;             &lt;a name="intro/mswindows"&gt;&lt;/a&gt;     For MS-Windows &lt;a href="javascript:if(confirm('http://www.redhat.com/%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.redhat.com/'" tppabs="http://www.redhat.com/"&gt;Cygnus&lt;/a&gt; provides the foundation for installing the &lt;em&gt;Windows port&lt;/em&gt; of the &lt;code&gt;GNU g++&lt;/code&gt; compiler.  &lt;/p&gt;&lt;p&gt;Cygnus is now distributed by &lt;a href="javascript:if(confirm('http://www.redhat.com/%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.redhat.com/'" tppabs="http://www.redhat.com/"&gt;RedHat&lt;/a&gt;. Currently (october 2000), the cygnus distribution is found at &lt;a href="javascript:if(confirm('http://sources.redhat.com/%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://sources.redhat.com/'" tppabs="http://sources.redhat.com/"&gt;http://sources.redhat.com&lt;/a&gt;. When linking to that URL, click on the &lt;code&gt;cygwin&lt;/code&gt; menu entry and then on &lt;code&gt;install now&lt;/code&gt;. This will download the file &lt;code&gt;setup.exe&lt;/code&gt;, which can be run to install &lt;code&gt;cygwin&lt;/code&gt;. The software to be installed can be downloaded by &lt;code&gt;setup.exe&lt;/code&gt; from the internet. There are alternatives (e.g., using a CD-ROM), which are described on the &lt;code&gt;cygwin&lt;/code&gt; page. Installation proceeds interactively. The offered defaults are normally what you would want. &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt; &lt;a name="l9"&gt;&lt;/a&gt;                &lt;strong&gt;2.2.2.2: Compiling a C++ source text&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;             &lt;a name="intro/compilesources"&gt;&lt;/a&gt;     In general, compiling a &lt;strong&gt;C++&lt;/strong&gt; source &lt;code&gt;source.cc&lt;/code&gt; is done as follows: &lt;/p&gt;&lt;center&gt;&lt;code&gt;g++ source.cc&lt;/code&gt; &lt;/center&gt; &lt;p&gt;This produces a binary program (&lt;code&gt;a.out&lt;/code&gt; or &lt;code&gt;a.exe&lt;/code&gt;). If the default name is not wanted, the name of the executable can be specified using the &lt;code&gt;-o&lt;/code&gt; flag: &lt;/p&gt;&lt;center&gt;&lt;code&gt;g++ -o source source.cc&lt;/code&gt; &lt;/center&gt; &lt;p&gt;If only a compilation is required, the compiled module can be generated using the &lt;code&gt;-c&lt;/code&gt; flag: &lt;/p&gt;&lt;center&gt;&lt;code&gt;g++ -c source.cc&lt;/code&gt; &lt;/center&gt; &lt;p&gt;This produces the file &lt;code&gt;source.o&lt;/code&gt;, which can be linked to other modules later on. &lt;/p&gt;&lt;p&gt;Using the &lt;code&gt;icmake&lt;/code&gt; program (to be downloaded from &lt;a href="javascript:if(confirm('ftp://ftp.icce.rug.nl/pub/unix%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='ftp://ftp.icce.rug.nl/pub/unix'" tppabs="ftp://ftp.icce.rug.nl/pub/unix"&gt;ftp://ftp.icce.rug.nl/icmake-X.YY.tar.gz&lt;/a&gt;) a maintenance script can be used to assist in the construction and maintenance of a &lt;strong&gt;C++&lt;/strong&gt; program. This script has been tested on &lt;strong&gt;Linux&lt;/strong&gt; platforms for several years now. It is described at  &lt;a href="javascript:if(confirm('http://www.icce.rug.nl/docs/programs/Cscript.html%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.icce.rug.nl/docs/programs/Cscript.html'" tppabs="http://www.icce.rug.nl/docs/programs/Cscript.html"&gt;http://www.icce.rug.nl/docs/programs/Cscript.html&lt;/a&gt;  &lt;/p&gt;&lt;p&gt;&lt;a name="Pretensions"&gt;&lt;/a&gt;&lt;a name="l10"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h2&gt;2.3: Advantages and pretensions of C++&lt;/h2&gt;     &lt;a name="intro/advantage"&gt;&lt;/a&gt;     Often it is said that programming in &lt;strong&gt;C++&lt;/strong&gt; leads to `better' programs. Some of the claimed advantages of &lt;strong&gt;C++&lt;/strong&gt; are: &lt;ul&gt;&lt;li&gt; New programs would be developed in less time because old code can     be reused. &lt;/li&gt;&lt;li&gt; Creating and using new data types would be easier than in &lt;strong&gt;C&lt;/strong&gt;. &lt;/li&gt;&lt;li&gt; The memory management under &lt;strong&gt;C++&lt;/strong&gt; would be easier and more     transparent. &lt;/li&gt;&lt;li&gt; Programs would be less bug-prone, as &lt;strong&gt;C++&lt;/strong&gt; uses a stricter     syntax and type checking. &lt;/li&gt;&lt;li&gt; `Data hiding', the usage of data by one program part while other     program parts cannot access the data, would be easier to implement with     &lt;strong&gt;C++&lt;/strong&gt;. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Which of these allegations are true? In our opinion, &lt;strong&gt;C++&lt;/strong&gt; is a little overrated; in general this holds true for the entire object-oriented programming (OOP). The enthusiasm around &lt;strong&gt;C++&lt;/strong&gt; resembles somewhat the former allegations about Artificial-Intelligence (AI) languages like Lisp and Prolog: these languages were supposed to solve the most difficult AI-problems `almost without effort'. Obviously, too promising stories about any programming language must be overdone; in the end, each problem can be coded in any programming language (even BASIC or assembly language).  The advantages or disadvantages of a given programming language aren't in `what you can do with them', but rather in `which tools the language offers to make the job easier'. &lt;/p&gt;&lt;p&gt;Concerning the above allegations of &lt;strong&gt;C++&lt;/strong&gt;, we think that the following can be concluded.  The development of new programs while existing code is reused can also be realized in &lt;strong&gt;C&lt;/strong&gt; by, e.g., using function libraries: thus, handy functions can be collected in a library and need not be re-invented with each new program. Still, &lt;strong&gt;C++&lt;/strong&gt; offers its specific syntax possibilities for code reuse, apart from function libraries (see chapter &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus15.html#Inheritance" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus15.html#Inheritance"&gt;15&lt;/a&gt;). &lt;/p&gt;&lt;p&gt;Creating and using new data types is also very well possible in &lt;strong&gt;C&lt;/strong&gt;; e.g., by using &lt;code&gt;struct&lt;/code&gt;s, &lt;code&gt;typedef&lt;/code&gt;s etc.. From these types other types can be derived, thus leading to &lt;code&gt;struct&lt;/code&gt;s containing &lt;code&gt;struct&lt;/code&gt;s and so on. &lt;/p&gt;&lt;p&gt;Memory management is in principle in &lt;strong&gt;C++&lt;/strong&gt; as easy or as difficult as in &lt;strong&gt;C&lt;/strong&gt;. Especially when dedicated &lt;strong&gt;C&lt;/strong&gt; functions such as &lt;code&gt;xmalloc()&lt;/code&gt; and &lt;code&gt;xrealloc()&lt;/code&gt; are used (these functions are often present in our &lt;strong&gt;C&lt;/strong&gt;-programs, they allocate or abort the program when the memory pool is exhausted). In short, memory management in &lt;strong&gt;C&lt;/strong&gt; or in &lt;strong&gt;C++&lt;/strong&gt; can be coded `elegantly', `ugly' or anything in between -- this depends on the developer rather than on the language. &lt;/p&gt;&lt;p&gt;Concerning `bug proneness' we can say that &lt;strong&gt;C++&lt;/strong&gt; indeed uses stricter type checking than &lt;strong&gt;C&lt;/strong&gt;. However, most modern &lt;strong&gt;C&lt;/strong&gt; compilers implement `warning levels'; it is then the programmer's choice to disregard or heed a generated warning. In &lt;strong&gt;C++&lt;/strong&gt; many of such warnings become fatal errors (the compilation stops). &lt;/p&gt;&lt;p&gt;As far as `data hiding' is concerned, &lt;strong&gt;C&lt;/strong&gt; does offer some tools.  E.g., where possible, local or &lt;code&gt;static&lt;/code&gt; variables can be used and special data types such as &lt;code&gt;struct&lt;/code&gt;s can be manipulated by dedicated functions.  Using such techniques, data hiding can be realized even in &lt;strong&gt;C&lt;/strong&gt;; though it needs to be said that &lt;strong&gt;C++&lt;/strong&gt; offers special syntactical constructions.  In contrast, programmers who prefer to use a global variable &lt;code&gt;int&lt;/code&gt; &lt;code&gt;i&lt;/code&gt; for each counter variable will quite likely not benefit from the concept of data hiding, be it in &lt;strong&gt;C&lt;/strong&gt; or &lt;strong&gt;C++&lt;/strong&gt;. &lt;/p&gt;&lt;p&gt;Concluding, &lt;strong&gt;C++&lt;/strong&gt; in particular and OOP in general are not solutions to all programming problems. &lt;strong&gt;C++&lt;/strong&gt;, however, &lt;em&gt;does&lt;/em&gt; offer some elegant syntactical possibilities which are worthwhile investigating. At the same time, the level of grammatical complexity of &lt;strong&gt;C++&lt;/strong&gt; has increased significantly compared to &lt;strong&gt;C&lt;/strong&gt;. In time we got used to this increased level of complexity, but the transition didn't take place fast or painless. With the annotations we hope to help the reader to make the transition from &lt;strong&gt;C&lt;/strong&gt; to &lt;strong&gt;C++&lt;/strong&gt; by providing, indeed, our &lt;em&gt;annotations&lt;/em&gt; to what is found in some textbooks on &lt;strong&gt;C++&lt;/strong&gt;. We hope you like this document and may benefit from it: Good luck! &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;&lt;a name="OOP"&gt;&lt;/a&gt;&lt;a name="l11"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h2&gt;2.4: What is Object-Oriented Programming?&lt;/h2&gt;     &lt;a name="intro/object"&gt;&lt;/a&gt;     Object-oriented programming propagates a slightly different approach to programming problems than the strategy which is usually used in &lt;strong&gt;C&lt;/strong&gt;. The &lt;strong&gt;C&lt;/strong&gt;-way is known as a `procedural approach': a problem is decomposed into subproblems and this process is repeated until the subtasks can be coded. Thus a conglomerate of functions is created, communicating through arguments and variables, global or local (or &lt;code&gt;static&lt;/code&gt;). &lt;p&gt;In contrast, or maybe better: in addition to this,  an object-oriented approach identifies the  &lt;strong&gt;keywords&lt;/strong&gt; in the problem. These keywords are then depicted in a diagram and arrows are drawn between these keywords to define an internal hierarchy. The keywords will be the objects in the implementation and the hierarchy defines the relationship between these objects. The term object is used here to describe a limited, well-defined structure, containing all information about some entity: data types and functions to manipulate the data. &lt;/p&gt;&lt;p&gt;As an example of an object-oriented approach, an illustration follows: &lt;/p&gt;&lt;blockquote&gt;&lt;code&gt;     The employees and owner of a car dealer and auto garage company are paid     as follows. First, mechanics who work in the garage are paid a certain sum     each month. Second, the owner of the company receives a fixed amount each     month. Third, there are car salesmen who work in the showroom and receive     their salary each month plus a bonus per sold car. Finally, the company     employs second-hand car purchasers who travel around; these employees     receive their monthly salary, a bonus per bought car, and a restitution of     their travel expenses. &lt;/code&gt;&lt;/blockquote&gt; &lt;p&gt;When representing the above salary administration, the keywords could be mechanics, owner, salesmen and purchasers. The properties of such units are: a monthly salary, sometimes a bonus per purchase or sale, and sometimes restitution of travel expenses. When analyzing the problem in this manner we arrive at the following representation: &lt;/p&gt;&lt;ul&gt;&lt;li&gt; The owner and the mechanics can be represented as the same type,     receiving a given salary per month. The relevant information for such a     type would be the monthly amount. In addition this object could contain     data as the name, address and social security number. &lt;/li&gt;&lt;li&gt; Car salesmen who work in the showroom can be represented as the     same type as above but with extra functionality: the number of     transactions (sales) and the bonus per transaction. &lt;p&gt;In the hierarchy of objects we would define the dependency between the     first two objects by letting the car salesmen be `derived' from     the owner and mechanics. &lt;/p&gt;&lt;/li&gt;&lt;li&gt; Finally, there are the second-hand car purchasers. These share the     functionality of the salesmen except for the travel expenses. The     additional functionality would therefore consist of the expenses made and     this type would be derived from the salesmen. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The hierarchy of the thus identified objects further illustrated         in figure &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus02.html#objects" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus02.html#objects"&gt;1&lt;/a&gt;. &lt;a name="objects"&gt;&lt;/a&gt;&lt;/p&gt;&lt;center&gt;&lt;img src="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/objects.gif" tppabs="http://www.icce.rug.nl/docs/cplusplus/intro/objects.gif" alt="figure 1 is shown here." align="bottom" /&gt;&lt;br /&gt;figure 1: Hierarchy of objects in the salary administration.  &lt;/center&gt;&lt;p&gt;      &lt;/p&gt;&lt;p&gt;The overall process in the definition of a hierarchy such as the above starts with the description of the most simple type. Subsequently more complex types are derived, while each derivation adds a little functionality. From these derived types, more complex types can be derived &lt;em&gt;ad infinitum&lt;/em&gt;, until a representation of the entire problem can be made. &lt;/p&gt;&lt;p&gt;In &lt;strong&gt;C++&lt;/strong&gt; each of the objects can be represented  in a &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus05.html#Classes" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus05.html#Classes"&gt;&lt;em&gt;class&lt;/em&gt;&lt;/a&gt;, containing the necessary functionality to do useful things with the variables (called &lt;em&gt;objects&lt;/em&gt;) of these classes. Not all of the functionality and not all of the properties of a class is usually available to objects of other classes. As we will see, classes tend to &lt;em&gt;encapsulate&lt;/em&gt; their properties in such a way that they are not immediately accessible from the outside world. Instead, dedicated functions are normally used to reach or modify the properties of objects.   &lt;/p&gt;&lt;p&gt;&lt;a name="l12"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h2&gt;2.5: Differences between C and C++&lt;/h2&gt;     &lt;a name="intro/differences"&gt;&lt;/a&gt;     In this section some examples of &lt;strong&gt;C++&lt;/strong&gt; code are shown. Some differences between &lt;strong&gt;C&lt;/strong&gt; and &lt;strong&gt;C++&lt;/strong&gt; are highlighted.  &lt;p&gt;&lt;a name="l13"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.1: End-of-line comment&lt;/h3&gt;         &lt;a name="intro/eoln"&gt;&lt;/a&gt;     According to the ANSI definition, `end of line comment' is implemented in the syntax of &lt;strong&gt;C++&lt;/strong&gt;. This comment starts with &lt;code&gt;//&lt;/code&gt; and ends with the end-of-line marker. The standard &lt;strong&gt;C&lt;/strong&gt; comment, delimited by &lt;code&gt;/*&lt;/code&gt; and &lt;code&gt;*/&lt;/code&gt; can still be used in &lt;strong&gt;C++&lt;/strong&gt;: &lt;pre&gt;    int main()&lt;br /&gt;{&lt;br /&gt;// this is end-of-line comment&lt;br /&gt;// one comment per line&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;    this is standard-C comment, over more&lt;br /&gt;    than one line&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;return (0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;The end-of-line comment was already implemented as an extension to &lt;strong&gt;C&lt;/strong&gt; in some &lt;strong&gt;C&lt;/strong&gt; compilers, such as the Microsoft &lt;strong&gt;C&lt;/strong&gt; Compiler V5.  &lt;/p&gt;&lt;p&gt;&lt;a name="l14"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.2: NULL-pointers vs. 0-pointers&lt;/h3&gt;         &lt;a name="intro/null"&gt;&lt;/a&gt;     In &lt;strong&gt;C++&lt;/strong&gt; all zero values are coded as &lt;code&gt;0&lt;/code&gt;. In &lt;strong&gt;C&lt;/strong&gt;, where pointers are concerned, &lt;code&gt;NULL&lt;/code&gt; is often used. This difference is purely stylistic, though one that is widely adopted. In &lt;strong&gt;C++&lt;/strong&gt; there's no need anymore to use &lt;code&gt;NULL&lt;/code&gt;. Indeed, according to the descriptions of the pointer-returning operator &lt;code&gt;new&lt;/code&gt; 0 rather than &lt;code&gt;NULL&lt;/code&gt; is returned when memory allocation fails. &lt;p&gt; &lt;/p&gt;&lt;p&gt;&lt;a name="l15"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.3: Strict type checking&lt;/h3&gt;                             &lt;a name="intro/type"&gt;&lt;/a&gt;     &lt;strong&gt;C++&lt;/strong&gt; uses very strict type checking. A prototype must be known for each function which is called, and the call must match the prototype. &lt;p&gt;The program &lt;/p&gt;&lt;pre&gt;    int main()&lt;br /&gt;{&lt;br /&gt;printf("Hello World\n");&lt;br /&gt;return (0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;does often compile under &lt;strong&gt;C&lt;/strong&gt;, though with a warning that &lt;code&gt;printf()&lt;/code&gt; is not a known function. Many &lt;strong&gt;C++&lt;/strong&gt; compilers will fail to produce code in such a situation (When GNU's g++ compiler encounters an unknown function, it assumes that an `ordinary' C function is meant. It does complain however.). The error is of course the missing &lt;code&gt;#include&lt;stdio.h&gt;&lt;/stdio.h&gt;&lt;/code&gt; directive.  &lt;/p&gt;&lt;p&gt;     &lt;a name="CPPCASTS"&gt;&lt;/a&gt;&lt;a name="l16"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.4: A new syntax for casts&lt;/h3&gt;         &lt;a name="intro/cast"&gt;&lt;/a&gt;     Traditionally, &lt;strong&gt;C&lt;/strong&gt; offers the following &lt;em&gt;cast&lt;/em&gt; construction:                          &lt;center&gt;&lt;code&gt;(typename)expression&lt;/code&gt; &lt;/center&gt; in which &lt;code&gt;typename&lt;/code&gt; is the name of a valid &lt;em&gt;type&lt;/em&gt;, and &lt;code&gt;expression&lt;/code&gt; an expression. Following that, &lt;strong&gt;C++&lt;/strong&gt; initially also supported the &lt;em&gt;function call style&lt;/em&gt; cast notation:                     &lt;center&gt;&lt;code&gt;typename(expression)&lt;/code&gt; &lt;/center&gt; But, these casts are now all called &lt;em&gt;old-style casts&lt;/em&gt;, and they are deprecated. Instead, four &lt;em&gt;new-style casts&lt;/em&gt; were introduced: &lt;ul&gt;&lt;li&gt; The standard cast to convert one type to another is                      &lt;center&gt;&lt;code&gt;static_cast&lt;type&gt;(expression)&lt;/type&gt;&lt;/code&gt; &lt;/center&gt;     &lt;/li&gt;&lt;li&gt; There is a special cast to do away with the &lt;code&gt;const&lt;/code&gt; type-modification:                       &lt;center&gt;&lt;code&gt;const_cast&lt;type&gt;(expression)&lt;/type&gt;&lt;/code&gt; &lt;/center&gt;     &lt;/li&gt;&lt;li&gt; A third cast is used to change the &lt;em&gt;interpretation&lt;/em&gt; of information:                    &lt;center&gt;&lt;code&gt;reinterpret_cast&lt;type&gt;(expression)&lt;/type&gt;&lt;/code&gt; &lt;/center&gt;     &lt;/li&gt;&lt;li&gt; And, finally, there is a cast form which is used in combination with polymorphism (see chapter &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus16.html#Polymorphism" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus16.html#Polymorphism"&gt;16&lt;/a&gt;): The                      &lt;center&gt;&lt;code&gt;dynamic_cast&lt;type&gt;(expression)&lt;/type&gt;&lt;/code&gt; &lt;/center&gt; is performed run-time to convert, e.g., a pointer to an object of a certain class to a pointer to an object in its so-called &lt;em&gt;class hierarchy&lt;/em&gt;. At this point in the &lt;em&gt;Annotations&lt;/em&gt; it is a bit premature to discuss the &lt;code&gt;dynamic_cast&lt;/code&gt;, but we will return to this topic in section &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus16.html#DYNAMICCAST" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus16.html#DYNAMICCAST"&gt;16.6.1&lt;/a&gt;.  &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;a name="l17"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.5: The 'static_cast'-operator&lt;/h3&gt; &lt;p&gt;The &lt;code&gt;static_cast&lt;type&gt;(expression)&lt;/type&gt;&lt;/code&gt; operator is used to convert one type to an acceptable other type. E.g., &lt;code&gt;double&lt;/code&gt; to &lt;code&gt;int&lt;/code&gt;. An example of such a cast is, assuming &lt;code&gt;intVar&lt;/code&gt; is of type &lt;code&gt;int&lt;/code&gt;:         &lt;/p&gt;&lt;center&gt;&lt;code&gt;intVar = static_cast&lt;int&gt;(12.45);&lt;/int&gt;&lt;/code&gt; &lt;/center&gt; Another nice example of code in which it is a good idea to use the &lt;code&gt;static_cast&lt;&gt;()&lt;/code&gt;-operator is in situations where the arithmetic assignment operators are used in mixed-type situations. E.g., consider the following expression (assume &lt;code&gt;doubleVar&lt;/code&gt; is a variable of type &lt;code&gt;double&lt;/code&gt;:          &lt;center&gt;&lt;code&gt;intVar += doubleVar;&lt;/code&gt; &lt;/center&gt; Here, the evaluated expression actually is:      &lt;center&gt;&lt;code&gt;intVar = static_cast&lt;int&gt;(static_cast&lt;double&gt;(intVar) + doubleVar);&lt;/double&gt;&lt;/int&gt;&lt;/code&gt; &lt;/center&gt;     &lt;code&gt;IntVar&lt;/code&gt; is first promoted to a &lt;code&gt;double&lt;/code&gt;, and is then added as &lt;code&gt;double&lt;/code&gt; to &lt;code&gt;doubleVar&lt;/code&gt;. Next, the sum is cast back to an &lt;code&gt;int&lt;/code&gt;.  These two conversions are a bit overdone. The same result is obtained by explicitly casting the &lt;code&gt;doubleVar&lt;/code&gt; to an &lt;code&gt;int&lt;/code&gt;, thus obtaining an &lt;code&gt;int&lt;/code&gt;-value for the right-hand side of the expression:         &lt;center&gt;&lt;code&gt;intVar += static_cast&lt;int&gt;(doubleVar);&lt;/int&gt;&lt;/code&gt; &lt;/center&gt;     &lt;a name="l18"&gt;&lt;/a&gt; &lt;h3&gt;2.5.6: The 'const_cast'-operator&lt;/h3&gt;     The &lt;code&gt;const_cast&lt;type&gt;(expression)&lt;/type&gt;&lt;/code&gt; operator is used to do away with the &lt;code&gt;const&lt;/code&gt;-ness of a (pointer) type. Assume that a function &lt;code&gt;string_op(char *s)&lt;/code&gt; is available, which performs some operation on its &lt;code&gt;char *s&lt;/code&gt; parameter. Furthermore, assume that it's known that the function does not actually alter the string it receives as its argument. How can we use the function with a string like &lt;code&gt;char const hello[] = "Hello world"&lt;/code&gt;? &lt;p&gt;Passing &lt;code&gt;hello&lt;/code&gt; to &lt;code&gt;fun()&lt;/code&gt; produces the warning      &lt;/p&gt;&lt;center&gt;&lt;code&gt;passing `const char *' as argument 1 of `fun(char *)' discards const&lt;/code&gt; &lt;/center&gt;         which can be prevented using the call                     &lt;center&gt;&lt;code&gt;fun(const_cast&lt;char&gt;(hello));&lt;/char&gt;&lt;/code&gt; &lt;/center&gt;     &lt;a name="l19"&gt;&lt;/a&gt; &lt;h3&gt;2.5.7: The 'reinterpret_cast'-operator&lt;/h3&gt; &lt;p&gt;The &lt;code&gt;reinterpret_cast&lt;type&gt;(expression)&lt;/type&gt;&lt;/code&gt; operator is used to reinterpret byte patterns. For example, the individual bytes making up a &lt;code&gt;double&lt;/code&gt; value can easily be reached using a &lt;code&gt;reinterpret_cast&lt;&gt;()&lt;/code&gt;. Assume &lt;code&gt;doubleVar&lt;/code&gt; is a variable of type &lt;code&gt;double&lt;/code&gt;, then the individual bytes can be reached using          &lt;/p&gt;&lt;center&gt;&lt;code&gt;reinterpret_cast&lt;char&gt;(&amp;amp;doubleVar)&lt;/char&gt;&lt;/code&gt; &lt;/center&gt;     This particular example also suggests the danger of the cast: it looks as though a standard &lt;code&gt;C&lt;/code&gt;-string is produced, but there is not normally a trailing 0-byte. It's just a way to reach the individual bytes of the memory holding a double value. &lt;p&gt;More in general: using the cast-operators is a dangerous habit, as it suppresses the normal type-checking mechanism of the compiler. It is suggested to prevent casts if at all possible. If circumstances arise in which casts have to be used, document the reasons for their use well in your code, to make double sure that the cast is not the underlying cause for a program to misbehave. &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;&lt;a name="l20"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.8: The void argument list&lt;/h3&gt;         &lt;a name="intro/void"&gt;&lt;/a&gt;      A function prototype with an empty argument list, such as &lt;pre&gt;    extern void func();&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;means in &lt;strong&gt;C&lt;/strong&gt; that the argument list of the declared function is not prototyped: the compiler will not be able to warn  against improper argument usage. When declaring a function in &lt;strong&gt;C&lt;/strong&gt; which has no arguments, the keyword &lt;code&gt;void&lt;/code&gt; is used, as in: &lt;/p&gt;&lt;pre&gt;    extern void func(void);&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;Because &lt;strong&gt;C++&lt;/strong&gt; maintains strict type checking, an empty argument list is interpreted as the absence of any parameter. The keyword &lt;code&gt;void&lt;/code&gt; can then be left out. In &lt;strong&gt;C++&lt;/strong&gt; the above two declarations are equivalent. &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;&lt;a name="l21"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.9: The #define __cplusplus&lt;/h3&gt;         &lt;a name="intro/cplus"&gt;&lt;/a&gt;     Each &lt;strong&gt;C++&lt;/strong&gt; compiler which conforms to the ANSI standard defines the symbol &lt;code&gt;__cplusplus&lt;/code&gt;: it is as if each source file were prefixed with the preprocessor directive &lt;code&gt;#define __cplusplus&lt;/code&gt;. &lt;p&gt;We shall see examples of the usage of this symbol in the following sections. &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;&lt;a name="l22"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.10: The usage of standard C functions&lt;/h3&gt;         &lt;a name="intro/cfunc"&gt;&lt;/a&gt;     Normal &lt;strong&gt;C&lt;/strong&gt; functions, e.g., which are compiled and collected in a run-time library, can also be used in &lt;strong&gt;C++&lt;/strong&gt; programs. Such functions however must be declared as &lt;strong&gt;C&lt;/strong&gt; functions. &lt;p&gt;As an example, the following code fragment declares a function &lt;code&gt;xmalloc()&lt;/code&gt; which is a &lt;strong&gt;C&lt;/strong&gt; function: &lt;/p&gt;&lt;pre&gt;    extern "C" void *xmalloc(unsigned size);&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;This declaration is analogous to a declaration in &lt;strong&gt;C&lt;/strong&gt;, except that the prototype is prefixed with &lt;code&gt;extern "C"&lt;/code&gt;. &lt;/p&gt;&lt;p&gt;A slightly different way to declare &lt;strong&gt;C&lt;/strong&gt; functions is the following: &lt;/p&gt;&lt;pre&gt;    extern "C"&lt;br /&gt;{&lt;br /&gt;.&lt;br /&gt;. (declarations)&lt;br /&gt;.&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;It is also possible to place preprocessor directives at the location of the declarations. E.g., a &lt;strong&gt;C&lt;/strong&gt; header file &lt;code&gt;myheader.h&lt;/code&gt; which declares &lt;strong&gt;C&lt;/strong&gt; functions can be included in a &lt;strong&gt;C++&lt;/strong&gt; source file as follows: &lt;/p&gt;&lt;pre&gt;    extern "C"&lt;br /&gt;{&lt;br /&gt;#   include &lt;myheader.h&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/myheader.h&gt;&lt;/pre&gt;  &lt;p&gt;The above presented methods can be used without problem, but are not very current. A more frequently used method to declare external &lt;strong&gt;C&lt;/strong&gt; functions is presented below.  &lt;/p&gt;&lt;p&gt;     &lt;a name="CHeaders"&gt;&lt;/a&gt;&lt;a name="l23"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.11: Header files for both C and C++&lt;/h3&gt;         &lt;a name="intro/header"&gt;&lt;/a&gt;     The combination of the predefined symbol &lt;code&gt;__cplusplus&lt;/code&gt; and of the possibility to define &lt;code&gt;extern "C"&lt;/code&gt; functions offers the ability to create header files for both &lt;strong&gt;C&lt;/strong&gt; and &lt;strong&gt;C++&lt;/strong&gt;. Such a header file might, e.g., declare a group of functions which are to be used in both &lt;strong&gt;C&lt;/strong&gt; and &lt;strong&gt;C++&lt;/strong&gt; programs. &lt;p&gt;The setup of such a header file is as follows: &lt;/p&gt;&lt;pre&gt;    #ifdef __cplusplus&lt;br /&gt;extern "C"&lt;br /&gt;{&lt;br /&gt;#endif&lt;br /&gt;.&lt;br /&gt;. (the declaration of C-functions occurs&lt;br /&gt;.  here, e.g.:)&lt;br /&gt;extern void *xmalloc(unsigned size);&lt;br /&gt;.&lt;br /&gt;#ifdef __cplusplus&lt;br /&gt;}&lt;br /&gt;#endif&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;Using this setup, a normal &lt;strong&gt;C&lt;/strong&gt; header file is enclosed by &lt;code&gt;extern "C" {&lt;/code&gt; which occurs at the start of the file and by &lt;code&gt;}&lt;/code&gt;, which occurs at the end of the file. The &lt;code&gt;#ifdef&lt;/code&gt; directives test for the type of the compilation: &lt;strong&gt;C&lt;/strong&gt; or &lt;strong&gt;C++&lt;/strong&gt;. The `standard' header files, such as &lt;code&gt;stdio.h&lt;/code&gt;, are built in this manner and therefore usable for both &lt;strong&gt;C&lt;/strong&gt; and &lt;strong&gt;C++&lt;/strong&gt;. &lt;/p&gt;&lt;p&gt;An extra addition which is often seen is the following. Usually it is desirable to avoid multiple inclusions of the same header file. This can easily be achieved by including an &lt;code&gt;#ifndef&lt;/code&gt; directive in the header file. An example of a file &lt;code&gt;myheader.h&lt;/code&gt; would then be: &lt;/p&gt;&lt;pre&gt;    #ifndef _MYHEADER_H_&lt;br /&gt;#define _MYHEADER_H_&lt;br /&gt;.&lt;br /&gt;. (the declarations of the header file follow here,&lt;br /&gt;.  with #ifdef _cplusplus etc. directives)&lt;br /&gt;.&lt;br /&gt;#endif&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;When this file is scanned for the first time by the preprocessor, the symbol &lt;code&gt;_MYHEADER_H_&lt;/code&gt; is not yet defined. The &lt;code&gt;#ifndef&lt;/code&gt; condition succeeds and all declarations are scanned. In addition, the symbol &lt;code&gt;_MYHEADER_H_&lt;/code&gt; is defined. &lt;/p&gt;&lt;p&gt;When this file is scanned for a second time during the same compilation, the symbol &lt;code&gt;_MYHEADER_H_&lt;/code&gt; &lt;strong&gt;is&lt;/strong&gt; defined. All information between the &lt;code&gt;#ifndef&lt;/code&gt; and &lt;code&gt;#endif&lt;/code&gt; directives is skipped. &lt;/p&gt;&lt;p&gt;The symbol name &lt;code&gt;_MYHEADER_H_&lt;/code&gt; serves in this context only for recognition purposes. E.g., the name of the header file can be used for this purpose, in capitals, with an underscore character instead of a dot. &lt;/p&gt;&lt;p&gt;Apart from all this, the custom has evolved to give  &lt;strong&gt;C&lt;/strong&gt; header files the extension &lt;code&gt;.h&lt;/code&gt;, and to give &lt;code&gt;C++&lt;/code&gt; header files &lt;em&gt;no&lt;/em&gt; extension. For example, the standard &lt;em&gt;iostreams&lt;/em&gt; &lt;code&gt;cin, cout&lt;/code&gt; and &lt;code&gt;cerr&lt;/code&gt; are available after inclusing the preprocessor directive &lt;code&gt;#include &lt;iostream&gt;&lt;/iostream&gt;&lt;/code&gt;, rather than &lt;code&gt;#include &lt;iostream.h&gt;&lt;/iostream.h&gt;&lt;/code&gt; in a source. In the Annotations this convention is used with the standard &lt;strong&gt;C++&lt;/strong&gt; header files, but not everywhere else (yet). &lt;/p&gt;&lt;p&gt;There is more to be said about header files. In section &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus05.html#CLASSHEADER" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus05.html#CLASSHEADER"&gt;5.7&lt;/a&gt; the preferred organization of header files when &lt;strong&gt;C++&lt;/strong&gt; classes are used is  discussed. &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;&lt;a name="l24"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.12: The definition of local variables&lt;/h3&gt;         &lt;a name="intro/local"&gt;&lt;/a&gt;      In &lt;strong&gt;C&lt;/strong&gt; local variables can only be defined at the top of a function or at the beginning of a nested block. In &lt;strong&gt;C++&lt;/strong&gt; local variables can be created at any position in the code, even between statements. &lt;p&gt;Furthermore local variables can be defined in some statements, just prior to their usage. A typical example is the &lt;code&gt;for&lt;/code&gt; statement: &lt;/p&gt;&lt;pre&gt;    #include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;for (register int i = 0; i &lt;&gt;&lt;/stdio.h&gt;&lt;/pre&gt;  &lt;p&gt;In this code fragment the variable &lt;code&gt;i&lt;/code&gt; is created inside the &lt;code&gt;for&lt;/code&gt; statement. According to the ANSI-standard, the variable does not exist  prior to the &lt;code&gt;for&lt;/code&gt;-statement and not beyond the &lt;code&gt;for&lt;/code&gt;-statement. With some compilers, the variable continues to exist after the execution of  the &lt;code&gt;for&lt;/code&gt;-statement, but a warning like  &lt;/p&gt;&lt;blockquote&gt;     warning: name lookup of `i' changed for new ANSI `for' scoping     using obsolete binding at `i' &lt;/blockquote&gt; will be issued when the variable is used outside of the &lt;code&gt;for&lt;/code&gt;-loop. The  implication seems clear: define a variable just before the &lt;code&gt;for&lt;/code&gt;-statement  if it's to be used beyond that statement, otherwise the variable can be defined at the &lt;code&gt;for&lt;/code&gt;-statement itself. &lt;p&gt;Defining local variables when they're needed requires a little getting used  to. However, eventually it tends to produce more readable code than defining variables at the beginning of compound statements. We suggest the following rules of thumb for defining local variables: &lt;/p&gt;&lt;ul&gt;&lt;li&gt; Local variables should be defined at the beginning of a function,     following the first &lt;code&gt;{&lt;/code&gt;, &lt;/li&gt;&lt;li&gt; or they should be created at `intuitively right' places, such as in     the example above. This does not only entail the &lt;code&gt;for&lt;/code&gt;-statement, but      also all situations where a variable is only needed, say, half-way through      the function. &lt;/li&gt;&lt;/ul&gt;  &lt;p&gt;     &lt;a name="FunctionOverloading"&gt;&lt;/a&gt;&lt;a name="l25"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.13: Function Overloading&lt;/h3&gt;         &lt;a name="intro/overload"&gt;&lt;/a&gt;      In &lt;strong&gt;C++&lt;/strong&gt; it is possible to define several functions with the same name, performing different actions. The functions must only differ in their argument lists. An example is given below: &lt;pre&gt;    #include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;void show(int val)&lt;br /&gt;{&lt;br /&gt;printf("Integer: %d\n", val);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void show(double val)&lt;br /&gt;{&lt;br /&gt;printf("Double: %lf\n", val);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void show(char *val)&lt;br /&gt;{&lt;br /&gt;printf("String: %s\n", val);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;show(12);&lt;br /&gt;show(3.1415);&lt;br /&gt;show("Hello World\n!");&lt;br /&gt;&lt;br /&gt;return (0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/stdio.h&gt;&lt;/pre&gt;  &lt;p&gt;In the above fragment three functions &lt;code&gt;show()&lt;/code&gt; are defined, which only differ in their argument lists: &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;double&lt;/code&gt; and &lt;code&gt;char *&lt;/code&gt;. The functions have the same name. The definition of several functions with the same name is called `function overloading'. &lt;/p&gt;&lt;p&gt;It is interesting that the way in which the &lt;strong&gt;C++&lt;/strong&gt; compiler implements function overloading is quite simple. Although the functions share the same name in the source text (in this example &lt;code&gt;show()&lt;/code&gt;), the compiler --and hence the linker-- use quite different names. The conversion of a name in the source file to an internally used name is called `name mangling'. E.g., the &lt;strong&gt;C++&lt;/strong&gt; compiler might convert the name &lt;code&gt;void&lt;/code&gt; &lt;code&gt;show&lt;/code&gt; &lt;code&gt;(int)&lt;/code&gt; to the internal name &lt;code&gt;VshowI&lt;/code&gt;, while an analogous function with a &lt;code&gt;char*&lt;/code&gt; argument might be called &lt;code&gt;VshowCP&lt;/code&gt;. The actual names which are internally used depend on the compiler and are not relevant for the programmer, except where these names show up in e.g., a listing of the contents of a library. &lt;/p&gt;&lt;p&gt;A few remarks concerning function overloading are: &lt;/p&gt;&lt;ul&gt;&lt;li&gt; The usage of more than one function with the same name but quite     different actions should be avoided. In the example above, the functions     &lt;code&gt;show()&lt;/code&gt; are still somewhat related (they print information to the     screen). &lt;p&gt;However, it is also quite possible to define two functions     &lt;code&gt;lookup()&lt;/code&gt;, one of which would find a name in a list while the other     would determine the video mode. In this case the two functions have     nothing in common except for their name. It would therefore be more     practical to use names which suggest the action; say, &lt;code&gt;findname()&lt;/code&gt; and     &lt;code&gt;getvidmode()&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;&lt;li&gt; &lt;strong&gt;C++&lt;/strong&gt; does not allow that several functions only differ in their     return value. This has the reason that it is always the programmer's     choice to inspect or ignore the return value of a function. E.g., the     fragment     &lt;pre&gt;        printf("Hello World!\n");&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;      holds no information concerning the return value of the function     &lt;code&gt;printf()&lt;/code&gt; (The return value is, by the way, an integer which     states the number of printed characters. This return value is practically     never inspected.). Two functions &lt;code&gt;printf()&lt;/code&gt; which would only     differ in their return type could therefore not be distinguished by the     compiler. &lt;/li&gt;&lt;li&gt; Function overloading can lead to surprises. E.g., imagine a     statement like &lt;pre&gt;        show(0);&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;given the three functions &lt;code&gt;show()&lt;/code&gt; above. The zero could be     interpreted here as a &lt;code&gt;NULL&lt;/code&gt; pointer to a &lt;code&gt;char&lt;/code&gt;, i.e., a     &lt;code&gt;(char *)0&lt;/code&gt;, or as an integer with the value zero. &lt;strong&gt;C++&lt;/strong&gt; will     choose to call the function expecting an integer argument, which might not     be what one expects. &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt; &lt;/p&gt;&lt;p&gt;&lt;a name="l26"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.14: Default function arguments&lt;/h3&gt;         &lt;a name="intro/default"&gt;&lt;/a&gt;      In &lt;strong&gt;C++&lt;/strong&gt; it is possible to provide `default arguments' when defining a function. These arguments are supplied by the compiler when not specified by the programmer. &lt;p&gt;An example is shown below: &lt;/p&gt;&lt;pre&gt;    #include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;void showstring(char *str = "Hello World!\n")&lt;br /&gt;{&lt;br /&gt;printf(str);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;showstring("Here's an explicit argument.\n");&lt;br /&gt;&lt;br /&gt;showstring();           // in fact this says:&lt;br /&gt;                        // showstring("Hello World!\n");&lt;br /&gt;return (0);                     &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/stdio.h&gt;&lt;/pre&gt;  &lt;p&gt;The possibility to omit arguments in situations where default arguments are defined is just a nice touch: the compiler will supply the missing argument when not specified. The code of the program becomes by no means shorter or more efficient. &lt;/p&gt;&lt;p&gt;Functions may be defined with more than one default argument: &lt;/p&gt;&lt;pre&gt;    void two_ints(int a = 1, int b = 4)&lt;br /&gt;{&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;two_ints();            // arguments:  1, 4&lt;br /&gt;two_ints(20);          // arguments: 20, 4&lt;br /&gt;two_ints(20, 5);       // arguments: 20, 5&lt;br /&gt;&lt;br /&gt;return (0);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;When the function &lt;code&gt;two_ints()&lt;/code&gt; is called, the compiler supplies one or two arguments when necessary. A statement as &lt;code&gt;two_ints(,6)&lt;/code&gt; is however not allowed: when arguments are omitted they must be on the right-hand side. &lt;/p&gt;&lt;p&gt;Default arguments must be known to the compiler when the code is generated where the arguments may have to be supplied. Often this means that the default arguments are present in a header file: &lt;/p&gt;&lt;pre&gt;    // sample header file&lt;br /&gt;extern void two_ints(int a = 1, int b = 4);&lt;br /&gt;&lt;br /&gt;// code of function in, say, two.cc&lt;br /&gt;void two_ints(int a, int b)&lt;br /&gt;{&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;Note that supplying the default arguments in the function definition instead of in the header file would not be the correct approach.  &lt;/p&gt;&lt;p&gt;&lt;a name="l27"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.15: The keyword typedef&lt;/h3&gt;         &lt;a name="intro/typedef"&gt;&lt;/a&gt;      The keyword &lt;code&gt;typedef&lt;/code&gt; is in &lt;strong&gt;C++&lt;/strong&gt; allowed, but no longer necessary when it is used as a prefix in &lt;code&gt;union&lt;/code&gt;, &lt;code&gt;struct&lt;/code&gt; or &lt;code&gt;enum&lt;/code&gt; definitions. This is illustrated in the following example: &lt;pre&gt;    struct somestruct&lt;br /&gt;{&lt;br /&gt;int&lt;br /&gt;    a;&lt;br /&gt;double&lt;br /&gt;    d;&lt;br /&gt;char&lt;br /&gt;    string[80];&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;When a &lt;code&gt;struct&lt;/code&gt;, &lt;code&gt;union&lt;/code&gt; or other compound type is defined, the tag of this type can be used as type name (this is &lt;code&gt;somestruct&lt;/code&gt; in the above example): &lt;/p&gt;&lt;pre&gt;    somestruct&lt;br /&gt;what;&lt;br /&gt;&lt;br /&gt;what.d = 3.1415;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt; &lt;/p&gt;&lt;p&gt;     &lt;a name="FunctionInStruct"&gt;&lt;/a&gt;&lt;a name="l28"&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;2.5.16: Functions as part of a struct&lt;/h3&gt;         &lt;a name="intro/struct"&gt;&lt;/a&gt;     In &lt;strong&gt;C++&lt;/strong&gt; it is allowed to define functions as part of a &lt;code&gt;struct&lt;/code&gt;. This is the first concrete example of the definition of an object: as was described previously (see section &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus02.html#OOP" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus02.html#OOP"&gt;2.4&lt;/a&gt;), an object is a structure containing all involved code and data. &lt;p&gt;A definition of a &lt;code&gt;struct point&lt;/code&gt; is given in the code fragment below. In this structure, two &lt;code&gt;int&lt;/code&gt; data fields and one function &lt;code&gt;draw()&lt;/code&gt; are declared. &lt;/p&gt;&lt;pre&gt;    struct point            // definition of a screen&lt;br /&gt;{                       // dot:&lt;br /&gt;int&lt;br /&gt;    x,              // coordinates&lt;br /&gt;    y;              // x/y&lt;br /&gt;void&lt;br /&gt;    draw(void);    // drawing function&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;A similar structure could be part of a painting program and could, e.g., represent a pixel in the drawing. Concerning this &lt;code&gt;struct&lt;/code&gt; it should be noted that: &lt;/p&gt;&lt;ul&gt;&lt;li&gt; The function &lt;code&gt;draw()&lt;/code&gt; which occurs in the &lt;code&gt;struct&lt;/code&gt; definition     is only a &lt;em&gt;declaration&lt;/em&gt;. The actual code of the function, or in other     words the actions which the function should perform, are located     elsewhere: in the code section of the program, where all code is     collected. We will describe the actual definitions of functions inside     &lt;code&gt;struct&lt;/code&gt;s later (see section &lt;a href="file:///E:/E-books/C%20&amp;amp;%20C++%20Compilation/C%20Plus%20Plus/C%20++/cplusplus03.html#FunctionsInStructs" tppabs="http://www.icce.rug.nl/docs/cplusplus/cplusplus03.html#FunctionsInStructs"&gt;3.2&lt;/a&gt;). &lt;/li&gt;&lt;li&gt; The size of the &lt;code&gt;struct&lt;/code&gt; &lt;code&gt;point&lt;/code&gt; is just two &lt;code&gt;int&lt;/code&gt;s. Even     though a function is declared in the structure, its size is not affected     by this. The compiler implements this behavior by allowing the function     &lt;code&gt;draw()&lt;/code&gt; to be known only in the context of a &lt;code&gt;point&lt;/code&gt;. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The &lt;code&gt;point&lt;/code&gt; structure could be used as follows: &lt;/p&gt;&lt;pre&gt;    point                   // two points on&lt;br /&gt;a,                  // screen&lt;br /&gt;b;&lt;br /&gt;&lt;br /&gt;a.x = 0;                // define first dot&lt;br /&gt;a.y = 10;               // and draw it&lt;br /&gt;a.draw();&lt;br /&gt;&lt;br /&gt;b = a;                  // copy a to b&lt;br /&gt;b.y = 20;               // redefine y-coord&lt;br /&gt;b.draw();              // and draw it&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;p&gt;The function which is part of the structure is selected in a similar manner in which data fields are selected; i.e., using the field selector operator (&lt;code&gt;.&lt;/code&gt;). When pointers to &lt;code&gt;struct&lt;/code&gt;s are used, &lt;code&gt;-&gt;&lt;/code&gt; can be used. &lt;/p&gt;&lt;p&gt;The idea of this syntactical construction is that several types may contain functions with the same name. E.g., a structure representing a circle might contain three &lt;code&gt;int&lt;/code&gt; values: two values for the coordinates of the center of the circle and one value for the radius. Analogously to the &lt;code&gt;point&lt;/code&gt; structure, a function &lt;code&gt;draw()&lt;/code&gt; could be declared which would draw the circle.  &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1232785149712460829-3488207523964713233?l=cprogramming4u.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cprogramming4u.blogspot.com/feeds/3488207523964713233/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1232785149712460829&amp;postID=3488207523964713233' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1232785149712460829/posts/default/3488207523964713233'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1232785149712460829/posts/default/3488207523964713233'/><link rel='alternate' type='text/html' href='http://cprogramming4u.blogspot.com/2008/06/c.html' title='C++'/><author><name>Mohammed Adil</name><uri>http://www.blogger.com/profile/09986294828972956062</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='http://3.bp.blogspot.com/_wFWi1AQ8PoE/SKqBETTZDsI/AAAAAAAAAVo/kn_ljn4WgI0/S220/7_6_batista.jpg'/></author><thr:total>0</thr:total></entry></feed>
