面向?qū)ο蟪绦蛟O(shè)計(jì)練習(xí)題_第1頁
面向?qū)ο蟪绦蛟O(shè)計(jì)練習(xí)題_第2頁
面向?qū)ο蟪绦蛟O(shè)計(jì)練習(xí)題_第3頁
面向?qū)ο蟪绦蛟O(shè)計(jì)練習(xí)題_第4頁
面向?qū)ο蟪绦蛟O(shè)計(jì)練習(xí)題_第5頁
已閱讀5頁,還剩14頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

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//成績(jī){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//學(xué)生{public:student(int,char*,float,float,float);~student();voidmodify(int,char*,float,float,float);voidprint();private:intnumber;char*name;scores;//對(duì)象成員//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.在下面橫線處填上適當(dāng)字句,完成類中成員函數(shù)的定義。classA{public:A(intaa=0){a=____}~A(){____}2.下面是一個(gè)函數(shù)模板,用于計(jì)算兩個(gè)向量的和。在下面橫線處填上適當(dāng)字句,完成函數(shù)模板定義。#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.下面是一個(gè)用戶口令檢查程序,在橫線處填上適當(dāng)語句完成程序功能。#include<iostream.h>#include<string.h>______voidmain(){charuser[10];______if((strcmp(user,pass))==0)cout<<″yourpasswordiscorrect″<<endl;cout<<″yourpasswordiserror″<<endl;}4.在下列程序的下劃線上填上合適的內(nèi)容,使程序功能完整。//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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論