, . :
void f1 (int a, double b, );
, a b , .
.
1. , :
va_ list va_ start ;
va_ arg ;
va_ end .
stdarg. h.
2. () .
3. va_ start , va_ list .
4. . .
5. va_ arg . .
. , , .
.
#include <stdarg.h>
#include <stdio.h>
void f1(double s, int n...) {
int v;
va_list p;
va_start(p, n);
printf(" \n Double S = %lf ", s);
for(int i=1; i<=n; i++) {
v = va_arg(p, int);
printf("\n Argument %d = %d ", i, v);
}
va_end(p);
}
void main(void) {
f1(1.5, 3, 4, 5, 6);
}
:
Double S = 1.500000
Argument 1 = 4
Argument 2 = 5
Argument 3 = 6
Press any key to continue
.
, . , . , .
.
.
: , .
|
|
.
1. :
int Max(int x, int y) {
return (x>y)? x: y;
}
double Max(double x, double y) {
return(x>y)? x: y;
}
...
int i, j, k;
k = Max(i, j); //
double a, b, d;
d = Max(a, b); //
...
, .
2. :
#define Max(x, y) ((x>y)? x: y)
:
, .. ;
, Max;
, . (template), ( ).
, , , , .
:
template
;
. class . , .
( ) . .
:
1. :
template <class T>
void f1(T par) {
f1
}
T , . T .
, .
2. :
template <class T>
void f2(T par, int x, int y) {
f2
}
3. :
template <class T2, class T1>
void f3(T1 par1, T2 par2) {
f3
}
ID .
, .. , . . .
:
#include <iostream.h>
template <class T>
T max(T x, T y) {
return(x>y)? x: y;
|
|
}
void main()
{
int i = 2, j = 3;
double x = 1.5, y = 2.5;
cout << I = <<i<< J = <<j<< Max = <<max(i, j)<<endl;
cout << X = <<x<< Y = <<y<< Max = <<max(x,y)<<endl;
}
:
i = 2 j = 3 Max = 3
X = 1.5 y = 2.5 Max = 2.5
, .