c 練習題(附答案)_第1頁
c 練習題(附答案)_第2頁
c 練習題(附答案)_第3頁
c 練習題(附答案)_第4頁
c 練習題(附答案)_第5頁
已閱讀5頁,還剩28頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

5.9

(11)下列關(guān)于C++函數(shù)的敘述中,正確的是

A)每個函數(shù)至少要具有一個參數(shù)B)每個函數(shù)都必須返回一個值

C)函數(shù)在被調(diào)用之前必須先聲明D)函數(shù)不能自己調(diào)用自己

(12)下列關(guān)于類和對象的敘述中,錯誤的是

A)一個類只能有一個對象

B)對象是類的具體實例

C)類是對某一類對象的抽象

D)類和對象的關(guān)系是一種數(shù)據(jù)類型與變量的關(guān)系

(13)在C++中,用于實現(xiàn)運行時多態(tài)性的是

A)內(nèi)聯(lián)函數(shù)B)重載函數(shù)

C)模板函數(shù)D)虛函數(shù)

(14)下列關(guān)于運算符重載的敘述中,正確的是

A)通過運算符重載,可以定義新的運算符

B)有的運算符只能作為成員函數(shù)重載

C)若重載運算符+,則相應的運算符函數(shù)名是+

D)重載一個二元運算符時,必須聲明兩個形參

(15)下列關(guān)于模板的敘述中,錯誤的是

A)模板聲明中的第一個符號總是關(guān)鍵字template

B)在模板聲明中用<和>括起來的部分是模板的形參表

C)類模板不能有數(shù)據(jù)成員

D)在一定條件下函數(shù)模板的實參可以省略

(16)對于語句cout?endl?x;中的各個組成部分,下列敘述中錯誤的是

A)"cout”是一個輸出流對象

B)"endl”的作用是輸出回車換行

C)"X"是一個變量

D)"<<"稱作提取運算符

(17)下列字符串中,正確的C++標識符是

A)foo-1B)2b

C)newD)_256

(18)已知枚舉類型定義語句為:

enumToken{NAME,NUMBER,PLUS=5,MINUS,PRINT=10};

則下列敘述中錯誤的是

A)枚舉常量NAME的值為1B)枚舉常量NUMBER的值為1

C)枚舉常量MINUS的值為6D)枚舉常量PRINT的值為10

(19)執(zhí)行語句序列

inti=0;while(i<25)i+=3;cout?i;

輸出結(jié)果是

A)24B)25

C)27D)28

(20)要定義一個具有5個元素的一維數(shù)組vect,并使其各元素的初值依次是30,50,-88,

0和0,則下列定義語句

中錯誤的是

A)intvectlJ={30,50,-88};B)intvect[]={30,50,-88,0,0};

C)intvect[5]={30,50,-88};D)intvect[5]={30,50,-88,0,0);

(21)有如下程序:

#include<iostream>

voidfun(int&x,inty){intt=x;x=y;y=t;}

intmain()

(

inta[2]={23,42);

fun(a[l],a[0]);

std::cout?a[0]?n,n?a[1]?std::endl;

return0;

)

執(zhí)行后的輸出結(jié)果是

A)42,42B)23,23

C)23,42D)42,23

(22)有如下類聲明:

classFoo{intbar;};

則Foo類的成員bar是

A)公有數(shù)據(jù)成員B)公有成員函數(shù)

C)私有數(shù)據(jù)成員D)私有成員函數(shù)

(23)有如下類聲明:

classMyBASE

(

intk;

public:

voidset(intn){k=n;}

intget()const{returnk;}

);

classMyDERIVED:protectedMyBASE

(

protected:

intj;

public:

voidset(intm,intn){MyBASE::set(m);j=n;}

intget()const{returnMyBASE::get()+j;}

);

則類MyDERIVED中保護的數(shù)據(jù)成員和成員函數(shù)的個數(shù)是

A)4B)3

C)2D)1

(24)已知在一^個類體中包含如下函數(shù)原型:VOLUMEoperator-(VOLUME)const;,下列關(guān)

于這個函數(shù)的敘述中,錯誤

的是

A)這是運算符-的重載運算符函數(shù)

B)這個函數(shù)所重載的運算符是一個一元運算符

C)這是一個成員函數(shù)

D)這個函數(shù)不改變類的任何數(shù)據(jù)成員的值

(25)有如下函數(shù)模板定義:

template<classT>

Tfunc(Tx,Ty){returnx*x+y*y;}

在下列對func的調(diào)用中,錯誤的是

A)func(3,5);B)func(3.0,5.5);

C)func(3,5.5);D)func<int>(3,5.5);

(27)在下列函數(shù)原型中,可以作為類AA構(gòu)造函數(shù)的是

A)voidAA(int);

B)intAA();

C)AA(int)const;

D)AA(int);

(28)下列關(guān)于this指針的敘述中,正確的是

A)任何與類相關(guān)的函數(shù)都有this指針

B)類的成員函數(shù)都有this指針

C)類的友元函數(shù)都有this指針

D)類的非靜態(tài)成員函數(shù)才有this指針

(29)有如下程序:

#include<iostream>

usingnamespacestd;

classTest

(

public:

Test(){n+=2;}

~Test(){n-=3;}

staticintgetNum(){returnn;}

private:

staticintn;

);

intlest::n=l;

intmain()

(

Test*p=newTest;

deletep;

cout?,,n=',?Test::getNum()?endl;

return0;

)

執(zhí)行后的輸出結(jié)果是

A)n=0B)n=l

C)n=2D)n=3

(30)有如下程序:

#include<iostream>

usingnamespacestd;

classA{

public:

A(){cout?,'An;}

);

classB{public:B(){cout?nBn;}}

classC:publicA{

Bb;

public:

C(){cout?,,CH;}

);

intmain(){Cobj;return0;}

執(zhí)行后的輸出結(jié)果是

A)CBAB)BAC

C)ACBD)ABC

(31)有如下類聲明:

classXA{

intx;

public:

XA(intn){x=n;}

);

classXB:publicXA{

inty;

public:

XB(inta,intb);

);

在構(gòu)造函數(shù)XB的下列定義中,正確的是

A)XB::XB(inta,intb):x(a),y(b){}B)XB::XB(inta,intb):XA(a),y(b){)

C)XB::XB(inta,intb):x(a),XB(b){}D)XB::XB(inta,intb):XA(a),XB(b){}

(32)有如下程序:

#include<iostream>

usingnamespacestd;

classBASE{

public:

-BASEO{cout?nBASEU;}

);

classDERIVED:publicBASE{

public:

-DERIVED(){cout?,'DERIVEDH;}

);

intmain(){DERIVEDx;return0;}

執(zhí)行后的輸出結(jié)果是

A)BASEB)DERIVED

C)BASEDERIVEDD)DERIVEDBASE

(33)在表達式x+y*z中,+是作為成員函數(shù)重載的運算符,*是作為非成員函數(shù)重載的

運算符。下列敘述中正確的

A)operator+有兩個參數(shù),operator*有兩個參數(shù)

B)operator+有兩個參數(shù),operator*有一個參數(shù)

C)operalor+有一個參數(shù),operator*有兩個參數(shù)

D)operator+有一個參數(shù),operator*有一個參數(shù)

(34)下面關(guān)于C++流的敘述中,正確的是

A)cin是一個輸入流對象

B)可以用ifslream定義一個輸出流對象

C)執(zhí)行語句序列char*y="PQMNn;cout?y;將輸出字符串“PQMN”的地址

D)執(zhí)行語句序列charx[80];cin.getline(x,80);時,若鍵入

Happynewyear

則x中的字符串是“Happy”

(35)有如下程序:

#include<iostream>

usingnamespacestd;

classAA{

intn;

public:

AA(intk):n(k){}

intget(){returnn;}

intget()const{returnn+1;}

);

intmain()

{

AAa(5);

constAAb(6);

cout?a.get()?b.get();

return0;

)

執(zhí)行后的輸出結(jié)果是

A)55B)57

C)75D)77

(6)下列函數(shù)的功能是判斷字符串str是否對稱,對稱則返回true,否則返回

false。請在橫線處填上適當內(nèi)容,實現(xiàn)該函數(shù)。

boolfun(char*str)

(

inti=0,j=0;

while(str[j]);

for(j-;i<j&&str[i]==str[j];i++,j-);

returnij;

(7)執(zhí)行語句序列

intx=10,&r=x;

cout<<x?'-,?r?endl;

輸出結(jié)果為O

(8)下列程序的輸出結(jié)果為

Objectid=0

Objectid=l

請將程序補充完整。

#include<iostream>

usingnamespacestd;

classPoint

(

public:

Point(intxx=0,intyy=0){X=xx;Y=yy;countP++;}

?Point(){countP--;}

intGetX(){returnX;}

intGetY(){returnY;}

staticvoidGetC(){cout?"Objectid=H?countP?endl;}

private:

intX,Y;

staticintcountP;

);

〃靜態(tài)數(shù)據(jù)成員的初始化

intmain()

(

Point::GetC();

PointA(4,5);

A.GetC();

return0;

)

)

(9)非成員函數(shù)應聲明為類的函數(shù)才能訪問這個類的private成員。

(10)派生類中的成員不能直接訪問基類中的成員。

(11)下列程序的輸出結(jié)果為2,請將程序補充完整。

#include<iostream>

usingnamespacestd;

classBase

(

public:

voidfun(){cout?l;}

);

classDerived:publicBase

{

public:

voidfun(){cout?2;}

);

intmain()

Base*p=newDerived;

p->fun();

deletep;

return0;

)

(12)請將下列類定義補充完整。

classBase{public:voidfun(){cout?"Base::fun"?endl;));

classDerived:publicBase

(

public:

voidfun(){

〃顯式調(diào)用基類的fun函數(shù)

cout?,'Derived::fun',?endl;

)

);

一、選擇題

(11)C)(12)A)(13)D)(14)B)(15)C)

(16)D)(17)D)(18)A)(19)C)(20)A)

(21)B)(22)C)(23)B)(24)B)(25)C)

(27)D)(28)D)(29)A)(30)D)

(31)B)(32)D)(33)C)(34)A)(35)B)

二、填空題

(6)j++>=

(8)intPoint::counlP=0;

(9)友元

(10)私有或private

(11)virtual

(12)Base::fun();

6.4

(11)下列關(guān)于C++函數(shù)的說明中,正確的是

A)內(nèi)聯(lián)函數(shù)就是定義在另一個函數(shù)體內(nèi)部的函數(shù)

B)函數(shù)體的最后一條語句必須是return語句

C)標準C++要求在調(diào)用一個函數(shù)之前,必須先聲明其原型

D)編譯器會根據(jù)函數(shù)的返回值類型和參數(shù)表來區(qū)分函數(shù)的不同重載形式

(13)下列關(guān)于虛函數(shù)的說明中,正確的是

A)從虛基類繼承的函數(shù)都是虛函數(shù)

B)虛函數(shù)不得是靜態(tài)成員函數(shù)

C)只能通過指針或引用調(diào)用虛函數(shù)

D)抽象類中的成員函數(shù)都是虛函數(shù)

(14)運算符重載是對已有的運算符賦予多重含義,因此

A)可以對基本類型(如int類型)的數(shù)據(jù),重新定義“+”運算符的含義

B)可以改變一個已有運算符的優(yōu)先級和操作數(shù)個數(shù)

C)只能重載C++中已經(jīng)有的運算符,不能定義新運算符

D)C++中已經(jīng)有的所有運算符都可以重載

(15)模板對類型的參數(shù)化提供了很好的支持,因此

A)類模板的主要作用是生成抽象類

B)類模板實例化時,編譯器將根據(jù)給出的模板實參生成一個類

C)在類模板中的數(shù)據(jù)成員都具有同樣類型

D)類模板中的成員函數(shù)都沒有返回值

(18)下列語句中,錯誤的是

A)constintbuffer=256;B)constinttemp;

C)constdouble*point;D)constdouble*rt=newdouble(5.5);

(20)有如下程序:

#include<iostream>

usingnamespacestd;

intmain()

(

int*p;

*p=9;

cout?"Thevalueatp:H?*p;

return0;

)

編譯運行程序?qū)⒊霈F(xiàn)的情況是

A)編譯時出現(xiàn)語法錯誤,不能生成可執(zhí)行文件

B)運行時一定輸出:Thevalueatp:9

C)運行時一定輸出:Thevalueatp:*9

D)運行時有可能出錯

(21)有如下程序:

#include<iostream>

usingnamespacestd;

intmain()

(

voidfunction(doubleval);

doubleval;

function(val);

cout?val;

return0;

)

voidfunction(doubleval)

(

val=3;

)

編譯運行這個程序?qū)⒊霈F(xiàn)的情況是

A)編譯出錯,無法運行B)輸出:3

C)輸出:3.0D)輸出一個不確定的數(shù)

(22)有如下類定義:

classAA

inta;

public:

intgetRef()const{return&a;}〃①

intgetValue()const{returna;}〃②

voidset(intn)const{a=n;}//(§)

friendvoidshow(AAaa)const{cout?a;)〃④

);

其中的四個函數(shù)定義中正確的是

A)①B)②

C)③D)④

(23)有如下程序:

#include<iostream>

usingnamespacestd;

classBase

(

public:

voidfun(){cout?nBase::funn?endl;}

);

classDerived:publicBase

(

public:

voidfun()

cout?nDerived::funM?endl;

)

);

intmain()

(

Derivedd;

d.fun();

return0;

)

已知其執(zhí)行后的輸出結(jié)果為:

Base::fun

Derived::fun

則程序中下劃線處應填入的語句是

A)Base.fun();B)Base::fun();

C)Base->fun();D)fun();

(24)下面是重載為非成員函數(shù)的運算符函數(shù)原型,其中錯誤的是

A)Fractionoperator+(Fraction,Fraction);B)Fractionoperator-(Fraction);

C)Fraction&operator=(Fraction&,Fraction);D)Fraction&operator+=(Fraction&,Fraction);

(25)有如下函數(shù)模板:

template<typenameT,typenameU>

Tcast(Uu){returnu;}

其功能是將U類型數(shù)據(jù)轉(zhuǎn)換為T類型數(shù)據(jù)。已知i為int型變量,下列對模板函數(shù)cast

的調(diào)用中正確的是

A)cast(i):B)casto(i);

C)cast<char*,int>(i);D)casKdouble,int>(i);

(26)若磁盤上已存在某個文本文件,其全路徑文件名為d:\ncre\test.txt,下列語句中不能

打開該文件的是

A)ifstreamfile("d:\ncre\test.txt");B)ifstreamfile("d:\\ncre\\test.txt");

C)ifstreamfile;file.open("d:\\ncre\\test.txt");D)ifstream*pFile=new

ifstream("d:\\ncre\\test.txt");

(27)有如下類定義:

classFoo

(

public:

Foo(intv):value(v){}〃①

~Foo(){}〃②

private:

Foo(){}〃③

intvalue=0;〃④

);

其中存在語法錯誤的行是

A)①B)②

C)③D)④

(28)有如下類定義:

classPoint

(

intx_,y_;

public:

Point():xJO),y_(0){}

Point(intx,inty=0):x_(x),y_(y){}

);

若執(zhí)行語句

Pointa(2),b[3],*c[4];

則Point類的構(gòu)造函數(shù)被調(diào)用的次數(shù)是

A)2次B)3次

C)4次D)5次

(29)有如下類定義:

classTest

public:

Test(){a=0;c=0;}//①

intf(inta)const{this->a=a;)〃②

staticintg(){returna;}〃③

voidh(intb){Test::b=b;};//④

private:

inta;

staticintb;

constintc;

);

intTest::b=0;

在標注號碼的行中,能被正確編譯的是

A)①B)②

C)③D)④

(30)有如下程序:

#include<iostream>

usingnamespacestd;

classA

(

public:

A(){cout?"A";}

?A(){cout?"?A";}

);

classB:publicA

(

A*p;

public:

B(){cout?"Bn;p=newA();}

?B(){cout?"?B";deletep;}

);

intmain()

(

Bobj;

return0;

)

執(zhí)行這個程序的輸出結(jié)果是

A)BAA~A~B~AB)ABABA?A

C)BAA~B~A~AD)ABA~A~B~A

(32)有如下程序:

#include<iostream>

usingnamespacestd;

classBase

public:

voidfun1(){cout?HBase\nn;}

virtualvoidfun2(){cout?"Base\n";)

);

classDerived:publicBase

(

public:

voidfun1(){cout?"Derived\nn;}

voidfun2(){cout?nDerived\nn;}

);

voidf(Base&b){b.funl();b.fun2();}

intmain()

(

Derivedobj;

f(obj);

return0;

)

執(zhí)行這個程序的輸出結(jié)果是

A)B)

BaseBase

BaseDerived

C)D)

DerivedDerived

BaseDerived

(33)有如下程序:

#include<iostream>

usingnamespacestd;

classComplex

!

doublere,im;

public:

Complex(doubler,doublei):re(r),im(i){}

doublereal()const{returnre;}

doubleimage()const{returnim;)

Complex&operator+=(Complexa)

(

re+=a.re;

im+=a.im;

return*this;

)

);

ostream&operator?(ostream&s,constComplex&z)

returns?'('?z.real()?,,,?z.image()?')';

intmain()

(

Complexx(l,-2),y(2,3);

cout?(x+=y)?endl;

return0;

)

執(zhí)行這個程序的輸出結(jié)果是

A)(1,-2)B)(2,3)

C)(3,5)D)(3,1)

(35)有如下類聲明:

classSAMPLE

(

intn;

public:

SAMPLE(inti=0):n(i){}

voidsetValue(intnO);

);

下列關(guān)于getValue成員函數(shù)的實現(xiàn)中,正確的是

A)SAMPLE::setValue(intn0){n=n0;}

B)voidSAMPLE::setValue(intn0){n=n0;}

C)voidsetValue(intn0){n=n0;}

D)(intn0){n=n0;}

(6)有一種只在循環(huán)語句或switch語句中使用的流程控制語句,表示該語句的關(guān)鍵字是

(7)程序中有如下語句

for(inti=0;i<5;i++)cout?*(p+i)?",H;

能夠依次輸出int型一維數(shù)組DATA的前5個元素。由此可知,變量p的定義及初始化

語句是o

(8)若下面程序運行時輸出結(jié)果為

l,A,10.1

2,B,3.5

請將程序補充完整。

#include<iostream>

usingnamespacestd;

intmain()

(

voidtest(int,char,double);

test(l,'A;10.1);

test(2,'B*);

return0;

voidtest(inta,charb,doublec)

cout?a?','?b?,,'<<c?end1;

}

(9)有如下類聲明:

classMyClass

(

inti;

private:intj;

protected:intk;

public:intm,n;

);

其中,私有成員的數(shù)量為。

(10)有如下程序:

#include<iostream>

usingnamespacestd;

classCon

(

charID;

public:

Con():ID(*A'){cout?1;}

Con(charlD):ID(ID){cout?2;}

Con(Con&c):ID(c.getID()){cout?3;}

chargetID()const{returnID;}

);

voidshow(Conc){cout?c.getID();}

intmain()

(

Conc1;

show(cl);

Conc2('B');

show(c2);

return0;

)

執(zhí)行上面程序的輸出是。

(11)有如下程序:

#include<iostream>

usingnamespacestd;

classONE

{

public:

virtualvoidf(){coutvvT;}

);

classTWO:publicONE

public:

TWO(){cout?“2";}

);

classTHREE:publicTWO

(

public:

virtualvoidf(){TWO::f();cout?”3";}

);

intmain()

(

ONEaa,*p;

TWObb;

THREEcc;

p=&cc;

p->f();

return0;

)

執(zhí)行上面程序的輸出是O

(12)有如下程序:

#include<iostream>

usingnamespacestd;

classPARENT

(

public:

PARENT(){cout?“PARENT";}

);

classSON:publicPARENT

(

public:

SON(){cout?"SON";}

);

intmain()

(

SONson;

PARENT*p;

p=&son;

return0;

)

執(zhí)行上面程序的輸出是.

(14)下面是復數(shù)類complex的定義,其中重載的運算符“+”的功能是返回一個新的復數(shù)

對象,其實部等于兩個操作

對象實部之和,其虛部等于兩個操作對象虛部之和;請補充完整:

classcomplex

doublereal;〃實部

doubleimag;〃虛部

public:

complex(doubler,doublei):real(r),imag(i){}

complexoperator+(complexa)

(

returncomplex();

)

};

(15)已知一個函數(shù)模板定義為

template<typenameTl,typenameT2>

T1FUN(T2n){returnn*5.0;}

若要求以int型數(shù)據(jù)7為函數(shù)實參調(diào)用該模板函數(shù),并返回一個double型數(shù)據(jù),則該調(diào)

用應表示為o

一、選擇題

(11)C)(13)B)(14)C)(15)B)

(18)B)(20)D)

(21)D)(22)B)(23)B)(24)C)(25)D)

(26)A)(27)D)(28)C)(29)D)(30)B)

(32)B)(33)D)(35)B)

二、填空題

(6)break

(7)int*p=DATA

(8)=3.5

(9)2

(10)13A23B

(11)2213

(12)PARENTSON

(14)[14]real+a.real,imag+a.imag

(15)[15]FUN<double>(7)或FUN<double,int>(7)

三、完善程序

1.用指針和數(shù)組來顯示Fibonacci數(shù)列。

#include<iostream>

usingnamespacestd;

intmainO

inti,fibon[10]={0,1,1,2,3,5,8,12,21,34},*pfibl,*pfib2;

pfibl=pfib2=;

cout<<〃使用數(shù)組顯示Fibonacci數(shù)列〃<<endl;

for(i=0;i<10;i++)

cout<<fibon[i]\t?<<?endl;

cout<<〃使用指針顯示Fibonacci數(shù)列〃<<endl;

for(i=0;i<10;i++)

cout?*(fibon+i)\t??<<endl;

cout?〃顯示指針相減,應為數(shù)組長度:〃;

cout<<pfib2-pfibl<<endl;

return0;

2.定義一個矩形類Rectangle,數(shù)據(jù)成員有:長a,寬b,均為整型,

私有訪問權(quán)限。對其操作有:輸入操作、計算周長perimeter、計算

面積和輸出操作。

classRectangle

inta,b;

public:

voidinput();

voidperimeter();

voidarea();

voidoutput();

}sl;

input()〃成員函數(shù)中訪問類中的數(shù)據(jù)成員

{

cout?〃請輸入矩形的長和寬:〃;

cin>>a?b;

)

voidRectangle::perimeter()〃注意類名不可省

(

cout<<〃其周長為:,z<<(a+b)*2<<endl;

)

voidRectangle::area()

cout<〈〃其面積為:〃<<a*b?endl;

voidRectangle::output()

perimeter();〃在成員函數(shù)中可以調(diào)用其他成員函數(shù)

voidmain()

(

si.input();

si.output();

)

3.用有參構(gòu)造函數(shù)來完成學生類的編寫。

#include<iostream>

usingnamespacestd;

classstudent

(

private:

charm_num[5];

floatmscore;

public:

student(charnum[5],floatscore);

voidoutput();

);

student::student(charnum[5],floatscore)

m_score=score;

)

voidstudent::output0

(

cout<<m_num<<,'<<m_name<<,’<<m_score?endl;

)

voidmain()

〃創(chuàng)建類對象,學號330105002,分數(shù)80

si.output();

4.完成商品類對象的應用實例。

#include<iomanip>

#include<string>

usingnamespacestd;

classCGoods

{

private:

charName[21];

intAmount;

floatPrice;

floatTotal_value;

public:

CGoods();〃無參構(gòu)造函數(shù)

CGoods(char[],int,float);/*有參構(gòu)造函數(shù),參數(shù)個數(shù)為3*/

voidCountTotal(void);/*函數(shù)聲明時,形參中可以只有返回類型*/

floatGetTotal_value(void);

);

CGoods::CGoods()

{

Name[0]=,\0,;Price=0.0;

Amount=0;Total_value=0.0;

)

CGoods::CGoods(charname[],intamount,floatprice)

Price=price;

Amount=amount;

Total_value=price:icamount;

)

voidCGoods::CountTotal()

(

Total_value=____________________

)

floatCGoods::GetTotal_value()

)

intmain()

CGoodsCarl(〃夏利〃,30,98000.0);

cout?setw(10)?Carl.GetPrice()<<setw(20)<<Carl.GetTotal_value()<

<endl;

return0;

)

5.打印楊輝三角形,要求使用類及多文件結(jié)構(gòu)實現(xiàn)。

/*頭文件:yang,h*/

classyang

{

inta[20][20];

intn;

public:

yang(int1=10);

voidshow();

};

/*源文件:yang.cpp*/

#include<iostream>

ttinclude<iomanip>

usingnamespacestd;

yang::yang(int1)

(

inti,j;

n=K=10?l:10;

for(i=0;i<n;i++)

for(j=0;j<=i;j++)

(

if()

a[i]

else

voidyang::show()

{

inti,j,k;

for(i=0;i<n;i++)

for(k=30-3*i;k>=0;k—)

COUt<<Z/

for(j=0;j<=i;j++)

cout<<setw(6)?a[i][j];

cout<<endl;

)

)

/*源文件:test,cpp*/

#include<iostream>

#include"yang.h"

usingnamespacestd;

voidmain()

(

inti;

yangs[4];

for(i=0;i<4;i++)

(

s[i]=(i+3);

s[i].show();

)

)

6.(1)使用指針作為函數(shù)參數(shù)交換兩個變量的值。

#include<iostream>

usingnamespacestd;

voidchange(int*pl,int*p2)/*以指針作為形參*/

(

intt;

t=*pl;

*p2=t;

)

voidmain()

intx=100,y=40;

cout<<〃交換前的值為〃<Xendl;

cout<<x<<55<<y<<endl;

cout?”交換后的值為“《endl;

cout?x<<,)?y<<endl;

)

(2)使用引用作為形參交換兩個變量的值。

#include<iostream>

usingnamespacestd;

voidchange(int&s,int&d)

(

intt;

t=s;

s=d;

d=t;

)

voidmain()

(

intx=100,y=40;

cout<X〃交換前的值為〃<Xendl;

cout<<x<<?5<<y<<endl;

cout<<〃交換后的值為〃。endl;

cout<<x<<?J<<y<<endl;

)

7.從鍵盤上輸入字符,寫入文件fl.txt中,'O'表示輸入結(jié)束。

voidmain()

(

ofstreamofile;

charch;

inti=0;

ofile.open(,ze:\\fl.txt〃);

do{

ch=cin.get();

ofile<<setw(3)?ch;

i++;

if(i==10)

ofile?endl;

i=0;

}while(ch!='O');

ofile.close();

)

打開文件fl.txt,讀出其內(nèi)容,在屏幕上輸出。

voidmain()

(

ifstreamifile;

charch;

ifile.open(/ze:\\fl.txt〃);

while(ifile.get(ch))

{

cout<<ch;

)

cout<<endl;

ifile.close();

)

8.構(gòu)造CPU類與Computer類,具體成員如右圖所示:

classCPU

(

charcputype[20];

floatcpuprice;

public:

CPU(chara_cputype[]="惠普",floata_cpuprice=234);

CPU(constCPU&a);

voidshow();

~CPU();

);

classComputer

(

CPUk;

charManufacturer[20];

floatPrice;

public:

Computer(chark_Manufacturer[]="達利園",floatk_Price=123,char

k_cputype[]="聯(lián)

”,floatk_cpuprice=7788):___________________________

strcpy(Manufacturer,kManufacturer);

Price=k_Price;

)

voidprint()

{

cout<〈”制造商是:“〈<Manufacturer?endl;

cout<<“電腦價格是:/z<<Price?endl;

-Computer。

{

cout〈<Price<〈”被析構(gòu)了“<〈endl;

}

);

CPU::CPU(chara_cputype[],floatacpuprice)

(一一

strcpy(cputype,a_cputype);

cpuprice=a_cpuprice;

CPU::CPU(constCPU&a)CPUComputer

{%cputype勒CPUk

Acpuprice^>manufecturer

^>price

cpuprice=a.cpuprice+100;?CPU()

?CPU()?Computer。

}?printf)

voidCPU::show()?show。?\Computer。

cout〈〈”CPU的類型:z,?cputype?endl;

cout?”CPU的價格:"<<cpuprice<〈endl;

CPU::"CPU0

{

cout?cputype<〈”被析構(gòu)了"<<endl;

}

voidmainO

Computers;

s.print();

9.定義復數(shù)類,通過重載+運算符進行復數(shù)類對象間的算術(shù)運算。

classcomplex

doublereal,image;

public:

complex(doubler=0,doublei=0);

complexoperator+(constcomplex&a);

voidshow();

);

complex::complex(doubler,doublei)

real=r;

complexcomplex::operator+(constcomplex&a)

{

complextemp;

temp.real=;

temp.image=image+a.image;

return;

)

voidcomplex::show()

(

cout?real?//+/z<<image?//i/z<<endl;

}

voidmain()

{

complexsi(1,1);

si.show();

complexs2(2,2);

s2.show();

complexs3=sl+s2;

s3.show();

)

10.掌握友元函數(shù)的用法,友元函數(shù)與成員函數(shù)的區(qū)別。

重載插入運算符“〈心,實現(xiàn)功能類似于成員函數(shù)print。

classcomplex

(

doublereal,image;

public:

complex(doubler=l.0,doublei=l.0)

(

real=r;image=i;

)

complex&operator+(complex&a);/*形參只有一個*/

friendcomplex&operator+(double&a,complex&b);

friendcomplex&operator+(complex&a,double&b);

(ostream&s,complex&a)〃重載。運算符

(

s<<a.real<<,,+,,?a.image<<z/iz,<<endl;

returns;

)

);

complex&complex::operator+(complex&a)

(

returncomplex();

)

complex&operator+(double&a,complex&b)

(

returncomplex(a+b.real,a+b.image);

)

complex&operator+(complex&a,double&b)

(

returncomplex(a.real+b,a.image+b);

voidmainO

doubles=4.0;

complexc(2.0,2.0),cl,x;

x=c+cl;

cout?x;/*使用友元可以直接寫cout”、、、,不需再調(diào)用*/

x=s+c;

cout?x;

x=cl+s;

cout<<x;

)

11.模板

定義求字符串及類對象的最值的函數(shù)。

usingnamespacestd;

template<typenameT>

Tgetmax(Ta[],intsize)

(

inti;

Tm=a[0];

for(i=l;i<size;i++)

if(a[i]>m)m=a[i]:

returnm;

}

classstudent

(

private:

stringnum;

floatscore;

public:

student(stringnu="21001”,floats_score=90)

{

num=nu;

score=s_score;

}

booloperator>(conststudent&s)

{

returnscore>s.score?true:false;

)

friendostream&operator<<(ostream&s,student&stu)

{

returns<<stu.num?,\t'<<stu.score<<endl;

}

friendistream&operator>>(istream&i,student&stu)

cout〈<〃請輸入學生的學號和分數(shù):,/?endl;

returni>>stu.num?stu.score;

voidmain()

(

studentstu[5];

for(inti=0;i<5;i++)

cin>>stu[i];

cout<<getmax(stu,5)<<endl;

}

12.排序模板

#include<iostream>

ttinclude<iomanip>

ttinclude<string>

usingnamespacestd;

classstudent

{

private:

stringnum;

stringname;

public:

student(stringnu="001",stringna="");

voidsetdata(stringnu,stringna);

booloperator>(conststudent&s)

(

returnnum>s.num?true:false;

}

friendostream&operator?(ostream&s,student&stu);

};

student::student(stringnu,stringna)

(

num=nu;

name=na;

}

voidstudent::setdata(stringnu,stringna)

{

num=nu;

name=na;

}

ostream&operator<<(ostream&s,student&stu)

{

returns<<stu.num<<>\t'<<stu.name?endl;

)

template<typenameT>

voidBubbleSort(Ta[],intn)

inti,j;

Ttemp;

for(i=0;i<n-l;i++)〃輪數(shù)

for(j=0;j<n-i-l;j++)〃每輪兩兩比較的次數(shù),即下標

if(a[j]>a[j+l])//a[j]>a[j+l]大于重載,如果改為

a[j+l"a[j]是否定確?

(

temp=a[j];

a[j]=a[j+l];

a[j+l]=temp;

)

}

template<typenameT>

voidprint(Ta[],intn)

{

for(inti=0;i〈n;i++)

cout?setw(8)<<a[i];

cout?endl;

}

voidmain()

(

/*inta[10]={2,13,4,35,6,47,24,657,20,9}:

print(a,10);

BubbleSort(a,10);

floatb[5]={2.4,3.1,1.3,23.6,12.9};

BubbleSort(b,5);

print(b,5);*/

stringnum[5]=n(H〃,〃012”,〃203〃,“404”,〃415"}:

stringname[5]={〃張三",〃李四",〃王五〃,〃趙六〃,“丁七〃};

studentstu[5];

for(inti=0;i<5;i++)

stu[i].setdata(num[i],name[i]);

BubbleSort(stu,5);〃出錯,原因是排序時不知道以姓名還是學號排

解決〉運算符重載

print(stu,5);

)

13.動態(tài)內(nèi)存分配

完成類中聲明的各函數(shù)定義,并在主函數(shù)中測試。

template<typenameT>

classAr

(

T*par;

public:

Ar(intn);〃動態(tài)申請n個T類型的數(shù)組元素空間

voidinput(intn);〃輸入n個數(shù)據(jù)

voiddisplay(intn);//輸出數(shù)組中n個元素

~Ar();〃撤消數(shù)組空間

);

參考代碼:

#include<iostream>

usingnamespacestd;

template<typenameT>

classArray

(

T*parray;

public:

Array(intn);〃動態(tài)申請n個T類型的數(shù)組元素空間

voidinput(intn);〃輸入n個數(shù)據(jù)

voiddisplay(intn);〃輸出數(shù)組中n個元素

“Array。;〃撤消數(shù)組空間

template<typenameT>Array<T>::Arr

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論