cout<<"name:"<
第四章
return x;
char name[20];
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:
int x; const int y1;
public:
const int y2;
Ctest(int
i2):y1(i1),y2(i2)
{
i1,int
y1 =10;//y1 为 常
量不能赋值
x = y1;
}
int readme() const;
};
int Ctest::readme ()const
{
int i;
i = x;
x++; //常函数内不能
改变成员值
int main()
{
public:
Student(int
Ctest c(2,8);
int i = c.y2;
c.y2 = i;//y2 为常量,不
*s="\0")
生对象
{
能改值
i = c.y1;//y1 私有,类外
}
i=0,char
//构造学
number=i;
strcpy(name,s);
不能访问
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;
void Print()
//输出结果
{
cout<<"Number:"<st2.number;
// 返 回 成 员
number 的比较结果
}
int main()
{
Student
st[5]={Student(65,"Li"),Stu
dent(78,"Zhang"),Student(8
0,"wang"),Student(92,"zhao
"),Student(50,"zhen")};
int max = 0;
int min = 0;
for(int i=1;i<5;i++)
{
if(!greaterthan(st[max],
author
=
new
char[strlen(b)+1];
st[i]))
max = i;
strcpy(author,b);
sale = c;
}
void print()
{cout<<"autor
"<
#include
using namespace std;
class Book
{
char *name;
char*author;
int sale;
public:
Book()
{ name = '\0';
author = '\0';
sale = -1;
}
Book(char* a ,char*
b,int c)
{
name
=
char[strlen(a)+1];
new
strcpy(name,a);
第五章
5-8
改错题答案不唯一
(1)
class DC {
int x;
public:
DC(){x =100;}
};
(2)编译无错,但逻辑错误,
可改为:
class BC
{
protected:
int x;
public:
BC(int i=0){x = i}
};
class DC:private BC
{
public:
DC(int i):BC(i){}
{
int x,y;
public:
Shape(int ix,int iy){x =
ix; y = iy;}
virtual
show(){cout<<"pos:
"<
using namespace std;
};
class Shape
int main()
C::print(),cinfo=2
C::print(),cinfo=2
D::print(),dinfo=4
B 类不能定义对象,否则
编译通不过,因为 B 未定
义基类 A 中的虚函数
print(),它也是个虚基类。
6-6
#include
using namespace std;
class Mammal
{
public:
virtual void Speak()
{cout<<"in
Mammal"<bark"<
Speak ();
return 0;
}
运行结果:
dog bark
6-7
#include
using namespace std;
class BaseClass
{
public:
}
结果将不能正常执行子类
析构
6-8 以圆形和正方形为例
#include
using namespace std;
class Shape
{
public:
virtual float area() = 0;
};
class Circle :public Shape
{
int radius;
public:
Circle(int
r):radius(r){}
pS[1] = new Circle(1);
new
pS[2]
Square(2);
=
areaSum
pS[1]->area ();
areaSum
pS[2]->area ();
+=
+=
cout<
using namespace std;
class Student
{
public:
virtual void show() =
virtual ~BaseClass()
{cout<<"destruct
float
area()
{return
3.14*radius*radius;}
0;
};
Base"<int main()
{
Student *pstu;
pstu = new Junior;
pstu->show ();
pstu = new Senior;
pstu->show ();
return 0;}
第七章
7-1
#include
#include
using namespace std;
class vector
{
int x,y;
public:
vector(int
ix=0,int
iy=0){x =ix;y = iy;}
vector
operator+(vector& v1)
{
return
vector(x+v1.x,y+v1.y);
}
vector&
operator+=(vector& v1)
{
x +=v1.x;
y +=v1.y;
return *this;
}
voidshow()
{
cout<<'('<
class Complex
{
private:
double real,image;
public:
Complex(double
x=0.0,double y=0.0)
{
bool
Complex &c);
Complex operator
Complex &c);
bool
operator
Complex &c);
Complex operator -();
operator
real =x; image =y;
}
!=(const
-(const
==(const
&operator
Complex
+=(const Complex &c);
void Print();
};
void Complex::Print()
{
cout<};
Date::Date(int y, int m, int
d)
{
if (m>0 && m<13)
month=m;
if (d>0 && d<32)
day=d;
if (y>0 && y<3000)
1;
year=y;
}
Date& Date::operator+(int
days)
{
i-= diff+1;
day = 1;
month++;
if(month>12)
{
year++;
month =
}
}
else
{
day+= i;
break;}
c3.Print();
if(c3==c1)cout<<"c3
equals to c1"<
using namespace std;
bool rn(int y)
{
bool flag = 0;
if(y%400==0 || y%4
==0&&y%100!=0)
flag = 1;
return flag;
}
class Date
{
private:
int month, day,
year;
public:
}
return *this;
}
void Date::showDate()
{
cout<0;)
{
int diff ;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: diff = 31
-day;break;
case 4:
case 6:
case 9:
case 11: diff = 30
}
-day;break;
case
2:if
(rn(year))diff = 29 - day;
else diff = 28
-day;
break;
Date(int m, int d,
}
int y);
Date&
operator+(int days);
void showDate();
if(i>diff)
{
7-4 以+,=为例
#include
#include
using namespace std;
class String
{