#includeiostream.hclassA{public:virtualvoidfunc1(){coutA1;}voidfunc2(){coutA2;}};classB:publicA{public:voidfunc1(){coutB1;}voidfunc2(){coutB2;}};voidmain(){A*p=newB;p-func1();p-func2();}运行结果:B1A2#includeiostream.hclassA{public:virtualvoidfunc1(){coutA1;}voidfunc2(){coutA2;}};classB:publicA{public:voidfunc1(){coutB1;}voidfunc2(){coutB2;}};voidmain(){B*p=newB;p-func1();p-func2();}运行结果:B1B2【练习题1】#includeiostream.hclassbase{public:virtualintfun(void){coutbase::funendl;return10;}};classderive:publicbase{public:intfun(void){coutderive::funendl;return20;}};voidmain(void){derived;base*b1=&d;base&b2=d;baseb3;b1-fun();b2.fun();b3.fun();}derive::fun【练习题2】运行结果:derive::funbase::fun#includeiostream.h#includestring.hclassX{char*a;public:X(char*aa=abc){a=newchar[strlen(aa)+1];strcpy(a,aa);}~X(){couta被释放endl;delete[]a;}char*Geta(){returna;}};voidmain(void){char*p1=1234;Xs1,s2(p1);couts1.Geta()s2.Geta()endl;}运行结果:abc12341234被释放abc被释放【练习题3】已知head指向一个带头结点的单向链表,链表中每个结点的数据结构为:structnode{intdata;node*next;};链表中各结点按数据域递增有序链接,以下函数PURGE删除链表中数据域相同的结点,使链表中各结点的数据值都不相同。函数返回链首指针。node*PURGE(node){node*p,*q;q=head;p=q-next;if(q==NULL)return(head);while(p!=NULL)if(p-data==q-data){;deletep;p=q-next;}else{q=p;;};}【练习题4】*headq-next=p-nextp=p-nextreturnhead建立一个梯形法求的类integral,具体要求如下:(1)私有数据成员:doublea,b,area:其中a、b分别存放积分区间的下限和上限,area存放求得的积分值;intn:存放积分区间的等分数;(2)公有成员函数integral():构造函数,初始化a,b,n的值,缺省时a,b赋值0,n赋值1;doubleCalcul(doubleaa,doublebb,intnn,double(*f)(double)):用aa,bb,nn分别重新设置a,b,n的值,计算由f指向的被积函数在区间[a,b]的定积分值,保存在area中并返回该值;voidShow():输出a,b,area的值;badxxf)(【练习题5】(3)在主函数中完成对该类的测试,用梯形法计算的积分近似值。梯形法的计算公式为:其中:,n为积分区间等分数,n取值为1000,且dxx42)sin(banxfxfxfbfafhdxxf)()()(2)()()(121nabh0*nixaxbxaih#includeiostream.h#includemath.hclassintegral{doublea,b,area;intn;public:integral();doubleCalcul(doubleaa,doublebb,intnn,double(*f)(double));voidShow();};integral::integral(){a=0;b=0;n=1;}voidintegral::Show(){coutarea=areaendl;}doubleintegral::Calcul(doubleaa,doublebb,intnn,double(*f)(double)){a=aa;b=bb;n=nn;doubleh=(b-a)/n;area=(f(a)+f(b))/2;for(inti=1;in;i++)area=area+f(a+i*h);area=h*area;returnarea;}voidmain(){integraljf;jf.Calcul(2,4,1000,sin);jf.Show();}建立一个数组类ARR,具体要求如下:1.私有数据成员:intn;数组实际元素个数inta[100];存放增序数组元素2.公有成员函数ARR(intx[],intsize);构造函数,用参数size初始化n,用参数数组x初始化a数组。voiddelsame();完成将数组a中相同元素的删除工作。voidshow();将数组以每行5个数的形式输出到屏幕上。3.在主函数中定义数组intb[16],其初值是:{1,2,2,3,4,4,5,6,6,7,8,8,8,9,10,10}。定义一个ARR类的对象,用b及数组元素的个数初始化该对象,则经删除后a数组的内容为{1,2,3,4,5,6,7,8,9,10}。在主函数中完成对该类的测试。【练习题6】#includeiostream.hclassARR{intn;inta[100];public:ARR(intx[],intsize);voiddelsame();voidshow();};ARR::ARR(intx[],intsize){n=size;for(inti=0;in;i++)a[i]=x[i];}voidARR::delsame(){for(inti=0;in-1;i++){if(a[i]==a[i+1]){for(intk=i;kn-1;k++)a[k]=a[k+1];n--;i--;}}}voidARR::show(){intk=0;for(inti=0;in;i++){couta[i]'\t';k++;if(k%5==0)coutendl;}coutendl;}voidmain(){intb[16]={1,2,2,3,4,4,5,6,6,7,8,8,8,9,10,10};ARRarr(b,sizeof(b)/sizeof(int));arr.delsame();arr.show();}试定义一个类ARRAY,实现对一维整型数组的排序。排序的规则如下:将一维数组中各元素按其各位的数字之和从小到大排序。具体要求如下:(1)私有数据成员:inta[100];待排序的数组;intn;数组中元素的个数;(2)公有成员函数ARRAY(intt[],intm);构造函数,利用参数t初始化成员a,参数m为数组t中元素的个数,用参数m初始化成员n;intsum(intx);求整数x的各位数字之和,并返回该值,此函数供成员函数fun()调用;voidfun();按要求对数组a的元素排序;voidprint();输出数组a的所有元素。(3)在主函数中对该类进行测试。要求输出的结果如下:排序前的数组为:297,735,624,158,312,900排序后的数组为:312,900,624,158,735,297【练习题7】//建立类#includeiostream.hclassARRAY{inta[100];intn;public:ARRAY(intt[],intm);intsum(intx);voidfun();voidprint();};//构造函数ARRAY::ARRAY(intt[],intm){n=m;for(inti=0;in;i++)a[i]=t[i];}//累加和算法intARRAY::sum(intx){ints=0;while(x){s=s+x%10;x=x/10;}returns;}//输出函数voidARRAY::print(){for(inti=0;in;i++)couta[i]'\t';coutendl;}//排序算法----冒泡法voidARRAY::fun(){inti,j,t;for(i=0;in-1;i++)for(j=0;jn-1-i;j++)if(sum(a[j])sum(a[j+1])){t=a[j];a[j]=a[j+1];a[j+1]=t;}}//测试类voidmain(){inta[]={297,735,624,158,312,900};ARRAYarr(a,sizeof(a)/sizeof(int));arr.print();arr.fun();arr.print();}