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;
char *ptr;
double t[10][20];
char *getenv(const char *var)
{
return ... ;
}
struct link
{
int val;
link *next;
};
class Date
{
int year;
int month;
int day;
public:
Date( int y, int m, int d)
{
setDate( y, m, d);
}
int getYear() const;
int getMonth() const;
int getDay() const;
void setDate( int y, int m, int d);
};
Date d(2004,3,10);
static int si;
const int ci = 10;
extern const int eci = 20;
Declarations:
extern int i;
extern char *ptr;
extern double t[][20];
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;
int *ip;
const int ci = 10;
int t[ci];
int f(int par);
int &refi = i;
ip = &i;
t[i] = f(*ip);
++refi;
int* ip1, ip2;
int **ipp;
int tt[10][20];
ipp = &ip;
*ipp = ip;
**ipp = 5;
t[i][j] = 5;
int *t[10];
int *t1 [10]
int (*t2)[10]
double *f1 (double);
double (*f2)(double);
Pointers to functions
=====================
extern double sin(double);
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;
return 0;
}
Converting vowels
=================
a -> e
e -> i
i -> o
o -> u
u -> a
#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;
}
#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;
}
}
#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;
}
#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;
}
#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;
}
#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;
}
#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'}};
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;
}
#include <iostream>
using namespace std ;
char conv(char);
struct conv_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;
}