请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 80 150 100 1 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#includeiostream.hclass vehicle{private: int MaxSpeed; int Weight;public://********found******** vehicle(int maxspeed,int weight):_________ ~vehicle(){); int getMaxSpeed() { return MaxSpeed;) int getWeight() {return Weight;)},//****found****class bicycle:_________publicvehicle{private: int Height;public: bicycle(int maxspeed,intweight,int height):vehicle(maxspeed, weight),Height(height)f) int getHeight(){returnHeight;);};//********found********class motorcar:________public vehicle{private: int SeatNum;public: motorcar(int maxspeed,int weight,int seatnum):vehicle(maxspeed, weight),SeatNum(seatnum){} int getSeatNum()f return Seat-Num;);};//********found********class motorcycle :________{public: motorcycle(int maxspeed,int weight,int height):vehicle(maxspeed, weight),bicycle(maxspeed,weight,height),motorcar(maxspeed,weight,1){}};void main(){ motorcycle a(80,150,100); couta.getMaxSpeed()endl; couta.getWeight()endl; couta.getHeight()endl; couta.getSeatNum()endl;}
请打开考生文件夹下的解决方案文件proj1,该工程中包含程序文件main.cpp,其中有类Clock(“时钟”)的定义和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Initial times are 0 d:0 h:0 m:59 s After one second times are 0 d:0 h:1 m:0 S 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。#includeiostreamusing namespace std;Class Clock{public: Clock(mRsigned long i=0); void set(unsigned long i=0); void print()const; void tick(); //时间前进一秒 Clock operator++();private: uns igned long total sec,seconds,minutes,hours,days;};Clock::C1ock(unsigned long i) :total sec(i),seconds(i% 60), minutes((i/60)% 60), hours((i/3600)% 24), days(i/86400){}void Clock::set(uns~gned long i){ total sec=i; seconds=i% 60; minutes=(i/60)%60 ; hours=(i/3600)%60 ; days=i/86400;}//ERROR *********found*********void Clock::print(){ tout days "d: " hours "h:" minutes " m: "| seconds"s"endl;}void Clock::tick(){//ERROR *********found********* set(total sec++);}C1ock C1ock::operator ++(){ tick();//ERROR *******found******* return this;}int main(){ Clock ck(59); cout "Initial times are"endl ; ck.print(); ++ck; cout "After one secondtimes are" endl; ck.print(); return 0;}
请打开考生文件夹下的解决方案文件proj2,其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为: (1,2,3,4,5) (0,0,0,0,0,0) 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#includeiostreamusing namespace std;class VectorBase{//向量基类,一个抽象类 int len;public: VectorBase(int len):len(len){} int length()const{returnlen;}//向量长度,即向量中元素的个数 virtual double getElement(int i)const=0;//取第i个元素的值 virtual double sum()const=0;//求所有元素的和 void show()const{//显示向量中所有元素 Cout"("; for(int i=0;ilength()-1;i++) coutgetElement(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;ilen;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;ilength();i++)_____________; return s; }};class ZeroVector:public VectorBase{//零向量类 public: ZeroVector(int len):Vector-Base(len){} //*******found******* double getElement(int index)const{____________;} double sum()const{return 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打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。 (1)定义私有成员变量year、month、day,分别表示年、月、曰,类型为int。请在注释∥********1********后添加适当的语句。 (2)完成构造函数,分别给year、month、day赋值,请在注释∥********2********后添加适当的语句。 (3)完成重载符号“+=”的定义,请在注释∥********3********后添加适当的语句。 (4)完成print()打印函数,如2008年8月8日到屏幕和文件modi3.txt格式相同,请在注释∥********4********后添加适当的语句。 注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。#includeiostream.h#includefstream#includeiomanip#includecmathusing namespace std;void WriteFile(int c){ 0fstream out1; out1.open(“modi 3.txt”,ios base::app); out1c ‘ ’: out1.close();}void WriteFile(char*str){ ofstream out1; out1.open(“modi3.txt”,ios base::app); outlstr;out1.close();}void ClearFiie(){ ofstream out1; out1.open(“modi3.txt”);out1.close();}c]ass Date{public: Date(int y,int m,int d) {∥********2******** } void print()const;∥********3******** { month+=m; int i=month/1 2; int j=month%12; if(j==0) { year+=(i一1); month=12; } else { year+=i; month=j; } return*this; }private:∥********1********};void Date::print()const{ ∥********4******** WriteFile(year); WriteFile(“年”); WriteFile(month); WriteFile(“月”); WriteFile(day); WriteFile(“日”);}int msin(){ ClearFile(); Date Oly—day(2 0 0 8,8,8); Oly day+=3; Oly—day.print(); return 0;}
请打开考生文件夹下的解决方案文件proj3,其中声明了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] ", "; out v[size-1] ')'; } void setArray(int i,int val) { v[i]=val; }};void writeToFile(const char*);//main.cpp#include "ValArray.h"ValArray:: ValArray (constValArrayother){//*********333*********//*********666*********}int main(){ const int a[]={1,2,3,4,5); ValArray vl(a,5); cout"ValArray vl="; v1.print(cout); coutendl; ValArray v2(v1); cout"ValArray v2="; v2.print(cout); coutendl; writeToFile(" "); return 0;}
已有函数fun(a,b),为了使函数指针变量p指向函数fun,则使用的赋值语句是_________
使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: Number=7 Number=12 注意:错误的语句在//********error********的下面,修改该语句即可。1 #include2 class CMyClass3 {4 public:5 //******error******6 CMyclass(int i):Number=i7 {8 ******error******9 return Number;10 }11 void set(int i)12 {13 Number=i;14 }15 void display()16 {17 cout18 }19 private:20 int Number;21 };22 void main()23 {24 //******error******25 CMyClass*p=new CMyClass;26 p->display();27 p->set(12);28 p->display();29 return;30 }
请打开考生文件夹下的解决方案文件proj1,该工程中包含程序文件main.cpp,其中有类Foo和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: X=a Y=42 注意:只修改每个“//ERROR *********found*********”下的那一行,不要改动程序中的其他内容。#include iostreamusing namespace std;class Foo{public: Foo(char x){x =x;} char getX()aonst{return x;}public: static int y;private: char x;};//ERROR *********found*********int Foo.y =42 ;int main(int argc, char*arqv[]){//ERROR *********found********* Foo f ;//ERROR *********found********* cout "X = " f.x endl; cout "Y = " f.y endl; return 0 ;}
请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋值给my-Array。类intArray重载了“=”运算符。程序中给出了一个测试数据文件input,不超过300个的整数。程序的输出是: 10 11 13 16 20 要求: 补充编制的内容写在“//**********333**********”与“//**********666**********”之间。实现重载赋值运算符函数,并将赋值结果在屏幕输出。格式不限。不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//intArray.hclass intArray{private: int*array; int length;public: intArray(char*filename); intArray(); intArrayoperator=(constintArraysrc); ~intArray(); void show();};void writeToFile(const char*path);//main.cpp#includeiostream#includefstream#includecstring#include"intArray.h"using namespace std;intArray::intArray(){ length=10; array=new int[length]; } intArray:: intArray (char*filename) { ifstream myFile(filename); array=new int[300]; length=0; while(myFilearray[length++]) length--; myFile.close(); } intArrayintArray::operator =(const intArraysrc) { if(array!=NULL)delete[]array; length=src.length; array:new int[length];//***************333***************//***************666*************** return*this;}intArray::~intArray(){delete[]array;}void intArray::show(){ int step=0; for(int i=0;ilength;i=i+step) { coutarray[i]endl; step++; }}void main(){ intArray*arrayp=.new intArray("input.dat"); intArray myArray; myArray=*arrayp; (*arrayp).show(); delete arrayp; writeToFile("");}
数组元素a[i]是该数组的第【 】个元素。
