FAQ: Exit Vi Using :q Can NOT exit with “:q” if you have not use “:w” to write changes.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
CSC 200 Lecture 2 Matt Kayala 1/25/06. Lecture Objectives More detailed intro to C++ Variables, Expressions, and Assignment Statements Console Input/Output.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
Chapter 1 C++ Basics Copyright © 2012 Pearson Addison-Wesley. All rights reserved.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Slide 1. Slide 2 Chapter 1 C++ Basics Slide 3 Learning Objectives  Introduction to C++  Origins, Object-Oriented Programming, Terms  Variables, Expressions,
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
Chapter 1 C++ Basics Copyright © 2016 Pearson, Inc. All rights reserved.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
C++ Primer I CMSC 202. Topics Covered Our first “Hello world” program Basic program structure main() Variables, identifiers, types Expressions, statements.
COMP 2710 Software Construction C++ Basics 2 and Exercises Dr. Xiao Qin Auburn University These slides.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
Bill Tucker Austin Community College COSC 1315
C++ 프로그래밍 Introduction to this class & C++
Variables, Identifiers, Assignments, Input/Output
Today Variable declaration Mathematical Operators Input and Output Lab
C++ First Steps.
Chapter 1 C++ Basics Copyright © 2016 Pearson, Inc. All rights reserved.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Data Types and Expressions
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
COMP 2710 Software Construction Midterm Exam 1 - Review
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Programmer-Defined Functions Dr.
Documentation Need to have documentation in all programs
Basic Elements of C++.
Revision Lecture
Basics (Variables, Assignments, I/O)
Lecture 2-3 C++ Basic Constructs
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
مبانی برنامه‌سازی با C++ جلسه دوم
Basics (Variables, Assignments, I/O)
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Variables, Identifiers, Assignments, Input/Output
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Fundamental Programming
C++ Programming Basics
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

FAQ: Exit Vi Using :q Can NOT exit with “:q” if you have not use “:w” to write changes.

FAQ: Linux Users Install two operating systems (i.e., Windows and Linux on your PC) Virtual Machines. (e.g., VirtualBox; VMWare) VirtualBox is running on Windows; Install Linux (e.g., Ubuntu in VirtualBox)

Learn More About g++: info g++

COMP 2710 Software Construction C++ Basics Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu These slides are adapted from notes by Dr. Walter Savitch (UCSD)

C++ Variables C++ Identifiers Variables Keywords/reserved words vs. Identifiers Must start with a letter or the underscore symbol The rest must be letters, digits, or the underscore symbol Case-sensitivity and validity of identifiers Meaningful names! (see also Page 13) Variables A memory location to store data for a program Must declare all data before use in program 1-5 5

C++ Variables - Examples X, x, abc, _yyy, TBL123, sum, count, RATE Which identifiers are poor choices for us? Why? 76, 9B, %Percent, time-2, firstprog.c, main.cpp Which identifier will be rejected by the compiler? Please read ch 1.2 on page 7 The first five. Not descriptive All are rejected. 1-6 6

C++ Variables - Examples (cont.) Are STUDENT, student, and Student three distinct identifiers? Why? Yes. C++ is case-sensitive 1-7 7

Keywords Reserved Words Predefined meaning in C++ Example: short, long, char, true, false, if, else, for, while, class, private, public, typedef, static, sizeof, new, delete, See Appendix 1 for a complete list of keywords. Yes. C++ is case-sensitive 1-8 8

Declare a Variable A variable must be declared before it is used! Tell the compiler what kind of data (i.e., data type) will be stored in the variable See what will happen when you compile a C++ program where a variable is not declared. Give an example. 1-9 9

Data Types: Display 1.2 Simple Types (1 of 2) Page 9: Short -32767 to 32767 = 65534 = 2^16 -> 16 bits for 2 Bytes -> 8 Bits per Byte double: commonly used floating-pint number Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-10 10

Data Types: Display 1.2 Simple Types (2 of 2) Char may be treated as an integer type to save space. Unsigned Version: unsigned int, unsigned long Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-11 11

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Assigning Data Initializing data in declaration statement Results "undefined" if you don’t! int myValue = 0; int count = 0, distance = 15, factor = 1; 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 © 2008 Pearson Addison-Wesley. All rights reserved. 1-12 12

Assigning Data: Tips All the initial values can be assigned in one function An assignment statement can be an expression on the right-hand side. Not Recommended. myDistance = (yourDistance = 15); //not recommended myDistance = yourDistance = 15; //not recommended Pitfall: Uninitialized Variables desiredNumber = minimumNumber + 10; //uninitialized variables? How to Guard your Input Parameters? Check input parameters before using them We may use an assignment statement as an expression on the right-hand side. But this is not recommended. 1-13 13

Assigning Data: Shorthand Notations Display, page 14 Variable Operator = Expression Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-14 14

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Data Assignment Rules Compatibility of Data Assignments Type mismatches General Rule: Cannot place value of one type into variable of another type intVar = 2.99; // 2 is assigned to intVar! Only integer part "fits", so that’s all that goes Called "implicit" or "automatic type conversion" You can assign int values to double variables double doubleVariable; doubleVariable = 2; Literals 2, 5.75, "Z", "Hello World" Considered "constants": can’t change in program You can assign int values to double variables You may store 25 to a variable of type char; but this is not recommended! Integer and bool can be assigned to each other; this is a poor style. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-15 15

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Literal Data Literals: Names for specific values Examples: 2 // Literal constant int 5.75 // Literal constant double "Z" // Literal constant char "Hello World" // Literal constant string Cannot change values during execution Called "literals" because you "literally typed" them in your program! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-16 16

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Escape Sequences "Extend" character set Backslash, \ preceding a character Instructs compiler: a special "escape character" is coming Following character treated as "escape sequence char" Display 1.3 next slide Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-17 17

Display 1.3 Some Escape Sequences (1 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-18 18

Display 1.3 Some Escape Sequences (2 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-19 19

Constants Naming your constants Use named constants instead Literal constants are "OK", but provide little meaning e.g., seeing 24 in a pgm, tells nothing about what it represents Use named constants instead Meaningful name to represent data const int NUMBER_OF_STUDENTS = 24; const int BRANCH_COUNT = 10, WINDOW_COUNT = 10; //not recommended, a separate line is clearer. Called a "declared constant" or "named constant" Now use it’s name wherever needed in program Added benefit: changes to value result in one fix 1-20 20

Display 1.4 Named Constant rate.cpp 1-21 21

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Summary C++ is case-sensitive Use meaningful names For variables and constants Variables must be declared before use Should also be initialized Use care in numeric manipulation Precision, parentheses, order of operations #include C++ libraries as needed Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-22 22

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Arithmetic Precision Precision of Calculations VERY important consideration! Expressions in C++ might not evaluate as you’d "expect"! "Highest-order operand" determines type of arithmetic "precision" performed Common pitfall! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-23 23

Arithmetic Precision Examples 17 / 5 evaluates to 3 in C++! Both operands are integers Integer division is performed! 17.0 / 5 equals 3.4 in C++! Highest-order operand is "double type" Double "precision" division is performed! int intVar1 =1, intVar2=2; intVar1 / intVar2; Performs integer division! Result: 0! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-24 24

Individual Arithmetic Precision Calculations done "one-by-one" 1 / 2 / 3.0 / 4 performs 3 separate divisions. First 1 / 2 equals 0 Then 0 / 3.0 equals 0.0 Then 0.0 / 4 equals 0.0! So not necessarily sufficient to change just "one operand" in a large expression Must keep in mind all individual calculations that will be performed during evaluation! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-25 25

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Type Casting Casting for Variables Can add ".0" to literals to force precision arithmetic, but what about variables? We can’t use "myInt.0"! static_cast<double>intVar Explicitly "casts" or "converts" intVar to double type Result of conversion is then used Example expression: doubleVar = static_cast<double>intVar1 / intVar2; Casting forces double-precision division to take place among two integer variables! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-26 26

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Type Casting Two types Implicit—also called "Automatic" Done FOR you, automatically 17 / 5.5 This expression causes an "implicit type cast" to take place, casting the 17  17.0 Explicit type conversion Programmer specifies conversion with cast operator (double)17 / 5.5 Same expression as above, using explicit cast (double)myInt / myDouble More typical use; cast operator on variable Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-27 27

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Shorthand Operators Increment & Decrement Operators Just short-hand notation Increment operator, ++ intVar++; is equivalent to intVar = intVar + 1; Decrement operator, -- intVar--; is equivalent to intVar = intVar – 1; Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-28 28

Shorthand Operators: Two Options Post-Increment intVar++ Uses current value of variable, THEN increments it Pre-Increment ++intVar Increments variable first, THEN uses new value "Use" is defined as whatever "context" variable is currently in No difference if "alone" in statement: intVar++; and ++intVar;  identical result Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-29 29

Post-Increment in Action Post-Increment in Expressions: int n = 2, valueProduced; valueProduced = 2 * (n++); cout << valueProduced << endl; cout << n << endl; This code segment produces the output: 4 3 Since post-increment was used Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-30 30

Pre-Increment in Action Now using Pre-increment: int n = 2, valueProduced; valueProduced = 2 * (++n); cout << valueProduced << endl; cout << n << endl; This code segment produces the output: 6 3 Because pre-increment was used Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-31 31

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Console Input/Output I/O objects cin, cout, cerr Defined in the C++ library called <iostream> Must have these lines (called pre- processor directives) near start of file: #include <iostream> using namespace std; Tells C++ to use appropriate library so we can use the I/O objects cin, cout, cerr Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-32 32

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Console Output What can be outputted? Any data can be outputted to display screen Variables Constants Literals Expressions (which can include all of above) cout << numberOfGames << " games played."; 2 values are outputted: "value" of variable numberOfGames, literal string " games played." Cascading: multiple values in one cout Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-33 33

Separating Lines of Output New lines in output Recall: "\n" is escape sequence for the char "newline" A second method: object endl Examples: cout << "Hello World\n"; Sends string "Hello World" to display, & escape sequence "\n", skipping to next line cout << "Hello World" << endl; Same result as above Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-34 34

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Formatting Output Formatting numeric values for output Values may not display as you’d expect! cout << "The price is $" << price << endl; If price (declared double) has value 78.5, you might get: The price is $78.500000 or: The price is $78.5 We must explicitly tell C++ how to output numbers in our programs! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-35 35

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Formatting Numbers "Magic Formula" to force decimal sizes: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); These stmts force all future cout’ed values: To have exactly two digits after the decimal place Example: cout << "The price is $" << price << endl; Now results in the following: The price is $78.50 Can modify precision "as you go" as well! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-36 36

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Error Output Output with cerr cerr works same as cout Provides mechanism for distinguishing between regular output and error output Re-direct output streams Most systems allow cout and cerr to be "redirected" to other devices e.g., line printer, output file, error console, etc. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-37 37

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Input Using cin cin for input, cout for output Differences: ">>" (extraction operator) points opposite Think of it as "pointing toward where the data goes" Object name "cin" used instead of "cout" No literals allowed for cin Must input "to a variable" cin >> num; Waits on-screen for keyboard entry Value entered at keyboard is "assigned" to num Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-38 38

Prompting for Input: cin and cout Always "prompt" user for input cout << "Enter number of dragons: "; cin >> numOfDragons; Note no "\n" in cout. Prompt "waits" on same line for keyboard input as follows: Enter number of dragons: ____ Underscore above denotes where keyboard entry is made Every cin should have cout prompt Maximizes user-friendly input/output Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-39 39

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Program Style Bottom-line: Make programs easy to read and modify Comments, two methods: // Two slashes indicate entire line is to be ignored /*Delimiters indicates everything between is ignored*/ Both methods commonly used Identifier naming ALL_CAPS for constants lowerToUpper for variables Most important: MEANINGFUL NAMES! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-40 40

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Libraries C++ Standard Libraries #include <Library_Name> Directive to "add" contents of library file to your program Called "preprocessor directive" Executes before compiler, and simply "copies" library file into your program file C++ has many libraries Input/output, math, strings, etc. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-41 41

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Namespaces Namespaces defined: Collection of name definitions For now: interested in namespace "std" Has all standard library definitions we need Examples: #include <iostream> using namespace std; Includes entire standard library of name definitions #include <iostream>using std::cin; using std::cout; Can specify just the objects we want Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-42 42

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Summary Object cout Used for console output Object cin Used for console input Object cerr Used for error messages Use comments to aid understanding of your program Do not overcomment Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-43 43