Declarations and definitions
============================


Declaration: tell the compiler that what to think about X

    For variables:
        tell the compiler, that variables with type X exist somewhere

    For functions:
        tell teh signaure (constness is part of)

    For classes:
        tell the classname ("forward declaration")


Definition: tell the compiler what is X in details

    For variables:
        tell the type and visibility and
        allocate the memory for that object

    For functions (incl. member-functions):
        describe the parameters, exact algorithm and return value

    For types/classes:
        describe the data structure, types and the methods





Definitions:


int    i;       // integer variable

char  *ptr;     // pointer to char

double t[10][20];  // array of 10 elements of arrays of 20 ints

char  *getenv(const char *var)  // function signature
{
    return ... ; // function body
}

struct link
{
  int   val;  // data member
  link *next; // self-reference: only for pointers
};

class Date      // data type (record)
{         // default is private
  int  year;    // field1: int
  int  month;   // field2: int
  int  day;     // field3: int
public:   // from here: public
       Date( int y, int m, int d) // constructor
       {
         setDate( y, m, d); // in-line function
       }
  int  getYear() const;  // const member-function
  int  getMonth() const; // const member-function
  int  getDay() const;   // const member-function
  void setDate( int y, int m, int d); // non-const member-function
};


Date d(2004,3,10);  // a variable called d of type Date 
                    // with constructor parameters

static int si;

const int ci = 10;
extern const int eci = 20;



Declarations:


extern int    i;

extern char  *ptr;

extern double t[][20];

/* extern */ char  *getenv(const char* var);

struct link;
class Date;

extern Date d;

extern const eci;




Forward declaration:


struct Current;

struct Other
{
    Current *cp;
};

struct Current
{
    Other *op;
};




Elementary type constructions
=============================


int   i;           // i is variable of type int
int  *ip;          // ip is pointer variable to int
const int ci = 10; // ci is const int 
int   t[ci];       // t is array of 10 ints 
int   f(int par);  // f is function returning int, and with int parameter
int  &refi = i;    // refi is a reference (synonim) of i


ip = &i;

t[i] = f(*ip);

++refi;


// Be care with one-line declarations!!
int*   ip1, ip2;   // ip1 is pointer, ip2 is int




int **ipp;         // pointer to pointer

int tt[10][20];    // array of arrays



     ipp = &ip;
    *ipp = ip;
   **ipp = 5;

 t[i][j] = 5;





    int *t[10];      // ???



int  *t1 [10]     // array of 10 pointers
int (*t2)[10]     // pointer to 10 element array



double  *f1 (double);   // function, returning double*
double (*f2)(double);   // pointer to double(double) function





Pointers to functions
=====================



extern double sin(double);      // declared in <cmath> header
extern double cos(double);      //


int main()
{
  char   ch;
  double x

  std::cin >> ch >> x;

  double (*fp)(double);

  fp = ('s' == ch ) ? sin : cos;

  std::cout << (*fp)(x) << std::endl;
  std::cout <<   fp (x) << std:.endl;   // ekvivalet with (*fp)(x)

  return 0;
}




Converting vowels
=================



        a -> e
        e -> i
        i -> o
        o -> u
        u -> a







/*
 * Converting vowels 
 * 0. version
 */

#include <iostream>

using namespace std ;


char conv(char);

int main()
{
    char ch;
    cin >> noskipws;

    while( cin >> ch )
    {
        cout << conv(ch);
    }
    return 0;
}

char conv(char ch)
{
    if ( ch == 'a' )
        return 'e';
    else if ( ch == 'e' )
        return 'i';
    else if ( ch == 'i' )
        return 'o';
    else if ( ch == 'o' )
        return 'u';
    else if ( ch == 'u' )
        return 'a';
    else
        return ch;
}






/*
 * Converting vowels 
 * 0a. version
 */

#include <iostream>

using namespace std ;


char conv(char);

int main()
{
    char ch;
    cin >> noskipws;

    while( cin >> ch )
    {
        cout << conv(ch);
    }
    return 0;
}

char conv(char ch)
{
    switch( ch )
    {
     default: return ch;
    case 'a': return 'e'; break;
    case 'e': return 'i'; break;
    case 'i': return 'o'; break;
    case 'o': return 'u'; break;
    case 'u': return 'a'; break;
    }
}







/*
 * Converting vowels 
 * 1. version
 */

#include <iostream>

using namespace std ;

const char from[] = {'a','e','i','o','u'};
const char to[]   = {'u','a','e','i','o'};

char conv(char);

int main()
{
    char ch;
    cin >> noskipws;

    while( cin >> ch )
    {
        cout << conv(ch);
    }
    return 0;
}

char conv(char ch)
{
    for ( int i = 0; i < 5; ++i)
    {
        if ( ch == from[i] )
        {
            return to[i];
        }
    }
    return ch;
}






/*
 * Converting vowels 
 * 2. version
 */

#include <iostream>

using namespace std ;


char conv(char);

int main()
{
    char ch;
    cin >> noskipws;

    while( cin >> ch )
    {
        cout << conv(ch);
    }
    return 0;
}

char conv(char ch)
{
    static const char from[] = {'a','e','i','o','u'};
    static const char to[]   = {'u','a','e','i','o'};

    for ( int i = 0; i < 5; ++i)
    {
        if ( ch == from[i] )
        {
            return to[i];
        }
    }
    return ch;
}








/*
 * Converting vowels 
 * 2/a. version
 */

#include <iostream>

using namespace std ;


char conv(char);

int main()
{
    char ch;
    cin >> noskipws;

    while( cin >> ch )
    {
        cout << conv(ch);
    }
    return 0;
}

char conv(char ch)
{
    static const char from[] = {'a','e','i','o','u'};
    static const char to[]   = {'u','a','e','i','o'};

    const int size = sizeof(from)/sizeof(from[0]);

    for ( int i = 0; i < size; ++i)
    {
        if ( ch == from[i] )
        {
            return to[i];
        }
    }
    return ch;
}









/*
 * Converting vowels 
 * 3. version
 */

#include <iostream>

using namespace std ;


char conv(char);

struct conv_t
{
    char from;
    char to;
};

int main()
{
    char ch;
    cin >> noskipws;

    while( cin >> ch )
    {
        cout << conv(ch);
    }
    return 0;
}

char conv(char ch)
{
    static const conv_t t[] =
              {{'a','e'}, {'e','i'}, {'i','o'}, {'o','u'}, {'u','a'}};

    const int size = sizeof(t)/sizeof(t[0]);

    for ( int i = 0; i < size; ++i)
    {
        if ( ch == t[i].from )
        {
            return t[i].to;
        }
    }
    return ch;
}







/*
 * Converting vowels 
 * 3a. version
 */

#include <iostream>

using namespace std ;


char conv(char);

struct conv_t
{
    char from;
    char to;
};

int main()
{
    char ch;
    cin >> noskipws;

    while( cin >> ch )
    {
        cout << conv(ch);
    }
    return 0;
}

char conv(char ch)
{
    static const conv_t t[] =
              {{'a','e'}, {'e','i'}, {'i','o'}, {'o','u'}, {'u'}}; //{'u','a'}

    const int size = sizeof(t)/sizeof(t[0]);

    for ( int i = 0; i < size; ++i)
    {
        if ( ch == t[i].from )
        {
            return t[i].to;
        }
    }
    return ch;
}







/*
 * Converting vowels 
 * 4. version
 */

#include <iostream>

using namespace std ;


char conv(char);

struct conv_t
{
    conv_t( char f, char t) : from(f), to(t) { }
    // conv_t( char f, char t) { from = f; to = t; }
    char from;
    char to;
};

int main()
{
    char ch;
    cin >> noskipws;

    while( cin >> ch )
    {
        cout << conv(ch);
    }
    return 0;
}

char conv(char ch)
{
    static const conv_t t[] =
              {{'a','e'}, {'e','i'}, {'i','o'}, {'o','u'}, {'u','a'}};

    const int size = sizeof(t)/sizeof(t[0]);

    for ( int i = 0; i < size; ++i)
    {
        if ( ch == t[i].from )
        {
            return t[i].to;
        }
    }
    return ch;
}