.


:




:

































 

 

 

 


(-)




() . , . , , .

 

const int N=20;//

char mas1[N][10];//

int mas2[N]//

void init(int i, char* name= , int age=17)

{

strcpy(mas1[i],name);

mas2[i]=age;

}

 

init():

1. for (int i=1;i<N;i++)

init(i);

mas1 , mas2 17.

2. for (int i=1;i<N;i++)

{

char Name[10];

cout<< :; cin>>Name;

init(I,Name);

}

mas1 Name, mas2 17.

++ , , , , . , . . :

 

( ,...)

{

}

 

, , , . , . , p=&k, p - , k , : k++; . , . :

1) , ;

2) .

 

//

//,

#include <iostream.h>

float sum(int k,...)

// k

{

int *p=&k;// k

int s=0;

for(;k!=0;k--)

s+=*(++p);

return s/k;

}

void main()

{

// 4+6

cout<<\n4+6=<<sum(2,4,6);

// 1+2+3+4

cout<<\n1+2++3+4=<<sum(4,1,2,3,4);

}

 

*p int. , (++p).

 

/* , */

#include<iostream.h>

int sum(int k, )

{

int *p = &k; // k

int s = *p; // s

for(int i=1;p!=0;i++) //

s += *(++p);

return s/(i-1);

}

 

void main()

{

// 4+6

cout<<\n4+6=<<sum(4,6,0);

// 1+2+3+4

cout<<\n1+2++3+4=<<sum(1,2,3,4,0);

}

, - . , . , .

 

#include <iostream.h>

#include <string.h>

 

//

int max(int a, int b)

{

if (a>b) return a;

else return b;

}

 

//

float max(float a, float b)

{

if(a>b)return a;

else return b;

}

 

//

char* max(char* a, char* b)

{

if (strcmp(a,b)>0) return a;

else return b;

}

 

void main()

{

int a1,b1;

float a2, b2;

char s1[20];

char s2[20];

 

cout<<"\nfor int:\n";

cout<<"a=?";cin>>a1;

cout<<"b=?";cin>>b1;

cout<<"\nMAX="<<max(a1,b1)<<"\n";

 

cout<<"\nfor float:\n";

cout<<"a=?";cin>>a2;

cout<<"b=?";cin>>b2;

cout<<"\nMAX="<<max(a2,b2)<<"\n";

 

cout<<"\nfor char*:\n";

cout<<"a=?";cin>>s1;

cout<<"b=?";cin>>s2;

cout<<"\nMAX="<<max(s1,s2)<<"\n";

}

 

:

  • .
  • , . .
  • , const : int& f1(int&, const int&){} int f1(int, int){} , . . , , , .

, , . , . . , , . . .

 

template <class _ [,class _]>

_

{

}

 

, 2 : template< > , / , , .

 

//

template<class T>

T max(T a, T b)

{

if (a>b) return a;

else return b;

}

 

, . , max(1,5), int max(int a, int b){}.

, . , , . . .

:

_(*_)( )

, .

:

(*_)( );

 

#include <iostream.h>

int f1(char* S)

{

cout<<S<<"\n";

return 1;

}

 

void main()

{

char s[20]="\nfunction1";

int(*ptr1)(char*); //

ptr1=f1; // f1

cout<<((*ptr1)(s)); // f1 c

}

, .

 

#include <iostream.h>

#include <math.h>

 

typedef float(*fptr)(float);//-

 

/* , */

float root(fptr f, float a, float b, float e)

{

float x;

do

{

x=(a+b)/2; //

if ((*f)(a)*f(x)<0) //

b=x;

else a=x;

}

while((*f)(x)>e && fabs(a-b)>e);

return x;

}

 

//,

float testf(float x)

{

return x*x-1;

}

 

void main()

{

/* root , */

float res=root(testf,0,2,0.0001);

cout<<\nX=<<res;

}





:


: 2016-12-06; !; : 409 |


:

:

- - , .
==> ...

1863 - | 1816 -


© 2015-2024 lektsii.org - -

: 0.023 .