几种参数传递问题(读c++ primer plus)

1:用数组做参数传递

#include   <iostream>

#include <cmath>



struct polar
{

double distance;:

double angle;

};
struct rect
{
double x;
double y;
}

void rect_to_polar(const *pxy,polar *pda);
void show_polar(const polar *pda);

int main()
{

rect rplace;
polar pplace;

while (cin >> rplace.x >> rplace.y)
{
rect_to_polar (&rplace,&pplace);
show_polar(&pplace)
}
return 0;
}

void show_polar(const polar* pda)
{
    using namespace std;
    cout <<pda->distance;
    cout << pda->angle;
}

void rect_to_polar (const rect *pxy,polar *pda)

{
using namespace std;
pda->distance =
sqrt(pxy ->x+pda->y*pda->y);
}
调用函数时候,将结构的指针的地址而不是结构本身传递给他,这一点很重要。
3:函数指针:
  1. #include <iostream>
    
  2. 
    
  3. double betsy(int);
    
  4. double pam(int);
    
  5. void estimate (int lines,double (*pf)(int));
    
  6. int main()
    
  7. {
    
  8.     using namespace std;
    
  9.     int code;
    
  10.     cin >> code;
    
  11.     estimate (code,betsy);
    
  12.     estimate(code,pam);
    
  13.     return 0;
    
  14. }
    
  15. double betsy(int lns)
    
  16. {
    
  17.     return 0.05*lns;
    
  18. }
    
  19. double pam (int lns)
    
  20. {
    
  21.     return 0.03*lns + 0.0004*lns*lns;
    
  22. }
    
  23. void estimate (int lines,double(*pf)(int))
    
  24. {
    
  25.     using namespace std;
    
  26.     cout << (*pf)(lines) ;
    
  27. }
    

第二次调用estimate()函数时候传递了函数pam()的地址。也就是*pf指针。pf=pam

posted on 2010-09-28 17:27  sunliming  阅读(389)  评论(1编辑  收藏  举报