Top Banner
Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University
48

Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

Jan 16, 2016

Download

Documents

Arnold Eaton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

Chapter 6 Fundamental Types

Dept of Computer EngineeringKhon Kaen University

Page 2: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 2

Major Concepts Data types

Numeric types Boolean type Enumeration type Character type

Arithmetic operations Type conversions Run-time errors:

Numeric overflow Round-off error

Page 3: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 3

Numeric Data Types There are two kinds of numbers

Whole numbers, Integers (จำ��นวนเต็�ม) Including negative whole numbers, 0, and

positive whole numbers Examples: 0, 1, 2, -1, -2

Decimal numbers Fraction where the denominator is a power of

ten and is therefore expressed using a decimal point.

Examples: 0.37 Rational numbers: จำ��นวนจำริ�งเป็�นต็�วเลขที่��ส�ม�ริถเข�ยนอย��ในริ�ป็ที่ศน�ยม, such as 2/10

Irrational numbers: cannot be expressed as a fraction, such as 2

Page 4: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 4

Fundamental Types Standard C++ has 14 different

fundamental types: 11 integral types and 3 floating-point types

Integral Types Boolean Type

bool Enumeration Type

enum

Page 5: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 5

Fundamental Types (Cont.)

Integral Types Character Types

char unsigned char wchar_t

Integer Types short int long

Page 6: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 6

Fundamental Types (Cont.)

Integral Types Integer Types

unsigned short unsigned int unsigned long

Floating-point Types float double long double

Page 7: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 7

The Boolean Type A boolean type is an integral type

whose variables can have only two values: False and true

These values are stored as the integers 0 and 1

The boolean type in Standard C++ is named bool

Page 8: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 8

Boolean Variables#include <iostream>using namespace std;int main(){// print the value of a boolean variable

bool flag = false;cout << “flag = “ << flag << endl;flag = true;cout << “flag = “ << flag << endl;

}

Page 9: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 9

Enumeration Types An enumeration type is an integral

type that is defined by the user with the syntaxenum typename { enumerator-list };

enum is a C++ keyword typename is an identifier that names

the type being defined enumerator-list is a list of acceptable

values of the variable that has that typename

Page 10: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 10

Enumeration Types (Cont.)

Example:enum Semester {FALL, WINTER, SPRING,

SUMMER}; We can then declare variables of this type

Semester s1, s2; We can use those variables and those

type values as we would with predefined types s1 = FALL; s2 = WINTER;

Page 11: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 11

Enumeration Types The actual values defined in the

enumerator-list are called enumerators In fact, they are ordinary integer constants Example: the enumerators FALL, WINTER,

SPRING, and SUMMER that are defined for the semester type could have been defined like this: const int FALL = 0; const int WINTER = 1; const int SPRING = 2; const int SUMMER = 3;

Page 12: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 12

Enumeration Types (Cont.)

The values 0, 1, … are assigned automatically when the type is defined

These default values can be overridden in the enumerator-list: enum Coin {PENNY = 1, NICKEL = 5,

DIME = 10, QUARTER = 25};

Page 13: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 13

Enumeration Types (Cont.)

If integer values are assigned to only some of the enumerators, then the ones that follow are given consecutive values enum Month {JAN=1, FEB, MAR, APR, MAY,

JUN, JUL, AUG, SEP, OCT, NOV, DEC}; Since enumerators are simply integer

constants, it is legal to have several different enumerators with the same value: enum Answer {NO = 0, FALSE = 0, YES = 1,

TRUE = 1, OK = 1}

Page 14: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 14

Enumeration Types (Cont.)

Enumeration types are usually defined to make code more self-documenting, i.e., easier for humans to understand enum Sex {FEMALE, MALE}; enum Day {SUN, MON, TUE, WED,

THU, FRI, SAT}; enum Radix {BIN=2, OCT=8, DEC=10,

HEX=16};

Page 15: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 15

Enumeration Types (Cont.)

Enumerators must be valid identifiers This following definition would not be valid

enum Grade {F, D, C-, C, C+, B-, B, A-, A}; Why is it invalid?

‘+’ and ‘-’ cannot be used in identifiers

Same values cannot be used in the same scope (same place) enum Month {JAN, …, OCT, …} enum Radix {BIN, OCT, …}

Page 16: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 16

Character Types A character type is an integral type

whose variables represent characters like the letter ‘A’ or the digit ‘s’

Characters are delimited by the apostrophe (‘)

Like all integral type values, character values are stored as integers

In C++, the type name for a character is char

Page 17: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 17

Character Types (Cont.) Using the ASCII encoding standard,

the examples of the values that represent these characters are as follow:‘ ’ represented by 32‘+’ represented by 43

‘A’ represented by 65 ‘a’ represented by 97

Page 18: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 18

Escape Sequences Escape sequences are used to print

out special characters \‘ Single quote \“ Double quote \\ Backslash \n Newline \t Horizontal tab \v Vertical tab

Page 19: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 19

Integer Types There are 6 integer types in Standard C++

short: 2 bytes int: 4 bytes long: 4 bytes unsigned short: 2 bytes unsigned int: 4 bytes unsigned long:4 bytes

The least value of variables with unsigned types are 0

Page 20: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 20

Integer Type Ranges This program prints the numeric ranges of the 6 integer types in C++#include <iostream>#include <limits> // defines the constants SHRT_MIN, etc. using namespace std;int main() { // prints some of the constants stored in the <limits> header:

cout << "minimum short = " << SHRT_MIN << endl; cout << "maximum short = " << SHRT_MAX << endl; cout << "minimum unsigned short = 0" << endl;

cout << "maximum unsigned short = " << USHRT_MAX << endl; cout << "minimum int = " << INT_MIN << endl;

cout << "maximum int = " << INT_MAX << endl;cout << "minimum unsigned int = 0" << endl;cout << "maximum unsigned int = " << UINT_MAX << endl; cout << "minimum long= " << LONG_MIN << endl; cout << "maximum long= " << LONG_MAX << endl; cout << "minimum unsigned long = 0" << endl;cout << "maximum unsigned long = " << ULONG_MAX << endl; }

Page 21: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 21

Integer Type Ranges (Cont.)

The output of the programminimum short = -32768maximum short = 32767minimum unsigned short = 0maximum unsigned short = 65535minimum int = -2147483648maximum int = 2147483647minimum unsigned int = 0maximum unsigned int = 4294967295minimum long= -2147483648maximum long= 2147483647minimum unsigned long = 0maximum unsigned long = 4294967295

Page 22: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 22

Arithmetic Operations C++ performs its numerical calculations

by means of the five arithmetic operators: +, -, *, /, and %

Operations are performed in this order 1. ( ) Perform the operation inside ( ) first 2. +,- Then, perform unary plus and unary

minus, such as +32, -16 3. *,/,% 4. +, -

If there is more than one operation with the same order, perform from left to right

Page 23: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 23

Arithmetic Operations (Cont.)

Math: a = bc/de C++: a = (b*c)/(d*e) Is b*c/d*e == (b*c)/(d*e)? Is b*c*d*e == (b*c)*(d*e)?

Math: m = C++: m = (y-b)/(x-a) Is y-b/x-a == (y-b)/(x-a)? Is y-b-x-a == (y-b)-(x-a)?

Page 24: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 24

Arithmetic Operations (Cont.)

-34*32 = ? Choice a: (-34)*32 Choice b: -(34*32)

7 – 2 % 3 = ? Choice a: (7-2)% 3 Choice b: 7 – (2 %3)

3 + 2 / 4 = ? Choice a: 3 + (2/4) Choice b: (3 + 2)/ 4

Page 25: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 25

Arithmetic Operations (Cont.)

#include <iostream>using namespace std;int main(){

// tests operators +, -, *, /, and %: int m=54; int n=20; cout << "m = " << m << " and n = " << n << endl; cout << "m+n = " << m+n << endl; // 54+20 = 74 cout << "m-n = " << m-n << endl; // 54-20 = 34 cout << "m*n = " << m*n << endl; // 54*20 = 1080 cout << "m/n = " << m/n << endl; // 54/20 = 2 cout << "m%n = " << m%n << endl; // 54%20 = 14 return 0;

}

Page 26: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 26

Increment & Decrement Operators

The values of integral objects can be incremented by one with the ++ operator and decremented by one with the – operator

Each of “++” and “--” operator has two versions: A “pre” version A “post” version

Page 27: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 27

++ & -- Operators The “pre” version performs the

operation on the object before the resulting value is used in its context Example: n = ++m m= m + 1, n = m

The “post” version performs the operation on the operation after the object’s current value ahs been used Example: n = m++ n = m, m = m + 1

Page 28: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 28

++ & -- Operators (Cont.)

int main(){ // shows the difference between m++ and ++m

m = 44;n = ++m;cout << “m = “ << m << “, n = “ << n << endl;

m = 44; n = m++;

cout << “m = “ << m << “, n = “ << n << endl;

}

Page 29: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 29

Composite Assignment Operators

The standard assignment operator in C++ is the equal sign ‘=‘

C++ also includes the composite assignment operators: ‘+=‘, ‘-=‘, ‘*=‘, ‘/=‘, and ‘%=‘

Examples: A += B; A = A + B; A *= B; A = A * B;

Page 30: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 30

Composite Assignment Operators

#include <iostream> using namespace std; int main() { // tests arithmetic assignment operators: int n=22; cout << "n = " << n << endl; n += 9; // adds 9 to n cout << "After n += 9, n = " << n << endl; n %= 7; // mod n with 7 cout << "After n %= 7, n = " << n << endl; }

Page 31: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 31

Floating-Point Types C++ has three types for storing

values with floating-point float: use 4 bytes double: use 8 bytes long double: use 8, 10, 12, or 16

bytes On most systems, double uses twice

as many bytes as float

Page 32: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 32

Floating-Point Arithmeticint main(){

double x = 54.0;double y = 20.0;cout << “x*y = “ << x*y << endl; // 1080cout << “x/y = “ << x/y << endl; // 2.7

} If x and y have type ‘int’, what is the

value of x/y? 2

Page 33: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 33

Using the sizeof Operator The programs use the sizeof operator

which returns the size in bytes of the type specified

Examples:cout << “size of integer is “ << sizeof(int) << endl; cout << “size of float is “ << sizeof(float)<< endl;

Output: size of integer is 4 size of float is 4

Page 34: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 34

Type Conversionsint n = 22;float x = 3.14159;n += x;x += n;cout << “n is “ << n << endl;cout << “x is “ << x << endl; What are the values of n and x?n is 25x is 28.1416

Page 35: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 35

Type Conversions (Cont.) If T is one type and v is a v value of

another type, then the expression T(v) converts v to type T

This process is called type casting Example:

double v = 1234.56789;int n = (int) v;cout << “n is “ << n endl;

What is the value of n? n is 1234

Page 36: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 36

Numeric Overflow Computers are finite, so the range of

any type must also be finite But in mathematics, there are

infinitely many integers Consequently, computers are

manifestly prone to error when their numeric values become too large

This kind of error is called numeric overflow

Page 37: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 37

Numeric Overflow (Cont.)int main(){ // print n until it overflows

int n = 10000;cout << “sizeof(int) is “ << sizeof(int) << endl;cout << “n is “ << n << endl;n *= 10000;cout << “n is “ << n << endl;n *= 10000;cout << “n is “ << n << endl;return 0;

} What is the maximum n can be?

2147483647

Page 38: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 38

Integer Overflow (Cont.) What is the size of an integer in bits?

32 bits 232 = 4,294,967,296 What is the maximum value of a variable

with type ‘int’? 2,147,483,647 ((232)/2) - 1

What is the minimum value of a variable with type ‘int’? -(232)/2

Page 39: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 39

Floating-Point Overflow#include <iostream> using namespace std; int main() { // prints x until it overflows: float x=1000.0; cout << "x = " << x << endl; x *= x; // multiplies n by itself; i.e., it squares x cout << "x = " << x << endl; x *= x; // multiplies n by itself; i.e., it squares x cout << "x = " << x << endl; x *= x; // multiplies n by itself; i.e., it squares x cout << "x = " << x << endl; x *= x; // multiplies n by itself; i.e., it squares x cout << "x = " << x << endl; }

Page 40: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 40

Floating-Point Overflow (Cont.)

Output x = 10000 x = 1e+08 x = 1e+16 x = 1e+32 x = inf

Integer overflow “wrap around” to negative integers

Floating-point overflow “sinks” into the abstract notion of infinity

Page 41: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 41

Round-off Error Round-off error is another kind of error

that often occurs when computers do arithmetic on rational numbers

Example: The number 1/3 might be stored as

0.33333, which is not exactly equal to 1/3 The difference is called round-off error In some cases, these errors can cause

serious problems

Page 42: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 42

Round-Off Error (Cont.)// Example 2.14 on page 28 // Round-off Error #include <iostream> using namespace std; int main() { // illustrates round-off error:: double x = 1000/3.0;

cout << "x = " << x << endl; // x = 1000/3 double y = x - 333.0;

cout << "y = " << y << endl; // y = 1/3 double z = 3*y - 1.0;

cout << "z = " << z << endl; // z = 3(1/3) - 1 if (z == 0)

cout << "z == 0.\n"; else

cout << "z does not equal 0.\n"; // z != 0 }

Page 43: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 43

Round-Off Error (Cont.) Program output

x = 1000/3 = 333.333 // x = 1000/3 y = x – 333 = 0.333333 // y = 1/3 z = 3*y – 1.0

= -5.68434e-14 // z = 3(1/3) - 1 z does not equal 0. // z != 0

It is better to avoid tests for equality with floating-point types

Overflow and round-off errors are run-time errrors

Page 44: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 44

E-Format for Floating-Point Values

When input or output, floating-point values may be specified in either of two formats: Fixed-point format

Example: 333.333, 0.1 Scientific format

Example: -5.68434x10-14 = -5.68434e-14

Page 45: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 45

E-Format (Cont.) Letter e stands for “exponent on 10” Thus, e-14 means 10-14

The scientific format is thus usually used to express very small or very large numbers

You can use either e or E in the scientific format

Page 46: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 46

Scope The scope of an identifier is that part of

the program where it can be used Variables cannot be used before they are

declared The scope of a variable extends from the

point where it is declared to the end of the internal block within which it is declared

{int a = 2; // a can be used from this point

}a cannot be used here

Page 47: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 47

Scope (Cont.)#include <iostream> using namespace std; int main() { // illustrates the scope of variables: x = 11; // Is this line OK? int x; {

x = 22; // Is this line OK? y = 33; // Is this line OK? int y;

y = 11; // Is this line OK?}

x = 66; // Is this line OK? y = 77; // Is this line OK? }

Page 48: Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.

178110: Computer Programming (II/2546) 48

Scope (Cont.)int x = 11;int main(){

int x = 22;{

int x = 33;cout << “In block inside main(): x = “ << x

<< endl; }

cout << “In main(): x = “ << x << endl;cout << “In main(): ::x = “ << ::x << endl;

} The scope resolution operator :: is to access the

global x that is otherwise hidden in main()