问答题

请使用“答题”菜单或使用VC6打开考生文件夹proj3下的工程proj3,其中声明了VaIArray类,该类在内部维护一个动态分配的整型数组。ValArray类的复制构造函数应实现对象的深层复制。请编写ValArray类的复制构造函数。在main函数中给出了一组测试数据,此情况下程序的输出应该是:
ValArray v1=1,2,3,4,5
ValArray v2=1,2,3,4,5
要求:补充编制的内容写在//********333********与//********666********两行之间,不得修改程序的其他部分。
注意:程序最后已经将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//源程序
#include"ValArray.h"
VaIArray:: VaIArray(const ValArray& other)

//********333********
//********666********
int main()
const int a[]=1,2,3,4,5 ;
ValArray v1(a,5);
cout<<"ValArray v1=";
v1.print(cout);
cout<<endl;
ValArray v2(v1);
cout<<"ValArray v2=";
v2.print(cout);
cout<<endl;
// writeToFile("K: \\b10 \\61000101 \\");
return 0;

【参考答案】

int*ss=new int[other.size+1];
int n=other.size:
fo......

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

问答题
使用“答题”菜单或从VC6中打开考生文件夹proj2下的工程proj2。此工程包含一个程序文件main.cpp,其中有类Quadritic、类Root以及主函数main的定义。一个Quadritic对象表示一个ax2+bx+c的一元二次多项式。一个Root对象用于表示方程ax2+bx+c=0的一组根,它的数据成员num_of_roots有3种可能的值0、1和2,分别表示根的3种情况:无实根、有两个相同的实根和有两个不同的实根。请在程序中的画线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为(注:输出中的X^2表示x2): 3X^2+4X+5=0.0 无实根 4.5X^2+6X+2=0.0 有两个相同的实根: -0.666667和-0.666667 1.5X^2+2X-3=0.0 有两个不同的实根: 0.896805和-2.23014 源程序 #include<iostream> #include<iomanip> #include<cmath> using namespace std; class Root 一元二次方程的根 public: const double x1; 第一个根 const double x2; 第二个根 const int num_of_roots; 不同根的数量:0、1或2 Root():x1(0.0),x2(0.0),num_of_roots(0) 创建一个“无实根”的Root对象 Root(double root) ************found************ :______ 创建一个“有两个相同的实根”的Root对象 Root(double root1,double root2) :x1(root1),x2(root2),num_of_roots(2) 创建一个有两个不同的实根的Root对象 void show() const 显示根的信息 cout<< t t : switch(num_of_roots) case 0: ************found************ case 1: cout<< 有两个相同的实根: <<x1<< 和 <<x2;break; default: cout<< 有两个不同的实根: <<x1<< 和 <<x2;break; ; class Quadratic 二次多项式 public: const double a,b,c; 分别表示二次项、一次项和常数项3个系数 Quadratic(double a,double b,double c) 构造函数 ************found************ :______ Quadratic(Quadratic&x) 复制构造函数 :a(x.a),b(x.b),c(x.c) Quadratic add(Quadratic x)const 求两个多项式的和 return Quadratic(a+x.a,b+x.b,c+x.c); Quadratic sub(Quadratic x)const 求两个多项式的差 ************found************ double value(double x)const 求二次多项式的值 return a *x*x+b*x+c: Root root() const 求一元二次方程的根 double delta=b*b-4*a*e; 计算判别式 if(delta<0.0)return Root(); if(delta==0.0)return Root(-b (2 *a)); double sq=sqrt(delta); return Root((-b+sq) (2*a),(-b-sq) (2*a)); void show() const 显示多项式 cout<<endl<<a<< X^2 <<showpos<<b<< X <<c<<noshowpos; void showFunction() 显示一元二次方程 show(); cout<< =0.0 : ; int main() Quadratic q1(3.0,4.0, 5.0), q2 (4.5,6.O,2.O),q3(q2.sub(ql)); q1.showFunction(); q1.root().show(); q2.showFunction(); q2.root().show(); q3.showFunction(); q3.root().show(); cout<<endl; return 0:
问答题
请使用“答题”菜单或从VC6中打开考生文件夹proj1下的工程proj1。此工程包含了类Pets(“宠物”)和主函数main的定义。程序中位于每个 ERRO************found************下的语句行有错,请加以改正。改正后程序的输出结果是: Name: sonny Type: dog Name: John Type: dog Name: Danny Type: cat Name: John Type: dog 注意:只能修改每个 ERROR************found************下的那一行,不要改动程序中的其他内容。 源程序 #include <iostream> using namespace std; enum Pets_typedog,cat,bird,fish; class Pets private: char*name: Pets_type type; public: Pets(const char*name= sonny ,Pets_type type=dog); Pets& operator=(const Pets &s); ~Pets(); void show() const; ; Pets::Pets(const char*name,Pets_type type) 构造函数 this->name=new char[strlen(name)+1]; strcpy(this->name,name); ERROR************found************ type=type; Pets::~Pets() 析构函数,释放name所指向的字符串 ERROR************found************ name=’ 0’: Pets&Pets::operator=(const Pets &s) if(&s==this) return*this; 确保不要向自身赋值 delete[]name; name=new char[strlen(s.name)+1]; ERROR************found************ strcpy(s.name,this->name); type=S.type; return*this: void Pets::show() const couL<< Name: <<name<< Type: ; switch(type) case dog: cout<< dog ;break; case cat: cout<< cat ;break; case bird: cout<< bird ;break; case fish: cout<< fish ;break; cout<<endl; int main() Pets mypet1,mypet2( John ,dog); Pets youpet( Danny ,cat); mypet1.show(); mypet2.show(); youpet.show(); youpet=mypet2; youpet.show(); return 0: