1 / 18

Chapter 3. Expressions and Interactivity

Chapter 3. Expressions and Interactivity. Csc 125 Introduction to C++. Type Conversion. Operations are performed between operands of the same type. If not of the same type, C++ will convert one to be the type of the other This can impact the results of calculations. Data type ranking.

Download Presentation

Chapter 3. Expressions and Interactivity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3. Expressions and Interactivity Csc 125 Introduction to C++

  2. Type Conversion • Operations are performed between operands of the same type. • If not of the same type, C++ will convert one to be the type of the other • This can impact the results of calculations.

  3. Data type ranking

  4. Type coercion • Type Coercion: automatic conversion of an operand to another data type • Promotion: convert to a higher type • Demotion: convert to a lower type

  5. Type coercion • char, short, unsigned short automatically promoted to int • When operating on values of different data types, the lower one is promoted to the type of the higher one. • When using the = operator, the type of expression on right will be converted to type of variable on left

  6. Overflow and Underflow • When a variable is assigned a value that is too large or too small in range for that variable’s data type, the variable overflows or underflows. • Variable contains value that is ‘wrapped around’ set of possible values • No warning or error message is given, if an overflow or underflow occurs, the program will use the incorrect number, and therefore produce incorrect results

  7. Type casting • Manual data type conversion • The general format of a type cast expression • static_cast<DataType> (Value) • Example double number=4.6; int val; val=static_cast<int> (number)

  8. Type casting int months, books; double perMonth; perMonth=static_cast<double>(books)/months perMonth=static_cast<double>(books/months)

  9. Type casting (Another Example) void main() { int number=99; cout<<number<<endl; cout<<static_cast<char>(number)<<endl; }

  10. Named constant • For example, assume that the tuition rate is $128 per credit hour. Compute monthly payment for Peter’s tuition and display the amount he needs to pay.

  11. Named constant void main() { float tuition,payment; int creditHours; count<<“How many credit hours you\’ll take in spring? ”; cin>>creditHours; tuition=creditHours*128; cout<<“Monthly payment= ”<<tuition/5<<endl; count<<“How many credit hours you\’ll take in Fall? ”; cin>>creditHours; tuition=creditHours*128; cout<<“Monthly payment= ”<< tuition/4 <<endl; }

  12. Named constant • A named constant is really a variable whose content is ready-only, and cannot be changed while the program is running • const double TUITION_RATE=128;

  13. Named constant void main() { float tuition,payment; int creditHours; const short tuitionRate=128; count<<“How many credit hours you\’ll take in spring? ”; cin>>creditHours; tuition=creditHours*?; cout<<“Monthly payment= ”<<tuition/?<<endl; count<<“How many credit hours you\’ll take in Fall? ”; cin>>creditHours; tuition=creditHours*?; cout<<“Monthly payment= ”<< tuition/? <<endl; }

  14. Named constant • To define common value that is difficult to remember, const double PI=3.1415926; • To indicate that array’s size. Here is an example const int SIZE=21; char name[SIZE];

  15. Multiple assignment • Multiple assignment means to assign the same value to several variable with one statement. • int a, b, c,d; • a=b=c=d=12;

  16. Combined assignment operators • Assume x=10; x=x+4; x=x-2; x=x*10; x=x/5; x=x%4;

  17. combined assignment operators • C++ provides a special set of operators designed specifically for these jobs • Compound operators

  18. More examples result*=a+5;

More Related