
Chapter 1 ABSOLUTE C++ C++Basics WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley.All rights reserved
Chapter 1 C++ Basics

Learning Objectives ◆Introduction to C++ Origins,Object-Oriented Programming,Terms Variables,Expressions,and Assignment Statements Console Input/Output ◆Program Style Libraries and Namespaces Copyright2006 Pears on Addison-Wesley.All rights reserved. 1-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output Program Style Libraries and Namespaces

Introduction to C++ ◆C++Origins ◆Low-level languages ◆Machine,assembly High-level languages +C,C++,ADA,COBOL,FORTRAN Object-Oriented-Programming in C++ ◆Ct+Terminology Programs and functions Basic Input/Output(1/O)with cin and cout Copyright2006 Pears on Addison-Wesley.All rights reserved. 1-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-3 Introduction to C++ C++ Origins Low-level languages Machine, assembly High-level languages C, C++, ADA, COBOL, FORTRAN Object-Oriented-Programming in C++ C++ Terminology Programs and functions Basic Input/Output (I/O) with cin and cout

Display 1.1 A Sample C++Program(1 of 2) Display 1.A Sample C++Program 1 #include 2 using namespace std; 3 int main() 4 int numberOfLanguages; 6 cout numberOfLanguages; 10 if (numberofLanguages 1) 11 cout <"Read the preface.You may prefer\n" 2 <<"a more elementary book by the same author.\n"; 13 else 14 cout <"Enjoy the book.\n"; 15 return 0; 163 Copyright2006 Pears on Addison-Wesley.All rights reserved. 1-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-4 Display 1.1 A Sample C++ Program (1 of 2)

Display 1.1 A Sample C++Program(2 of 2) SAMPLE DIALOGUE I Hello reader. Welcome to C++. How many programming languages have you used?0-User types in 0 on the keyboard. Read the preface.You may prefer a more elementary book by the same author. SAMPLE DIALOGUE 2 Hello reader. Welcome to C++. How many programming languages have you used?1-User types in 1 on the keyboard. Enjoy the book Copyright 2006 Pearson Addison-Wesley.All rights reserved. 1-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-5 Display 1.1 A Sample C++ Program (2 of 2)

C++Variables ◆C++ldentifiers Keywords/reserved words vs.Identifiers Case-sensitivity and validity of identifiers ◆Meaningful names! ◆Variables A memory location to store data for a program Must declare all data before use in program Copyright006 Pearson Addison-Wesley.All rights reserved. 1-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-6 C++ Variables C++ Identifiers Keywords/reserved words vs. Identifiers Case-sensitivity and validity of identifiers Meaningful names! Variables A memory location to store data for a program Must declare all data before use in program

Data Types: Display 1.2 Simple Types (1 of 2) Display 1.2 Simple Types TYPE NAME MEMORY USED SIZE RANGE PRECISION short 2 bytes -32,767t032,767 Not applicable (also called short int) int 4 bytes -2,147,483,647t0 Not applicable 2,147,483,647 long 4bytes -2,147,483,647t0 Not applicable (also called 2,147,483,647 long int) float 4bytes approximately 7 digits 1038to1o38 double 8 bytes approximately 15 digits 103o8to103o8 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 1-7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-7 Data Types: Display 1.2 Simple Types (1 of 2)

Data Types: Display 1.2 Simple Types(2 of 2) long double io bytes approximately 9digits 104932t0104932 char I byte All ASCII characters Not applicable (Can also be used as an integer type, although we do not recommend doing s0.) bool i byte true,false Not applicable The values listed here are only sample values to give you a general idea of how the types differ. The values for any of these entries may be different on your system.Precision refers to the num- ber of meaningful digits,including digits in front of the decimal point.The ranges for the types float,double,and Long double are the ranges for positive numbers.Negative numbers have a similar range,but with a negative sign in front of each number. Copyright2006 Pears on Addison-Wesley.All rights reserved. 1-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-8 Data Types: Display 1.2 Simple Types (2 of 2)

Assigning Data Initializing data in declaration statement Results "undefined"if you don't! ◆int myValue=O; Assigning data during execution Lvalues (left-side)&Rvalues (right-side) Lvalues must be variables Rvalues can be any expression ◆Example: distance rate time; Lvalue:"distance" Rvalue:"rate time" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 1-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-9 Assigning Data Initializing data in declaration statement Results "undefined" if you don’t! int myValue = 0; Assigning data during execution Lvalues (left-side) & Rvalues (right-side) Lvalues must be variables Rvalues can be any expression Example: distance = rate * time; Lvalue: "distance" Rvalue: "rate * time

Assigning Data:Shorthand Notations ◆Display,page14 EXAMPLE EQUIVALENT TO count +=2; countcount +2; total-=discount; total total-discount; bonus *=2; bonus bonus 2; time /rushFactor; time time/rushFactor; change %100; change change 100; amount *cntl +cnt2; amount amount (cntl cnt2); Copyright2006 Pears on Addison-Wesley.All rights reserved. 1-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-10 Assigning Data: Shorthand Notations Display, page 14