下载
加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 指针虚函数继承c++练习

指针虚函数继承c++练习.doc

指针虚函数继承c++练习

icygin
2013-01-26 0人阅读 举报 0 0 暂无简介

简介:本文档为《指针虚函数继承c++练习doc》,可适用于IT/计算机领域

第一题#include<iostream>usingnamespacestdclassteacher{protected:charnameintcoursetimepublic:teacher(chartname,inttime)virtualintpay()=virtualvoidprint()=char*getname()intgetcoursetime()}classprofessor:publicteacher{public:professor(charpname,inttime)intpay()voidprint(){cout<<"教授:"<<getname()}}classassociateprofessor:publicteacher{public:associateprofessor(charpname,inttime)intpay()voidprint(){cout<<"副教授:"<<getname()}}classlecturer:publicteacher{public:lecturer(charpname,inttime)intpay()voidprint(){cout<<"讲师:"<<getname()}}**********Program**********teacher::teacher(chartname,inttime){for(inti=i){namei=tnameiif(tnamei=='')break}coursetime=time}char*teacher::getname(){returnname}intteacher::getcoursetime(){return(coursetime)}professor::professor(charpname,inttime):teacher(pname,time){for(inti=i){namei=pnameiif(pnamei=='')break}coursetime=time}intprofessor::pay(){return(*coursetime)}associateprofessor::associateprofessor(charpname,inttime):teacher(pname,time){for(inti=i){namei=pnameiif(pnamei=='')break}coursetime=time}intassociateprofessor::pay(){return(*coursetime)}lecturer::lecturer(charpname,inttime):teacher(pname,time){for(inti=i){namei=pnameiif(pnamei=='')break}coursetime=time}intlecturer::pay(){return(*coursetime)}**********End**********intmain(){professorpobj("李小平",)pobjprint()cout<<'t'<<"工资:"<<pobjpay()<<endlassociateprofessorapobj("王芳芳",)apobjprint()cout<<'t'<<"工资:"<<apobjpay()<<endllecturerlobj("何大建",)lobjprint()cout<<'t'<<"工资:"<<lobjpay()<<endl}第三题#include<iostream>usingnamespacestdconstdoublePI=**********Program**********classcircle{public:circle(doubler){radius=r}virtualdoublearea(){return}virtualdoublevolume(){return}protected:doubleradius}classsphere:publiccircle{public:sphere(doubler):circle(r){}doublearea(){return*PI*radius*radius}doublevolume(){return*PI*radius*radius*radius}}classcolumn:publiccircle{public:column(doubler,doubleh):circle(r){height=h}doublearea(){return*PI*radius*(heightradius)}doublevolume(){returnPI*radius*radius*height}private:doubleheight}**********End**********intmain(){circle*pspheresobj()p=sobjcout<<"球体:"<<endlcout<<"体积="<<p>volume()<<endlcout<<"表面积="<<p>area()<<endlcolumncobj(,)p=cobjcout<<"圆柱体:"<<endlcout<<"体积="<<p>volume()<<endlcout<<"表面积="<<p>area()<<endl}第二题#include<iostream>usingnamespacestdconstdoublePI=*************program***********classBasic基类{protected:doublerpublic:Basic(){r=}Basic(doublea):r(a){}}classCircular:publicBasic从基类派生圆类{protected:doubleareapublic:Circular(doublea){r=aarea=area=*r*r}doublegetArea()返回圆面积{returnarea}}classColumn:publicCircular从圆类派生圆柱类{protected:doublehdoublecubagepublic:Column(doublea,doubleb):Circular(a){h=bcubage=getArea()*h}doublegetCubage()返回圆柱体积函数{returncubage}}************end*************voidmain(){Circularcircular()Columncolumn(,)cout<<"圆的面积:"<<circulargetArea()<<endlcout<<“圆柱的体积:”<<columngetCubage()<<endl}完成函数fun()其功能是求出M行N列二维数组每行元素中的最小值并计算它们的和值和值通过形参传回主函数输出。程序与运行结果如下:#include<iostream>usingnamespacestdconstintM=constintN=voidfun(intaMN,int*sum){**********Program***********sum=inti,jfor(i=i<Mi){intmin=aifor(j=j<Nj){if(aij<min)min=aij}*sum=min}**********End**********}voidmain(){intxMN={{,,,},{,,,}}intsfun(x,s)cout<<s<<endlreturn}完成空出函数fun(char*schar*t)部分。函数fun(char*schar*t)的功能是:将在字符串s中下标为偶数位置上的字符紧随其后重复出现一次放在一个新串t中t中字符串按原字符串中的字符的顺序排列。程序运行结果如下:Pleaseenterstrings:ABCDEFTheresultis:AACCEE#include<iostream>usingnamespacestdvoidfun(char*s,char*t){**********Program**********intifor(i=i<i){if(i==)ti=sielseti=si}**********End**********}voidmain(){chars,tcout<<"Pleaseenterstrings:"<<endlcingetline(s,)fun(s,t)cout<<"Theresultis:"<<t<<endlreturn}完成函数fun(char*str,inta)其功能是把str中出现的数字转换成数字存储在a中然后返回转换的个数。例如:str="abcdeaert"则a中存储着返回:程序运行结果如下:#include<iostream>usingnamespacestdintfun(char*str,inta){**********Program**********inti=,j=while(stri!=''){if(stri>stri<){aj=strij}i}returnj**********End**********}intmain(){intaintlen=fun("rwtrwgrt",a)for(inti=i<leni){cout<<ai<<''}cout<<endlreturn}.请完成函数fun(intn)求出n*n矩阵的对角线数字的平方和如果为奇数则对角线交叉部分数字只参与一次运算。程序运行结果如下:pleaseinputN(<=N<=)#include<iostream>usingnamespacestdconstintMAXN=intAMAXNMAXNintfun(intn){**********Program**********intsum=intifor(i=i<ni){sum=Aii*AiiAini*Aini}if(n!=){sum=A(n)(n)*A(n)(n)}returnsum**********End**********}voidmain(){intndo{cout<<"pleaseinputN(<=N<=)n"cin>>nif(n<n>=){break}}while()for(inti=i<ni){for(intj=j<nj){cin>>Aij}}cout<<fun(n)<<endlreturn}杨辉三角#include<iostream>#include<iomanip>usingnamespacestdintmain(){inty={},row,col赋初值的结果是y=,其他的均是for(row=row<row){yrow=for(col=col<=rowcol)yrowcol=yrowcolyrowcol}for(row=row<row){for(col=col<=rowcol)cout<<yrowcol<<""cout<<endl}return}矩阵#include<iostream>#include<iomanip>usingnamespacestdvoidinverse(int,int)voidmulti(int,int,int)voidoutput(int)voidoutput(int)voidmain(){intmiddle,resultintmatrix={,,,,,,,,,,,,,,,,,}intmatrix={,,,,,,,,,,,}inverse(matrix,middle)multi(middle,matrix,result)output(result)cout<<endl<<endloutput(middle)return}voidinverse(intmatrix,intmiddle){inti,jfor(i=i<i){for(j=j<j){middleji=matrixij}}return}voidmulti(intmiddle,intmatrix,intresult){inti,j,kfor(i=i<i){for(j=j<j){resultij=for(k=k<k)resultij=middleik*matrixkj}}return}voidoutput(intresult){inti,jcout<<"aftermultiplyingresult"<<endlfor(i=i<i){for(j=j<j)cout<<setw()<<resultijcout<<endl}return}voidoutput(intmiddle){inti,jcout<<"afterinverse"<<endlfor(i=i<i){for(j=j<j){cout<<setw()<<middleij}cout<<endl}return}

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/11

指针虚函数继承c++练习

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利