1 Objects Types, Variables, and Constants Chapter 3.

Slides:



Advertisements
Similar presentations
1 © 1998, 2001, 2002Calvin College, Keith Vander Linden, Jeremy D. Frens, Larry R. Nyhoff Objects (Chap. 2) Variables and Constants.
Advertisements

CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Data Types and Expressions
Types and Variables. Computer Programming 2 C++ in one page!
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Objectives You should be able to describe: Data Types
CPS120: Introduction to Computer Science Lecture 8.
Simple Data Type Representation and conversion of numbers
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.
Objects Types, Variables, and Constants 1 Chapter 3.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
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
PROCESSING Types. Objectives Be able to convert between binary and decimal numbers. Be able to declare and initialize character variables and constants.
Chapter 2 Variables.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
COMP Primitive and Class Types Yi Hong May 14, 2015.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
A Sample Program #include using namespace std; int main(void) { cout
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 2 Variables.
ECE Application Programming
Documentation Need to have documentation in all programs
ITEC113 Algorithms and Programming Techniques
What to bring: iCard, pens/pencils (They provide the scratch paper)
Chapter 2: Introduction to C++
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Introduction to Abstract Data Types
Chapter 2 Variables.
Chapter 2: Introduction to C++.
Unit 3: Variables in Java
Chapter 2 Variables.
The Fundamentals of C++
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

1 Objects Types, Variables, and Constants Chapter 3

Overview of C++ Statements 2 C++ Statements Declarations;Expressions; Other ObjectsOperations

Expressions In a C++ program, any finite sequence of ________ and __________ that combine to produce a value is called an expression. Examples from our temperature problem: __________ 1.8 * celsius 1.8 * celsius + 32 ________________________________________ 3 Even: 1.8; 32; "Hello"; 1 + 1; ;;;; We focus now on C++ objects and will look at operations later. Aside #1: expression Aside #1: expression ; is a statement.) Aside #2: expressions may be empty

Object Categories There are three kinds of objects: ___________: unnamed objects having a value ( 0, -3, 2.5, 2.998e8, 'A', "Hello\n",... ) named objects whose values can change during program execution __________: named objects whose values can change during program execution execution __________: named objects whose values cannot change during program execution 4 See Footnote on p.46 Have memory allocated to them

Literals – –_______ literals are integers: - 27, 0, 4, +4 – –_______ literals are real numbers, and can be: fixed-point: , 0.5, 1.414,... floating-point: 2.998e8, e9,... – –There are just two _____ literals: false, true – –_______ literals are single characters: 'A', 'a', '9', '$', '?',... – –_______ literals are sequences of characters: " Hello", "Goodbye", "Goodbye\n",... 5 or E Enclose in ' Enclose in " 5 basic types Not inter- changeable Some Variations: unsigned, long, short Variations: float, long double

Variable Declarations Examples: ___________________ cin >> celsius; _____________________________________________________ char letterGrade = 'A'; bool ok, done = false; Pattern: type name; type name = expression; 6 Allocates a memory location for values of this type and associates its address with name Declare only once Variables are used to store values, and can be either uninitialized or initialized. They must be _______________ before they are used.

Assignment Statements The value of a variable can be changed during execution by an assignment statement: Examples: _______________ ___________________________ letterGrade = 'B'; done = true; Pattern: name = expression; 7 Changes value in name's memory location to value of expression

Constant Declarations Constants are used to represent a value with a meaningful name, and must be initialized. They cannot be changed later. Examples: ____________________________________________ const char MIDDLE_INITIAL = 'A'; const string PROMPT = "Enter a number: "; Pattern: const type NAME = expression; 8 Allocates a memory location for values of this type, associates its address with NAME, puts value of expression in it, and locks it. Could use in Proj. 1.3

Identifiers The name of an object is called an _________ (because it identifies the object). C++ identifiers must begin with a letter (underscores are permitted, but discouraged) followed by zero or more letters, digits or underscores. Valid: age, r2d2, myGPA, MAX_SCORE Invalid: 123go, coffee-time, sam's, $name 9 May not be C++ keywords (See Appendix B)

Conventions We will use the following commonly-used convention to keep variable and constant objects distinct. Constant names: all uppercase, with multiple words separated by underscores (e.g., ___________) Variable names: all lowercase, with the first letter of each word after the first capitalized (e.g., ___________) 10 Use meaningful identifiers for readability! “camelback” notation

Objects char Objects Represented in memory by a ________ – –ASCII uses 8 bits (1 byte) to represent a character, allowing for 2 8 = 256 different characters – Now, 16, 24, or 32 bits. –Unicode's first version used 16 bits to represent a character, allowing for 2 16 = 65,536 different characters. Now, 16, 24, or 32 bits. Most commonly used code in C++ is ASCII: 'A' = ________________ 'a' = 97 = '0' = 48 = p.60 App. A Java chars can be treated as small integers; e.g,. char ch1 = 'A', ch2; int x = 2*ch1 + 1; ch2 = ch1 + 1; cout << x << ' ' << ch2 << '\n'; Output? ___________

Escape Characters C++ provides a number of _________ characters : '\n' newline character '\t' horizontal tab '\v' vertical tab '\f' form feed '\a' alert/bell '\\' backslash char '\'' apostrophe '\"' double quote '\ooo' char with octal code ooo '\xhhh' char with hex code hhh 12 See Appendix A

Objects int Objects Three forms: – –decimal (base-10) begin with 0 or a _________ digit or sign (-45, -2, 0, +21, 36, 65536,...) – –octal (base 8): begin with a ___ followed by octal digits (01, 02, 03, 04, 05, 06, 07, – –___________________________ _____________...) – –hexadecimal(base 16): begin with ____ followed by digits with a, b, c, d, e, f = 10, 11, 12, 13, 14, 15 (0x1, 0x2,..., 0x7, 0x8, 0x9, 0x, 0x, 0x, 0x, 0x, 0x, 0x, 0x,...) 13 See second item on Other Course Information class page

Objects unsigned Objects For integer objects whose values are never negative, C++ provides the ______________ type. They also may be expressed in decimal, octal, and hexadecimal forms. The major advantage of using unsigned instead of int when values being processed are known to be nonnegative is that larger values can be stored. 14

15 Using 32 bits, int values range from ( ) to ( ), whereas unsigned values range from 0 to ( ). An int value "loses" one of its bits to the sign, and so the maximum int value is about half of the maximum unsigned value. INT_MININT_MAX defines INT_MIN, INT_MAX, UINT_MAX, and other constants that specify ranges of C++ integers; does this for C++ real values. (See App. D) UINT_MAX Ranges of Integer Values

16 //-- Program to demonstrate the effects of overflow #include using namespace std; int main() { int number = 2; for (int i = 1; i <= 15; i = i + 1) { cout << number << '\n'; number = 10 * number; } Output: Strange Behavior of Integer Overflowoverflow Overflow: when a value gets out of range; for example, an integer exceeds INT_MAX = =

17 //-- Program to demonstrate the modular "wrap-around" effect of overflow #include using namespace std; int main() { int number = INT_MAX - 3; for (int i = 1; i <= 7; i = i + 1) { cout << number << '\n'; number = number + 1; } Output: Why this strange behavior? Check INT_MAX + 1, INT_MAX + 2,... INT_MAX INT_MIN Arithmetic is ___________________ INT_MIN... INT_MAX

Representation int Representation Integers are often represented internally in the ____________________ format, where the high- order (leftmost) bit indicates the number’s sign : 2 10 = = = = = Here we show 16 bits, but 32 or 64 are common. 18 Sec. 3.3

Twos-Complement To find twos-complement representation of a negative number: Represent its absolute value in binary: (_________________) Invert (complement) the bits: (_________________) Add 1 (_________________) Simpler: Flip all bits ________________________ 19 Try practice exercises — Week 3 of schedule

* : –6: Why Use Two's Complement? Because usual algorithms for +, * work! 111  carry bits

Objects double Objects Real values are often represented in 64 bits using the IEEE floating point standard: 5.75 = = sign (1 bit) mantissa (52 bits) exponent (11 bits) 21 float (single precision): bias = 127; exponent: 8 bits mantissa: 23 bits Try practice exercises on course schedule See Ch. 3, pp

22 //-- Effect of roundoff error #include #include using namespace std; int main() { for (double x = 0; x != 50.0; x = x + 0.1) for (double x = 0; x != 50.0; x = x + 0.1) { double y = x * sqrt(1 + sin(x)); double y = x * sqrt(1 + sin(x)); cout << x << " " << y << '\n'; cout << x << " " << y << '\n'; //if (x > 60) break; //if (x > 60) break; }} Execution: Problem:Binary representations of most real numbers do not terminate; e.g., 13.1 = … So they can't be stored exactly; error in this representation is called ____________________ Patriot missile failure See Other Course Information

23 /* temperature.cpp converts a Celsius temperature to Fahrenheit. John Doe Lab 1 Jan. 6, 2011 CPSC 104X Input: A Celsius temperature Output: Corresponding Fahrenheit temperature */ #include // cin, cout, > using namespace std; int main() { cout << "John Doe CPSC 104X -- Lab 1\n\n"; cout << "** Convert Celsius temps to Fahrenheit **\n"; cout << "Please enter a temperature in Celsius: "; double celsius; cin >> celsius; double fahrenheit = 1.8 * celsius + 32; cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit.\n"; }