问答题

请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程prog3,其中声明了ValArmy类,该类在内部维护一个动态分配的整型数组。ValArray类的复制构造函数应实现对象的深层复制。请编写ValArray类的复制构造函数。在main函数中给出了一组测试数据,此种情况下程序的输出应该是:ValArrayvl={1,2,3,4,5}ValArrayv2={2,2,2,2,2}要求:补充编制的内容写在“//********333********”与“//********666********”之间。不要修改程序的其他部分。注意:相关文件包括:mmn.cpp、ValArray.h。程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obi文件。//ValArray.h#include<iostream>usingnamespacestd;classValArray{int*v;intsize;public:ValArray(constint*P,intn):size(n){v=newint[size];for(inti=0;i<size;i++)v[i]=P[i];}ValArray(constValArray&other);~ValArray(){delete[]V;lvoidsetElement(inti,intval){v[i]=val;}voidprint(ostream&out)const{out<<’{’;for(inti=0;i<size-1;i++)out<<v[i]<<",";out<<v[size-1]<<’)’;}};voidwriteToFile(constchar*);//main.cpp#include"ValArray.h"ValArray::ValArray(constValArray&other){//********333********//********666********}intmain(){constinta[]={1,2,3,4,5);ValArrayvl(a,5);ValArrayv2(v1);for(inti=0;i<5;i++)v2.setElement(i,2);cout<<”ValArrayvl=”;v1.print(cout);cout<<end1;cout<<"ValArrayv2=";v2.print(cout);cout<<end1;writeToFile("");return0;}

【参考答案】

正确答案:Size=other.s1ze; //把对象数组的大小赋值给size v=newint[other.size]......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motocycle类。要求将vehicle作为虚基类,避免二义性问题。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:Avehicleisrunning!Avehiclehasstopped!Abicycleisrunning!Abicyclehasstopped!Amotorcarisnmning!Amotocycleisrunning!注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream.h>classvehicle{private:intMaxSpeed;intWeight;public:vehicle():MaxSpeed(0),Weight(0){}vehicle(intmax_speed,intweight):MaxSpeed(max_speed),Weight(weight){}//**********found**********________Run(){cout<< AvehicleiSrunning! <<end1;}//**********found**********________Stop(){cout<< Avehiclehasstopped! <<end1;}};classbicycle:virtualpublicvehicle{private:intHeight;public:bicycle():Height(0){}bicycle(intmax_speed,intweight,intheight):vehicle(max_speed,weight),Height(height){};voidRun(){cout<< Abicycleisrunning! <<end1;}voidStop(){cout<<”Abicyclehasstopped!”<<end1;));classmotorcar:virtualpublicvehicle{private:intSeatNum;public:motorcar():SeatNum(0){}motorcar(intmaxspeed,intweight,intseat_num)//**********found**********:________{}voidRun(){cout<< Amotorcarisrunning! <<end1;}voidStop(){cout<< Amotorcarhasstopped! <<end1;}};//**********found**********classmOtOrcycle:________{public:motorcycle(){}motorcycle(intmax_speed,intweight,intheight,intseet_num):bicycle(max_speed,weight,height),motorcar(max_speed,weight,seet_num){};~motorcycle(){};voidRun(){cout<< Amotorcycleisrunning! <<end1;}voidStop(){cout<< AmotOrcyclehasstopped! <<end1;}};intmain(){vehicle*ptr;vehiclea;bicycleb;motorcarc;motorcycled;a.Run();a.Stop();b.Run();b.Stop();ptr=&c;ptr->Run();ptr=&d;ptr->Run(),return0;}