使用VC6打开考生文件夹下的工程test2_3。此工程包含一个test2_3.cpp,其中定义了类Stud,但类的定义并不完整。请按要求完成—下列操作,将程序补充完整。
(1)定义类的公有数据成员no,name[10],其中no是int型的数据,name[10]是字符型一维数组。请在注释“//**1**”之后添加适当的语句。
(2)完成函数set的定义,分别将参数n,na的值赋给数据成员no,name。注意:请使用this指针完成对no的赋值,使用字符串函数实现对name和cname的赋值。请在注释“//**2**”之后添加适当的语句。
(3)完成类的成员函数disp的定义,使其按no,deg,name和cname的顺序输出类的数据成员的值,中间使用跳格符分隔,请在注释“//**3**”之后添加适当的语句。
输出结果如下:
1 李华 95 990701
2 王东 83 990702
注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。
源程序文件test2_3.cpp清单如下:
include<iostream.h>
include<string.h>
class Stud
{
public:
// ** 1 **
int deg;
char cname[10];
Stud(){}
void set(int n,char na[],int d,char cn[])
{
// ** 2 **
deg=d;
strcpy(cname,cn);
}
void disp()
{
// ** 3 **
}
};
void main()
{
Stud obj[2];
obj[0].set(1,,"李华",95,"990701");
obj[1].set(2,"王东",83,"990702");
obj[0].disp();
obj[1].disp();
}
第1题:
使用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;
}
第2题:
使用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;
}
第3题:
使用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;
}
第4题:
使用VC6打开考生文件夹下的工程MyProj2。此工程包含一个源程序文件 MyMain2.cpp,此程序的运行结果为:
Derive1's Print() Called.
Derive2's Print() called.
其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
①定义函数Print()为无值型纯虚函数。请在注释“//**1**”之后添加适当的语句。
②建立类Derivel的构造函数,请在注释“//**2**”之后添加适当的语句。
③完成类Derive2成员函数Print()的定义。请在注释“//**3**”之后添加适当的语句。
④定义类Derivel的对象指针d1,类Derive2的对象指针d2。其初始化值分别为1和2。
源程序文件MyMain2.cpp中的程序清单如下:
//MyMain2. cpp
include <iostream>
using namespace std;
class Base
{
public:
Base(int i)
{
b=i;
}
//* * 1 * *
protected:
int b;
};
class Derivel: public Base
{
public:
//* * 2 * *
void print ()
{
cout<<" Derivel's Print() called."<<end1;
}
};
class Derive2 : public Base
{
public:
Derive2(int i) :Base(i) { }
//* * 3 * *
};
void fun (Base *obj)
{
obj->Print ();
}
int main ( )
{
//* * 4 * *
fun (d1);
fun (d2);
return 0;
}
第5题:
使用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;
}
第6题:
使用VC6打开考生文件夹下的工程test4_1,此工程包含一个源程序文件test4_1.cpp,但该程序在类的定义中存在问题,请改正类定义中的错误,使程序的输出结果如下:
a=3 b=13
a=5 b=13
注意:请勿修改主函数main中的任何内容。
源程序文件rcst4_1.cpp清单如下:
include<iostream.h>
class T
{
public:
/*********found***+******/
T(int x){a=x; b+=x;)
/+********found**********/
void display(T c)
{ cout<<"a="<<c.a<<"\t"<<"b="<<c.b<<endl;)
private:
const int a;
static int b;
};
/**********found*********/
int b=5;
void main()
{
T A(3),B(5);
T::display(A);
T::display(B);
}
第7题:
使用VC6打开考生文件夹下的工程test42_3。此工程包含—个test42_3.cpp,其中定义了类Cpolygon、COutput和CTriangle,其中CTriangle类由Cpolygon和COutput类public派生,但三个类的定义并不完整。请按要求完成下列操作,将程序补充完整。
(1)定义类CPolygon的保护数据成员width和height,它们都是int型的数据。请在注释“//**1**”之后添加适当的语句。
(2)完成类CPolygon的成员函数set_values(int a,int b),使参数a和b分别赋值给保护数据成员width和height,请在注释“//**2**”之后添加适当的语句。
(3)完成类Coutput的成员函数output(int)的定义,将传入的参数为血型的i输出到屏幕并换行,请在注释“//**3**”之后添加适当的语句。
(4)完成派生类CTriangle的声明,它由Cpolygon和COutput类public派生,请在注释“//**4**”之后添加适当的语句。
源程序文件test42_3.cpp清单如下:
include <iostream.h>
class CPolygon
{
protected:
// ** 1 **
public:
void set_values(int a, int b)
{
// ** 2 **
}
};
class COutput
{
public:
void output(int i);
};
void COutput::output(int i)
{
// ** 3 **
}
// ** 4 **
{
public:
int area (void)
{
return (width * height / 2);
}
}
int main ()
{
CTriangle trgl;
trgl.set_values (4,5);
trgl.output (trgl.area());
return 0;
}
第8题:
使用VC6打开考生文件夹下的工程test40_3。此工程包含一个test40_3.cpp,其中定义了类CVector,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。
(1)完成类CVector构造函数的定义,把传入的int型参数a和b分别赋值给类的成员变量x和y。请在注释“//**1**”之后添加适当的语句。
(2)完成类CVector的“+”运算符重载函数的定义,使Cvector类的两个对象相加时可以直接使用符号“+”,并且结果仍然为类Cvector的对象,请在注释“//**2**”之后添加适当的语句。
(3)在主函数中添加适当的语句,把类Cvector的对象a和b相加,并且把结果保存在对象c中,最后以“x,y”的形式输出在屏幕上,请在注释“//**3**”之后添加适当的语句。
源程序文件test40_3.cpp清单如下:
include <iostream.h>
class CVector
{
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
};
CVector::CVector (int a, int b)
{
//**1**
}
CVector CVector::operator+ (CVector param)
{
//**2**
temp.y = y + param.y;
return (temp);
}
int main ()
{
CVector a (3,1);
CVector b (1,2);
CVector c;
//**3**
return 0;
}
第9题:
使用VC6打开考生文件夹下的工程MyProj11。此工程包含一个源程序文件 MyMain11.cpp。程序中定义了3个类A、B和C,但类C的定义并不完整。
请按要求完成下列操作,将类的定义补充完成:
①类C是基类A和B公有派生来的。请在注释“//* *1* *”之后添加适当的语句。
②完成构造函数C(int k)定义,采用初始化列表的方式使基类A私有成员a初始化为k-2,基类B的私有成员b初始化为k+2,类C的私有成员c初始化为k。请在注释“//* *2* *”之后添加适当的语句。
③完成类Derived的成员函数disp()的类体外的定义。函数disp()中要分别显式调用基类A、B的disp()函数,然后要输出私有成员c的值。请在注释“//* *3* *”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件MyMain11.cpp清单如下;
//MyMain11.cpp
include <iostream>
using namespace std;
class A
{
private:
int a;
public:
A(int i) { a=i;}
void disp()
{
cout<<a<<" , "
}
};
class B
{
private:
int b;
public:
B(int j){ b=j;}
disp ( )
{
cout<<b<<", ";
}
};
//* * *1* * *
{
private:
int c;
public:
//* * *2* * *
void disp();
};
void c::disp()
{
//* * *3* * *
}
int main ( )
{
C obj(10);
obj.disp();
return 0;
}