【正确答案】解:使用类参数传递被积函数,因类有数据域,也可以同时传递积分上下限,可由读者进行修改。
#include
#include
using namespace std;
class Base{
protected:
double result,a,b,step;//Intevalue积分值,a积分下限,b积分上限
int n;
public:
virtual double fun(double x)=0;//被积函数声明为纯虚函数
virtual void Integerate(){
cout<<"这里是积分函数"<Integerate();//动态,可以访问派生类定义的被积函数
bp->Print();
sinL sl(0.0,3.1415926535/2.0,100);
bp=&sl;
bp->Integerate();//动态,可以访问派生类定义的被积函数
bp->Print();
sinS ss(0.0,3.1415926535/2.0,100);
bp=&ss;
bp->Integerate();//动态,在层次中选
bp->Print();
expR er(0.0,1.0,100);
bp=&er;
bp->Integerate();//动态,可以访问派生类定义的被积函数
bp->Print();
expL el(0.0,1.0,100);
bp=⪙
bp->Integerate();//动态,可以访问派生类定义的被积函数
bp->Print();
expS es(0.0,1.0,100);
bp=&es;
bp->Integerate();//动态,在层次中选
bp->Print();
otherR or(0.0,1.0,100);
bp=∨
bp->Integerate();//动态,可以访问派生类定义的被积函数
bp->Print();
otherL ol(0.0,1.0,100);//增加到100000也达不到辛普生法的精度
bp=&ol;
bp->Integerate();//动态,可以访问派生类定义的被积函数
bp->Print();
otherS os(0.0,1.0,100);
bp=&os;
bp->Integerate();//动态,在层次中选
bp->Print();
return 0;
}
【答案解析】