请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,其中在编辑窗口内显示的主程序文件中定义有类AAA和主函数main。程序文本中位于每行“//ERROR****found****”下面的一行有错误,请加以改正。改正后程序的输出结果应该是: sum=60 注意:只修改每个“//ERROR****found****”下面的一行,不要改动程序中的其他任何内容。#includeiostreamusing 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;in;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;i6; i++) sum+=x.a[i]; cout"sum="sumendl; return 0; }
请打开考生文件夹下的解决方案文件proj1,此工程中包含一个源程序文件main.cpp,其中有类Book(“书”)和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是: 书名:C++语句程序设计 总页数:299 已把“C++语言程序设计”翻到第50页 已把“C++语言程序设计”翻到第51页 已把“C++语言程序设计”翻到第52页 已把“C++语言程序设计”翻到第51页 已把书合上。 当前页:0 注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。#includeiostreamusing namespace std;class Book{ char*titie; int num pages;//页数 int cur page://当前打开页面的页码,0表示书未打开public://ERROR *******found******* Book(const char*theTitle,int pages)num pageS(pages) { title=new char [ strlen(theTitle)+1 ]; strcpy(title,theTitle); coutendl"书名:"title "总页数:"num pages; } ~Book(){delete[]title;} bool is Closed()const(return cur page==0;} //书合上时返回true.否则返回false bool isOpen()const{return!isClosed();}//书打开时返回true,否则返回false int numOfPages()const{ return num pages;} //返回书的页数 int currentPage()const{return cur page;} //返回打开页面的页码 //ERROR *********found********* void openAtPage(int page no)const{ //把书翻到指定页 coutendl: if(page no1 | | page nonum pages){ cout"无法翻到第"curpage"页。"; close(); } else{ cur_page=page_no; cout"已把“"title"”翻到第"cur page"页"; } } void openAtPreVPage(){ openAtPage(cur_page-1);)//把书翻到上一页 void openAtNextPage(){ openAtPage(cur_page+1);}//把书翻到下一页 void close(){ //把书合上 coutendl; if(isClosed()) cout"书是合上的。"; else{//ERROR *********found********* num pages=0; cout"已把书合上。"; } coutendl; }};int main(){Book book("C++语言程序设计",299); book.openAtPage(50); book.openAtNextPage(); book.openAtNextPage(); book.openAtPrevPage(); book.close(); cout"当前页:"book.currentPage()endl; return 0;}
请使用【答题】菜单命令或直接用VC6打开考生文件夹下的工程prog3,其中声明了ValArray类,该类在内部维护—个动态分配的整型数组。ValArray类的复制构造函数应实现对象的深层复制。请编写ValArray类的复制构造函数。在main函数中给出了一组测试数据,此种隋况下程序的输出应该是: ValArray v1={1,2,3,4,5} ValArray v2={1,2,3,4,5} 要求: 补充编制的内容写在“//*********333*********”与“//*********666*********”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为boj文件,并且在本程序中调用。//ValArray.h#includeiostreamusing namespace std;class ValArray{ int*V; int size;public: ValArray(const int*p,int n):size(n) { v=new int[size]; for(int i=0;isize;i++) v[i]=p[i]; } ValArray(const ValArrayother); ~ValArray(){delete[]v;) void print(ostreamout)const { out'{'; for(int i=0;isize-1 ; i++) out v[i] ","; outv[size-1] '}'; } void setArray(int i,int val) { v[i]=val ; }};void writeToFile(const char*);//main.cpp#include"ValArray.h"ValArray:: ValArray (const ValArrayother){//*********333*********//*********666*********}int main(){ const int a[]={1,2,3,4,5); ValArray vl(a,5); cout"ValArray v1="; v1.print(cout); coutendl; ValArray v2(v1); cout"ValArray v2="; v2.print(cout); coutendl; writeToFile(""); return 0; }
请打开考生文件夹下的解决方案文件proj3,其中包含主程序文件main.cpp和用户定义的头文件Array.h,整个程序包含有XArray类的定义和main主函数的定义。请把主程序文件中的XArray类的成员函数sum( )的定义补充完整,补充的内容填写在"//********333********"与"//********666********"两行之间。经修改后运行程序,得到的输出为:10d=43注意:只允许在"//********333********"和"//********666********"两行之间填写内容,不允许修改其他任何地方的内容。//Array.h#include#includeusing namespace std;class XArray{//数组类int*a;int size;public:XArray(int b[ ],int len):size(len)//构造函数{if(sizea=new int[size];for(int i=0;i}int sum( );//返回数组a[size]中的最大值与最小值之和int length( )const{returnsize;}//返回数组长度~XArray( ){delete[ ]a;}};void writeToFile(const char*);//不用考虑此语句的作用//main.cpp#include"Array.h"//返回数组a[size]中的最大值与最小值之和int XArray::sum( ){//补充函数体//********333********//********666********}void main( ){int s1[10]={23,15,19,13,26,33,18,30,20,10};XArray x(s1,10);intd=x.sum( );coutcoutwriteToFile("c:\\test\\");//不用考虑此语句的作用}
使用VC6打开考生文件夹下的proj1工程目录内的proj1.dsw文件,其中在编辑窗口内显示的主程序文件中定义有Xabc类和主函数main。在程序文本中位于每行”//ERROR*********found*********下面的一行有错误,请加以更正。 更正后程序的输出为:57 注意:只允许修改每个"//ERROR*********found*********下面的一行语句,不允许改动程序中的其他任何内容。#includeiostreamusng namespace std;class Xabc{ int*a;int n; public: Xabc(int aa[],int nn):n(nn){ a=new int[n]; for(int i=0;in;i++) //ERROR*****found****** aa[i]=a[i]; } int GetA(~nt i)COnSt{return a[i];} int SumA(int n); ~xabc(){delete[]a;}};int Xabc::SumA(int n){ int s=0; for(int j=0;jn;j++) s+=a[j]; return s;}int main(){ int a[6]={2,5,8,3,6,9}; Xabc x(a,6); a[3]=19; int d=0; for(int i=0;i6;i++) //ERROR*****found****** d+=x.a[i]; //ERROR*****found****** int f=SumA(5); coutd+fendl; return 0;}
下面叙述不正确的是( )。
请打开考生文件夹下的解决方案文件proj1,此工程包含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: (4,4) 注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。 1 //proj1.cpp 2 #includeiostream 3 using namespace std; 4 class Point{ 5 public: 6 //ERROR ********found******** 7 Point(double x,double y)x (x), y(y){} 8 double GetX()const{return x;} 9 double GetY() const { return y;} 10 //ERROR ******found****** 11 void Hove(double xoff,double yOff)const 12 {_x+=xOff;_y+=yOff;} 13 protected: 14 double _x,_y; 15 }; 16 int main() 17 { 18 Point pt(1.5,2.5); 19 pt.Move(2.5,1.5); 20 //ERROR *******found******* 以下语句输出pt成员x和y的值 21 cout '('pt. x ',' pt._y')'endl; 22 return 0 ; 23 }
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程中包含一个头文件shape.h,其中包含了类Shape、Point和Triangle的声明;包含程序文件shape.cpp,其中包含了类Triangle的成员函数和其他函数的定义;还包含程序文件proj2.cpp,其中包含测试类Shape、Point和Triangle的程序语句。请在程序中的横线处填写适当的代码并删除横线,以实现上述功能。此程序的正确输出结果应为: 此图形是一个抽象图形,周长=0,面积=0 此图形是一个三角形,周长=6.82843,面积=2 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。//shape.hclass Shape{public: virtual double perimeter()const{return 0;} //返回形状的周长 virtual double area()const{return 0;} //返回形状的面积 virtual const char*name()const{return"抽象图形";}//返回形状的名称};class Point{ //表示平面坐标系中的点的类 double x; double y;public://************found************ Point(double x0,double y0):________{}//用x0、y0初始化数据成员x.y double getX () const{return x;} double getY () const{return y;}};class Triangle:public Shape{//********found******** _________;//定义3个私有数据成员public: Triangle(Point p1,Point p2,Point p3):point1(p1),point2(p2),point3(p3){) double perimeter()const; double area()const; const char*name()const{return"三角形";}};//shape.cpp#include"shape.h"#includecmathdouble length(Point p1,Point p2){ return sqrt((p1.getX()一p2.getX())*(p1.getX()一p2.getX())+(p1.getY()一p2.getY())*(p1.getY()一p2.getY()));}double Triangle::perimeter()const{//一个return语句,它利用length函数计算并返回三角形的周长//***********found***********__________;}double Triangle::area()const{ double s=perimeter()/2.0; return sqrt(s*(s—length(point1,point2))* (s—length(point2,point3))球 (s—length(point3,point1)));}//proj2.cpp#include"shape.h"#includeiostreamusing namespace std;//***********found***********//show函数的函数头(函数体以前的部分){cout"此图形是一个"shape.name()",周长="shape.perimeter()",面积="shape.area()endl;}int main(){ Shape s; Triangle tri(Point(0,2),Point(2,0),Point(0,0)); show(s); show(tri); return 0;}
