Introduction WHAT IS C++ THE ORIGIN OF C++ _________________________________________________________________ The C programming language was developed at AT&T for the purpose of writing the operating system for the PDP-11 series of computers which ultimately became the unix operating system. C was developed with the primary goal of operating efficiency. Bjarne Stroustrup, also of AT&T, developed C++ in order to add object oriented constructs to the language. Because object oriented technology was new at the time and all existing implementations of object oriented languages were very slow and inefficient, the primary goal of C++ was to maintain the efficiency of C. C++ can be viewed as a procedural language with some additional constructs, some of which are added for object oriented programming and some for improved procedural syntax. A well written program will reflect elements of both object oriented programming style and classic procedural programming. C++ is actually an extendible language since we can define new types in such a way that they act just like the predefined types which are part of the standard language. C++ is designed for large scale software development. HOW TO GET STARTED IN C++ _________________________________________________________________ The C programming language was originally defined by the classic text authored by Kernigan and Ritchie, "The C Programming language", and was the standard used by all C programmers until a few years ago. The ANSI standard for C was finally approved in December of 1989 and has become the official standard for programming in C. Since the ANSI standard adds many things to the language which were not a part of the Kernigan and Ritchie definition, and changes a few, the two standards are not absolutely compatible and some of the more experienced C programmers may not have studied the newer constructs added to the language by ANSI-C. This tutorial will assume a thorough knowledge of the C programming language and little time will be spent on the fundamental aspects of the language. However, because we realize that many programmers have learned the dialect of C as defined by Kernigan & Ritchie, some sections will be devoted to explaining the newer additions as provided by the ANSI-C standard. As the ANSI-C standard was in development, many of the newer constructs from C++ were included as parts of C itself, so even though C++ is a derivation and extension of C, it would be fair to say that ANSI-C has some of its roots in C++. An example is prototyping which was developed for C++ and later added to C. Page I-1 Introduction - What is C++ The best way to learn C++ is by using it. Almost any valid C program is also a valid C++ program, and in fact the addition of about 12 keywords is the only reason that some C programs will not compile and execute as a C++ program. There are a few other subtle differences but we will save the discussion of them until later. Since this is true, the best way to learn C++ is to simply add to your present knowledge and use a few new constructs as you need them for each new program. It would be a tremendous mistake to try to use all of the new constructs in your first C++ program. You would probably end up with an incomprehensive mixture of code that would be more inefficient than the same program written purely in C. Add a few new constructs to your toolkit each day, and use them as needed while you gain experience with their use. As an illustration of the portability of C to C++, all of the example programs included in the Coronado Enterprises C tutorial compile and run correctly when compiled as C++ programs with no changes. None of the C++ programs will compile and execute correctly with any C compiler however, if for no other reason than the use of the new style of C++ comments. HOW TO USE THIS TUTORIAL _________________________________________________________________ This tutorial is best used while sitting in front of your computer. It is designed to help you gain experience with your own C++ compiler in addition to teaching you the proper use of C++. Display an example program on the monitor, using whatever text editor you usually use, and read the accompanying text which will describe each new construct introduced in the example program. After you study the program, and understand the new constructs, compile and execute the program with your C++ compiler. After you successfully compile and execute the example program, introduce a few errors into the program to see what kind of error messages are issued. If you have done much programming, you will not be surprised if your compiler gives you an error message that seems to have nothing to do with the error introduced. This is because error message analysis is a very difficult problem with any modern programming language. The most important result of these error introduction exercises is the experience you will gain using your compiler and understanding its nuances. In the text of the preprinted version of this tutorial, keywords, variable names, and function names will be written in bold type as an aid when you are studying the example programs. It is impossible to include the bold words in a pure ASCII file which must be used for the shareware version, but the context will indicate which words are keywords, variable names, or function names. Page I-2 Introduction - What is C++ The way this tutorial is written, you are not required to compile and execute every program. At the end of each example program, listed in comments, you will find the result of execution of that program. Some of the constructs are simple and easy for you to understand, so you may choose to ignore compilation and execution of that example program, depending upon the result of execution to give you the output. Some students have used these results of execution to study several chapters of this tutorial on an airplane by referring to a hardcopy of the example programs. DIFFERENT C++ IMPLEMENTATIONS _________________________________________________________________ There are primarily two standards for naming C++ files, one using the extension CPP and the other using the extension CXX. All files in this tutorial use the CPP extension for naming files. If your compiler requires the CXX extension it will be up to you to rename the files. Even though we have tried to use the most generic form of all constructs, it is possible that some constructs will not actually compile and run with some C++ compilers. As we find new implementations of C++, and acquire copies of them, we will compile and execute all files in an attempt to make all example programs as universal as possible. The C++ language is very new and is changing rapidly. The developer of the language, AT&T, has changed the formal definition several times in the last few years and the compiler writers are staying busy trying to keep up with them. For this reason, we have included the following notes on some of the more popular compilers and how they fare with the example programs in this tutorial. Zortech C++ version 1.07 - This compiler is based on C++ version 1.2, an earlier version defined by AT&T. Most of the files will compile correctly if the #include file is changed from "iostream.h" to "stream.hpp". Borland TURBO C++ version 1.0 - This compiler is based on C++ version 2.0. All example programs will compile without error using this compiler. Zortech C++ version 2.1 - This compiler is based on C++ version 2.0. None of the files have been tested with this compiler yet. PRINTING OUT THE EXAMPLE PROGRAMS _________________________________________________________________ Some students prefer to work from a hardcopy of the example programs. If you desire to print out the example programs, there is a batch file on the distribution disk to help you do this. Make Page I-3 Introduction - What is C++ the distribution disk the default drive and type PRINTALL at the user prompt and the system will print out about 150 pages of C++ programs, all of the example programs in this tutorial. The PRINTALL batch file calls the program named LIST.EXE once for each example program on the distribution disk. LIST.EXE was written in TURBO Pascal and compiled for your use in listing any program with line numbers and the date the program was last modified, the date and time of the file. TURBO Pascal was used because Borland does not require a licensing fee for distributing copies of the resulting run time files. PROGRAMMING EXERCISES _________________________________________________________________ There are programming exercises given at the end of each chapter to enable you to try a few of the constructs given in the chapter. These are for your benefit and you will benefit greatly if you attempt to solve each programming problem. If you merely read this entire tutorial, you will have a good working knowledge of C++, but you will only become a C++ programmer if you write C++ programs. The programming exercises are given as suggestions to get you started programming. An answer for each programming exercise is given in the ANSWERS directory on the distribution disk. The answers are all given in compilable C++ source files named in the format CHnn_m.CPP, where nn is the chapter number and m is the exercise number. If more than one answer is required, an A, B, or C, is included following the exercise number. RECOMMENDED ADDITIONAL READING _________________________________________________________________ Richard S. Wiener & Lewis J. Pinson. "An Introduction to Object- Oriented Programming and C++". Addison-Wesley, 1988. This is the first book recommended since it covers both object oriented programming and C++ in depth. It is clearly written and well organized. Brad Cox. "Object Oriented Programming, An Evolutionary Approach". Addison-Wesley, 1986. This book is excellent for a study of object oriented programming and what it is, but since it is based on Objective-C, it covers nothing of the C++ language or how to use it. John Berry. "The Waite Group's C++ Programming". Howard W. Sams, 1988. This book covers the fundamentals of the language well and includes a significant amount of information on the application of object oriented programming techniques. Page I-4 Introduction - What is C++ Bjarne Stroustrup. "The C++ Programming Language". Addison-Wesley, 1986. This is the definitive book on C++, but it would be difficult for a beginner to learn the language from this book alone. Note that the C++ culture is in rapid change and by the time you read this, there will be additional well written texts available as aids to your learning the syntax and proper use of the C++ programming language. Page I-5