面向对象程序设计课后答案完整版

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第二章2-4

#include

using namespace std;

Add(int a,int b);

int main()

{

int x,y,sum;

cout<<"please input x and y:";

cin>>x>>y;

sum = add(x,y);

cout <

}

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

using namespace std;

int main()

{

int *p,*init;

int countp=0;

int countn=0;

p = new int[20];

init = p;

for(int i=0;i<20;i++)

{

cin>>*p;

p++;

}

p = p-20;

for( i=0;i<20;i++)

{

if(*p>0) countp++;

if(*p<0) countn++;

cout<<*p<<" ";

p++;

}

cout<<"正数有:"<

p = init;

delete[] p;

return 0;}

2-7不做要求

#include

//#include

using namespace std;

void checkagescore(string name,int age) {

if (name == "exit") throw name;

if(age<0||age>50)

throw age;

int main()

{

string name;

int age;

for(int i=0 ;i<5 ;i++ )

{

cin.ignore ();

getline(cin,name );

cin>>age ;

try

{

checkagescore(name,age);

}

catch( string)

{

cout<<"exception :name is exit"<

continue;

}

catch(int)

{

cout<<"exception :age is not proper"<

continue;

}

cout<<"name:"<

}

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(){

cout<

}

改为

void Test::Print(){

cout<

}

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(){cout<

};

3-11

3-12

}

相关文档
最新文档