使用VC6打开考生文件夹下的工程test13_3。此工程包含一个test13_.cpp,其中定义了类Vector,但类的定义并不完整。请按要求完成下列操作,将程序补充完整。
(1)完成构造函数的定义,把数据成员size初始化为参数s的值,数据成员buffer指向动态申请的int型size大小的空间。请在注释“//**1**”之后添加适当的语句。
(2)完成拷贝构造函数的定义,注意解决多次删除的问题。请在注释“//**2**”之后添加适当的语句。
(3)完成成员函数elem的定义,该函数返回buffer的第ndx个元素的值,注意如果ndx超界,请输出“error in index”。请在注释“//**3**”之后添加适当的语句。
(4)完成析构函数的定义,要求先将字符d打印在屏幕上,再释放buffer指向的空间。请在注释“//**4**”之后添加适当的语句。
输出结果如下:
1234567891012345678910dd
注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。
源程序文件test13_3清单如下:
include<iostream.h>
include<stdlib.h>
class Vector
{
public:
Vector(int s=100);
Vector(Vector &v);
int &elem(int ndx);
void display();
void set();
~Vector();
protected:
int size;
int *buffer;
};
Vector::Vector(int s)
{
// ** 1 **
}
Vector::Vector(Vector &v)
{
// ** 2 **
for(int i=0; i<size; i++)
{
*(buffer+i)=*(v.buffer+i);
}
}
int &Vector::elem(int ndx)
{
// ** 3 **
{
cout<<"error in index"<<endl;
exit(1);
}
return buffer[ndx];
}
void Vector::display()
{
for(int j=0;j<size;j++)
cout<<elem(i)<<endl;
}
void Vector::set()
{
for(int j=0;i<size;j++)
elem(j)=j+1;
}
Vector::~Vector()
{
// ** 4 **
}
void main()
{
Vector a(10);
Vector b(a);
a. set();
b. set();
a. display();
b. display();
}
第1题:
使用VC6打开考生文件夹下的工程MyProj15。此工程包含一个源程序文件MyMain15.cpp。程序中定义了3个类A、B和C,但类的定义并不完整。
请按要求完成下列操作,将类的定义补充完成:
①类Inherit是类Base的公有派生类。请在注释“//* *1* *”之后添加适当的语句。
②完成类Inherit成员函数setvalue(int a,int b,int c,int d)的定义,此函数实现的功能是将基类成员x、y、z和派生类的数据成员m的值分别设置成a、b、c和d。请在注释“//* *2* *”之后添加适当的语句。
③完成类Inherit成员函数display()的类体外定义,此函数实现的功能是以“,,,,”的格式将x、y、z和m的值输出到屏幕上。请在注释“//* *3* *”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件MyMain15.cpp清单如下:
//Mymain15.cpp
include<iostream>
using namespace std;
class Base
{
private:
int x;
protected:
int y;
public:
int z;
void setx(int i)
{
x=i;
}
int getx()const
{
return x;
}
};
//* * *1* * *
{
private:
int m;
public:
void setvalue(int a,int b,int c,int d)
{
//* * *2* * *
}
void display()const;
};
//* * *3* * *
int main()
{
Inherit A;
A.setvalue(1,2,3,4);
A.display();
return 0;
}
第2题:
使用VC6打开考生文件夹下的工程RevProj15。此工程包含一个源程序文件RevMain15.cpp,但该程序中类的定义有错误。请改正程序中的错误,使它能得到正确结果。
注意,不要改动主函数,不得删行或增行,也不得更改程序的结构。
源程序文件RevMain15.cpp中的程序清单如下:
//RevMain15.cpp
include<iostream>
using namespace std;
class Sample
{
private:
int x;
static int y;
public:
Sample(int a)
{
x=a;
y+=x;
}
static void print(Sample s)
{
cout<<"x="<<x<<<<",y="<<y<<end1;
}
Sample::y=5;
int main()
{
Sampel s1(10);
Sample s2(15);
Sample::print(s1);
Sample::print(s2);
return 0;
}
第3题:
使用VC6打开考生文件夹下的工程MyProj10。此工程包含一个源程序文件MyMain10.cpp。程序中定义了两个类Base和Derived,但类的定义并不完整。
请按要求完成下列操作,将类的定义补充完成:
①类Derived是基类Base公有派生来的。请在注释“//* *1* *”之后添加适当的语句。
②完成构造函数Derived(int i)定义,采用初始化列表的方式使基类Base私有成员a初始化为i+1,类Derived的私有成员b初始化为i。请在注释“//* *2* *”之后添加适当的语句。
③完成类Derived的成员函数show()的类体外的定义。函数show()中要显式调用基类的show()函数,然后要输出私有成员b的值。请在注释“//* *3**”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件MyMain10.cpp清单如下:
//MyMain10.cpp
include<iostream>
using namespace std;
class Base
{
public:
Base(int x)
{
a=x
}
void show()
{
cout<<a;
}
private:
int a;
};
//* * *1* * *
{
public:
//* * * 2 * * *
void show();
private:
int b;
};
void Derived :: show()
{
//* * * 3 * * *
}
int main()
{
Derived d(1), *pb;
pb=&d;
pb->show();
return 0;
}
第4题:
使用VC6打开考生文件夹下的工程test21_1,此工程包含一个源程序文件test21_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
The grade is 3
源程序文件test21_1.cpp清单如下:
include<iostream.h>
class student
{
private:
int grade;
public:
/**************** found*******************/
student(int thegra):(thegra){}
~student(){}
int get_grade(){return grade;}
};
void main()
{
int thegra=3;
/**************** found*******************/
student point=new student(thegra);
/**************** found*******************/
cout<<"The grade is"<<point.get_grade()<<endl;
delete point;
}
第5题:
使用VC6打开考生文件夹下的工程MyProj12。此工程包含一个源程序文件MyMain12.cpp。程序中定义了两个类Base和Derived,但类的定义并不完整。
请按要求完成下列操作,将类的定义补充完成:
①类Derived是基类Base公有派生来的。请在注释“//* *1* *”之后添加适当的语句。
②完成构造函数Derived(int x)定义,采用初始化列表的方式使基类Base私有成员a初始化为x,类Derived的私有成员b初始化为x+1。请在注释“//* *2* *”之后添加适当的语句。
③完成类Derived的成员函数show()的类体外的定义。函数show()中要输出基类数据成员a的值,然后要输出私有成员b的值。请在注释“//* *3* *之后添加适当的语句。
注意;除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件MyMain12.cpp清单如下:
//MyMain12.cpp
include<iostream>
using namespace std;
class Base
{
public:
int a;
Base(int i)
{
a=i;
}
};
//* * * 1 * * *
{
private:
int b;
public:
//* * * 2 * * *
void show();
};
void Derived::show()
{
//* * * 3 * * *
}
int main()
{
Derived d(1);
d.show();
return 0;
}