第六章 习题6-51-习题6-60

习题6-51

#include <iostream>
#include<vector>
using namespace std;
void f()
{
	cout << "I LOVE YOU" << endl;
}
void f(int a)
{
	cout << " I  Love " << a<<endl;
}
void f(int a, int b)
{
	cout << a << " Love" << b << endl;
}
void f(double a, double b)
{
	cout << a << "hate" << b << endl;
}
void main()
{
	//f(2.56, 42);    //第一个会报错,有多个重载函数;
	f(42);
	f(42, 0);
	f(2.56, 3.14);
}

习题6-52

(a)类型提升;

(b)算数类型转换;

习题6-53

(c)不合法,重复定义,P208页重载和const形参:“一个拥有顶层const的形参无法和另一个没有顶层const的形参区分开来”;

习题6-54

using F=int(*)(int,int)
vector<F> V1;

using F=int(int,int)
vector<F*> V1;

习题6-55-习题6-56

#include <iostream>
#include<vector>
using namespace std;
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return (a - b); }
int multiply(int a, int b) { return a*b; }
int divide(int a, int b) { return b != 0 ? a / b : 0; }
void main()
{
	typedef int(*p) (int, int);
	vector<p> vec{ add, sub,multiply, divide };
	for (auto f : vec) {
		cout << f(2, 4) << endl;
	}
}

#include <iostream>
#include<vector>
using namespace std;
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return (a - b); }
int multiply(int a, int b) { return a*b; }
int divide(int a, int b) { return b != 0 ? a / b : 0; }
void main()
{
    using p= int(*)(int, int);
	vector<p> vec = { &add,&sub,multiply,divide }; //直接用函数名和用取址函数名等价
	for (auto f : vec)
		cout << f(2, 4) << endl;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值