C++ Session 3

1,051 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,051
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
48
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C++ Session 3

  1. 1. Session 3
  2. 2. Session objectives (1) <ul><ul><li>Identify operators </li></ul></ul><ul><ul><li>Assignment </li></ul></ul><ul><ul><li>Arithmetic </li></ul></ul><ul><ul><li>Compound </li></ul></ul><ul><ul><li>Assignment </li></ul></ul><ul><ul><li>Relational </li></ul></ul><ul><ul><li>Logical </li></ul></ul><ul><ul><li>Bitwise </li></ul></ul><ul><ul><li>Understand precedence and evaluation </li></ul></ul><ul><li>of operators </li></ul>
  3. 3. <ul><ul><li>Discuss mixed mode expressions </li></ul></ul><ul><li>and implicit type conversions </li></ul><ul><ul><li>Understand type casting </li></ul></ul><ul><li>and type compatibility </li></ul><ul><ul><li>Identify C++ shorthand operators </li></ul></ul>Session objectives (2)
  4. 4. Introduction Operators are: <ul><ul><li>Are a set of characters </li></ul></ul><ul><ul><li>Have specific meaning particular to a language </li></ul></ul><ul><ul><li>Do not include special characters </li></ul></ul><ul><li>; “ : , </li></ul>
  5. 5. Operands
  6. 6. Binary Arithmetic Operators (1) Require two operands
  7. 7. Binary Arithmetic Operators (2) Adds the values of operands result = The addition operator
  8. 8. Binary Arithmetic Operators (3) Subtracts the second operand from first answer = The subtraction operator
  9. 9. Multiplies the operands ans = The multiplication operator Binary Arithmetic Operators (4)
  10. 10. Divides the first operand by the second The division operator result = Binary Arithmetic Operators (5)
  11. 11. Store the remainder after integer division The modulus operator answer = Binary Arithmetic Operators (6)
  12. 12. void main(void) { : cout << endl ; f_dist = f_speed * f_time ; cout<<&quot;The distance covered by the vehicle = &quot; ; cout<< f_dist ; cout << endl ; } Multiplication operator Binary Arithmetic Operators (7)
  13. 13. Unary arithmetic operators (1) Require one operand <ul><ul><li>Negation operator - </li></ul></ul><ul><ul><li>Increment operator ++ </li></ul></ul><ul><ul><li>Decrement operator - - </li></ul></ul>int x; int x= -3 stnd ++ OR ++stnd puls - - OR - - puls
  14. 14. #include <iostream.h>   void main(void) { int value ; value = 1 ;   cout << &quot;THE INCREMENT OPERATOR&quot; << endl ; cout << &quot;Value of value: &quot; << value << endl ; cout << &quot;Pre-fix increment operator (++value): &quot; << ++value ; cout << endl ; cout << &quot;Value of value: &quot; << value << endl ; cout << &quot;Post-fix increment operator (value++): &quot; << value++ ; cout << endl ; cout << &quot;Value of value: &quot; << value << endl ;   Unary arithmetic operators (2)
  15. 15. cout << &quot; THE DECREMENT OPERATOR&quot; << endl ; cout << &quot;Value of value: &quot; << value << endl ; cout << &quot;Pre-fix decrement operator (--value): &quot; << --value ; cout << endl ; cout << &quot;Value of value: &quot; << value << endl ; cout << &quot;Post-fix decrement operator (value--): &quot; << value-- ; cout << endl ; cout << &quot;Value of value: &quot; << value << endl ; } Unary arithmetic operators (3)
  16. 16. Assignment operator Assignment operator
  17. 17. Multiple assignment int var_1, var_2, var_3 ; var_1 = 70 ; var_2 = 70 ; var_3 = 70 ; var_1 = var_2 = var_3 = 70 ; int var_1 = int var_2 = int var_3 ;
  18. 18. Relational operators (1) - Comparative action on data - test relationship between variables
  19. 19. Relational operators (2) == Equal to > Greater than < Less than != Not equal to >= Greater than or equal to <= Less than or equal to
  20. 20. Relational operators (3) b == 3 b== c Equal to Greater than b > 3 b > c Less than b < 3 b < c Less than Equal to b <= 3 b <= c Greater than Equal to b >= 3 b>= c Not equal to b!= 3 b!= c
  21. 21. Logical operators (1)
  22. 22. Logical operators (2) qual == 12 && age == 18 qual == 10 || age == 18 ( ! ( age >= 18) )
  23. 23. Precedence and Order of Evaluation
  24. 24. Mixed mode Expressions & Implicit Type conversion X = 5 * 6.7 ; integer float No error Integer converted to double automatically
  25. 25. Conversion table
  26. 26. Type conversion (1) Floating point variables Integer values Value is not changed
  27. 27. Type conversion (2) Floating point variables Integer values Value is changed
  28. 28. Adding integers to characters
  29. 29. Type casting (1) Use explicit type conversions in mixed mode expressions float a ; (int) a ; Often leads to
  30. 30. Type casting (2) float a =10.0, b = 3.0, c ; c = a / b ; Ans= 3.333333 c = (int) a / (int) b OR c = (int) ( a / b ) Ans= 3
  31. 31. C++ Shorthand Operators (1) Help in reducing the code required in programming constructs test_val = test_val + 5; test_val += 5;
  32. 32. C++ Shorthand Operators (2) Every shorthand operator consists of An arithmetic operator +, -, /, *, %
  33. 33. C++ Shorthand Operators (3)

×