Sunday, June 29, 2014

Operators in C++


The numerical operators in C++ can be grouped into five types: arithmetic, assignment, comparison, logical and bitwise operators.
1. Arithmetic operators
Example
x = 4 + 5;   // x=9 // addition
x = 13 - 3;   // x=10 // subtraction
x = 6 * 3;   // x=18 // multiplication
x = 10 / 3; // x=3 // division
x = 10 % 3; // x=1 // modulus (division remainder)

2. Assignment operators
a. Assign a value to a variable
x=10;
b. Assign a variable to a variable
int a=10, b;
b=a;
c. Assign a expression to a variable
int a, b=5, c=6;
a=b+d;

3. Combined assignment operators
Example
int x=10;
x += 5;  // x = x+5  // x=15;
x -= 4;  // x = x-4;  //x=6;
x *= 3; // x = x*3;  //x=30;
x /= 2; // x = x/2;  //x=5;
x %= 3; // x = x%3; //x=1;

4. Operators to Increment (++) and Decrement (--)
Example
int x=6;
x++;   // x = x+1;  //x=7; 
x--;    // x = x-1;   //x=5;
Both of these can be used either before or after a variable.
x++;   // post-increment
x--;     // post-decrement
++x;   // pre-increment
--x;     // pre-decrement
The result on the variable is the same whichever is used. The difference is that the 
post-operator returns the original value before it changes the variable, while the 
pre-operator changes the variable first and then returns the value.
x = 5; y = x++; // y=5, x=6
x = 5; y = ++x; // y=6, x=6

5. Comparison operators
The comparison operators compare two values and return either true or false. They are mainly used to specify conditions, which are expressions that evaluate to either true or false.
Example:
bool x = (2 == 3); // false // equal to
x = (2 != 3); // true // not equal to
x = (2 > 3); // false // greater than
x = (2 < 3); // true // less than
x = (2 >= 3); // false // greater than or equal to

x = (2 <= 3); // true // less than or equal to

6. Logical operators
bool x = (true && false);  // false // logical and
x = (true || false);  // true // logical or

x = !(true);  // false // logical not

7. Bitwise operators
int x = 5 & 4; // 101 & 100 = 100 (4) // and
x = 5 | 4; // 101 | 100 = 101 (5) // or
x = 5 ^ 4; // 101 ^ 100 = 001 (1) // xor
x = 4 << 1; // 100 << 1 =1000 (8) // left shift
x = 4 >> 1; // 100 >> 1 = 10 (2) // right shift
x = ~4; // ~00000100 = 11111011 (-5) // invert
The bitwise operators also have combined assignment operators.
int x=5; x &= 4; // 101 & 100 = 100 (4) // and
x=5; x |= 4; // 101 | 100 = 101 (5) // or
x=5; x ^= 4; // 101 ^ 100 = 001 (1) // xor
x=5; x <<= 1;// 101 << 1 =1010 (10)// left shift

x=5; x >>= 1;// 101 >> 1 = 10 (2) // right shift

Wednesday, June 11, 2014

Declare Constant and Use Constant in C++

Syntax:
#define <constant_name> <value> 

or 
const <data_type> <constant_name> = <value>

Declare:
#define MAX 100 
#define PI 3.14 
#define Newline '\n'

const int MAX = 100;
const float PI = 3.14;
const char answer = ‘Y’;

Escape Sequences
Escape Sequences      Represents
\a                                     Bell (alert)
\b                                     Backspace
\f                                      Formfeed
\n                                     New line
\r                                      Carriage return
\t                                      Horizontal tab
\v                                     Vertical tab
\'                                      Single quotation mark
\"                                     Double quotation mark
\\                                      Backslash
\?                                     Literal question mark

Example 1: Calculate Area, Circumference of Circle using const keyword 

#include <iostream>
#include <conio.h>
using namespace std;
const double Pi=3.14;
double Radius, Area, Circumference;
int main(){
cout<<"Input Radius of Circle: "; cin>>Radius;
Area = Pi * Radius * Radius;
Circumference = 2 * Pi * Radius;
cout<<"Area of Circle = "<<Area<<endl;
cout<<"Circumference of Circle = "<<Circumference;
getch();
return 0;
}

Example 2: Calculate Circle using #define keyword 
#include <iostream>
#include <conio.h>
using namespace std;
#define PI 3.14159
#define NEWLINE '\n'
int main(){
double radius=7.0;       
  double circle;   
  circle = 2 * PI * radius;
  cout <<"CIRCLE: " <<circle;
  cout << NEWLINE;
getch();
return 0;
}

Tuesday, June 10, 2014

List Keywords You Can't Use as Variable or Constant Names

Some words are reserved by C++, and you can't use them as variable names or constant names. These keywords have special meaning to the C++ compiler.
asm      else          new                       this
auto     enum        operator                throw
bool     explicit     private                   true
break   export      protected               try
case     extern       public                    typedef
catch    false         register                  typeid
char     float          reinterpret_cast     typename
class    for            return                    union
const   friend        short                     unsigned
goto    signed       const_cast             using
 if        sizeof        virtual                    continue
void    default       inline                     static 
delete  int             static_cast             volatile
do       long          struct                     wchar_t
double mutable    switch                    while
dynamic_cast       namespace            template

Monday, June 9, 2014

Examples: How to use variables in C++


Example 1: Integer Variables
Syntax: int variable;       //comment

#include <iostream>
using namespace std;
int term;  // term used in two expressions
int main(){
term = 4 * 7; 
cout <<"Twice " << term << " is "<< 2*term <<endl; 
cout <<"Three times " << term << " is " << 3*term <<endl; 
return 0;
}

Example 2: Floating Point Variables
Syntax:  float variable;     //comment

#include <iostream>
#include <conio.h>
using namespace std;
int i;    // an integer variable
float f; // a floating point number 
int main(){
f = 1.0 / 2.0;          // assign floating 0.5 
cout<<" f= "<<f<<endl;
i = 1 / 3;              // assign integer 0 
cout<<" i= "<<i<<endl;
f = (1 / 2) + (1 / 2);  // assign floating 0.0 
cout<<" f= "<<f<<endl;
f = 3.0 / 2.0;          // assign floating 1.5 
cout<<" f= "<<f<<endl;
i = f;                  // assign integer 1 
cout<<" i= "<<i<<endl;
return 0;
}

Example 3: Characters Variables
Syntax:  char variable;   //comment

#include <iostream>
#include <conio.h>
using namespace std;
char c1;  // first character 
char c2;  // second character 
char c3;  // third character 
int main(){
c1 = 'X'; 
c2 = 'Y'; 
c3 = 'Z'; 
cout << c1 << c2 << c3 << " reversed is "<<c3 << c2 << c1 << "\n";
return 0;
}

Friday, June 6, 2014

Variable and Using Variable in C++

Declaring Variables to Access and Use Memory
When programming in languages like C++, you define variables to store those values. Defining a variable is quite simple and follows this pattern:
       variable_type variable_name;
               or
       variable_type variable_name = initial_value;

Example:
     int i;   // variable_type: int;  variable_name: i;
     float x=1.25;  //variable_type: float;  variable_name: x; initial_value: 1.25

Declaring and Initializing Multiple Variables of a Type
You could condense the declaration of these multiple variables to one line of code that would

look like this:


Variable Types in C++:

Using Type bool to Store Boolean Values
A sample declaration of an initialized Boolean variable is
     bool male = true;
An expression that evaluates to a Boolean type is

     bool shopping = (UserSelection == “no”);

Using Type char to Store Character Values
     char User_Input = 'A';     // initialized char to ‘A’

Using Type short, int, long to Store Signed Integer Values
     short Age = 22;
     int Number = -70000;
     long LargeNumber = -70000;    //on some platforms, long is an int

     
Using Type float, double to Store Floating-Point Values
     float PI=3.14;
     double x=a/b;

Thursday, June 5, 2014

Writting Your First C++ Application (Hello Word !)

Create a new project and write a basic program "Hello World"
Now that you know the tools and the steps involved, it is time to program your first C++ application. If you are on Windows and using Microsoft Visual C++ Express, you can follow these steps:
1. Create a new project via the menu option File -> New -> Project.

2. Choose type Win32 Console Application.

3.  And uncheck the Use Precompiled Header option.

4. Name your project Hello and replace the automatically generated contents in Hello.cpp with the code snippet:

Building and Executing Your First C++ Application
If you’re using Microsoft Visual C++ Express, press Ctrl + F5 to run your program directly via the IDE. This compiles, links, and executes your application. 


You could also do the individual steps: 
1. Right-click the project and select Build to generate the executable.
2. Navigate to the path of the executable using the command-prompt (typically under the Debug directory of the project folder).
3. Run it by typing the name of the executable.