ebook img

An Introduction to Object-Oriented Programming in C++ PDF

1072 Pages·1996·25.989 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview An Introduction to Object-Oriented Programming in C++

An Introduction to Object-Oriented Programming in c++ With Applications in Computer Graphics Springer London Berlin Heidelberg New¥ork Barcelona Budapest Hong Kong Milan Paris Santa Clara Singapore Tokyo Graham M. Seed An Introduction to Object-Oriented c++ Programming in With Applications in Computer Graphics Springer Graham M. Seed, BEng, MPhil, PhD Department of Mechanical & Chemical Engineering Heriot Watt University, Edinburgh EH 14 4AS, UK ISBN-13: 978-3-540-76042-9 e-ISBN-13: 978-1-4471-3378-0 DOl: 10.1007/978-1-4471-3378-0 British Library Cataloguing in Publication Data Seed, Graham Mark An introduction to object -oriented programming in C++ : with applications in computer graphics I.C++ (Computer program language) 2.0bject-oriented programming (Computer Science) 1.Title 005.1'33 Library of Congress Cataloging-in-Publication Data Seed, Graham M" 1965- An introduction to object-oriented programming in C++ : with applications in computer graphics 1 Graham M, Seed. p. cm. Includes bibliographical references and index. 1. Object-oriented programming (Computer science) 2. C++ (Computer program language) 3. Computer graphics. 1. Title. QA76,64,S375 1996 006.6'6--dc20 96-21786 Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms of licences issued by the Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to the publishers. © Springer-Verlag London Limited 1996 Softcover reprint of the hardcover 1s t edition 1996 The use of registered names, trademarks etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant laws and regulations and therefore free for general use, The publisher makes no representation, express or implied, with regard to the accuracy of the information contained in this book and cannot accept any legal responsibility or liability for any errors or omissions that may be made. Typesetting: Ian Kingston Editorial Services, Nottingham Printed and bound by Bell & Bain Ltd, Glasgow, UK 34/3830-543210 Printed on acid-free paper To the memory of Tom Bannister (1909-1992) what a guy! Preface Why Another Book on c++ and why Programming and Graphics? Anyone who has browsed through the 'Computing' section of a bookshop (assuming it has one) will not need much convincing that there are a lot of C++ books out there. So why add yet another to the shelf! This book attempts to introduce you to the C++ language via computer graphics because the object-oriented programming features of C++ naturally lend themselves to graphics. Thus, this book is based around a central theme: computer graphics and the development of 'real' object-oriented tools for graphical modelling. This approach is adopted (as opposed to learning by small, unrelated, often hypothetical, examples) because I didn't want to introduce C++ as a collection oflanguage features. While introducing the syntax and features of C++, it is just as important to demonstrate simultaneously the reason for such features and when to apply them -in otherwords,language and design are given equal priority. Also, a key objective in writing this book is to present you with a comprehensive introductory text on programming in the C++ language. The first part of the book introduces the fundamentals of C++ programming. After the foundations have been laid, the object -oriented programming features of C++ are introduced via the development of classes such as Po in t and Line. These primitives are then put to good use in developing powerful two-and three-dimensional graphics classes such as Circle, Sphere, Triangle, Quadrilateral, Polygon and Tetrahedra. Along the way, tool classes are also developed, such as Vector, Vector3D,Ma trix,Cornplex,String,LinkedList,ReadFile and Wri te File,World,DIBitrnap and many more. What you Need to Know No previous programming experience in C or C++ is required for this book. If you are considering learning C before C++, then my advice is: don't. When I first started to learn C++ I asked myself the exact same question and unfortunately made the wrong decision and developed a good understanding of C before attempting to learn C++. I wouldn't say that this proved to be a complete waste of time, but it is certainly not necessary to learn C in order to learn C++. As a C++ programmer you will develop programs and think about program design in a completely different manner than the traditional procedural techniques in C. C++ offers a programmer object-oriented features such as encapsulation, abstraction, inheritance and multiple inheritance, and polymorphism through the use of classes and objects, member functions, operator overloading, virtual functions, templates, exception handling, run-time type information and so on. In addition, C++ offers a lot more. C++ provides many new features (for instance const, new and delete), stronger type checking and minor, but nevertheless important, modifications to C. vii viii Preface If you do know another procedural language, such as Fortran, Basic, Pascal or C, then great. Some skills that you have learned with such languages will undoubtedly prove useful (such as the meaning of variables, conditions and loops), whereas other skills will appear to conflict with the c++ way of programming. This book does assume that you have had some contact with computers before and are familiar with Microsoft's DOS and Windows operating systems. Hardware and Software Requirements It is assumed that readers of this book have access to a computer and a compiler, such as Borland C++,Microsoft Visual c++ or Symantec c++ for a Windows environment. It is difficult to specify general minimum hardware and software requirements that cover all compilers, but as an example, Borland c++ version 5.0 for Windows requires Windows 95 or Windows NT, a hard disk with 175 Mbyte free disk space for a full installation, at least 16 Mbyte RAM, a 3.5"/1.44 Mbyte floppy drive or CD-ROM and a Windows-compatible mouse. An Intel 486 or higher processor is recommended. Obtaining the Program Files All of the source code, including solutions to chapter exercises, presented throughout the book is available by an Internet ftp connection following the procedure: ftp host name: ftp.springer.co.uk login name: ftp password: <your e-mail address> ftp directory: /pub/springer/Books/3540760423 get: cpp-prog.zip The program fIles have been compressed in to a single PKZIP fIle CPP _PROG.ZIP. To preserve the chapter directory structure of the fIles use the -d option when unzipping the fIles. For instance: c:\cpp-prog>pkunzip cpp-prog.zip -d Alternatively, the program fIles can be obtained via the following URL: ftp://ftp.springer.co.uk/pub/springer/Books/3540760423 All of the programs have been tested with the Borland C++ and Microsoft Visual C++ compilers. Preparation of the Book The text of this book was written with the aid of Microsoft's Word for Windows version 6.0. The C++ program code was written with the Borland Integrated Devel opment Environment (IDE) editor. The program code was transferred from IDE to Preface ix Word using Copy and Paste. Tables and figures were completed using Word and CorelDRAW version 5.0. All program code developed for the book was developed exclusively on a desktop PC consisting of an Intel Pentium 90 MHz CPU, 16 Mbyte RAM, 1 Mbyte video memory and 1 Gbyte hard disk space. How this Book !s Organised It would be great to dive straight into the object-oriented programming (OOP) features of c++. Unfortunately, this is simply not possible for an introductory text. Therefore, the initial chapters lay the foundations of the c++ language by examining the 'traditional' procedural C features of c++. If you already have experience of another programming language, particularly C, it may be tempting to skip the first few chapters and jump straight into the object oriented features of c++. If you choose to do this then be careful. c++ has many differences from and additions to C, some of which you may miss if you jump for OOP. The object-oriented features really begin in Chapters 8 and 9, when structures and classes are introduced. The book begins by presenting a general overview of the C++ programming language and object-oriented programming and what is meant by an object. We take a look at the history of C++, its relationship to C and what makes C++ an object-ori ented programming language. Chapter 2 briefly discusses your programming envi ronment and what support to expect from a C++ compiler. Chapter 3 introduces several key elements of program construction in C++ via the simplest of programs. Each line of a simple C++ program is introduced in detail to help the reader who is learning C++ for the first time feel comfortable with the basics of program construction. The first elements of a C++ program discussed are user comments, preprocessor directives, the use of header files, a function definition, program statements, strings, the cou t standard output stream object, the endl manipulator and the « insertion operator. The importance of developing a consis tent programming style is stressed and several different popular styles are presented. The fundamental data types of the C++ language, and their corresponding data type modifiers, are introduced in Chapter 4. The notion of a constant or variable identifier is demonstrated with the aid of numerous examples for each fundamental data type, and the distinction between an identifier declaration and definition is noted. Once data types have been introduced, statements, operators, expressions, type conversions and casting are covered. Chapter 4 concludes by discussing storage class specifiers and declaring assembly language program code directly within a C++ program. Chapter 5 examines the process of making decisions and performing repetition in C++. Decisions are implemented using either if-else or switch statements, whereas repetition is performed using a for, while or do-while-loop. In addi tion, C++ supports keywords such as break and default for if-else and swi tch statements and the keywords break and continue for loops which allow a programmer to interrupt program flow. Both decision-making and repetition require the use of the-relational and logical operators, and Chapter 5 examines these operators and the bitwise and shift operators in detail. The function is the basic tool of the majority of programming languages for modularising a large complex program. Chapter 6 discusses the C++ function: its x Preface construction, definition, scope and application. Chapter 6 also discusses the powerful technique of function overloading, which C++ supports, and inline functions. The chapter concludes with an application of functions to the popular geometric problem of the intersection of two straight lines. Arrays are covered in Chapter 7. The chapter discusses what an array is, how the elements of an array are indexed, the dimension of an array and typical applications of arrays. Chapter 8 presents structures, unions and enumerations for the creation of user defined data types and typedefs for user-defined synonyms of data types. The struct statement allows a programmer to create a new data type which is a collection of similar or different data types. A union data type is similar to struct except that a union can hold only one of its data members at any given time. The user-defined enum data type is a collection of named data members which have equivalent integer values. A typedef is a synonym for an existing data type. The class is the key object-oriented programming feature of the c++ language, and Chapter 9 covers the class in detail via a class called Point. Similar to struct, the class allows a programmer to create new data types by encapsulating data members, but the class also allows a programmer to encapsulate member functions which operate on a class's data members. Operators and overloading operators for user-defined data types are examined in Chapter 10. Overloading operators for a user-defined class can significantly enhance the level of abstraction of the class. Friend functions and classes are introduced in Chapter 11. Basically, a friend function or class has access to the private data members of an object of a given class, although the friend is not a member of that class. Chapter 12 comprehensively examines the tricky subject of pointers. The dynamic memory allocation and de allocation operators new and delete and the this pointer are examined. Useful Vector and Matrix classes are developed which make good use of pointer manipulation and the new and delete operators, and help apply many of the topics covered in previous chapters. Generic functions and parametrised data types are introduced in Chapter 13 through the template language-based feature. The exact type of object(s) that a function or c las s operates on is left unspecified in the function definition or class declaration until a function is called or an object is defined. The C++ try-catch-throw termination mechanism for handling runtime errors is pre sented in Chapter 14 and demonstrated for various classes. The topic of class inheritance is covered in Chapter 15, which allows a derived class to inherit the features of one or more other base classes. Several properties of inheritance are discussed with reference to a Shape class hierarchy, including polymorphic classes which contain one or more virtual functions. Chapter 16 examines the run-time type information mechanism, which allows a programmer to obtain the type of an object at run-time, and the related topic of casting. Both C and c++ streams are discussed in Chapter 17. Standard input and output and fIle streams are covered and an overview is given of the C function-based and C++ class-based stream libraries. Chapter 18 discusses the preprocessor, although it is not strictly part of the C++ language. Chapter 19 discusses namespaces, one of the newest major features to be added to the C++ language. Namespaces are a language-based solution to the problem of name clashes resulting from C++'s single name space for a translational unit. The final chapter concludes the book with an application of the C++ programming language to the development of a simple object-oriented ray-tracing program. The

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.