问答题请使用“答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2,其中声明了CDeep-Copy类,它是一个用于表示动态数组的类。请编写其中的复制构造函数。 要求:补充编制的内容写在//********333********与//********666********两行之间,不得修改程序的其他部分。 注意:程序最后已经将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 //源程序 #include"CDeepCopy.h" CDeepCopy::~CDeepCopy()delete[]p; CDeepCopy::CDeepCopy(int k)n=k; p=new int[n]; //构造函数实现 CDeepCopy::CDeepCopy(const CDeepCopy&r) //复制构造函数 //********333******** //********666******** int main() CDeepCopy a(2),d(3); a.p[0]=1; d.p[0]=666; //对象a、d数组元素的赋值 CDeepCopy b(a); a.p[0]=88; cout<<b.p[0]; //显示内层局部对象的数组元素 cout<<d.[0]; //显示d数组元素a.[0]的值 cout<<"d fade away; /n";cout<<a.p[0]; //显示a数组元素a.p[0]的值 //writeToFile("K://bl0//61000101//"); return 0:
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义私有数据成员year、month、day,分别用于年、月、日,它们都是int型的数据,请在注释
∥********1********后添加适当的语句。
(2)完成判断数据成员date2是否是闰年函数intDate::IsLeapYear0的定义,返回数据成员date2是否是闰年的值,是则返回1,否则返回0。请在注释 ∥********2********后添加适当的语句。
(3)设置SetData0的默认时间为:2004.12.1,请在注释 ∥********3********后添加适当的语句。
(4)完成主函数main(),完成输出date2 is aleapyear,请在注释 ∥********4********后添加适当的语句。
提示:公元纪年的年数可以被四整除,即为闰年;被100整除而不能被400整除为平年;被100整除也可被400整除的为闰年。
注意:增加或修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
#include
using namespace std;
Class Date
{
public:
∥********3********
void SetDate(int y,int m,int n);
int IsLeapYear();
void Print()
{cout<
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序。函数convert(char*stroct)的功能是将八进制转换为十进制。
提示:要每一位转换,然后把转换后得到的数累加起来即可。
注意:不能修改其他部分的代码。
#include
#include
int convert(char*stroct)
{
}
int main()
{
cout<
问答题使用VC6打开考生文件夹下的工程test23_3,此工程包含一个源程序文件test23_3.cpp,其中定义了类X和它的友元类Y,但它们的定义并不完整。请按要求完成下列操作,将程序补充完整。 (1)将类X的静态数据成员y初始化为1。请在注释“//**1**”之后添加适当的语句。 (2)完成类Y的构造函数Y(int i,int j)的定义,将参数i赋值给类Y的数据成员,X的对象a中的数据成员x,并且把参数j赋值给类X的数据成员y,请在注释“//**2**”之后添加适当的语句。 (3)完成类Y的成员函数void Display()的定义,将Y的数据成员,X的对象a中的数据成员x和类X的数据成员y按照“x=_,y=_”的形式显示出来(“_”代表一个数字),请在注释“//**3**”之后添加适当的语句。 输出结果如下: x=5,y=l x=6,y=9 x=5,y=9 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件test23_3.cpp清单如下: include<iostream.h> class X friend class Y; public: void Set(int i) x=i; void Display() cout<<"x="<<x<<","; cout<<"y="<<y<<endl; private: int x; static int y; ; class Y public: Y(int i, int j) void Display(); private: X a; ; //**1** Y::Y(int i, int j) //**2** void Y::Display() //**3** void main ( ) X b; b.Set(5); b.Display(); Y c(6,9); c.Display(); b.Display();
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,并使程序输出的结果为:TestClass2TestClass3注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//******error******的下面。#include<iostream>class TestClass1{publiC:virtual void fun()=0;};class TestClass2:public TestClassI{//********error********int fun(){using namespace std;cout<<"TestClass2"<<endl;}};class TestClass3:public TestClassi{//********error********void fun(){//********error********cout<<"TestClass3"<<endl;}};vcid main(){TestClass1 *p;TestClass2 obj1;TestClass3 obj2;p=&obj1;p->fun();obj2.fun();return;}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能: (1)完成CMyClass的构造函数,请改正注释//********1********后的错误。 (2)完成fun()函数定义,请改正注释//********2********后的错误。 (3)初始化CMyClass的成员变量Number3为4,请改正注释//********3********后的错误。 (4)完成fun()函数,请改正注释//********4********后的错误。 输出的结果为: Number1=12 Number2=18 Number3=16 Number1=4 Number2=2 Number3=19 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#include<iostream.h>class CMyClass{public: //********1******** CMyClass(int i,int j) { Number1=i; Number3+=i; } void display() { cout<<"Numberl="<<Number1<<endl; cout<<"Number2="<<Number2<<endl; cout<<"Number3="<<Number3<<endl; } Void AddNum(int i) { Number3+=i; }private: int Number1; const int Number2; StatiC int Number3; //********2******** }; //********3******** Void func() { CMyClass obj(1,2); obj.Number1=4; //********4******** obj.AddNum(); obj.display();}Void main(){ CMyClass myObj(12,18); myObj.display(); func(); return; }
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成以下函数:int factorial(int n):求出n的阶乘,必须使用递归调用。如果n小于1则返回0。注意:不能修改函数的其他部分。#include<iostream.h>#include<ctype.h>int factorial(int n){}void main(){cout<<factorial(1)<<endl;cout<<factorial(5)<<endl;cout<<factorial(0)<<endl;return;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中定义了Shape类和Point类。Shape类表示抽象的形状,其成员函数draw声明了显示形状的接口。Point是Shape的派生类,表示平面直角坐标系中的点,其成员函数draw用于在屏幕上显示Point对象;成员函数distance用于计算两个点之间的距离。提示:在平面直角坐标系中,点(x1,y2)和点(x2,y2)之间的距离为:d=;标准库函数spn用于求平方根。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:(3,0)(0,4)Distance=5注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。#include<iostream>#include<math.h>usingnamespacestd;classShape{public://**********found**********______virtual~Shape(){}};classPoint:publicShape{public:Point(doublex,doubley):x_(x),y_(y){}virtualvoiddraw()const;//**********found**********doubledistance(______)const{returnsqrt((x_-pt.x_)*(x_-pt.x_)+(y_-pt.y_)*(y_-pt.y_));}private://**********found**********______};voidPoint::draw()const{//**********found**********cout<<'('<<______<<')'<<endl;}intmain(){Point*pt1=newPoint(3,0);Point*pt2=newPoint(0,4);Shape*s=pt1;a->draw();s=pt2;a->draw();cout<<"Distance="<<pt1->distance(*pt2)<<endl;deletept1;deletept2;return0;}
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为:
TestClass 1
TestClass2
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在 ∥********error********的下面。
#include
#include
struct TestClas0
{
∥********error********
virtual void fun();
};
class TestClassl:public
TestClass0
{
void fun()
{
coutfun();
∥********error********
P=*obj 2;
P一>fun();
return;
}
问答题改错题
使用VC6打开考生文件夹下的工程test18_1,此工程包含一个源程序文件test18_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
Enter x and y:
-4 -5
xoffset=1
yoffset=1
angle=45
radius=1.41421
源程序文件test18_1.cpp清单如下:
#include
#include
class point
{
public:
void set(double ix,double iy)
{
x=ix;
y=iy;
}
double xoffset()
{
return x;
}
double yoffset()
{
return y;
}
double angle()
{
return (180/3.14159)*atan2(y,x);
}
/**************** found *******************/
inline radius()
{
return sqrt(x*x+y*y);
}
protected:
double x;
double y;
};
void main()
{
point p;
double x,y;
cout>x>>y;
p.set(x,y);
/**************** found *******************/
p.x+=5;
p.y+=6;
/**************** found *******************/
cout<<"xoffset="<
问答题改错题
使用VC6打开考生文件夹下的工程test3_1,此工程包含一个源程序文件test3_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
A:a=5
display1:a=6
display2:a=5
源程序文件test3_1.cpp清单如下:
#include
class A
{
public:
/**********found**********/
A(){}
void printa(){cout<<"A:a="<
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,输出的结果为:
Constructor,i=O,
Destructor
注意:错误的语句在∥********4********的下面,修改访语句即可。
#include
class CObj
{
int i;
public:
CObj();
void display();
-CObj();
};
∥********error********
CObj:cobj()
{
cout<<“Constructor"‘<“.”,
i=0;
}
{
CObj:display()
{
cout<<“i=”<
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2。其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为: (1,2,3,4,5) (0,0,0,0,0,0) 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 #include<iostream> using namespace std; class VectorBase //向量基类,一个抽象类 int len; public: VectorBase(int len):len(len) int length()constreturn len; //向量长度,即向量中元素的个数 virtual double getElement(int i)const=0;//取第i个元素的值 virtual double sum()const=0; //求所有元素的和 void show()const//显示向量中所有元素 cout<<"("; for(int i=0;i<length()-1;i++) cout<<getElement(i)<<","; //********found******** cout<<______<<")"<<endl;//显示最后一个元素 ; class Vector:public VectorBase //向量类 double*val; public: Vector(int len,double v[]=NULL): VectorBase(len) val=new double[len]; for(int i=0;i<len;i++)val[i]=(v==NuLL?0.0:v[i]); //********found******** ~Vector()______; double getElement(int index)const return val[ index]; double sum()const double S=0.0; //********found******** for(int i=0;i<length();i++)______; return s; ; class ZeroVector:public VectorBase //零向量类 public: ZeroVector(int len):VectorBase double getElement(int index)const______; double sum()constreturn 0.0;; int main() VectorBase * V; double d[]=1,2,3,4,5; v=new Vector(5,d); v->show(); delete V; V=new ZeroVector(6); V->show(); delete V; return 0;
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。完成fun()函数,其功能是:将两个从小到大有序数组a和b,复制合并出一个有序整数序列c,其中形参n和m分别是数组a和b的元素个数。
注意:不能修改程序的其他部分,只能修改fun()函数。
{}include
void fun (int a[],int n,int
b[],int m,int*c)
{
}
void main()
{
int A[]={3,5,7,9,11,1 8,21);
int B[]={6,1 5,1 9,2 1,3 9);
int C[25],i ;
for(i=0;i<2 5;i++)C[i]=0;
cout<<“A[]=”;
for(i=0;i<7;i++)
cout<
问答题请使用VC6或使用【答题】菜单打开proj3下的工程proj3,其中定义的Matrix是一个用于表示矩阵的类。成员函数max_value的功能是求出所有矩阵元素中的最大值。例如,若有3×3矩阵则调用max_value函数,返回值为3。请编写成员函数max_value。要求:补充编制的内容写在“//*******333*******”与“//*******666*******”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//Matrix.h#include<iostream>#include<iomanip>usingnamespacestd;constintM=18;constintN=18;classMatrix{intarray[M][N];public:Matrix(){}intgetElement(inti,intj)const{returnarray[i][j];}voidsetElement(inti,intj,intvalue){array[i][j]=value;}intmax_value()const;voidshow(constchar*s)const{cout<<endl<<s;for(inti=0;i<M;i++){cout<<endl;for(intj=0;j<N;j++)cout<<setw(4)<<array[i][j];}}};voidreadFromFile(constchar*,MatrixvoidwriteToFile(char*,constMatrix//main.cpp#include"Matrix.h"#include<fstream>voidreadFromFile(constchar*f,Matrixif(infile.fail()){cerr<<"打开输入文件失败!";return;}intk;for(inti=0;i<M;i++)for(intj=0;j<N;j++){infile>>k;m.setElement(i,j,k);}}intMatrix::max_value()const{//********333********//********666********}intmain(){Matrixm;readFromFile("",m);m.show("Matrix:");cout<<endl<<"最大元素:"<<m.max_value()<<endl;writeToFile("",m);return0;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹pros3下的工程prog3,其中包含了类P0lynomial(“多项式”)的定义。
形如5x
4
+3.4x
2
一7x+2的代数式称为多项式,其中的5为4次项系数,3.4为2次项系数,一7为1次项系数,2为0次项(常数项)系数。此例缺3次项,意味着3次项系数为0,即省略了0x
3
。在Polynomial中,多项式的各个系数存储在一个名为coef的数组中。例如,对于上面的多项式,保存在coef[0]、coef[1]…coef[4]中的系数依次为:2.0、一7.0、3.4、0.0,5.0,也即对于i次项,其系数就保存在coef[i]中。成员函数getValue计算多项式的值,多项式中x的值是由参数指定的。
请补充完成文件Polynomial.cpp中成员函数getValue的定义。此程序的正确输出结果应为:
Value of pl when x=2.0:59.8
Value of p2 when x=3.0:226.8
注意:只在函数getValue的“//**********333**********”和“//**********666**********”之间填入若干语句,不要改动程序中的其他内容。
//Polynomiac.h
#include
using namespace std; class,Polynomial{ //“多项式”类 public: Polynomial(double coef[],int num):coef(new double[num]),num of terms(num){ for(int i=0;icoef[i]=coef[i]; } 一Polynomial(){delete[]coef;} //返回指定次数项的系数 double getCoefficient(int power) const{return coef[power];} //返回在X等于指定值时多项式的值 double getValue(double x)const; private: //系数数组,coef[0]为0次项(常数项)系数,coef[1]为1次项系数,coef[2]为2次项(平方项)系数,余类推。 double*cogf; int hum_of_terms; }; void writeToFile(const char*path); //Polymomial.cpp #include”Polynomial.h” double Polynomial::getValue(double X)const{ //多项式的值value为各次项的累加和 double value=coef[0]; //********333******** //********666******** return value; } //main.cpp #include”Polynomial.h” int main(){ double pl[]=:{5.0,3.4,-4.0,8.0), p2[]={0.0,一5.4,0.0,3.0,2.0); Polynomial polyl(pl,sizeof(p1)/ sizeof(double)),poly2(p2,sizeof (p2)/sizeof(double)); cout
问答题使用VC6打开下的源程序文件modi1.cpp,该程序运行时有错误,请改正程序中的错误。本程序要求实现的功能为:从键盘输入一个字符串,并将结果保存到文件modi1.txt中。注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//******error******的下面。#include<iostream>#include<fstream>//********error********usingstd;voidWriteFile(char*s){ofstreamout1;//********error********out1.open("modi1.txt",binary|app);for(inti=0;s[i]!=0;i++){//********error********out1.puts(s[i]);}out1.close();}voidClearFile(){ofstreamout1;out1.open("modi1.txt");out1.close();}intmain(){chars[1024];ClearFile();cout"pleaseinputastring:"endl;cin.getline(s,1024);WriteFile(s);return0;}
问答题使用打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)利用define定义常量TRUE为1,定义常量FALSE
为0,请在注释∥********1********后添加适当的语句。
(2)在类A2前增加Al的声明,请在注释∥********2********后添加适当的语句。
(3)在类C1中声明友元函数bool func(A2
return 0;
}
问答题请使用VC6或使用【答题】菜单打开
proj1下的工程proj1,其中在编辑窗口内显示的主程序文件中定义有类AAA和主函数main。程序文本中位于每行“// ERROR ****found****”下面的一行有错误,请加以改正。改正后程序的输出结果应该是:
sum=60
注意:只修改每个“// ERROR ****found****”下面的一行,不要改动程序中的其他任何内容。
#include <iostream>
using namespace std;
class AAA {
int a[10]; int n;
//ERROR *******found*******
private:
AAA (int aa [], int nn): n(nn) {
//ERROR *******found*******
for (int i = 0; i < n; i++) aa[i]=a[i];
}
int Geta (int i) {return a[i];}
};
int main() {
int a[6] = {2,5,8,10,15,20};
AAA x(a,6);
int sum = 0;
//ERROR *******found*******
for (int i=0; i<6; i++)
sum+=x.a[i];
cout << "sum =" << sum << endl;
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,其中有枚举PetType、宠物类Pet和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
There is a dog named Doggie
There is a eat named Mimi
There is an unknown animal named Puppy
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。函数strcpy(char*p,const char*q)的作用是将q指向的字符串复制到p指向的字符数组中。
#include <iostream>
using namespace std;
enum PetType {DOG, CAT, BIRD, FISH, REPTILE, INSECT, OTHER};
class Pet{ //宠物类
PetType type; //类别
char name[20]; //名字
public:
Pet(PetType type, char name[]){
this ->type =type;
//将参数name中的字符串复制到作为数据成员的namer数组中
// ERROR ********** found**********
this ->name =name;
}
// ERROR ********** found**********
PetType getType () const { return PetType; }
const char* getName () const { return name; }
const char* getTypeString()const{
switch (type) {
case DOG: return "a dog";
case CAT: return "a cat";
case BIRD: return "a bird";
case FISH: return "a fish";
case REPTILE: return "a reptile";
case INSECT: return "an insect";
}
// ERROR ********** found**********
return OTHER;
}
void show () const {
cout <<"There is" << getTypeString() <<"named" <<name <<endl;
}
};
int main () {
Pet a_dog (DOG, "Doggie");
Pet a_cat (CAT, "Mimi");
Pet an_animal (OTHER, "Puppy");
a_dog, show ();
a_cat.show();
an_animal, show ();
return 0;
}
