一、改錯題
使用VC6開啟考生資料夾下的工程kt6_1,此工程包含一個源程式檔案kt6_,但該程式執行有問題,請改正程式中的錯誤,使程式的輸出結果如下:
Constructor2
Constructor1
i=0
i=10
Destructor
源程式檔案kt6_清單如下:
#include
using namespace std;
class CSample
{
int i;
public:
CSample(){cout<<"Constructor1"<
CSample(int val){cout<<"Constructor2"<
~CSample(){cout<<"Destructor"<
void disp();
};
/**********found**********/
void disp()
{ cout<<"i="<
void main()
{
CSample *a,b(10);
/**********found**********/
a->disp();
/**********found**********/
b->disp();
}
【參考答案】
(1)將void disp()
改為:void CSample::disp()
(2)將a->disp();
改為:a=new CSample; a->disp();
(3)將b->disp();
改為:();
【試題解析】
(1)主要考查類成員函式定義格式的熟練掌握,對於類體外函式的實現,應該使用作用域符"::",按照返回值型別類名::函式名(引數列表)的形式進行說明;
(2)主要考查對動態儲存分配的掌握,根據前面的定義,a是一個指標型別的變數,指向一個物件,但是並沒有被初始化,此時a中的資料無任何意義,應該使用動態儲存分配new生成一個新的物件,並將返回的指標賦值給a;
(3)主要考查物件指標與物件在呼叫成員函式時格式的不同,b是一個物件變數,使用b呼叫成員函式應該用"."運算子。
修改後程式碼:
#include
using namespace std;
class CSample
{
int i;
public:
CSample(){cout<<"Constructor1"<
CSample(int val){cout<<"Constructor2"<
~CSample(){cout<<"Destructor"<
void disp();
};
/**********found**********/
void CSample::disp()//void disp()
{ cout<<"i="<
void main()
{
CSample *a,b(10);
/**********found**********/
a=new CSample;a->disp();
/**********found**********/
();
}
二、簡單應用題
編寫函式fun(),它的功能是利用以下所示的簡單迭代方法求方程cos(x)-x=0的一個實根。
xn+1=cos(xn)
迭代步驟如下:
(1)取x1初值為0.0。
(2)x0=x1,把x1的值賦給x0。
(3)x1=cos(x0),求出一個新的x1。
(4)若x0-x1的絕對值小於0.000001,則執行步驟(5),否則執行步驟(2)。
(5)所求x1就是方程cos(x)-x=0的一個實根,做為函式值返回。
程式輸出結果Root=0.739085。
注意:部分源程式已存在檔案kt6_中。
請勿改動主函式main和其他函式中的任何內容,僅在函式fun的花括號中填入所編寫的若干語句。
檔案kt6_2的內容如下:
#include
#include
using namespace std;
float fun()
{
}
void main(){
cout<<"Root="<
}
【參考答案】
float fun()
{
float x1=0.0,x0;
do {
x0=x1;
x1=cos(x0);}
while(fabs(x0-x1)>=1e-6);
return x1;
}
【試題解析】解答本題的關鍵之處在於看清題中所給的“迭代步驟”,同時要理解xn+1=cosxn通式的含義,要考慮到x1的初值為0.0。
fabs(x0-x1)>=0.000001和fabs(x0-x1)>=1e-6的輸出結果一致,
但是如果沒有fabs,最後輸出結果會直接為Root=1而非Root=0.739085,
ps:fabs(x)為對x求絕對值。說明:計算|x|,當x不為負時返回x,否則返回-x。abs和fabs是一對常用的數學函式,abs是整數取絕對值,而fabs主要用於求浮點數的絕對值。
#include
#include
using namespace std;
float fun()
{
float x1=0.0,x0;
do
{
x0=x1;
x1=cos(x0);
}while(fabs(x0-x1)>=0.000001);
return x1;
}
void main(){
cout<<"Root="<
}
三、綜合應用題
使用VC6開啟考生資料夾下的工程kt6_3,此工程包含一個源程式檔案kt6_,其中定義了用於表示考生的類Student,請按要求完成下列操作,將程式補充完整。
(1)定義私有資料成員code、english分別用於表示考生的編號、英語成績、它們都是int型的資料。
。請在註釋“//**1**”之後新增適當的語句。
(2)完成成員函式voidStudent::inputinformation()的定義,該函式用於使用者輸入一個考生物件的資訊,輸入格式如下所示:
輸入編號:
英語成績:
計算機成績:
請在註釋“//**2**”之後新增適當的語句。
(3)利用已實現的類Student的成員函式,完成函式voidfirstname(Student*A[],intnum)的定義,該函式根據考生資訊A[],輸出num個考生中總分最高者的編號及其相應的'總分,在此不考慮總分相同的情況。請在註釋“//**3**”之後新增適當的語句。
注意:除在指定位置新增語句之外,請不要改動程式中的其他內容。
源程式檔案kt6_清單如下:
#include
using namespace std;
class Student
{
private:
//**1**
int computer;
int total;
public:
void getinformation();
void computesum();
int getcode();
int gettotalscore();
~Student();
};
void Student::getinformation()
{
//**2**
cout<<"英語成績:";
cin>>english;
cout<<"計算機成績:";
cin>>computer;
}
void Student::computesum()
{
total=english+computer;
cout<<"編號"<
}
int Student::getcode()
{
return code;
}
int Student::gettotalscore()
{
return total;
}
void firstname(Student *A[],int num)
{
//**3**
tempsum=(*A[0])otalscore();
for(int i=1;i
{
if(((*A[i])otalscore())>tempsum)
{
tempcode=(*A[i])ode();
tempsum=(*A[i])otalscore();
}
}
cout<<"總分最高者--"<
}
void main()
{
Student*A[3];
int i,n=3;
for(i=0;i
{
A[i]=new Student;
A[i]->getinformation();
}
for(i=0;i
{
A[i]->computesum();
}
firstname(A,3);
}
【參考答案】
(1)int code;
int english;
(2)cout<<"輸入編號:";
cin>>code;
(3)int tempcode,tempsum;
tempcode=(*A[0])ode();
【試題解析】
本題是對C++程式設計的綜合考查,類的成員及成員函式的定義與呼叫,資料的輸入輸出,for迴圈語句,if條件判斷語句等多個知識點,其中(3)中為指標陣列的使用,指標陣列是一組指標,每一個成員都按照指標的操作規則,但是整個訪問規則仍然使用陣列下標方式,如A[0]指的是第一個指標,而* A[0]是取出第一個指標指向的內容。
#include
using namespace std;
class Student
{
private:
//**1**
int code;
int english;
int computer;
int total;
public:
void getinformation();
void computesum();
int getcode();
int gettotalscore();
~Student();
};
void Student::getinformation()
{
//**2**
cout<<"輸入編號:";
cin>>code;
cout<<"英語成績:";
cin>>english;
cout<<"計算機成績:";
cin>>computer;
}
void Student::computesum()
{
total=english+computer;
cout<<"編號"<
}
int Student::getcode()
{
return code;
}
int Student::gettotalscore()
{
return total;
}
void firstname(Student *A[],int num)
{
//**3**
int tempsum,tempcode;
tempcode=(*A[0])ode();
tempsum=(*A[0])otalscore();
for(int i=1;i
{
if(((*A[i])otalscore())>tempsum)
{
tempcode=(*A[i])ode();
tempsum=(*A[i])otalscore();
}
}
cout<<"總分最高者--"<