《C++入门经典》读书笔记+习题解答 第9章 函数

读书笔记:

9.1 函数的重载(使用同一函数名的不同函数)需要满足以下条件之一:

  (1)每个函数的参数个数不同;

  (2)参数个数相同,但至少有一对对应参数的类型不同;

  Tips:重载函数不能仅通过返回值类型来区分。


9.2 编译器对重载函数的使用:

  (1)参数没有对应匹配的,自动转向更高一级的类型(例如float -> double);

  (2)参数没有对应匹配的,有2个更高的类型均满足时,需要用户通过强制转换来确定到底使用哪个版本(例如int -> long , int -> double)。


9.3 const仅能用于定义函数签名时,区分是为引用定义参数还是为指针定义参数 P283.


9.4 函数模板是一种自动生成重载函数的方法:

  (1)格式:template<class T>T large(T a, T b)

  (2)在必要时,采用显式模板定义或者通过模板说明(特殊模板)来避免错误。


9.5 函数指针

  (1)函数指针存储了函数的地址,以及参数类型和个数、返回值信息等;

  (2)格式:返回类型(*指针名)(参数类型列表)例:long (*pfun)(long*, int);

      double (*pfun[3])(double, double) = {sum, prodect, difference};

  (3)可以使用函数指针来调用包含的地址上的函数,还可以把函数指针作为函数参数来传送。


9.6 递归函数

  (1)递归函数是调用自身的函数;

  (2)采用递归方式实现的算法可以得到非常简明的代码,但与实现同一算法的其他方法相比,采用递归常常需要更多的时间


 习题参考解答(自编)

1. code:9-1

#include <iostream>
#include <string>

//using namespace std;
using std::cout;
using std::endl;
using std::string;

int plus(int a, int b)
{
	return a + b;
}
double plus(double a, double b)
{
	return a + b;
}
string plus(const string& a, const string& b)
{
	return a + b;
}
int main()
{
	//for test
	int n = plus(3, 4);
	cout<< n <<endl;
	
	double d = plus(3.2, 4.2);
	cout<< d <<endl;
	
	string s = plus("he", "llo");
	cout<< s <<endl;
	
	string s1 = "aaa"; string s2 = "bbb";
	string s3 = plus(s1, s2);
	cout<< s3 <<endl;
	
	//d = plus(3, 4.2);
	return 0;
} 
  String版本传递参数最有效的方式:传引用;

  下面为什么不工作:参数包含int项和double项,编译器无法选择重载函数是(int, int)或者是(double, double)


2. code:9-2

#include <iostream>
#include <string>

using std::cout;
using std::endl; 
using std::string;

template <class T>T plus(T a, T b){
	return a + b;
}

int main()
{
	//for test 
	int n = plus(3, 4);
	cout<< n <<endl;
	
	double d = plus(3.2, 4.2);
	cout<< d <<endl;
	
	string s = plus(static_cast<string>("he"), static_cast<string>("llo"));
	cout<< s <<endl;
	return 0;
}

  不能,编译器自动识别为char*类型,从而不能进行”+“操作

  解决方法:强制转换成string,则编译器生成一个对应string类型的重载函数


3. code:9-3

#include <iostream>
#include <cmath>

using std::cout;
using std::endl;

const double pi = 3.1415926;
const double degs = 180;
double calc(double a, double (*p)(double))
{
	return p(a);
}
int main()
{
	//for test
	double degree = 30;
	cout<< calc(pi*degree/degs, sin) <<endl;
	cout<< calc(pi*degree/degs, cos) <<endl;
	cout<< calc(pi*degree/degs, tan) <<endl;
	
	for(degree = 0; degree <= degs; degree++)
	{
		double temp = pi*degree/degs;
		if(calc(temp, sin) != sin(temp) ||
		   calc(temp, cos) != cos(temp) ||
		   calc(temp, tan) != tan(temp))
		   break;
	}
	if(degree > degs)
	{
		cout<< "\nPass\n" <<endl;
		static double (*save_func[3])(double) = {sin, cos, tan};
		
		cout<< "Test again" <<endl;
		degree = 30;
	    cout<< save_func[0](pi*degree/degs) <<endl;
    	    cout<< save_func[1](pi*degree/degs) <<endl;
    	    cout<< save_func[2](pi*degree/degs) <<endl;
	}
	return 0;
}

4. code:9-4

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int ack(int m, int n)
{
	if(m == 0)
	return n + 1;
	else if(n == 0 && m > 0)
	return ack(m-1, 1);
	else if(n > 0 && m > 0)
	return ack(m-1, ack(m, n-1));
}
int main()
{
	int a,b;
	cout<<"input m and n\n";
	cin>> a >> b;
	cout<< ack(a, b) <<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值