# 2：为什么要用函数重载

#include<iostream>
using namespace std;
int abc(int a, int b)
{
int sum;
sum = a + b;
return sum;
}
float abc(float a, float b)
{
float sum;
sum = a + b;
return sum;

}
float abc(float a, int b)
{
float sum;
sum = a + b;
return sum;
}
float abc(int a, float b)
{
float sum;
sum = a + b;
return sum;

}
void main()
{
int a, b;
float c, d;
double sum1,sum2,sum3,sum4;
cout << "请输入a b c d的值" << endl;
cin >> a  >> b  >> c  >> d;
sum1 = abc(a, b);
sum2 = abc(c , d);
sum3 = abc(a, d);
sum4 = abc(b, c);
cout <<"a和b的合为"<< sum1 << endl<<"c和d的合为" << sum2 <<endl<<"a和d的和为"<<sum3<<endl<<"b和c的和为"<<sum4<< endl;
}


# 3：什么是函数的值传递

#include<iostream>
using namespace std;
void abc(int a)
{
a = a + 1;

}
void main()
{
int a = 5;
cout << "带入函数前值为" << a << endl;
abc(a);
cout <<"带入函数后值为"<< a << endl;
}


# 4：什么是地址传递

#include<iostream>
using namespace std;
void abc(int *a)
{
*a = *a + 1;

}
void main()
{
int a = 5;
cout << "带入函数前值为" << a << endl;
abc(&a);//将地址传入
cout <<"带入函数后值为"<< a << endl;
}


# 5.设计实验和教案，分析如何编写递归函数

#include<iostream>
using namespace std;
int fib(int n)
{
if (n == 1 || n == 2)//终止条件
return 1;
else
return fib(n - 1) + fib(n - 2);
}
void main()
{
int a;
cout << "请输入一个大于等于3的整数" << endl;
cin >> a;
int b = fib(a);
cout << "计算所得的fib级数为" << b << endl;
}


posted @ 2019-09-15 09:40  不见风雨不见你  阅读(169)  评论(0编辑  收藏  举报