Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 value for data type Two categories of data types –Built-in (primitive or atomic) such as int or float –Class (user or programmer-defined)

2 2Object-Oriented Program Development Using C++ Table 2-1 Built-In Data Type Operations

3 3Object-Oriented Program Development Using C++ Integer Data Types C++ provides nine built-in integer data types Variations related to storage capacity Most commonly used: int, bool, and char Unsigned data types enforce sign convention

4 4Object-Oriented Program Development Using C++ Figure 2-2 C++ Integer Data Types

5 5Object-Oriented Program Development Using C++ Integer Data Types (continued) The int data type –Signed integers within compiler specific range –Common storage allocation: 4 bytes Range of values: -2,147,483,648 to 2,147,483,647 The char data type –Stores individual characters (letters, numbers, symbols) –ASCII: standard accommodates 256 codes –Unicode: accommodates 65,536 characters

6 6Object-Oriented Program Development Using C++ Table 2-2 The ASCII Uppercase Letter Codes

7 7Object-Oriented Program Development Using C++ The escape character –Escape sequence A backslash followed by one or more characters Example: newline character ‘\n’ –Tells compiler to escape from normal interpretation routine The bool data type –Represents Boolean (logical) –Range of values restricted to 1 (True) or 0 (False) Integer Data Types (continued)

8 8Object-Oriented Program Development Using C++ Table 2-3 Escape Sequences

9 9Object-Oriented Program Development Using C++ Integer Data Types (continued) Determining storage size –sizeof ( ) operator: evaluates capacity in bytes –Data type argument inserted between ( ): sizeof(int) Signed and unsigned data types –Signed data type permits zero, positive and negative values –Unsigned data type restricted to zero, positive values char, bool, and other integers

10 10Object-Oriented Program Development Using C++ Table 2-4 Integer Data Type Storage

11 11Object-Oriented Program Development Using C++ Floating-Point Types Floating-point numbers are real numbers Include decimal point in explicit references; e.g., 3.0 C++ supports 3 types: float, double, long double Exponential Notation –Alternative system for representing floating-point data –Similar to scientific notation –Example: 5.67e2 Assume base 10. Letter ‘e’ = exponent, 2 = power

12 12Object-Oriented Program Development Using C++ Table 2-5 Floating-Point Data Types

13 13Object-Oriented Program Development Using C++ Arithmetic Operations Five binary arithmetic operators: +, -, *, /, % Binary operators require two operands Syntax template for simple binary expression –literalValue operator literalValue –Example: 3 + 5 cout object accommodates evaluation and display

14 14Object-Oriented Program Development Using C++ Arithmetic Operations (continued) Expression –Combination of operators and operands –Can be evaluated to yield a value Integer expression: operands exclusively int type Floating-point expression: operands exclusively floating-point Mixed-mode expression: result of operation is double precision value

15 15Object-Oriented Program Development Using C++ Arithmetic Operations (continued) Integer division –Truncates remainder associated with Real division –Examples: 9/3 = 3, 10/3 = 3, 11/3 = 3 –Remainder value captured with modulo arithmetic Negation –Generate negative value by prefixing minus sign (-) –Example: -10 represents negative 10 –Negation is an instance of a unary operation

16 16Object-Oriented Program Development Using C++ Table 2-6 Summary of Arithmetic Operators

17 17Object-Oriented Program Development Using C++ Operator Precedence and Associativity Operator precedence: defines hierarchy of operations Three levels of precedence –P1: All negations are done first –P2: Multiplication, division, and modulo operations –P3: Addition and subtraction Parentheses trump normal precedence Associativity: defines operation sequence on level Pn –P1: right to left –P2 and P3: from left to right

18 18Object-Oriented Program Development Using C++ Table 2-7 Operator Precedence and Associativity

19 19Object-Oriented Program Development Using C++ Using Variables to Construct a Data Declaration Section Variable: container identified by three parts –Name (represents storage location or memory address) –Data type –Value Programmer selects variable name according to rules Assignment statement stores value in variable –Example: int numOne = 17

20 20Object-Oriented Program Development Using C++ Figure 2-5 Naming Storage Locations

21 21Object-Oriented Program Development Using C++ Declaration Statements Declaration instructs compiler to allocate memory Syntax –dataType variableName; –Example: int myNumber; Variables of same type may be declared on same line

22 22Object-Oriented Program Development Using C++ Initialization Initialization: assigning initial value in declaration Example: float finalGrade = 98.3; Valid initializers –Literals –Expressions with literals –Expressions with literals and previously initialized variables

23 23Object-Oriented Program Development Using C++ The Data Declaration Section Four variable classifications –Instance, class, local, global –Parameter sometimes included as fifth type Instance variables –Automatically created with declaration –Provided to each created object –Default access set to private (keyword) Class variables: shared by all objects of a class

24 24Object-Oriented Program Development Using C++ Table 2-8 Determination of a Variable’s Type

25 25Object-Oriented Program Development Using C++ Memory Allocation Compiler performs several functions –Extracts storage requirements from data type –Associates variable name with memory address –Flags misspelled variables as error (undeclared identifier) Local variables and objects –Declared within a method (or function) –Automatically allocated memory Instance variables: undefined until object creation

26 26Object-Oriented Program Development Using C++ Figure 2-9A Defining the Integer Variable Named total

27 27Object-Oriented Program Development Using C++ Summary Built-in data types: range of values and set of operations Programmer-defined types: classes Variable: data type, name, value Variable name represents memory address Initialization: assigning value in declaration

28 28Object-Oriented Program Development Using C++ Summary (continued) Operand: variables or literal included in expression Operands and operators constitute expression Expressions evaluate to produce value Two sets of operation rules : operator precedence and associativity


Download ppt "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."

Similar presentations


Ads by Google