Friday 3 July 2015

C++ Operators and Data Types

Last time we discussed writing simple C++ programs. Now, to expand, we shall start with the different operators and data types in C++.

Let's start with the basics, naming variables in your program.


Variable Names
  • Correspond to actual locations in computer's memory
  • When new value placed into variable, overwrites previous value: destructive operation
  • Reading variables from memory: nondestructive operation
Every variable has: 

  1. Name - Integer1
  2. Type - int
  3. Value
  4. Size
std::cin >> integer1;        (assume user entered 45)                           
         std::cin >> integer2;        (assume user entered 72)    
         sum = integer1 + integer2;


Fundamental Data Types:





const
You can add the const modifier to the declaration of a variable to tell the compiler that the value cannot be changed:
const double factor = 5.0/9.0;
const double offset = 32.0;
celcius = (fahr - offset)*factor;

The compiler will complain if your code tries to modify a const variable:
const int temp = 100;
temp = 21;

Error: l-value specifies const object

Arithmetic Operators:
•Arithmetic calculations
*
•Multiplication
/
•Division
•Integer division truncates remainder
7 / 5 evaluates to 1
%
•Modulus operator returns remainder
7 % 5 evaluates to 2

•Rules of operator precedence
–Operators in parentheses evaluated first
•Nested/embedded parentheses
–Operators in innermost pair first
–Multiplication, division, modulus applied next
•Operators applied from left to right
Addition, subtraction applied last



Decision Making: Equality and Relational Operators
if structure
–Make decision based on truth or falsity of condition
•If condition met, body executed
•Else, body not executed
•Conditions are made using
–Equality and relational operators
•Relational operators
–Same level of precedence, Associate left to right
•Equality operators
–Same level of precedence, Associate left to right



Precedence and Associativity
•Rules of Precedence control the order of evaluation of operators with different precedence.
–A high precedence means an operator is evaluated (applied) before any lower precedence operators.
•Rules of Associativity control the order of evaluation of operators with same precedence.
‒Operators that have the same precedence are evaluated from left to right mostly but NOT always.

Here's a sample code using what we've learned so far:




Equality (==) and Assignment (=) Operators
Be careful not to mix up the two as they each are different.
•Example
if ( payCode == 4 )
   cout << "You get a bonus!" << endl;
–If paycode is 4, bonus given

•If == was replaced with =
if ( payCode = 4 )
 
cout << "You get a bonus!" << endl;
–Paycode set to 4 (no matter what it was before)
–Statement is true (since 4 is non-zero)
–Bonus given in every case


Expressions

          •C++ expressions are used to express computation.
          •Expression is made of Operators and Operands
          •Operands can be variables or constants.


Unary and Binary Operators
•Unary Operator – one operand: a++
•Binary Operator – two operands: a - b

Division
•Floating point division
–when at least one of the operands is FP
•Integer division
–when both operands are integer
Promotion Heirarchy for C++ Data types 







Type Coercion and Type Casting
•Coercion – automatic conversion of a value from one type to the other
–As happening in mixed type expressions
For Example :   float a=8.6;
  double b;  
  b=a;  
Value of ‘a’ will be automatically promoted from float to double.
•Casting – explicit conversion of a value from one type to the other.
  For Example :
  int a=8;
  double b;
  b=float(a);   ‘b’ will now contain 8.0

About the Author

Unknown

Editorial Team

Post a Comment

 
Codexify © 2015 - Designed by Templateism.com