Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types,...

30
Non-numeric types char string bool const Reserved words Operators Operator = Arithmetic operators Operator precedence Operators on characters Non-numeric types, boolean types, arithmetic operators Comp Sci 1570 Introduction to C++

Transcript of Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types,...

Page 1: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Non-numeric types, boolean types, arithmeticoperators

Comp Sci 1570 Introduction to C++

Page 2: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Outline

1 Non-numeric typescharstringbool

2 const

3 Reserved words

4 OperatorsOperator =Arithmetic operatorsOperator precedenceOperators on characters

Page 3: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Outline

1 Non-numeric typescharstringbool

2 const

3 Reserved words

4 OperatorsOperator =Arithmetic operatorsOperator precedenceOperators on characters

Page 4: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

ASCII

Page 5: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

ASCII

Page 6: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Type: character

• Allocation: 1 byte

• Range of values: a char type variable can take on thevalues of the ASCII character set. There are 256characters in the set, including the digits, uppercasealphas, lower case alphas, punctuation, and many otherspecial characters.

Example declaration:

char c o n t i n u e R e s p o n s e = ’ y ’ ;

See demo code.

Page 7: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Escape characters

Page 8: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Outline

1 Non-numeric typescharstringbool

2 const

3 Reserved words

4 OperatorsOperator =Arithmetic operatorsOperator precedenceOperators on characters

Page 9: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Type: string

• Allocation: varies

Example declaration:

s t r i n g g r e e t i n g = ” h e l l o . ”s t r i n g name ;c i n >> name ; // suppose you type Bob Smithcout << name ; // ou tpu t s Bob ( not Smith )

• Use the double quotes for string variable initializations,while using the single quotes (or ticks) for characters.

• Though you can initialize a string variable to any string,even one that includes spaces, reading into a stringvariable using a cin statement is different.

• If you use a cin statement to read a value into a stringvariable, it will only read up to the first space and thenstop. We’ll show a solution later.

See demo code.

Page 10: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Outline

1 Non-numeric typescharstringbool

2 const

3 Reserved words

4 OperatorsOperator =Arithmetic operatorsOperator precedenceOperators on characters

Page 11: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Type: Boolean

• Allocation: 1 byte

• Possible values: true or false

Example declaration:

bool q u i t = f a l s e ;bool p a s s e d = true ;

true and false are reserved words, which means is that they(and many others) have special meanings to the compiler andcannot be used as identifiers such as variable names.

bool variable can be used as a primitive if statement.See demo code.

Page 12: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Outline

1 Non-numeric typescharstringbool

2 const

3 Reserved words

4 OperatorsOperator =Arithmetic operatorsOperator precedenceOperators on characters

Page 13: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Constants

• Declaring a variable as constant will tell the compiler notto allow it to be changed.

• A constant may NOT be on the left hand side of anassignment statement.

const f l o a t PI = 3 . 1 4 1 5 9 ;const double DISTORTION COEF = 5 . 6 6 2 3 9 8 ;const f l o a t TAX RATE = . 0 2 3 ;PI = 4 ; // NO, w i l l not comp i l e

Benefits of CONSTANTS

• variables can be protected from inadvertent corruption.• In addition, the variable name identifies them, taking the

mystery out of an unidentifiable number.• Perhaps a value in your program needs to be changed at

some point in time. It is easy to change the value of aconstant in one location rather than have to searchperhaps thousands of lines of code to change all instances.

Page 14: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

When to use constants

const short TWO = 2 ;f l o a t v a l 1 , v a l 2 , a v e r a g e ;a v e r a g e = ( v a l 1 + v a l 2 ) / TWO;

const short TWO = 3 ;f l o a t v a l 1 , v a l 2 , v a l 3 , a v e r a g e ;a v e r a g e = ( v a l 1 + v a l 2 + v a l 3 )/TWO;

const short DIVISOR = 2 ;

See demo code.

Page 15: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Outline

1 Non-numeric typescharstringbool

2 const

3 Reserved words

4 OperatorsOperator =Arithmetic operatorsOperator precedenceOperators on characters

Page 16: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Names not to use

• There are ”words” that are used in the C++ languagethat should not use in any other way.

• Thus, you should be tempted to name a variable ”new”,since that is a reserved word.

• Do not use any of the following for your own namedobjects:

auto const double float int short struct unsigned break continueelse for long signed switch void case default enum goto registersizeof typedef volatile char do extern if return static unionwhile asm dynamic cast namespace reinterpret cast boolexplicit new static cast typeid catch false try operator templatetypename class friend private this using const cast inline publicthrow virtual delete mutable protected true wchar t and bitandcompl not eq or eq xor eq and eq bitor not or xor cin endlINT MIN iomanip main npos std cout include INT MAXiostream MAX RAND NULL string

Page 17: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Outline

1 Non-numeric typescharstringbool

2 const

3 Reserved words

4 OperatorsOperator =Arithmetic operatorsOperator precedenceOperators on characters

Page 18: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Outline

1 Non-numeric typescharstringbool

2 const

3 Reserved words

4 OperatorsOperator =Arithmetic operatorsOperator precedenceOperators on characters

Page 19: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Operator =

• Symbol used for assignment of a value to a variable is =

t a x = income ∗ RATE ; // RATE i s con s tRATE = . 0 5 ; // not a l l owed !4 = income + 6 4 ; // not a l l owed !

The compiler goes through these steps to execute:

1 Value of the rhs (right hand side) is determined. In thiscase, memory locations for income and RATE have to beread, and then those values multiplied.

2 Types of the rhs and lhs are determined. If the type of thelhs can be ”converted” to the type of the rhs withoutlosing information, then it is done. If information would belost in doing so, a compiler warning is issued.

3 Value of the rhs is copied into the memory location of thelhs. Thus, the lhs MUST BE A MODIFIABLE MEMORYLOCATION. That is, lhs cannot be a constant.

Page 20: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Outline

1 Non-numeric typescharstringbool

2 const

3 Reserved words

4 OperatorsOperator =Arithmetic operatorsOperator precedenceOperators on characters

Page 21: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Arithmetic operators

”+” , ”−” , ”∗” , ”/” , ”%” ( modular a r i t h m e t i c )someValue = num1 + num2 ; // a d d i t i o nsomeValue = num1 − num2 ; // s u b t r a c t i o nsomeValue = num1 ∗ num2 ; // m u l t i p l i c a t i o nsomeValue = num1 / num2 ; // d i v i s i o nsomeValue = num1 % num2 ; // modulus

Page 22: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Arithmetic operators

A=10; B=20;

Page 23: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Division

i n t hour , minute ;hour = 1 1 ;minute = 5 9 ;cout << ”Number o f minutes s i n c e m i d n i g h t : ” ;cout << hour ∗60 + minute << e n d l ;cout << ” F r a c t i o n o f th e hour p a s s e d : ” ;cout << minute /60 << e n d l ;

See code demo. What does this output??More to come next class

Page 24: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Outline

1 Non-numeric typescharstringbool

2 const

3 Reserved words

4 OperatorsOperator =Arithmetic operatorsOperator precedenceOperators on characters

Page 25: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Operator precedence

• Multiplication and division happen before addition andsubtraction. So 2*3-1 yields 5, not 4, and 2/3-1 yields -1,not 1 (remember that in integer division 2/3 is 0).

• If the operators have the same precedence they areevaluated from left to right. So in the expressionminute*100/60 , the multiplication happens first, yielding5900/60 , which in turn yields 98. If the operations hadgone from right to left, the result would be 59*1 which is59, which is wrong.

• Any time you want to override the rules of precedence (oryou are not sure what they are) you can use parentheses.Expressions in parentheses are evaluated first, so 2 * (3-1)is 4. You can also use parentheses to make an expressioneasier to read, as in (minute * 100) / 60 , even though itdoesn’t change the result.

Page 26: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Associativity

To see how associativity works, consider the expression2 - 3 - 4The two operators are the same, so they have equalprecedence. Should the first subtraction operator be appliedbefore the second, as in(2 - 3) - 4(that is,-5), or rather is2 - (3 - 4)(that is, 3) the correct interpretation? The former (-5) is thecorrect interpretation. We say that the subtraction operator isleft associative, and the evaluation is left to right. Thisinterpretation agrees with standard arithmetic rules. All binaryoperators except assignment are left associative.

Page 27: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Associativity

Assignment is an exception; it is right associative. To see whyassociativity is an issue with assignment, consider the statementw = x = y = z;This is legal C++ and is called chained assignment.Assignment can be used as both a statement and anexpression. The statementx = 2;assigns the value 2 to the variable x. The expressionx = 2assigns the value 2 to the variable x and evaluates to the valuethat was assigned; that is, 2. Since assignment is rightassociative, the chained assignment example should beinterpreted asw = (x = (y = z)); which behaves as follows:The expression y = z is evaluated first. z’s value is assigned toy, and the value of the expression y = z is z’s value.

Page 28: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Arithmetic operators

Page 29: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Outline

1 Non-numeric typescharstringbool

2 const

3 Reserved words

4 OperatorsOperator =Arithmetic operatorsOperator precedenceOperators on characters

Page 30: Non-numeric types, boolean types, arithmetic operators · Non-numeric types, boolean types, arithmetic ... Operators on characters Type: Boolean Allocation: ... the variable name

Non-numerictypes

char

string

bool

const

Reservedwords

Operators

Operator =

Arithmeticoperators

Operatorprecedence

Operators oncharacters

Operators on characters?

#inc lude <i o s t r e a m>#inc lude <s t r i n g >using namespace s t d ;

i n t main ( ){char l e t t e r ;l e t t e r = ’ a ’ + 1 ;cout << l e t t e r << e n d l ; // ??cout << l e t t e r + ’ b ’ << e n d l ; // ??l e t t e r = ’ a ’ + ’ b ’ ;cout << l e t t e r << e n d l ; // ??

s t r i n g word ;word = ” h e l l o ” ;cout << word + ” w o r ld ” << e n d l ; // ??cout << word − ” w o r l d ” << e n d l ; // ??cout << word ∗ ” w o r l d ” << e n d l ; // ??cout << word / ” w or l d ” << e n d l ; // ??

}