C++

Main reference was here.





using namespace std;
#include <iostream>        // This is a key C++ library
#include <cmath>           // The standard C library math.h

int main ()
{
   double a;

   a = 1.2;
   a = sin (a);

   cout << a << endl;

   char s [100];  
   cout << "Type your name: ";
   cin >> s;

   cout << endl;

   cout << "Hello " << s;

   return 0;
}

To execute:
$ g++ test01.cpp -o test01
$ ./test01




SAMPLE 2.




#include<stdio.h>
 #include <iostream>   // one of two
 using namespace std;    // two of two, yay!
 int main(void)
 {



int i = 0,j,k;
cout<<i;
{
    int i = 9;
    cout<<i;
}
i=j=k=6;

printf("\n");
if(!(i - -100>78 && 5<=9)){
    printf("the C++ program is working%i",i);
printf("\n");
}

   cout << "A";
printf("\n");
   return 0;
 }



TO RUN
    $ g++ a.c
    $ ./a.out













double &b = a;         // b is a ===> double &b = a is translated to double *b = &a and all subsequent b are replaced by *b
The value of REFERENCE b cannot be changed after its declaration. For example you cannot write, a few lines further, &b = c expecting that b is now c. It won't work. Everything is said on the declaration line of b. Reference b and variable a are married on that line and nothing will separate them!

References can be used to allow a function to modify a calling variable:


 using namespace std;
#include <iostream>
void change (double &r, double s)
{
   r = 100;
   s = 200;
}

int main ()
{
   double k, m;

   k = 3;
   m = 4;

   change (k, m);

   cout << k << ", " << m << endl;        // Displays 100, 4.

   return 0;
}
//// output: 100, 4
 This actually happens

using namespace std;
#include <iostream>

void change (double *r, double s)
{
   *r = 100;
   s = 200;
}

int main ()
{
   double k, m;

   k = 3;
   m = 4;

   change (&k, m);

   cout << k << ", " << m << endl;        // Displays 100, 4.

   return 0;
}


Function return value as a reference
using namespace std;
#include <iostream>

double &biggest (double &r, double &s)
{
if (r > s) return r;
else return s;
}

int main ()
{
double k = 3;
double m = 7;

cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;

biggest (k, m) = 10;

cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10
cout << endl;

biggest (k, m) ++;

cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11
cout << endl;

return 0;
} ////output 3,7 -- 3,10 -- 3, 11

Namespaces
using namespace std;
#include <iostream>
#include <cmath>

namespace first
{
   int a;
   int b;
}

namespace second
{
   double a;
   double b;
}

int main ()
{
   first::a = 2;
   first::b = 5;

   second::a = 6.453;
   second::b = 4.1e4;

   cout << first::a + second::a << endl;
   cout << first::b + second::b << endl;

   return 0;
}


Inline (macro) -- both couts are equal after function inlination
using namespace std;
#include <iostream>
#include <cmath>

inline double hypothenuse (double a, double b)
{
return sqrt (a * a + b * b);
}

int main ()
{
double k = 6, m = 9;

// Next two lines produce exactly the same code:

cout << hypothenuse (k, m) << endl;
cout << sqrt (k * k + m * m) << endl;

return 0;
}
Exception control structure
try
   {
      if (a > 100) throw 100;
      if (a < 10)  throw 10;
      throw a / 3;
   }
   catch (int result)
   {
      cout << "Result is: " << result << endl;
      b = result + 1;
   }


/////////OR
  char zero []     = "zero";
   char pair []     = "pair";
   char notprime [] = "not prime";
   char prime []    = "prime";

   try
   {
      if (a == 0) throw zero;
      if ((a / 2) * 2 == a) throw pair;
      for (int i = 3; i <= sqrt (a); i++)
      {
         if ((a / i) * i == a) throw notprime;
      }
      throw prime;
   }
   catch (char *conclusion)
   {
      cout << "The number you typed is "<< conclusion << endl;
   }



////////////////////////////////////////////////////////=============================================================
macro
First, remember that the exact text of the macro argument is "pasted in" to the macro. surround the whole macro body with parentheses, SURROUND PARAMETERS WITH PARENTHESIS. Surround multi-line macros in parenthesis to avoid problems if it is used after an if statement without curly braces.

#define MULT(x, y) x * y
int z = MULT(3 + 2, 4 + 2);
int z = 3 + 2 * 4 + 2;    // 2 * 4 will be evaluated first!
TO AVOID #define MULT(x, y) (x) * (y)

#define ADD_FIVE(a) (a) + 5
int x = ADD_FIVE(3) * 3; // this expands to (3) + 5 * 3, so 5 * 3 is evaluated first
TO AVOID
#define ADD_FIVE(a) ((a) + 5)
int x = ADD_FIVE(3) * 3;
Other issues:
#define SWAP(a, b)  { a ^= b; b ^= a; a ^= b; }

int x = 10;
int y = 5;
int z = 4;

// What happens now?
if(x < 0)
    SWAP(x, y);
else
    SWAP(x, z);
The semicolon after swap in if is compiling problem! To overcome do while!
#define SWAP(a, b)  do { a ^= b; b ^= a; a ^= b; } while ( 0 )

int x = 10;
int y = 5;
int z = 4;

// What happens now?
if(x < 0)
    SWAP(x, y);
else
    SWAP(x, z);
Perhaps the most irritating problem with macros is that you don't want to pass arguments with "side effects" to macros. The problem with side effects is that macros don't evaluate their arguments; they just paste them into the macro text when performing the substitution.
It turns out that by using a the "\" to indicate a line continuation
#define SWAP(a, b)  {                   \
                        a ^= b;         \
                        b ^= a;         \
                        a ^= b;         \
                    }
#define
The #define directive takes two forms:
defining a constant
#define token [value]
creating a macro.
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Note that when writing macros there are a lot of small gotchas; you can read more about it here: the c preprocessor . To define a multiline macro, each line before the last should end with a \, which will result in a line continuation.
















Comments