COMP 2710 Software Construction C++ Basics 2 and Exercises Dr. Xiao Qin Auburn University These slides.

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.
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
CSC 200 Lecture 2 Matt Kayala 1/25/06. Lecture Objectives More detailed intro to C++ Variables, Expressions, and Assignment Statements Console Input/Output.
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 1.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
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.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Slide 1. Slide 2 Chapter 1 C++ Basics Slide 3 Learning Objectives  Introduction to C++  Origins, Object-Oriented Programming, Terms  Variables, Expressions,
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
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.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Recap……Last Time [Variables, Data Types and Constants]
Chapter 1 C++ Basics Copyright © 2016 Pearson, Inc. All rights reserved.
3/6/2016Chung-Chih ISU IT 2791 Spec of the program outputs: What's your name? Dennis Hi! Dennis! Input the price for 10% discount: 200 Input the quantity:
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
 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.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
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++
FAQ: Exit Vi Using :q Can NOT exit with “:q” if you have not use “:w” to write changes.
Chapter 1 C++ Basics Copyright © 2016 Pearson, Inc. All rights reserved.
© by Pearson Education, Inc. All Rights Reserved.
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++ Chapter 1.
Introduction to C++ Programming
Chapter 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.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Basic Elements of C++.
Nahla Abuel-ola / WIT.
COMP 2710 Software Construction File I/O
Lecture 2-3 C++ Basic Constructs
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
elementary programming
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
C++ Programming Basics
Spec of the program outputs:
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
C++ for Engineers and Scientists Second Edition
Presentation transcript:

COMP 2710 Software Construction C++ Basics 2 and Exercises Dr. Xiao Qin Auburn University These slides are adapted from notes by Dr. Walter Savitch (UCSD)

1-2 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.

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

1-4 Constants Naming your constants – 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

Programming Exercise 1 Write a simple C++ program. A user provide your program with an amount of deposit, your program calculate the new balance after one year savings period. Note that the interest rate is a constant in your program. 3-5

See rate.cpp 1-6

1-7 Arithmetic Precision Examples Examples: – 17 / 5 evaluates to ? in C++! Both operands are integers Integer division is performed! – 17.0 / 5 equals ? in C++! Highest-order operand is "double type" Double "precision" division is performed! – int intVar1 =1, intVar2=2; intVar1 / intVar2; Performs integer division! Result: ? 3 How about 19/5? 3.4 How about 19.0/5? 0

1-8 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-9 Precision - Exercise 2 Calculations done "one-by-one" 1 / 2 / 3.0 / 4 = ? 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! Performs 3 separate divisions. First  1 / 2 equals 0 Then  0 / 3.0 equals 0.0 Then  0.0 / 4 equals 0.0!

1-10 What is 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 intVar – Explicitly "casts" or "converts" intVar to double type Result of conversion is then used Example expression: doubleVar = static_cast intVar1 / intVar2; – Casting forces double-precision division to take place among two integer variables! Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

1-11 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-12 Shorthand Operators Increment & Decrement Operators – Just short-hand notation – Increment operator, ++ intVar++; is equivalent to ? – Decrement operator, -- intVar--; is equivalent to ? intVar = intVar + 1; intVar = intVar – 1;

1-13 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-14 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-15 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-16 Increment & Decrement Operators - Exercise 3 int a = 5; int b = 3; int c = a * b++; int d = a * ++b; cout << “c is” <<c << endl; cout << “d is” << d << endl; c is 15 d is 20

1-17 Increment & Decrement Operators - Exercise 4 int x=2; int y; y=++x+x+x; What is the result of y? y is 9

1-18 Console Input/Output I/O objects cin, cout, cerr Defined in the C++ library called Must have these lines (called pre- processor directives) near start of file: – #include 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-19 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 $ 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-20 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-21 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! Google C++ Style Guide:

1-22 Libraries C++ Standard Libraries #include – 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-23 Namespaces Namespaces defined: – Collection of name definitions For now: interested in namespace "std" – Has all standard library definitions we need Examples: #include using namespace std; Includes entire standard library of name definitions #include using std::cin; using std::cout; Can specify just the objects we want Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

1-24 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.