- 14
- 0
- 约2.02万字
- 约 37页
- 2017-12-10 发布于河南
- 举报
李爱华、程磊_面向对象程序设计课后答案(完整版)
第二章
2-4
#include iostream
using namespace std;
Add(int a,int b);
int main()
{
int x,y,sum;
coutplease input x and y:;
cinxy;
sum = add(x,y);
cout x+y=sumendl;
}
Add(int a,int b)
{
return a+b;
}
2-5
(1)this is a C++ program.
(2)
x=50.6 y=10 z=A
x=216.34 y=10 z=A
x=216.34 y=2 z=A
x=216.34 y=2 z=E
(3)
x y z
500 1000 0
500 1500 1500
500 200 1500
2-6
#include iostream
using namespace std;
int main()
{
int *p,*init;
int countp=0;
int countn=0;
p = new int[20];
init = p;
for(int i=0;i20;i++)
{
cin*p;
p++;
}
p = p-20;
for( i=0;i20;i++)
{
if(*p0) countp++;
if(*p0) countn++;
cout*p ;
p++;
}
cout正数有:countpendl;
cout负数有:countnendl;
p = init;
delete[] p;
return 0;}
2-7不做要求
#include iostream
//#include string
using namespace std;
void checkagescore(string name,int age)
{
if (name == exit) throw name;
if(age0||age50)
throw age;
}
int main()
{
string name;
int age;
for(int i=0 ;i5 ;i++ )
{
cin.ignore ();
getline(cin,name );
cinage ;
try
{
checkagescore(name,age);
}
catch( string)
{
coutexception :name is exitendl;
continue;
}
catch(int)
{
coutexception :age is not properendl;
continue;
}
coutname:name age :ageendl;
}
return 0;
}
第三章
3-1
(1)A (2)C (3)B (4)C (5)C
(6)B (7)B (8)C (9)C
3-7
(1)
main()函数中
p1.age = 30;语句是错误的。age 是类的私有成员
(2)
构造函数应当给常数据成员和引用成员初始化,将构造函数改为:
A(int a1,int b1):a(a1),b(b1){}
或
A(int a1 ):a(a1),b(a){}再将main中的A a(1,2); 改为A a(1);
(3)
(1)在Test 类中添加语句:
void print();
void Print(){
coutx-y=x-yendl;
}
改为
void Test::Print(){
coutx-y=x-yendl;
}
main函数中
Init(38,15);改为:
A.Init(38,15);
Print();改为:
A.Print();
3-8
(1)
Constructing A
Constructing B
Destructing B
Destructing A
(2)
double a,double b
point p
p.x
3-9
class box
{
int len1,len2,len3;
public:
box(int l1,int l2,int l3){len1 = l1;len2 = l2; len3 = l3;}
long volumn(){return len1*len2*len3;}
};
3-10
class Test{
int m1,m2;
public:
void Init(int a,int b){m1 = a;m2 = b;}
void Pring(){coutm1 m2endl
原创力文档

文档评论(0)