二、基本操作題(18分)
41、請使用VC6或使用【答題】菜單打開考生文件夾projl下的工程projl,該工程含有一個(gè)源程序文件pn,jl.cpp。其中位于每個(gè)注釋“//ERROR 木}}水found}}水木”之后的一行語句存在錯(cuò)誤。請改正這些錯(cuò)誤,使程序的輸出結(jié)果為:
The value is 10
注意:只修改注釋“//ERROR **** found****”的下一行語句,不要改動(dòng)程序中的其他內(nèi)容。
//pmjl.cpp
#include
using namespace std;
class MyClass{
int value;
public:
//ERROR********found*********
void MyClass(int val):value(val){}
int GetValue()const{return value;}
void SetValue(int val);
};
//ERROR********found********
inline void SetValue(int val){value=val;}
int main()
MyClass obj(O);
obj.SetValue(10);
//ERROR********found********下列語句功能是輸出obj的成員value的值
tout<<”The value is”< return 0;
}
三、簡單應(yīng)用題(24分)
42、
請使用VC6或使用【答題】菜單打開考生文件夾proj2下的工程proj2,其中有矩陣基類MatrixBase、矩陣類Matrix和單位陣UnitMatrix的定義,還有main函數(shù)的定義。請?jiān)跈M線處填寫適當(dāng)?shù)拇a并刪除橫線,以實(shí)現(xiàn)上述類定義。此程序的正確輸出結(jié)果應(yīng)為:
1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 注意:只能在橫線處填寫適當(dāng)?shù)拇a,不要改動(dòng)程序中的其他內(nèi)容,也不要?jiǎng)h除或移動(dòng)“//t+++found母
***”。
#nClude using namespaCe std; //矩陣基礎(chǔ)類,一個(gè)抽象類
Class MatrixBase{ int rows,Cols; publiC:
MatrixBase(int rows,int Cols):roWS(rows),Cow(Cols){} int getRows()Const{retum rows;}//矩陣行數(shù)
int getCols()Const{return Cols;}//矩陣列數(shù)
virtual double getElement(int r,int C)Const=0;//取第i個(gè)元素的值 void show()Const{//分行顯示矩陣中所有元素
for(int i=0;i for(int j=0;j } }; //矩陣類
Class Matrix:publiC MatfixBase{ double*val;
publiC: //**********found**********
Matrix(int rows,int Cols,double in[]=NULL):——{ //**********found**********
val=;
for(int i=0:i val[i]=(m==NULL?0.0:Ill[i]);
}
一Matrix(){delete[]val;} double getElement(int r,int C)Const{retum val[r*getCols()+C];}
};
//單位陣(主對角線元素都是l,其余元素都是0的方陣)類 Class UnitMatrix:publiC MatrixBase{
publiC:
UnitMatrix(int rOWS):MatrixBase(rows,rows){} //單位陣行數(shù)列數(shù)相同
double getElement(int r,int C)Const{ //**********found**********
if(_____)retum l.0; retum 0.0;
} }; int main(){
MatrixBase*m;
double d[][5]={{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}}; ITI=new Matrix(3,5,(double*)d);
m→show(); delete m;
Cout< m=new UnitMatrix(6); Ill→show();
delete m; retum0; } .
四、綜合應(yīng)用題(18分)
43、
請使用VC6或使用【答題】菜單打開考生文件夾proj3下的工程proj3,其中聲明的CDeepCopy是一個(gè)用于表示矩陣的類。請編寫這個(gè)類的賦值運(yùn)算符成員函數(shù)0perator=,以實(shí)現(xiàn)深層復(fù)制。
要求:
補(bǔ)充編制的內(nèi)容寫在“//************333************”與“//****************666*************”之間。不得修改程序的其他部分。
注意:程序最后將結(jié)果輸出到文件out.dat中。輸出函數(shù)writeToFile已經(jīng)編譯為obj文件,并且在本程序中調(diào)用。
//CDeepCopy.h
#include
#include
using namespace std;
class CDeepCopy
{
public:
int n;//動(dòng)態(tài)數(shù)組的元素個(gè)數(shù)
int *p;//動(dòng)態(tài)數(shù)組首地址
CDeepCopy(int);
~CDeepCopy();
CDeepCopy&operator=(const CDeepCopy&r);//賦值運(yùn)算符函數(shù)
};
void writeToFile(char});
//main.cpp
#include”CDeepCopy.h”
CDeepCopy::一CDeepCopy(){delete[]P;}
CDeepCopy::CDeepCopy(int k){n=k;p=new int[n];}//構(gòu)造函數(shù)實(shí)現(xiàn)
CDeepCopy&CDeepCopy::0perator=(const CDeepCopy&r)//賦值運(yùn)算符函數(shù)實(shí)現(xiàn)
{
//***********333*********
//***********666***********
}
int main()
{
CDeepCopy a(2),d(3);
a.p[O]=1;d.p[O]=666;//對象a,d數(shù)組元素的賦值
{
CDeepCopy b(3);//調(diào)用賦值運(yùn)算符函數(shù)
a.p[O]=88;b=a;
cout< }
cout< cout<<”d fade away;\n”;
cout< writeToFile(””);
return 0;
}
41、請使用VC6或使用【答題】菜單打開考生文件夾projl下的工程projl,該工程含有一個(gè)源程序文件pn,jl.cpp。其中位于每個(gè)注釋“//ERROR 木}}水found}}水木”之后的一行語句存在錯(cuò)誤。請改正這些錯(cuò)誤,使程序的輸出結(jié)果為:
The value is 10
注意:只修改注釋“//ERROR **** found****”的下一行語句,不要改動(dòng)程序中的其他內(nèi)容。
//pmjl.cpp
#include
using namespace std;
class MyClass{
int value;
public:
//ERROR********found*********
void MyClass(int val):value(val){}
int GetValue()const{return value;}
void SetValue(int val);
};
//ERROR********found********
inline void SetValue(int val){value=val;}
int main()
MyClass obj(O);
obj.SetValue(10);
//ERROR********found********下列語句功能是輸出obj的成員value的值
tout<<”The value is”< return 0;
}
三、簡單應(yīng)用題(24分)
42、
請使用VC6或使用【答題】菜單打開考生文件夾proj2下的工程proj2,其中有矩陣基類MatrixBase、矩陣類Matrix和單位陣UnitMatrix的定義,還有main函數(shù)的定義。請?jiān)跈M線處填寫適當(dāng)?shù)拇a并刪除橫線,以實(shí)現(xiàn)上述類定義。此程序的正確輸出結(jié)果應(yīng)為:
1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 注意:只能在橫線處填寫適當(dāng)?shù)拇a,不要改動(dòng)程序中的其他內(nèi)容,也不要?jiǎng)h除或移動(dòng)“//t+++found母
***”。
#nClude using namespaCe std; //矩陣基礎(chǔ)類,一個(gè)抽象類
Class MatrixBase{ int rows,Cols; publiC:
MatrixBase(int rows,int Cols):roWS(rows),Cow(Cols){} int getRows()Const{retum rows;}//矩陣行數(shù)
int getCols()Const{return Cols;}//矩陣列數(shù)
virtual double getElement(int r,int C)Const=0;//取第i個(gè)元素的值 void show()Const{//分行顯示矩陣中所有元素
for(int i=0;i for(int j=0;j } }; //矩陣類
Class Matrix:publiC MatfixBase{ double*val;
publiC: //**********found**********
Matrix(int rows,int Cols,double in[]=NULL):——{ //**********found**********
val=;
for(int i=0:i val[i]=(m==NULL?0.0:Ill[i]);
}
一Matrix(){delete[]val;} double getElement(int r,int C)Const{retum val[r*getCols()+C];}
};
//單位陣(主對角線元素都是l,其余元素都是0的方陣)類 Class UnitMatrix:publiC MatrixBase{
publiC:
UnitMatrix(int rOWS):MatrixBase(rows,rows){} //單位陣行數(shù)列數(shù)相同
double getElement(int r,int C)Const{ //**********found**********
if(_____)retum l.0; retum 0.0;
} }; int main(){
MatrixBase*m;
double d[][5]={{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}}; ITI=new Matrix(3,5,(double*)d);
m→show(); delete m;
Cout< m=new UnitMatrix(6); Ill→show();
delete m; retum0; } .
四、綜合應(yīng)用題(18分)
43、
請使用VC6或使用【答題】菜單打開考生文件夾proj3下的工程proj3,其中聲明的CDeepCopy是一個(gè)用于表示矩陣的類。請編寫這個(gè)類的賦值運(yùn)算符成員函數(shù)0perator=,以實(shí)現(xiàn)深層復(fù)制。
要求:
補(bǔ)充編制的內(nèi)容寫在“//************333************”與“//****************666*************”之間。不得修改程序的其他部分。
注意:程序最后將結(jié)果輸出到文件out.dat中。輸出函數(shù)writeToFile已經(jīng)編譯為obj文件,并且在本程序中調(diào)用。
//CDeepCopy.h
#include
#include
using namespace std;
class CDeepCopy
{
public:
int n;//動(dòng)態(tài)數(shù)組的元素個(gè)數(shù)
int *p;//動(dòng)態(tài)數(shù)組首地址
CDeepCopy(int);
~CDeepCopy();
CDeepCopy&operator=(const CDeepCopy&r);//賦值運(yùn)算符函數(shù)
};
void writeToFile(char});
//main.cpp
#include”CDeepCopy.h”
CDeepCopy::一CDeepCopy(){delete[]P;}
CDeepCopy::CDeepCopy(int k){n=k;p=new int[n];}//構(gòu)造函數(shù)實(shí)現(xiàn)
CDeepCopy&CDeepCopy::0perator=(const CDeepCopy&r)//賦值運(yùn)算符函數(shù)實(shí)現(xiàn)
{
//***********333*********
//***********666***********
}
int main()
{
CDeepCopy a(2),d(3);
a.p[O]=1;d.p[O]=666;//對象a,d數(shù)組元素的賦值
{
CDeepCopy b(3);//調(diào)用賦值運(yùn)算符函數(shù)
a.p[O]=88;b=a;
cout< }
cout< cout<<”d fade away;\n”;
cout< writeToFile(””);
return 0;
}