Author : Eric Brasseur
Page : << Previous 2 Next >>
return r;
else return s;
}
void main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;
biggest (k, m) = 10;
cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;
biggest (k, m) ++;
cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;
}
Again, provided you're used at pointer arithmetics and if you wonder how the program above works, just think the compiler translated it into the following standard C program:
#include <iostream.h>
double *biggest (double *r, double *r)
{
if (*r > *s) return r;
else return s;
}
void main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;
(*(biggest (&k, &m))) = 10;
cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;
(*(biggest (&k, &m))) ++;
cout << "k: " << k << endl;
cout << "m: " << m << endl;
cout << endl;
}
To end with, for people who have to deal with pointers yet do not like it, references are very useful to un-pointer variables:
#include <iostream.h>
double *silly_function () // This function returns a pointer to a double
{
static double r = 342;
return &r;
}
void main()
{
double *a;
a = silly_function();
double &b = *a; // Now b IS the double towards which a points!
b += 1; // Great!
b = b * b; // No need to write *a everywhere!
b += 4;
cout << "Content of *a, b, r: " << b << endl;
}
8
If they contain just simple lines of code, use no for loops or the like, C++ functions can be declared inline. This means their code will be inserted right everywhere the function is used. That's somehow like a macro. Main advantage is the program will be faster. A little drawback is it will be bigger, because the full code of the function was inserted everywhere it is used:
#include <iostream.h>
#include <math.h>
inline double hypothenuse (double a, double b)
{
return sqrt (a * a + b * b);
}
void 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;
}
9
You know the classical structures of C: for, if, do, while, switch... C++ adds one more structure named EXCEPTION:
#include <iostream.h>
#include <math.h>
void main ()
{
int a, b;
cout << "Type a number: ";
cin >> a;
cout << endl;
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;
}
cout << "b contains: " << b << endl;
cout << endl;
// another example of exception use:
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;
}
cout << endl;
}
10
It is possible to define default parameters for functions:
#include <iostream.h>
double test (double a, double b = 7)
{
return a - b;
}
void main ()
{
cout << test (14, 5) << endl;
cout << test (14) << endl;
}
11
One important advantage of C++ is the OPERATOR OVERLOAD. Different functions can have the same name provided something allows to distinguish between them: number of parameters, type of parameters...
#include <iostream.h>
double test (double a, double b)
{
return a + b;
}
int test (int a, int b)
{
return a - b;
}
void main ()
{
double m = 7, n = 4;
int k = 5, p = 3;
cout << test(m, n) << " , " << test(k, p) << endl;
}
12
The operators overload can be used to define the basic symbolic operators for new sorts of parameters:
#include <iostream.h>
struct vector
{
double x;
double y;
};
vector operator * (double a, vector b)
{
vector r;
r.x = a * b.x;
r.y = a * b.y;
return r;
}
void main ()
{
vector k, m; // No need to type "struct vector"
k.x = 2; // Keep cool, soon you'll be able
k.y = -1; // to write k = vector (45, -4).
m = 3.1415927 * k; // Magic!
cout << "(" << m.x << ", " << m.y << ")" << endl;
}
Besides multiplication, 43 other basic C++ operators can be overloaded, including +=, ++, the array [], and so on...
The operation cout << is an overload of the binary shift of integers. That way the << operator is used a completely different way. It is possible to overload the << operator for the output of vectors:
#include <iostream.h>
struct vector
{
double x;
double y;
};
ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")";
return o;
}
void main ()
{
vector a;
a.x = 35;
a.y = 23;
cout << a << endl;
}
13
Tired of defining five times the same function? One definition for int type parameters, one definition for double type parameters, one definition for float type parameters... Didn't you forget one type? What if a new data type is used? No problem: the C++ compiler is capable of generating automatically every version of the function that is necessary! Just tell him how the function looks like by declaring a template function:
#include <iostream.h>
template <class ttype>
ttype min (ttype a, ttype b)
{
ttype r;
r = a;
if (b < a) r = b;
return r;
}
void main ()
{
int i1, i2, i3;
i1 = 34;
i2 = 6;
i3 = min (i1, i2);
cout << "Most little: " << i3 << endl;
double d1, d2, d3;
d1 = 7.9;
d2 = 32.1;
d3 = min (d1, d2);
cout << "Most little: " << d3 << endl;
cout << "Most little: " << min (d3, 3.5) << endl;
}
The function min is used three times in above program yet the C++ compiler generates only two versions of it: int min (int a, int b) and
Page : << Previous 2 Next >>