An Introduction
to C++
Abstract The aim of the notes is to provide an introduction to the C++ programming language. Author: Ian D Chivers Email:
[email protected],
[email protected] Version: 9.00 Date: November 2006 © Ian D Chivers. Permission to copy all or part of this work is granted, provided that the copies are not made or distributed for resale (except a nominal copy fee may be charged), and provided that the Author, Copyright, & No Warranty sections are retained verbatim and are displayed conspicuously. If anyone needs other permissions that aren’t covered by the above, please contact the author. No Warranty: This work is provided on an as is basis. The author provides no warranty whatsoever, either express or implied, regarding the work, including warranties with respect to its merchantability or fitness for any particular purpose. All comments welcome.
Dedication These notes are dedicated to the people who have implemented the gcc suite of software, to Dinkumware and to Microsoft. Gcc and g++ The gcc home page is · http://gcc.gnu.org/ The following comments are taken from their home page. GCC, the GNU Compiler Collection, includes front ends for C, C++, Objective-C, Fortran, Java, and Ada, as well as libraries for these languages (libstdc++, libgcj,...). We strive to provide regular, high quality releases, which we want to work well on a variety of native and cross targets (including GNU/Linux), and encourage everyone to contribute changes and help testing GCC. Our sources are readily and freely available via CVS and weekly snapshots. Major decisions about GCC are made by the steering committee, guided by the mission statement. GCC steering committee members The steering committee consists of the following members. The best place to reach them is the gcc mailinglist. Per Bothner, Joe Buck (Synopsys), David Edelsohn (IBM), Kaveh R. Ghazi, Jeffrey A. Law (Red Hat), Marc Lehmann (Technische Universität Karlsruhe), Jason Merrill (Red Hat), David Miller (Red Hat), Mark Mitchell (CodeSourcery), Toon Moene (Koninklijk Nederlands Meteorologisch Instituut), Gerald Pfeifer (SUSE), Joel Sherrill (OAR Corporation), Jim Wilson (Specifix Inc) Membership in the steering committee is a personal membership. Affiliations are listed for identification purposes only; steering committee members do not represent their employers or academic institutions. Dinkumware The Dinkumware home page is ·
http://www.dinkumware.com/ They provide the only standard C++ library at this time. Microsoft
You've all done a great job!
Contents
3
Table of Contents
1 Overview...............................................................................................................16 1.1 1.2 1.3 1.4 1.5 1.5.1 1.5.2
1.6 1.7 1.8 1.9
Aims............................................................................................................................16 Assumptions ...............................................................................................................16 Web resources ............................................................................................................16 Additional material.....................................................................................................17 Compilers and Standards............................................................................................19 Free and low cost compilers ............................................................................................20 Contents of the standard...................................................................................................20
Old and New...............................................................................................................21 Coda............................................................................................................................21 Course Details ............................................................................................................22 Problems .....................................................................................................................22
2 An Introduction to Programming Languages and Object Oriented Programming ...................................................................................................................24 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13 2.14 2.15 2.15.1 2.15.2 2.15.3 2.15.4 2.15.5 2.15.6 2.15.7 2.15.8 2.15.9 2.15.10 2.15.11 2.15.12 2.15.13 2.15.14
2.16 2.16.1 2.16.2 2.16.3 2.16.4
2.17 2.18 2.18.1 2.18.2
2.19 2.20 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6
Fortran 66, 1966 .........................................................................................................24 Pascal, 1975, ANSI & BSI 1982, ISO 1983, Extended Pascal 1991?......................24 Fortran 77, 1978 .........................................................................................................25 C, K&R 1978, Standard 1989. ...................................................................................25 Modula 2, 1982, Standard 1996? ...............................................................................25 Ada, ISO 8652: 1987 .................................................................................................25 C++, 1986, Standard November 1997 .......................................................................25 Oberon 2, Late 1980’s, early 1990’s. ........................................................................25 Fortran 90, 1991. ........................................................................................................26 Eiffel, 1988 .................................................................................................................26 Ada, ISO 8652: 1995 .................................................................................................26 Java .............................................................................................................................26 Visual Basic................................................................................................................27 Language Comparison................................................................................................27 Language Features......................................................................................................29 Independent Compilation .................................................................................................29 Separate Compilation .......................................................................................................29 Concrete ; char lowercase[]="abcdefghijklmnopqrstuvwxyz"; char ch; int i; int ic; int usize=sizeof(uppercase)/sizeof(char); int lsize=sizeof(lowercase)/sizeof(char); cout