二、基本操作題(18分)
41、清使用VC6或使用【答題】菜單打開考生文件夾projl下的工程projl,此工程中包含了類Pets(“寵物”) 和主函數(shù)main的定義。程序中位于每個(gè)“//ERROR****found ****”之后的一行語句有錯(cuò)誤,請加以改 正。改正后程序的輸出結(jié)果應(yīng)為:
Name:sonny Type:dog
Name:John Type:dog
Name:Danny Typc:cat
Name:John Type:dog
注意:只修改每個(gè)“//ERROR ****found ****”下的那一行,不要改動(dòng)程序中的其他內(nèi)容。
#include
using namespace sm; enum Pets_type{d09,cat,bird,fish};
class Pets{ private:
char *name; Pets_type type; public:
Pets(const char *name=”sonny”,Pets_type type=dog);
Pets&operator=(const Pets&s);
~Pets();
void show()eonst;};
Pets::Pets(eonst char$naIne,Pets_type type)
//構(gòu)造函數(shù) {
This ->name=new char[strlen(name)+1];
strcpy(this一>name,name);
//ERROR *********found********* type=type;
}{ Pets::~Pets()//析構(gòu)函數(shù),釋放name所指向的字符串
{ //ERROR *********found********* name=’/0‘;
}
Pets&Pets::0perator=(const Pets&s){
if(&s==this)//確保不要向自身賦值 return *this;
delete[]name;
name=new char[strlen(S.name)+1];//ERROR *********found*********
strcpy(this一>nmne,name); type=S.type:
return *this;}
void Pets::show()const
cout<<“Name:”<
三、簡單應(yīng)用題(24分)
42、請使用VC6或使用【答題】菜單打開考生文件夾proj2下的工程proj2,該工程中包含一個(gè)程序文件main.Cpp,其中有坐標(biāo)點(diǎn)類point、線段類Line和三角形類Triangle的定義.還有main函數(shù)的定義。程序中兩

Side l:9.43398 Side 2:5
Side 3:8 area:20 注意:只在橫線處填寫適當(dāng)?shù)拇a,不要改動(dòng)程序中的其他內(nèi)容,也不要?jiǎng)h除或移動(dòng)“//****found****”。
#inClude #nClude using namespaCe std;
Class Point{//坐標(biāo)點(diǎn)類 publiC:
Const double x,y;
Point(double x=0.0,double Y=0.0):x(x),Y(Y){} //**********found**********
double distanCeTo(_____) Const{
//到指定點(diǎn)的距離
return sqrt((x-P.X)*(x-P.x)+(Y-P.Y)*(Y-P.Y)); }
};
Class Line{//線段類 publiC:
eonst Point pl,p2;//線段的兩個(gè)端點(diǎn)
//**********found**********
Line(Point pl,Point p2):——{}
double length()eonst/retum pl.distanCeTo(p2);}//線段的長度 };
Ct彝SS Triangle{//三角形類 publiC:
Const Point pl,p2,p3;//三角形的三個(gè)頂點(diǎn) //**********found**********
Triangle(_____):pl(p1),p2(p2),p3(p3){} double lengthl()Const{//邊pl,p2的長度
retum Line(pl,p2).1ength(); }
double length2()Const{//邊p2,p3的長度 return Line(p2,p3).1ength();
}
double length3()Const{//邊p3,pl的長度 returnLine(p3,pl).1ength();
}
double area()Const{//三角形面積
//**********found********** double s=_____;
return sqrt(s{(S-lengthl())*(s-length2())*(s—length3())); }
};
int main(){
Triangle r(Point(0.0,8.0),Point(5.0,0.0),Point(0.0,0.0)); Cout<<”Side l:”< Cout<<”Side 2:”< retum 0;
四、綜合應(yīng)用題(18分)
43、
請使用VC6或使用【答題】菜單打開考生文件夾proj3下的工程proj3,其中聲明的DataList類,是一個(gè)用于表示數(shù)據(jù)表的類。DataList的重載運(yùn)算符函數(shù)0perator+,其功能是求當(dāng)前數(shù)據(jù)表與另一個(gè)相同長度的數(shù)據(jù)表之和;即它返回一個(gè)數(shù)據(jù)表,其每個(gè)元素等于相應(yīng)兩個(gè)數(shù)據(jù)表對(duì)應(yīng)元素之和。請編寫這個(gè)operator+函數(shù)。程序的正確輸出應(yīng)該是:
兩個(gè)數(shù)據(jù)表: 1,2,3,4,5,6 3,4,5,6,7,8 兩個(gè)數(shù)據(jù)表之和:
4,6,8,10,12,14 要求:
補(bǔ)充編制的內(nèi)容寫在“//********333********”與“//********666********”之間,不得修改程序
的其他部分。
注意:程序最后將結(jié)果輸出到文件。ut.dat中。輸出函數(shù)writeToFile已經(jīng)編譯為。bj文件,并且在本程序中調(diào)用。
//DataList.h
#inClude using namespaCe std;
ClaSS DataList{//數(shù)據(jù)表類 intfen;
double*d; publiC:
DataList(int len,double data[]=NULL); DataList(DataList&data);
int length()Const{retum len;}
double getElement(int i)Constt return d[i];}
DataList operator+(Const DataList&list)Const;//兩個(gè)數(shù)據(jù)表求和 void show()Const;//顯示數(shù)據(jù)表
};
void writeToFile(Char$,Const DataList&); //main.Cpp
#inClude”DataList.h”
DataList::DataList(int len,double data[]):len(ien){ d=new double[1en];
for(int i=0;i d㈨i=(data==NULL?0.0:data[i]);
}
DataList::DataList(DataList&data):len(data.1en){ d=new double[1en];
for(int i=0;i
DataList DataList::0perator+(Con8t DataList&list,)Const{//兩個(gè)數(shù)據(jù)表求和 double*dd=new double[1ist.1ensth()];
//********333********
//********666********
return DataList(1ist.1ength(),dd); }
void DataList::show()Const{//顯示數(shù)據(jù)表 f()r(int。i=0;i eout< Cout<
int main(){
double sl[]={1,2,3,4,5,6};
double s2[]={3,4,5,6,7,8};
DataList list1(6,sl),list2(6,s2);//定義兩個(gè)數(shù)據(jù)表對(duì)象 eout<<”兩個(gè)數(shù)據(jù)表:”< listl.show(): list2.show(); eout< (1istl+list2).show();
writeToFile(””,listl+list2); return 0:
41、清使用VC6或使用【答題】菜單打開考生文件夾projl下的工程projl,此工程中包含了類Pets(“寵物”) 和主函數(shù)main的定義。程序中位于每個(gè)“//ERROR****found ****”之后的一行語句有錯(cuò)誤,請加以改 正。改正后程序的輸出結(jié)果應(yīng)為:
Name:sonny Type:dog
Name:John Type:dog
Name:Danny Typc:cat
Name:John Type:dog
注意:只修改每個(gè)“//ERROR ****found ****”下的那一行,不要改動(dòng)程序中的其他內(nèi)容。
#include
using namespace sm; enum Pets_type{d09,cat,bird,fish};
class Pets{ private:
char *name; Pets_type type; public:
Pets(const char *name=”sonny”,Pets_type type=dog);
Pets&operator=(const Pets&s);
~Pets();
void show()eonst;};
Pets::Pets(eonst char$naIne,Pets_type type)
//構(gòu)造函數(shù) {
This ->name=new char[strlen(name)+1];
strcpy(this一>name,name);
//ERROR *********found********* type=type;
}{ Pets::~Pets()//析構(gòu)函數(shù),釋放name所指向的字符串
{ //ERROR *********found********* name=’/0‘;
}
Pets&Pets::0perator=(const Pets&s){
if(&s==this)//確保不要向自身賦值 return *this;
delete[]name;
name=new char[strlen(S.name)+1];//ERROR *********found*********
strcpy(this一>nmne,name); type=S.type:
return *this;}
void Pets::show()const
cout<<“Name:”<
三、簡單應(yīng)用題(24分)
42、請使用VC6或使用【答題】菜單打開考生文件夾proj2下的工程proj2,該工程中包含一個(gè)程序文件main.Cpp,其中有坐標(biāo)點(diǎn)類point、線段類Line和三角形類Triangle的定義.還有main函數(shù)的定義。程序中兩

Side l:9.43398 Side 2:5
Side 3:8 area:20 注意:只在橫線處填寫適當(dāng)?shù)拇a,不要改動(dòng)程序中的其他內(nèi)容,也不要?jiǎng)h除或移動(dòng)“//****found****”。
#inClude #nClude using namespaCe std;
Class Point{//坐標(biāo)點(diǎn)類 publiC:
Const double x,y;
Point(double x=0.0,double Y=0.0):x(x),Y(Y){} //**********found**********
double distanCeTo(_____) Const{
//到指定點(diǎn)的距離
return sqrt((x-P.X)*(x-P.x)+(Y-P.Y)*(Y-P.Y)); }
};
Class Line{//線段類 publiC:
eonst Point pl,p2;//線段的兩個(gè)端點(diǎn)
//**********found**********
Line(Point pl,Point p2):——{}
double length()eonst/retum pl.distanCeTo(p2);}//線段的長度 };
Ct彝SS Triangle{//三角形類 publiC:
Const Point pl,p2,p3;//三角形的三個(gè)頂點(diǎn) //**********found**********
Triangle(_____):pl(p1),p2(p2),p3(p3){} double lengthl()Const{//邊pl,p2的長度
retum Line(pl,p2).1ength(); }
double length2()Const{//邊p2,p3的長度 return Line(p2,p3).1ength();
}
double length3()Const{//邊p3,pl的長度 returnLine(p3,pl).1ength();
}
double area()Const{//三角形面積
//**********found********** double s=_____;
return sqrt(s{(S-lengthl())*(s-length2())*(s—length3())); }
};
int main(){
Triangle r(Point(0.0,8.0),Point(5.0,0.0),Point(0.0,0.0)); Cout<<”Side l:”< Cout<<”Side 2:”< retum 0;
四、綜合應(yīng)用題(18分)
43、
請使用VC6或使用【答題】菜單打開考生文件夾proj3下的工程proj3,其中聲明的DataList類,是一個(gè)用于表示數(shù)據(jù)表的類。DataList的重載運(yùn)算符函數(shù)0perator+,其功能是求當(dāng)前數(shù)據(jù)表與另一個(gè)相同長度的數(shù)據(jù)表之和;即它返回一個(gè)數(shù)據(jù)表,其每個(gè)元素等于相應(yīng)兩個(gè)數(shù)據(jù)表對(duì)應(yīng)元素之和。請編寫這個(gè)operator+函數(shù)。程序的正確輸出應(yīng)該是:
兩個(gè)數(shù)據(jù)表: 1,2,3,4,5,6 3,4,5,6,7,8 兩個(gè)數(shù)據(jù)表之和:
4,6,8,10,12,14 要求:
補(bǔ)充編制的內(nèi)容寫在“//********333********”與“//********666********”之間,不得修改程序
的其他部分。
注意:程序最后將結(jié)果輸出到文件。ut.dat中。輸出函數(shù)writeToFile已經(jīng)編譯為。bj文件,并且在本程序中調(diào)用。
//DataList.h
#inClude using namespaCe std;
ClaSS DataList{//數(shù)據(jù)表類 intfen;
double*d; publiC:
DataList(int len,double data[]=NULL); DataList(DataList&data);
int length()Const{retum len;}
double getElement(int i)Constt return d[i];}
DataList operator+(Const DataList&list)Const;//兩個(gè)數(shù)據(jù)表求和 void show()Const;//顯示數(shù)據(jù)表
};
void writeToFile(Char$,Const DataList&); //main.Cpp
#inClude”DataList.h”
DataList::DataList(int len,double data[]):len(ien){ d=new double[1en];
for(int i=0;i d㈨i=(data==NULL?0.0:data[i]);
}
DataList::DataList(DataList&data):len(data.1en){ d=new double[1en];
for(int i=0;i
DataList DataList::0perator+(Con8t DataList&list,)Const{//兩個(gè)數(shù)據(jù)表求和 double*dd=new double[1ist.1ensth()];
//********333********
//********666********
return DataList(1ist.1ength(),dd); }
void DataList::show()Const{//顯示數(shù)據(jù)表 f()r(int。i=0;i eout< Cout<
int main(){
double sl[]={1,2,3,4,5,6};
double s2[]={3,4,5,6,7,8};
DataList list1(6,sl),list2(6,s2);//定義兩個(gè)數(shù)據(jù)表對(duì)象 eout<<”兩個(gè)數(shù)據(jù)表:”< listl.show(): list2.show(); eout< (1istl+list2).show();
writeToFile(””,listl+list2); return 0: