Duffy Daniel J., Introduction to C++ for Financial Engineers - An Object-Oriented Approach (Wiley, 2006).pdf

(4321 KB) Pobierz
665081033 UNPDF
665081033.003.png
for
Financial Engineers
++
An object-oriented approach
Daniel J. Duffy
Introduction to C
665081033.004.png 665081033.005.png
2006 Daniel J Duffy
Published by John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester,
West Sussex PO19 8SQ, England
Telephone (+44) 1243 779777
Email (for orders and customer service enquiries): cs-books@wiley.co.uk
Visit our Home Page on www.wiley.com
All Rights Reserved. No part of this publication may be reproduced, stored in a retrieval system
or transmitted in any form or by any means, electronic, mechanical, photocopying, recording,
scanning or otherwise, except under the terms of the Copyright, Designs and Patents Act 1988
or under the terms of a licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham
Court Road, London W1T 4LP, UK, without the permission in writing of the Publisher.
Requests to the Publisher should be addressed to the Permissions Department, John Wiley &
Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex PO19 8SQ, England, or emailed
to permreq@wiley.co.uk, or faxed to (+44) 1243 770620.
Designations used by companies to distinguish their products are often claimed as trademarks. All brand
names and product names used in this book are trade names, service marks, trademarks or registered
trademarks of their respective owners. The Publisher is not associated with any product or vendor
mentioned in this book.
This publication is designed to provide accurate and authoritative information in regard to
the subject matter covered. It is sold on the understanding that the Publisher is not engaged
in rendering professional services. If professional advice or other expert assistance is
required, the services of a competent professional should be sought.
Other Wiley Editorial Offices
John Wiley & Sons Inc., 111 River Street, Hoboken, NJ 07030, USA
Jossey-Bass, 989 Market Street, San Francisco, CA 94103-1741, USA
Wiley-VCH Verlag GmbH, Boschstr. 12, D-69469 Weinheim, Germany
John Wiley & Sons Australia Ltd, 42 McDougall Street, Milton, Queensland 4064, Australia
John Wiley & Sons (Asia) Pte Ltd, 2 Clementi Loop #02-01, Jin Xing Distripark, Singapore 129809
John Wiley & Sons Canada Ltd, 6045 Freemont Blvd, Mississauga, ONT, L5R 4J3, Canada
Wiley also publishes its books in a variety of electronic formats. Some content that appears
in print may not be available in electronic books.
Library of Congress Cataloging-in-Publication Data
Duffy, Daniel J.
Introduction to C ++ for financial engineers : an object-oriented approach / Daniel J Duffy.
p. cm.—(Wiley finance series)
Includes bibliographical references and index.
ISBN-13: 978-0-470-01538-4 (cloth: alk. paper)
ISBN-10: 0-470-01538-1 (cloth : alk. paper)
1. Financial engineering—Computer programs. 2. C
++
(Computer program
language) I. Title.
HG176.7.D843 2006
005.13 3024332—dc22
2006020622
British Library Cataloguing in Publication Data
A catalogue record for this book is available from the British Library
ISBN 13 978-0-470-01538-4 (HB)
ISBN 10 0-470-01538-1 (HB)
Typeset in 10/12pt Times by TechBooks, New Delhi, India
Printed and bound in Great Britain by Antony Rowe Ltd, Chippenham, Wiltshire
This book is printed on acid-free paper responsibly manufactured from sustainable forestry
in which at least two trees are planted for each one used for paper production.
Copyright C
665081033.006.png
Contents
0 Goals of this Book and Global Overview
1
0.1 What is this book?
1
0.2 Why has this book been written?
1
0.3 For whom is this book intended?
2
0.4 Why should I read this book?
2
0.5 The structure of this book
2
0.6 What this book does not cover
2
0.7 More information and support
3
PA RT I C
++
ESSENTIAL SKILLS
5
1 Introduction to C
++
and Quantitative Finance
7
1.1 Introduction and objectives
7
1.2 A short history of C
++
7
1.3 C
++
, a multi-paradigm language
8
1.3.1 Object-oriented paradigm
8
1.3.2 Generic programming
10
1.3.3 Procedural, modular and functional programming
11
1.4 C
++
and quantitative finance: what’s the relationship?
11
1.5 What is software quality?
11
1.6 Summary and conclusions
13
1.7 Exercises
14
2 The Mechanics of C
++
: from Source Code to a Running Program
15
2.1 Introduction and objectives
15
2.2 The compilation process
15
2.3 Header files and source files
16
2.4 Creating classes and using their objects
19
2.5 Template classes and template functions
22
2.6 Kinds of errors
25
2.6.1 Compiler errors
25
2.6.2 Linker errors
26
2.6.3 Run-time errors
26
2.7 The struct concept
27
665081033.001.png
vi
Contents
2.8 Useful data conversion routines
27
2.9 Summary and conclusions
29
2.10 Exercises and projects
29
3C
++
Fundamentals and My First Option Class
31
3.1 Introduction and objectives
31
3.2 Class == member data + member functions
32
3.3 The header file (function prototypes)
33
3.4 The class body (code file)
35
3.5 Using the class
38
3.6 Examining the class in detail
40
3.6.1 Accessibility issues
40
3.6.2 Using standard libraries
40
3.6.3 The scope resolution operator ‘::’
41
3.6.4 Virtual destructor: better safe than sorry
41
3.7 Other paradigms
41
3.8 Summary and conclusions
45
3.9 Questions, exercises and projects
45
4 Creating Robust Classes
49
4.1 Introduction and objectives
49
4.2 Call by reference and call by value
49
4.3 Constant objects everywhere
52
4.3.1 Read-only (const) member functions
52
4.4 Constructors in detail
54
4.4.1 Member initialisation
55
4.5 Static member data and static member functions
55
4.6 Function overloading
57
4.7 Non-member functions
58
4.8 Performance tips and guidelines
58
4.8.1 The ‘inline’ keyword
58
4.8.2 Anonymous objects in function code
59
4.8.3 Loop optimisation
60
4.9 Summary and conclusions
60
4.10 Questions, exercises and projects
60
5 Operator Overloading in C
++
63
5.1 Introduction and objectives
63
5.2 What is operator overloading and what are the possibilities?
63
5.3 Why use operator overloading? The advantages
65
5.4 Operator overloading: the steps
68
5.4.1 A special case: the assignment operator
70
5.5 Using operator overloading for simple I/O
71
5.6 Friend functions in general
72
5.6.1 Friend classes
73
5.7 Summary and conclusions
74
5.8 Exercise
74
Appendix: useful data structures in C
++
75
665081033.002.png
Zgłoś jeśli naruszono regulamin