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.

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

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types and Expressions
Types and Variables. Computer Programming 2 C++ in one page!
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
String Escape Sequences
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CPS120: Introduction to Computer Science Lecture 8.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Input & Output: Console
CS2311 Computer Programming Dr. Yang, Qingxiong (with slides borrowed from Dr. Xu, Henry) Lecture 2: Basic Syntax – Part I: Variables and Constants.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CPS120: Introduction to Computer Science
Lecture #5 Introduction to C++
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.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
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.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
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.
CPS120: Introduction to Computer Science Variables and Constants.
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.
1 Objects Types, Variables, and Constants Chapter 3.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Variables and Types
ECE Application Programming
Documentation Need to have documentation in all programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Variables and Primative Types
Introduction to Abstract Data Types
C++ Basics.
C++ Data Types Data Type
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Variables and Constants
The Fundamentals of C++
Programming Fundamental-1
Presentation transcript:

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 identifier names Study variables and constants What they are How they differ How to declare How to use Investigate internal representations First look at class attribute variables

C++ An Introduction to Computing, 3rd ed. 3 Types and Declarations Fundamental Types Integers (whole numbers, negatives) int Integer variations short, long, unsigned Reals (fractional numbers) float, double, long double Characters (letters, digits, symbols, punctuation) char Booleans (logical values, true and false ) bool

C++ An Introduction to Computing, 3rd ed. 4 Integer Memory used for an int depends on word size used by the hardware Usually 16, 32, or 64 bits used 16 Bits (2 bytes) – short int Range … bits (4 bytes) – long int (or long ) Range … (maybe 64) One bit used for the sign

C++ An Introduction to Computing, 3rd ed. 5 Integer Integers can be specified as unsigned Then the sign bit not needed Gives larger positive range unsigned short (16 bits) Range 0 … unsigned long (32 bits) Range 0 …

C++ An Introduction to Computing, 3rd ed. 6 Integer Literals Decimal integers Sequence of digits without a decimal point Octal integers Sequence of digits beginning with a 0 Base 8 Hexadecimal integers Sequence of digits beginning with an X Base 16

C++ An Introduction to Computing, 3rd ed. 7 Reals float Usually a 32-bit value (4 bytes) double A 64-bit value (8 bytes) long double A 96- or 128-bit value The programmer should choose which type based on degree of precision desired for the object

C++ An Introduction to Computing, 3rd ed. 8 Reals Values are stored internally in scientific notation (base 2) A sign for the number Significant digits of the number The power of 10 Sign for the power

C++ An Introduction to Computing, 3rd ed. 9 Reals Literal values (real Literals are treated as double) A sequence of digits with leading sign, containing a decimal point Scientific notation – any one of the following forms 0.12e11 1.2E E9 12.e9 12E9

C++ An Introduction to Computing, 3rd ed. 10 Characters char type Represents individual characters See ASCII character set in Appendix A Characters represented in memory by numeric values Character literals Characters enclosed in single quotes 'X' '7' '>' 'e'

C++ An Introduction to Computing, 3rd ed. 11 Characters Escape characters A backslash \ combined with another character hold special meaning CharacterC++ Escape Sequence Newline \n Horizontal tab \t Vertical tab \v Backspace \b Carriage Return \r Form Feed \f Alert (beep) \a

C++ An Introduction to Computing, 3rd ed. 12 Strings Need #include ; using namespace std; A sequence of characters Enclosed in double quotes "Hi Mom” Can include escape characters "\nThe answer is " Warning "A" is a string literal 'A' is a character literal

C++ An Introduction to Computing, 3rd ed. 13 Identifiers Names given to software objects Rules: Must not be C++ keywords (see Appdx B) int, if, while, … Must start with a letter (or the underscore _ ) Followed by numerals, letters, underscore Recommendation Use meaningful identifiers Go for longer names, rather than shorter

C++ An Introduction to Computing, 3rd ed. 14 Identifiers C++ is case sensitive firstName is not the same as firstname Typical usage Constants are all caps PI Variables Start with lower case Capitalize first letter of successive words monthlyElectricCharge

C++ An Introduction to Computing, 3rd ed. 15 Object Categories There are three kinds of objects: Literals: unnamed objects having a value ( 0, -3, 2.5, 2.998e8, ‘A’, “Hello\n”,... ) Variables: named objects values can change during program execution Constants: named objects values do not change during program execution

C++ An Introduction to Computing, 3rd ed. 16 Literals int literals are whole numbers: -27, 0, 4, +4,012,0xA3B double literals are real numbers, and can be: fixed-point: , 0.5, 1.414,... floating-point: 2.998e8, e9,... There are just two bool literals: false, true char literals are single ASCII characters: ‘A’, ‘a’, ‘9’, ‘$’, ‘?’,... string literals are ASCII character sequences: “Hello”, “Goodbye”, “Goodbye\n”,...

C++ An Introduction to Computing, 3rd ed. 17 Constants Declaration of software objects that remain constant const double HOURLY_WAGE = 6.75; Rules const is a keyword Specify type Specify the name (caps recommended) Must be initialized with value at declaration

C++ An Introduction to Computing, 3rd ed. 18 Constants Reasons to use constants Improve readability of the source code Facilitate program modification Good programming practice Place all constant declarations at beginning of function where used See some predefined constants in and libraries. (page 43)

C++ An Introduction to Computing, 3rd ed. 19 Variables Give a name to a memory location Compiler accesses specific memory location when program uses a given variable Refer to objects in the program for which the value can change Declaration type variableName; // or type variableName = initializer_expression;

C++ An Introduction to Computing, 3rd ed. 20 Variables Variables Declaration Can be either initialized or uninitialized... If variable is uninitialized Contents must be considered "garbage value" Examples: int age = 18; double GPA = 3.25, credits; char letterGrade = ‘A’; bool ok, done = false;

C++ An Introduction to Computing, 3rd ed. 21 int Representation Integers are often represented in the twos-complement format, High-order bit indicates the number’s sign: 2 10 = = = = = We show 16 bits, but 32 or 64 are common.

C++ An Introduction to Computing, 3rd ed. 22 unsigned Objects Some objects have values that are never negative, C++ provides the unsigned type: = = = = = No sign bit, numbers can be twice as big.

C++ An Introduction to Computing, 3rd ed. 23 int vs. unsigned Using 32 bits, int values range from ( ) to ( ), unsigned values range from 0 to ( ). An int value “loses” one of its bits to the sign, Maximum int value is about half of the maximum unsigned value.

C++ An Introduction to Computing, 3rd ed. 24 double Objects Real values are often represented in 64 bits Use the IEEE floating point standard: mantissa (52 bits) exponent (11 bits) sign (1 bit)

C++ An Introduction to Computing, 3rd ed. 25 double Objects Overflow Exponent is too large Not enough bits to represent Underflow Number is so small Not enough bits to represent negative exponent

C++ An Introduction to Computing, 3rd ed. 26 char and String Objects Characters represented internally with binary codes 8 bits – only 128 characters Strings stored as a sequence of these binary codes

C++ An Introduction to Computing, 3rd ed. 27 char and String Objects Unicode uses 16 bit codes Possible to represent more than 65,000 characters Can include special characters

C++ An Introduction to Computing, 3rd ed. 28 Booleans Only two values true and false true stored as 1 Anything non zero will be interpreted as true false stored as 0 Could be stored in a single bit Usually stored in a word or byte