Preliminaries. Part I

Similar documents
Tokens, Expressions and Control Structures

C++ (Non for C Programmer) (BT307) 40 Hours

September 10,

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Kakadu and Java. David Taubman, UNSW June 3, 2003

These are notes for the third lecture; if statements and loops.

CS3157: Advanced Programming. Outline

Chapter 2 Basic Elements of C++

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

A Fast Review of C Essentials Part I

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Variables and Constants

Variables. Data Types.

Fast Introduction to Object Oriented Programming and C++

Types, Values, Variables & Assignment. EECS 211 Winter 2018


University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

BLM2031 Structured Programming. Zeyneb KURT

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Programming in C and C++

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

printf( Please enter another number: ); scanf( %d, &num2);

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley

Short Notes of CS201

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS201 - Introduction to Programming Glossary By

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Chapter 2: Introduction to C++

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

CPSC 427: Object-Oriented Programming

PIC 10A Objects/Classes

Interview Questions of C++

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Motivation was to facilitate development of systems software, especially OS development.

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

Introduction to Programming using C++

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Basic Types, Variables, Literals, Constants

Lecture Topics. Administrivia

Design Principles for a Beginning Programming Language

Motivation was to facilitate development of systems software, especially OS development.

C++ Programming Fundamentals

Type Checking and Type Equality

APPENDIX A : Example Standard <--Prev page Next page -->

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Spemmet - A Tool for Modeling Software Processes with SPEM

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 2. Procedural Programming

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

2.1 Why did C need a ++?

Software Architecture

Chapter 15 - C++ As A "Better C"

Full file at

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

SML Style Guide. Last Revised: 31st August 2011

D Programming Language

6. Pointers, Structs, and Arrays. 1. Juli 2011

C++\CLI. Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017

Overview of C++ Support in TI Compiler Tools July 2008

Lecture 10: building large projects, beginning C++, C++ and structs

explicit class and default definitions revision of SC22/WG21/N1582 =

C++11 and Compiler Update

CSE 303: Concepts and Tools for Software Development

Introduce C# as Object Oriented programming language. Explain, tokens,

CS 376b Computer Vision

UEE1302 (1102) F10: Introduction to Computers and Programming

The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Objectives. In this chapter, you will:

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright DAX

Introduction to C++ Systems Programming

Object-Oriented Programming

LECTURE 02 INTRODUCTION TO C++

Explicit Conversion Operator Draft Working Paper

Virtualization. Q&A with an industry leader. Virtualization is rapidly becoming a fact of life for agency executives,

All the subjective part of 2011 papers solved complete reference numbers

CS1102: What is a Programming Language?

Implementing Object Equivalence in Java Using the Template Method Design Pattern

Increases Program Structure which results in greater reliability. Polymorphism

EEE145 Computer Programming

Appendix G C/C++ Notes. C/C++ Coding Style Guidelines Ray Mitchell 475

Guidelines for Writing C Code

Fundamental Concepts and Definitions

C Refresher, Advance C, Coding Standard, Misra C Compliance & Real-time Programming

PROGRAMMING IN C++ COURSE CONTENT

18-642: Code Style for Compilers

Sample Copy. Not for Distribution.

Transcription:

Part I Preliminaries Chapters 1 through 4 present an introduction to C++ that provides the basis for understanding the rest of the material in this book. This part also provides professional programmers with insight into how their managers and technical leaders view life. This material is intended to help developers understand how their organization works so they can participate more fully in the decision-making process.

Chapter 1 Introduction What is the purpose of this chapter? FAQ 1.01 To explain what the book is all about, how it is related to the electronic FAQ and the first edition, and what conventions are used. This chapter discusses the purpose of the book and the conventions it follows. This chapter also discusses our approach to FAQs and why you should buy this book if you have the first edition or have access to the electronic FAQ. What are C++ FAQs? FAQ 1.02 Frequently Asked Questions that should be asked about object-oriented programming and C++. Each FAQ provides specific guidance in the form of in-depth answers. Many FAQs also provide a complete, working program that illustrates the principles espoused by the FAQ. The word FAQs is pronounced like facts. These FAQs aren t necessarily questions people have asked; rather, they are the questions people should ask. Although we never say it publicly, most of these FAQs are based on dumb things we see people do on a fairly regular basis. We got tired of 3

4 Preliminaries explaining the same fundamental notions over and over again and decided to write them down in this book. On the other hand, you have taken a step toward OO and C++ competence by purchasing this guidebook; now take the next step by reading and understanding its message. FAQ 1.03 Who is the target audience for this book? Professional software developers. This book is aimed at developers including programmers, architects, and designers. It is a fine way for the experienced programmer to learn object-oriented C++. This book is not for beginners who are just learning to program since it assumes previous programming background. Familiarity with C wouldn t hurt but is not absolutely necessary. FAQ 1.04 Is this a book about C++ per se? This is a C++ book with a twist. This book focuses on the object-oriented aspects of C++. Thus, whenever you see the word C++, you should assume that the words object-oriented are present (and we ll occasionally inject the words object-oriented as a reminder to the reader). This book focuses on practical ways to use C++; it does not explore all of the dark corners of the language beloved by language lawyers. In this way, this book is not the traditional C++ book written from the perspective of the language and stressing the syntax and features of C++ in all their gory detail. Instead, this book concentrates on the key aspects of C++ (such as its OO features) and how to apply them effectively. Another reason for this approach is that the language is so large that it is hard for developers to understand what is relevant and how to apply it. In this vein, one of the main contributions of this book is to focus on the moral use of C++ rather than simply describing the legal use of C++. In this context, using C++ morally means adhering to a programming discipline (i.e., a subset of all possible combinations of all the constructs of C++) that is relatively risk-free (whereas using C++ legally simply refers to any use of the language that the compiler accepts). We have found that many of the problems that developers run into stem from trying to combine

Introduction 5 C++ features in incompatible and seemingly random ways; therefore using C++ morally is vital to using C++ effectively. This book also tries to bridge the gap between software architecture and OO design and C++ programming (see Chapter 4). Why do developers need a guidebook for C++ and OO technology? FAQ 1.05 Learning to use C++ and OO properly is a long journey with many pitfalls. Because of the sophistication and complexity of C++, developers need a road map that shows how to use the language properly. For example, inheritance is a powerful facility that can improve the clarity and extensibility of software, but it can also be abused in ways that result in expensive design errors. The field of object-oriented technology is large, evolving, and heterogeneous. Under these circumstances, a guidebook is essential. These FAQs cover the latest innovations so that you don t have to stumble around for years learning the same lessons others have already learned. The FAQs also expose incorrect and questionable practices. To be effective, programmers need to understand the language features and how the features of the language can be combined. For example, pointer arithmetic and the is-a conversion (see FAQ 2.24) are both useful, but combining them has some subtle edge effects that can cause big problems; see FAQ 8.16. Similar comments apply when combining overloading and overriding (FAQ 29.02), overriding and default parameters, abstract base classes and assignment (FAQ 24.05), and so on. So it is not enough to understand each feature of C++. What kind of guidance is given in the answers to these FAQs? FAQ 1.06 Explanations of language features, directions for using these features properly, and guidelines indicating programming practices to avoid. The FAQs can be divided into roughly three categories: 1. FAQs that explain what a particular language feature is and how to use it in compliance with C++ semantics.

6 Preliminaries 2. FAQs that explain how to use C++ properly. Some of these answers deal with only a single language feature, while others explain how to use several different language features in concert. Combining language features allows sophisticated designs that can simultaneously satisfy multiple technical requirements and business goals. 3. FAQs that expose poor programming practices. These show design and programming practices that are legal in C++ but should be avoided because they can lead to programs that are bug-ridden, hard to comprehend, expensive to maintain, difficult to extend, and lacking reuse value. FAQ 1.07 What is the electronic FAQ and why buy this book when the electronic FAQ is free? The electronic FAQ is a set of C++ questions and answers, originally prepared and distributed on the Internet by Marshall Cline. The Internet version is currently updated and distributed by Marshall and is available through the news group comp.lang.c++. This book has substantially more material than the electronic FAQ. This book and the electronic FAQ were inspired by a seemingly unquenchable thirst among C++ developers for more and better information about C++ through comp.lang.c++. Addison-Wesley decided to provide an expanded form of that information in book format. This book covers a broader range of topics and goes into greater depth than the electronic FAQ. It provides deeper coverage of the key points with extensive new examples. Most of the programming examples are working, stand-alone programs, complete with their own main(), all necessary #include files, and so on. All examples have been compiled directly from the source text of the book; those that are complete programs have also been run.

Introduction 7 Why should you buy this edition if you already have a copy of the first edition? FAQ 1.08 new Because the world has changed and you want to keep up with technology. The OO world and the C++ language have changed significantly in the last few years. There are new language constructs such as Run Time Type Identification (RTTI) and namespaces. The Standard Template Library (STL) is a massive addition to the C++ body of essential knowledge. Design notation has apparently standardized on the Unified Modeling Language (UML). Java, CORBA, and ActiveX are now topics that every C++ developer needs to understand. The goal of this second edition is to bring you up to speed on all of these new developments while still keeping the pithy style and FAQ format that was so well received in the first edition. Finally, the second edition is much more self-contained than the first, with lots of syntax and semantics. We ve appreciated all your comments and suggestions and have tried to accommodate them wherever possible. What conventions are used in this book? FAQ 1.09 The undecorated word inheritance means public inheritance. Private or protected inheritance is referred to explicitly. Similarly the undecorated term derived class means public derived class. Derived classes produced via private or protected inheritance are explicitly designated private derived class or protected derived class, respectively. The class names Base and Derived are used as hypothetical class names to illustrate the general relationship between a base class and one of its (publicly) derived classes. The term out-lined function indicates a function that is called via a normal CALL instruction. In contrast, when an inlined function is invoked, the compiler inserts the object code for that function at the point-of-call. The term remote ownership is used when an object contains a pointer to another object that the first object is responsible for deleting. The default destruction and copy semantics for objects that contain remote ownership are incorrect, so explicit controls are needed.

8 Preliminaries To allow compilation while simplifying the presentation to the reader, examples that use the standard library have a line that says using namespace std;. This dumping of the entire standard namespace is acceptable as a short-term conversion technique or as a pedagogical aid, but its use in production systems is controversial. Most authorities recommend introducing class names as needed or using the std:: qualifier. The term OO is used as an abbreviation for object-oriented. The term method is used as a synonym for member function. NULL is used rather than 0 to make the code more readable. Organizational standards and guidelines should be consulted before the reader continues this practice. The term C programming language refers to the ISO version of C. The compiler is assumed (per the C++ Standard) to insert an implicit return 0; at the end of main(). The intrinsic data type bool is used, which has literal values true and false. For compilers that don t have a built-in bool type, insert the following at the beginning of each example: typedef char bool; const bool false = 0; const bool true = 1; The expression new MyClass, where MyClass is some type, is assumed to throw an exception if it runs out of memory it never returns NULL. Most compilers implement this correctly, but some do not. Most examples use protected: data rather than private: data. In the real world, this is appropriate for most developers and most applications, but framework developers probably should not use protected: data, since this would create a data coupling between the derived classes and the protected: data of the base class. In general, framework developers should use private: data with protected: access functions. Type names (names of classes, structs, unions, enums, and typedefs) start with a capital letter; preprocessor symbols are all capitals; all other identifiers start with a lowercase letter. Data member names and class-scoped enumerations end with a single underscore. It is assumed that the file extensions.cpp and.hpp are appropriate. Some compilers use a different convention. Universal Modeling Language (UML) notation is used to express design relationships. The following priorities were used in designing the examples: (1) unity of purpose, (2) compactness, and (3) self-contained functionality. In other words, each example demonstrates one basic point or technique, is as short as possible, and, if possible, is a complete, working program. The examples are not intended for plug-in reuse in industrial-strength settings because balancing the resultant (subtle) tradeoffs would conflict with these priorities.

Introduction 9 To avoid complicating the discussions with finding the optimal balance between the use of virtual and inline for member functions, virtual is used more often than strictly necessary (see FAQ 21.15). To achieve compactness, some member functions are defined in the class body even if they wouldn t normally be inline or even if moving them down to the bottom of a header file would improve specification (see FAQ 6.05). Uncalled functions are often left undefined. Some functions that are called are also undefined, since compactness is a higher priority than self-contained functionality. Also for compactness, examples are not wrapped in preprocessor symbols that prevent multiple expansions (see FAQ 2.16). The examples put the public: part at the beginning of the class rather than at the end of the class. This makes it easier for those who simply want to use the class as opposed to those who want to go in and change the internal implementation of the class. This is normally the right tradeoff since a class is normally used a lot more often than it is changed. It is assumed that the C++ compiler and standard library are both compliant with the Standard and work correctly. In the real world, this is probably not a safe assumption, and you should be cautious.