程序设计语言a2010期中试卷参_第1页
程序设计语言a2010期中试卷参_第2页
程序设计语言a2010期中试卷参_第3页
程序设计语言a2010期中试卷参_第4页
程序设计语言a2010期中试卷参_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

上海交 大 试卷(A)参考答(2009至2010学年第_二_学期期中考班级 学 课程名称 成绩 一.填空题(每题1分,共10分C++源文件名的后缀是 编写C++语言源程序并上机运行的一般过程为编辑、编译、和执行。其中,编辑的件,的作用是将此目标文件与被调用到的其他目标文件在一起。如果A是已经定义好的一个类,r2是A类的一个对象,p是指向A类对象的指针。执pnewA(r2)时,系统会自动调用拷贝构造函数,执行deletep系统会自动调用析构函数。5。类的私有成员只能通过它的成员函数以及来。无论哪种派生方法,基类中的私有成员既不许外部函数,也不许派生类中的成员函数。6。公有继承时,基类的保护成员在派生类中是保护成员成员,基类中的公有成员在派生类中是公有成员成员。承诺人 二.看程序,写结果(每题5分,共30分)1.写出下列程序执行结果【构造函数和析构函数的顺序】 Point{ x,Point(inta=1,intb={x=a;y=cout<<"Point("<<x<<","<<y<<")"<<}~Point(){cout<<"Point["<<x<<","<<y<<"]"<<endl; Point { origin,*pp=newPoint(3,{Pointpoint1(-5,}Pointpoint2(1,2);deletepp;return}class{friendostream&operator<<(ostream&os,constCST&ob);CST(intv1=0){data=v1;++count;cout<<“constructing“<<data<<~CST(){ count--;cout<<“deconstructing“<<data<<endl;}operatorint()const{returndata;}staticintcount;intostream&operator<<(ostream&os,constCST&ob{os<<"data="<<os<<“,count=”<<ob.count<<endl;returnos;}ConstructingCST::count=ConstructingCST::count=Constructingdata=4,count=deconstructingCST::count=1Constructing10deconstructing10data=10,count=1deconstructingint{CSTcs,intcout<<"CST::count="<<CST::count<<ptr1=newcout<<deletecout<<"CST::count="<<CST::count<<ptr2=*ptr2=cout<<value=cout<<value<<return}}class{intx,y;coord(inti=0,intj=0){x=i;y=j;voidprint(){cout<<"x:"<<x<<",y:"<<y<<endl;}coordoperator++(){++x;return*this;}coordoperator++(intn){coordtmp=*this;++y;returnx:x:5,y:x:5,y:x:5,y:int{coord(ob++).print();print2(x=5,y=10ob.print();operator++(intny1,xx=5,y=11return}classcomplexintreal;//intimag;//complex(intr=0,inti=0){real=r;imag=complex(constcomplex&t){real=t.real+2;imag=t.imag+1;}voidshow(){cout<<real<<"+"<<imag<<"I";}};void{complex c2=cout<<c1= 赋值运算,调用的是默认的赋值函数,c2的成员变量值赋给c1。写出下列程序的执行结果classC_AC_A(charvalue=‘@’){data cout<<"Object"<<data<<"constructor" C_A(constC_A&obj){ obj.data+cout<<"Object"<<data<<"constructor"{cout<<"Object"<<data<<"destructor"<<endl;} ObjectaconstructorObject8constructorObject9constructorObjectmconstructorObjectmdestructorObject8destructorObjectbconstructorObjectObjectaconstructorObject8constructorObject9constructorObjectmconstructorObjectmdestructorObject8destructorObjectbconstructorObjectmconstructorObjectmdestructorObjectbdestructorObjectadestructorObject9destructor staticC_Af // Objectbconstructorintmain(){C_Areturn}classCBaseCBase(int{m_data=cout<<"ConstructorofCBase.m_data="<<m_data}virtual~CBase(){cout<<"DestructorofCBase.m_data="<<m_data<<endl;}virtualvoidprint(){cout<<"CBase::m_data="<<m_data<<endl;}intclassCDerived:public{CDerived(int

{cout<<"ConstructorofCDerived."<<endl;)~CDerived(){cout<<"DestructorofCDerived."voidprint(){cout<<"CDerived::m_data="<<m_data<<endl;}intint{CBase*p=newCDerived(67),baseObj=p-ConstructorofCBase.m_data=63ConstructorofCDerived.deletereturn}DestructorofDestructorofCBase.DestructorofCBase.二.程序填空(每空2分,共30分classA{ operator==(constA&a1constA&a2)//判两数据成员指向的对象相{return *(a1.a)==*(a2.a) ;}A(intaa0a newint(aa) //aa初始化aA A&obj a=new *a= } delete }//释放动态空int*在如下Rational类的基础上又定义了一个Complex类。复数类具有输入输出和加能。classRationalfriendistream&operator>>(istream&in,Rational&obj);//friendostream&operator<<(ostream&os,constRational&obj);//输出重载函数friendRationaloperator+(constRational&r1,constRational&r2运算符重载friendRationaloperator*(constRational&r1,constRational&r2);//*运算符重载intnum;intden;void Rational(intn0,intd1)numn;dend;ReductFraction();}operatordouble //double型{ (double(num)/den);classfriendComplexoperator+(constComplex&x,constComplex&y);friendistream&operator>>(istream&is, Complex&obj friendostream&operator<<(ostream&os,constComplex&obj);Rationalreal; Rational Complex(intr1=0,intr2=1,inti1=0,inti2= real(r0,r1),imag(i1, Complexoperator+(constComplex&x,constComplex{Complextmp.real=x.real+ tmp.imag=x.imag+y.imag }三。编程(每题10分,共30分int //输出:10 //输出:21 //输出:为1号服务 //输出:30 //3return} 4构造函 2Come函 2Go函 2classintco{covoid{co}void{if(com-goNum==0) } void{Aa1(3,15),a2(2,5),a3,a3=a1+a2;a4=a1-a2;cout<<a3;cout<<a4}

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论