面向对象程序设计练习题_第1页
面向对象程序设计练习题_第2页
面向对象程序设计练习题_第3页
面向对象程序设计练习题_第4页
面向对象程序设计练习题_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

mbermberember#include<iostream.h>classMyClass{public:MyClass(inti){member=i;}voidSetMember(intm){member=m;}intGetMember()const{returnmember;}voidprint()const{cout<<"member="<<member<<endl;}private:intmember;voidmain(){objobj1.print();MyClassobj2(3);berberobj1.print();obj2.print();}Astudent…Agraduatestudent…#include<iostream.h>classstudent{public:intcout<<"Astudent…"<<endl;}};public:voidprint(){cout<<"Agraduatestudent…"<<endl;}};voidmain(){students1,*ps;GStudents2;s1.print();ps=&s2;int//Saveasscore.hclassscore//成绩{public:score(float,float,float);score();voidprint();voidmodify(float,float,float);private:floatcomputer;floatenglish;floatmathematics;//Saveasscore.cpp#include"score.h"#include<iostream.h>score::score(floatc,floate,floatm){computer=c;english=e;mathematics=m;}score::score(){computer=0;english=0;mathematics=0;}voidscore::modify(floatc,floate,floatm){computer=c;english=e;mathematics=m;}voidscore::print(){cout<<"\nscorecomputer:"<<computer<<"\nscoreenglish:"<<english<<"\nscoremathematics:"<<mathematics<<endl;}//Saveasstudent.h#include"score.h"classstudent//学生{public:student(int,char*,float,float,float);~student();voidmodify(int,char*,float,float,float);voidprint();private:intnumber;char*name;scores;//对象成员//Saveasstudent.cpp#include"student.h"#include<string.h>#include<iostream.h>student::student(intn,char*na,floats1,floats2,floats3)ssss3);//(原因:)number=n;name=newchar[strlen(na)+1];strcpy(name,na);}student::~student(){deletename;}//(改正:)voidstudent::modify(intn,char*na,floats1,floats2,floats3){number=n;delete[]name;name=newchar[strlen(na)+1];strcpy(name,na);s.modify(s1,s2,s3);}voidstudent::print(){cout<<"\nnumber:"<<number<<"name:"<<name;s.print();}//Saveasmain.cpp#include"student.h"voidmain(){studentastudent(28,"Wu",78,64,69);astudent.print();astudent.modify(28,"Wu",87,72,99);}1.在下面横线处填上适当字句,完成类中成员函数的定义。classA{public:A(intaa=0){a=____}~A(){____}2.下面是一个函数模板,用于计算两个向量的和。在下面横线处填上适当字句,完成函数模板定义。#include<iostream.h>template<classT>T*f(T*a,T*b,intn){T*c=____for(inti=0;i<n;i++)c[i]=____returnc;}voidmain(){inta[5]={1,2,3,4,5},b[5]={10,20,30,40},*p;p=f(a,b,5);for(inti=0;i<5;i++)cout<<p[i]<<endl;}3.下面是一个用户口令检查程序,在横线处填上适当语句完成程序功能。#include<iostream.h>#include<string.h>______voidmain(){charuser[10];______if((strcmp(user,pass))==0)cout<<″yourpasswordiscorrect″<<endl;cout<<″yourpasswordiserror″<<endl;}4.在下列程序的下划线上填上合适的内容,使程序功能完整。//Saveascircle.h#ifndefCIRCLE_H#defineCIRCLE_H#include<iostream.h>#definePI3.1415926classCircle//圆{protected:floatx,y;floatradius;public:Circle(){x=y=0;radius=0;}Circle(floatx1,floaty1,floatr){x=x1;y=y1;radius=r;}doublegetArea(){returnradius*radius*PI;}voidshow(){cout<<"x="<<x<<";y="<<y<<endl;cout<<"radius="<<radius<<endl<<endl;}#endif//Saveascylinder.h#ifndefCYLINDER_H#defineCYLINDER_H#include<iostream.h>#include"Circle.h"classCylinder:publicCircle//圆柱{private:floatheight;public:Cylinder(){height=0;}Cylinder(floatx1,floaty1,floatr,floath):{height=h;}doublegetArea(){return2*radius*radius*PI+2*radius*height*PI;}voidshow(){cout<<"x="<<x<<";y="<<y<<endl;cout<<"radius="<<radius<<endl<<"height="<<height<<endl<<endl;}#endif//Saveasmain.cpp#include"Circle.h"#include"Cylinder.h"voidmain(){Circlea(1,2,3);Cylinderb(4,5,6,7);}#include<iostream.h>classSample{public:Sample(inti){n=i;}friendintadd(Sample&s1,Sample&s2);};intadd(Sample&s1,Sample&s2){returns1.n+s2.n;}voidmain(){Samples1(-10),s2(20);cout<<add(s1,s2)<<endl;}#include<iostream.h>intadd(intx,inty){returnx+y+1;}doubleadd(doublex,doubley){returnx+y-1;}voidmain(){inta=2,b=5;doublec=3.5,d=9.3;cout<<"a+b="<<add(a,b)<<endl;cout<<"c+d="<<add(c,d)<<endl;} #include<iostream.h>classA{public:nclassB:virtualpublicA{};classC:virtualpublicA{};classD:publicB,publicC{intgetn(){returnB::n;}};voidmain(){Dd;d.B::n=10;d.C::n=20;cout<<"d.B::n="<<d.B::n<<endl;cout<<"d.C::n="<<d.C::n<<endl;} s;}voidprint(){voidmainm3.print();} //Saveasanimal.h#ifndefA#defineA#include<iostream.h>#include<string.h>classanimal{protected:charname[10];public:virtualvoidshow()=0;#endif//Saveascat.h#include"animal.h"classcat:publicanimal{public:voidshow(){strcpy(name,"cat");cout<<"Name:"<<name<<endl;}//Saveasdog.h#include"animal.h"classdog:publicanimal{sort(a,size);public:voidshow(){strcpy(name,"dog");cout<<"Name:"<<name<<endl;}//Saveasmain.cpp#include"dog.h"#include"cat.h"voidmain(){dogd;catc;} //Saveasmain.cpp#include<iostream.h>constintsize=5;template<classT>voidsort(TArray[],constintn){for(inti=0;i<n-1;i++)for(intj=i+1;j<n;j++)if(Array[i]>Array[j]){Tt=Array[i];Array[i]=Array[j];Array[j]=t;}}template<classT>voidshow(TArray[],constintn){for(inti=0;i<n;i++)cout<<Array[i]<<"";cout<<endl;}voidmain(){inta[size]={23,42,36,19,16};floatb[size]={23.5,42.5,36.5,19.5,16.5};show(a,size);sort(b,size);show(b,size);} //Saveascomplex.hclassComplex{private:floatreal;floatimag;public:Complex(){real=0.0;imag=0.0;}Complex(floatr,floati){real=r;imag=i;}voidprint();Complexoperator+(Complexa);Complexoperator*(Complexa);Complexoperator/(Complexa);//Saveascomplex.cpp#include<iostream.h>#include"complex.h"voidComplex::print(){cout<<real;if(imag>0)cout<<"+";if(imag!=0)cout<<imag<<"i"<<endl;cout<<endl;}ComplexComplex::operator+(Complexa){Complex

温馨提示

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

评论

0/150

提交评论