Destructing B
Destructing A
(2)
double a,double b
point & p
p.x
3-9
class box
{
public:
int len1,len2,len3;
};
3-10
class Test{
};
3-11
略
3-12
}
box(int l1,int l2,int l3){len1 = l1;len2 = l2; len3 = l3;}
long volumn(){return len1*len2*len3;}
int m1,m2;
public:
void Init(int a,int b){m1 = a;m2 = b;}
void Pring(){cout<
第四章
4-6
(1)D (2)D (3)D (4)D (5)B
(6)D
4-7
(1)
static int count = 0;这样初始化静态成员值是不对的将其改为
static int count;
在类外,main 函数前加
int Sample::count = 0;
(2)
#include
//#include
using namespace std;
class Ctest
{
private:
public:
int x; const int y1;
const int y2;
Ctest(int i1,int i2):y1(i1),y2(i2)
{
y1 =10;//y1 为常量不能赋值
x = y1;
}
int readme() const;
};
int Ctest::readme ()const
{
int i;
i = x;
x++; //常函数内不能改变成员值
return x;
}
int main()
{
Ctest c(2,8);
int i = c.y2;
c.y2 = i;//y2 为常量,不能改值
i = c.y1;//y1 私有,类外不能访问
return 0;
}
将出错语句全部注释
4-8
(1)
题中印刷错误,将 class C 构造函数改为:
C()
{cout<<"constructor C:";}
运行结果为:
constructor A
constructor B
constructor C
(2)
40
(3)
3
4
3
4-9 略
4-10
#include
#include
class Student
int number;
char name[20];
{
}
public:
Student(int i=0,char *s="\0")
{
number=i;
strcpy(name,s);
//构造学生对象
void Print()
{
//输出结果
cout<<"Number:"<st2.number;
//返回成员 number 的比较结果
st[5]={Student(65,"Li"),Student(78,"Zhang"),Student(80,"wang"),Student(92,"zhao"),
Student(50,"zhen")};
int max = 0;
int min = 0;
for(int i=1;i<5;i++)
{
if(!greaterthan(st[max],st[i]))
if(!greaterthan(st[i],st[min]))
max = i;
min = i;
}
cout<<"最大成绩:"<
#include
using namespace std;
class Book
{
char *name;